Go to Post FIRST is inspiration. No matter how you do it, you learn something. Stop being silly. Stop making accusations. Go build robots. Now. - Aignam [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-01-2017, 16:47
verlander13 verlander13 is offline
Registered User
AKA: Ian
FRC #1025 (IMPIS)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Ferndale
Posts: 9
verlander13 is an unknown quantity at this point
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;
}
Reply With Quote
  #2   Spotlight this post!  
Unread 23-01-2017, 16:48
verlander13 verlander13 is offline
Registered User
AKA: Ian
FRC #1025 (IMPIS)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Ferndale
Posts: 9
verlander13 is an unknown quantity at this point
Re: Vision

I also made a command going off of this
Reply With Quote
  #3   Spotlight this post!  
Unread 23-01-2017, 17:02
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 136
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
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;
  }
}
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #4   Spotlight this post!  
Unread 23-01-2017, 17:06
verlander13 verlander13 is offline
Registered User
AKA: Ian
FRC #1025 (IMPIS)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Ferndale
Posts: 9
verlander13 is an unknown quantity at this point
Re: Vision

Thank you!
Reply With Quote
  #5   Spotlight this post!  
Unread 23-01-2017, 17:07
verlander13 verlander13 is offline
Registered User
AKA: Ian
FRC #1025 (IMPIS)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Ferndale
Posts: 9
verlander13 is an unknown quantity at this point
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() {
}
}
Reply With Quote
  #6   Spotlight this post!  
Unread 23-01-2017, 18:30
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 136
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
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;
}
should be

Code:
synchronized (Robot.visionSubsystem.imgLock) {
  this.centerX = Robot.visionSubsystem.centerX();
}
isFinished should also return true when the robot's driven to wherever it should be. As is, the command will never finish and the robot will never stop.
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #7   Spotlight this post!  
Unread 23-01-2017, 20:39
verlander13 verlander13 is offline
Registered User
AKA: Ian
FRC #1025 (IMPIS)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Ferndale
Posts: 9
verlander13 is an unknown quantity at this point
Re: Vision

ok thanks
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 12:58.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi