Go to Post Football has been a school institution for over 100 years--we can't expect FIRST to show the same potential in a 10th of that. - Petey [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 26-01-2017, 16:05
mjbergman92's Avatar
mjbergman92 mjbergman92 is offline
Registered User
AKA: Malachi
FRC #3534 (House of Cards)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2026
Location: michigan
Posts: 5
mjbergman92 is an unknown quantity at this point
Listener from VisionThread? What do I put in this?

This is our first year of vision processing. I am trying to figure out what goes in for the "listener" portion for the VisionThread.

Code:
visionThread = new VisionThread(server, new GripPipeline(),?){
            if (!gripPipeline.filterContoursOutput().isEmpty()) {
                Rect r = Imgproc.boundingRect(gripPipeline.filterContoursOutput().get(0));
                synchronized (imgLock){
                    centerX = r.x + (r.width / 2);
                }
            }
        }
Please help!
Reply With Quote
  #2   Spotlight this post!  
Unread 26-01-2017, 16:16
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: 161
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: Listener from VisionThread? What do I put in this?

The listener in the example is a lambda expression, or an "anonymous function". Basically, a method that's defined where it's used. It's similar to an anonymous class, but is more succinct.

Code:
visionThread = new VisionThread(camera, new GripPipeline(), pipeline -> {
  if (!gripPipeline.filterContoursOutput().isEmpty()) {
    Rect r = Imgproc.boundingRect(gripPipeline.filterContoursOutput().get(0));
    synchronized (imgLock){
      centerX = r.x + (r.width / 2);
    }
  }
});
can also be written as

Code:
visionThread = new VisionThread(camera, new GripPipeline(), new VisionRunner.Listener<GripPipeline>() {
  @Override
  public void copyPipelineOutputs(GripPipeline pipeline) {
    // same as the contents of the lambda above
  }
});
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #3   Spotlight this post!  
Unread 26-01-2017, 16:36
mjbergman92's Avatar
mjbergman92 mjbergman92 is offline
Registered User
AKA: Malachi
FRC #3534 (House of Cards)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2026
Location: michigan
Posts: 5
mjbergman92 is an unknown quantity at this point
Re: Listener from VisionThread? What do I put in this?

Thank you for your reply the only thing that doesn't work is my camera is a camera server of type CameraServer. What do I do? Here is my complete Robot.java class code.

Code:
// RobotBuilder Version: 2.0
//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.


package org.usfirst.frc3534.DriveTest;

import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.imgproc.Imgproc;

import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.CvSource;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.cscore.UsbCameraInfo;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.vision.VisionPipeline;
import edu.wpi.first.wpilibj.vision.VisionRunner;
import edu.wpi.first.wpilibj.vision.VisionThread;

import org.usfirst.frc3534.DriveTest.commands.AutonomousCommand;
import org.usfirst.frc3534.DriveTest.commands.GripPipeline;
import org.usfirst.frc3534.DriveTest.subsystems.*;

/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the IterativeRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the manifest file in the resource
 * directory.
 */
public class Robot extends IterativeRobot {
	private VisionThread visionThread;
	private GripPipeline gripPipeline;
	private double centerX = 0.0;
	private RobotDrive drive;
	
	private final Object imgLock = new Object();
	
    Command autonomousCommand;

    public static OI oi;
    public int counter;
    public CameraServer server;
    
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
    public static DriveTrain driveTrain;

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS

    /**
     * This function is run when the robot is first started up and should be
     * used for any initialization code.
     */
    public void robotInit() {
    	
    	
    	
    	visionThread = new VisionThread(camera, new GripPipeline(), pipeline -> {
    		  if (!gripPipeline.filterContoursOutput().isEmpty()) {
    		    Rect r = Imgproc.boundingRect(gripPipeline.filterContoursOutput().get(0));
    		    synchronized (imgLock){
    		      centerX = r.x + (r.width / 2);
    		    }
    		  }
    		});
    	
    
    	visionThread.start();
        
        drive = new RobotDrive(1, 2);
        
    	RobotMap.init();
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
        driveTrain = new DriveTrain();
        
    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
        // OI must be constructed after subsystems. If the OI creates Commands
        //(which it very likely will), subsystems are not guaranteed to be
        // constructed yet. Thus, their requires() statements may grab null
        // pointers. Bad news. Don't move it.
        oi = new OI();
        
        server = CameraServer.getInstance();
        server.setSize(50);
        //the camera name (ex "cam0") can be found through the roborio web interface
        server.startAutomaticCapture();
        
        // instantiate the command used for the autonomous period
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS

        autonomousCommand = new AutonomousCommand();
        

        
        
        
        	
    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS
    }

    /**
     * This function is called when the disabled button is hit.
     * You can use it to reset subsystems before shutting down.
     */
    public void disabledInit(){

    }

    public void disabledPeriodic() {
        Scheduler.getInstance().run();
    }

    public void autonomousInit() {
        // schedule the autonomous command (example)
        if (autonomousCommand != null)autonomousCommand.start();
    }

    /**
     * This function is called periodically during autonomous
     */
    public void autonomousPeriodic() {
        Scheduler.getInstance().run();
        log();
    }

    public void teleopInit() {
        // This makes sure that the autonomous stops running when
        // teleop starts running. If you want the autonomous to
        // continue until interrupted by another command, remove
        // this line or comment it out.
        if (autonomousCommand != null) autonomousCommand.cancel();
    }

    /**
     * This function is called periodically during operator control
     */
    public void teleopPeriodic() {
        Scheduler.getInstance().run();
        log();
    }

    /**
     * This function is called periodically during test mode
     */
    public void testPeriodic() {
        LiveWindow.run();
        log();
    }
    private void log(){
    	counter++;
    	
    	if(counter >= 5){
    		SmartDashboard.putNumber("Left Encoder Count", driveTrain.getLtEncoderPosition());
        	SmartDashboard.putNumber("Right Encoder Count", driveTrain.getRtEncoderPosition());
        	SmartDashboard.putNumber("Encoder Difference", driveTrain.getEncoderDifference());
        	SmartDashboard.putNumber("Rt Value", driveTrain.getRtValue());
        	SmartDashboard.putNumber("Correction Value", driveTrain.getCorrection());
    		
    		counter = 0;
    }
    }
}
Reply With Quote
  #4   Spotlight this post!  
Unread 26-01-2017, 16:54
YairZiv's Avatar
YairZiv YairZiv is offline
Registered User
FRC #5951 (Makers Assemble)
Team Role: Programmer
 
Join Date: Oct 2016
Rookie Year: 2016
Location: Tel Aviv, Israel
Posts: 41
YairZiv is an unknown quantity at this point
Re: Listener from VisionThread? What do I put in this?

Quote:
Originally Posted by mjbergman92 View Post
Thank you for your reply the only thing that doesn't work is my camera is a camera server of type CameraServer. What do I do? Here is my complete Robot.java class code.

Code:
// RobotBuilder Version: 2.0
//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.


package org.usfirst.frc3534.DriveTest;

import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.imgproc.Imgproc;

import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.CvSource;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.cscore.UsbCameraInfo;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.vision.VisionPipeline;
import edu.wpi.first.wpilibj.vision.VisionRunner;
import edu.wpi.first.wpilibj.vision.VisionThread;

import org.usfirst.frc3534.DriveTest.commands.AutonomousCommand;
import org.usfirst.frc3534.DriveTest.commands.GripPipeline;
import org.usfirst.frc3534.DriveTest.subsystems.*;

/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the IterativeRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the manifest file in the resource
 * directory.
 */
public class Robot extends IterativeRobot {
	private VisionThread visionThread;
	private GripPipeline gripPipeline;
	private double centerX = 0.0;
	private RobotDrive drive;
	
	private final Object imgLock = new Object();
	
    Command autonomousCommand;

    public static OI oi;
    public int counter;
    public CameraServer server;
    
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
    public static DriveTrain driveTrain;

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS

