|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Vision
Dos this code work?
package org.usfirst.frc.team1025.robot.subsystems; import org.opencv.core.Rect; import org.opencv.imgproc.Imgproc; import org.usfirst.frc.team1025.robot.GripPipeline; import com.ctre.CANTalon; import edu.wpi.cscore.CvSink; import edu.wpi.cscore.CvSource; import edu.wpi.cscore.UsbCamera; import edu.wpi.first.wpilibj.CameraServer; import edu.wpi.first.wpilibj.IterativeRobot; import edu.wpi.first.wpilibj.RobotDrive; import edu.wpi.first.wpilibj.command.Subsystem; import edu.wpi.first.wpilibj.vision.VisionRunner; import edu.wpi.first.wpilibj.vision.VisionThread; public class Vision extends Subsystem { public static final int IMG_WIDTH = 320; private static final int IMG_HEIGHT = 240; private VisionThread visionThread; UsbCamera camera; public double centerX = 0.0; //public RobotDrive drive; public final Object imgLock = new Object(); public Vision() { camera = CameraServer.getInstance().startAutomaticCapture() ; // camera.setResolution(640, 480); // CvSink cvSink = CameraServer.getInstance().getVideo(); // CvSource cvSource = CameraServer.getInstance().putVideo("Rectangle", IMG_WIDTH, IMG_HEIGHT); visionThread = new VisionThread(camera, new GripPipeline(), pipeline -> { if (!pipeline.filterContoursOutput().isEmpty()) { Rect r = Imgproc.boundingRect(pipeline.filterContoursOutput ().get(0)); synchronized (imgLock) { centerX = r.x + (r.width / 2); } } }); visionThread.start(); //drive = new RobotDrive(1, 2); } public void initDefaultCommand() { } public Object getImgLock() { return imgLock; } public double centerX() { return centerX; } public int imageWidth() { return IMG_WIDTH; } public int imageHeight(){ return IMG_HEIGHT; } |
|
#2
|
|||
|
|||
|
Re: Vision
I also made a command going off of this
|
|
#3
|
||||
|
||||
|
Re: Vision
Depends what you're trying to accomplish. I'll note that the post-processing you're doing in the lambda assumes the first contour it sees is one of the boiler stripes, which may not always be the case.
centerX() should synchronize on the lock, otherwise the main robot thread may not get the most recent values (or any values at all. Threads are weird that way). Code:
public double centerX() {
synchronized (imgLock) {
return centerX;
}
}
|
|
#4
|
|||
|
|||
|
Re: Vision
Thank you!
![]() |
|
#5
|
|||
|
|||
|
Re: Vision
Thanks. Does this command work with it?
package org.usfirst.frc.team1025.robot.commands; import org.usfirst.frc.team1025.robot.Robot; import org.usfirst.frc.team1025.robot.subsystems.Vision; import edu.wpi.first.wpilibj.command.Command; /** * */ public class VisionAuton extends Command { double centerX; public VisionAuton() { // Use requires() here to declare subsystem dependencies // eg. requires(chassis); requires(Robot.visionSubsystem); requires(Robot.ChassisSubsystem); } // Called just before this Command runs the first time protected void initialize() { } // Called repeatedly when this Command is scheduled to run protected void execute() { synchronized (Robot.visionSubsystem.imgLock) { Robot.visionSubsystem.centerX = this.centerX; } double Turn = centerX - (Vision.IMG_WIDTH / 2 ); // ask // Robot.chassisSubsystem.SimpleDrive(leftSpeed, rightSpeed); Robot.ChassisSubsystem.getRobotDrive().arcadeDrive (-0.6, Turn * 0.005); } // Make this return true when this Command no longer needs to run execute() protected boolean isFinished() { return false; } // Called once after isFinished returns true protected void end() { } // Called when another command which requires one or more of the same // subsystems is scheduled to run protected void interrupted() { } } |
|
#6
|
||||
|
||||
|
Re: Vision
execute() won't work. You're overwriting the vision centerX instead of copying it. You should make the centerX variable in VisionSubsystem be private to avoid problems like this.
Code:
synchronized (Robot.visionSubsystem.imgLock) {
Robot.visionSubsystem.centerX = this.centerX;
}
Code:
synchronized (Robot.visionSubsystem.imgLock) {
this.centerX = Robot.visionSubsystem.centerX();
}
|
|
#7
|
|||
|
|||
|
Re: Vision
ok thanks
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|