    /**
     * This function is run when the robot is first started up and should be
     * used for any initialization code.
     */
    public void robotInit() {
    	
    	
    	
    	visionThread = new VisionThread(camera, new GripPipeline(), pipeline -> {
    		  if (!gripPipeline.filterContoursOutput().isEmpty()) {
    		    Rect r = Imgproc.boundingRect(gripPipeline.filterContoursOutput().get(0));
    		    synchronized (imgLock){
    		      centerX = r.x + (r.width / 2);
    		    }
    		  }
    		});
    	
    
    	visionThread.start();
        
        drive = new RobotDrive(1, 2);
        
    	RobotMap.init();
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
        driveTrain = new DriveTrain();
        
    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
        // OI must be constructed after subsystems. If the OI creates Commands
        //(which it very likely will), subsystems are not guaranteed to be
        // constructed yet. Thus, their requires() statements may grab null
        // pointers. Bad news. Don't move it.
        oi = new OI();
        
        server = CameraServer.getInstance();
        server.setSize(50);
        //the camera name (ex "cam0") can be found through the roborio web interface
        server.startAutomaticCapture();
        
        // instantiate the command used for the autonomous period
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS

        autonomousCommand = new AutonomousCommand();
        

        
        
        
        	
    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS
    }

    /**
     * This function is called when the disabled button is hit.
     * You can use it to reset subsystems before shutting down.
     */
    public void disabledInit(){

    }

    public void disabledPeriodic() {
        Scheduler.getInstance().run();
    }

    public void autonomousInit() {
        // schedule the autonomous command (example)
        if (autonomousCommand != null)autonomousCommand.start();
    }

    /**
     * This function is called periodically during autonomous
     */
    public void autonomousPeriodic() {
        Scheduler.getInstance().run();
        log();
    }

    public void teleopInit() {
        // This makes sure that the autonomous stops running when
        // teleop starts running. If you want the autonomous to
        // continue until interrupted by another command, remove
        // this line or comment it out.
        if (autonomousCommand != null) autonomousCommand.cancel();
    }

    /**
     * This function is called periodically during operator control
     */
    public void teleopPeriodic() {
        Scheduler.getInstance().run();
        log();
    }

    /**
     * This function is called periodically during test mode
     */
    public void testPeriodic() {
        LiveWindow.run();
        log();
    }
    private void log(){
    	counter++;
    	
    	if(counter >= 5){
    		SmartDashboard.putNumber("Left Encoder Count", driveTrain.getLtEncoderPosition());
        	SmartDashboard.putNumber("Right Encoder Count", driveTrain.getRtEncoderPosition());
        	SmartDashboard.putNumber("Encoder Difference", driveTrain.getEncoderDifference());
        	SmartDashboard.putNumber("Rt Value", driveTrain.getRtValue());
        	SmartDashboard.putNumber("Correction Value", driveTrain.getCorrection());
    		
    		counter = 0;
    }
    }
}
Where did you initiate camera? I don't see an object with that name in the variable declaring section?
Reply With Quote
  #5   Spotlight this post!  
Unread 26-01-2017, 17:07
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: 161
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: Listener from VisionThread? What do I put in this?

Don't copy-paste code that someone gives as an example and expect it to work.

And definitely don't copy and paste it into a program and then ask someone if it works before you even try to run it. It's lazy and very few people will be willing to help you.
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #6   Spotlight this post!  
Unread 26-01-2017, 17:09
mjbergman92's Avatar
mjbergman92 mjbergman92 is offline
Registered User
AKA: Malachi
FRC #3534 (House of Cards)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2026
Location: michigan
Posts: 5
mjbergman92 is an unknown quantity at this point
Re: Listener from VisionThread? What do I put in this?

I know. I am asking what to do to change the code from CameraServer to UsbCamera so it works.
Reply With Quote
  #7   Spotlight this post!  
Unread 26-01-2017, 17:18
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: 161
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: Listener from VisionThread? What do I put in this?

Have you read the screensteps page?
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #8   Spotlight this post!  
Unread 26-01-2017, 17:23
mjbergman92's Avatar
mjbergman92 mjbergman92 is offline
Registered User
AKA: Malachi
FRC #3534 (House of Cards)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2026
Location: michigan
Posts: 5
mjbergman92 is an unknown quantity at this point
Re: Listener from VisionThread? What do I put in this?

I have seen this and read this multiple times today but, I just looked and found what I was looking for

Code:
UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();
duh!
thank you guys!
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 13:15.

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