Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Technical Discussion (http://www.chiefdelphi.com/forums/forumdisplay.php?f=22)
-   -   How to make the camera work? (Java) (http://www.chiefdelphi.com/forums/showthread.php?t=134443)

5580SQUAD 12-02-2015 17:19

How to make the camera work? (Java)
 
We are trying to get the camera to show it's display on the dashboard but we've failed to get any kind of input showing.




And here's our code:

Code:

package org.usfirst.frc.team5580.robot;
 
import com.ni.vision.NIVision;
import com.ni.vision.NIVision.DrawMode;
import com.ni.vision.NIVision.Image;
import com.ni.vision.NIVision.ShapeMode;
 
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.Timer;
 
/**
 * 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 {
    /**
    * This function is run when the robot is first started up and should be
    * used for any initialization code.
    */
        final int JOYSTICK_PORT = 0; //Computer port 0
        final int FRONT_LEFT_SC = 1; //PWM, PORT 0
        final int FRONT_RIGHT_SC = 2; // PWM, PORT 1
        final int BACK_LEFT_SC = 3; // PWM, PORT 2
        final int BACK_RIGHT_SC = 4; //PWM, PORT 3
        final int LIFT_SC = 5; //PWM, PORT 4
        int session;
    Image frame;
     
        Joystick driver;
        Talon frontLeft;
        Talon frontRight;
        Talon backLeft;
        Talon backRight;
        DoubleSolenoid sol1;
        Compressor comp;
        Talon lift;
        CameraServer camera;
 
    public void robotInit() {
        driver = new Joystick(JOYSTICK_PORT);
        frontLeft = new Talon(FRONT_LEFT_SC);
        frontRight= new Talon(FRONT_RIGHT_SC);
        backLeft = new Talon(BACK_LEFT_SC);
        backRight= new Talon(BACK_RIGHT_SC);
        sol1 = new DoubleSolenoid(0,1);
        comp = new Compressor(0);
        comp.setClosedLoopControl(true);
        lift = new Talon(LIFT_SC);
        frame = NIVision.imaqCreateImage(NIVision.ImageType.IMAGE_RGB, 0);
 
        // the camera name (ex "cam0") can be found through the roborio web interface
        session = NIVision.IMAQdxOpenCamera("cam0",
                NIVision.IMAQdxCameraControlMode.CameraControlModeController);
        NIVision.IMAQdxConfigureGrab(session);
    }
 
    /**
    * This function is called periodically during autonomous
    */
    public void autonomousPeriodic() {
        while (isEnabled()&&isAutonomous()){
             
        }
    }
 
    /**
    * This function is called periodically during operator control
    */
    public void teleopPeriodic() {
        while (isEnabled()&&isOperatorControl()){
                double leftSpeed = driver.getRawAxis(1)*-1;
                double rightSpeed = driver.getRawAxis(5);
                double turns=driver.getRawAxis(3)*-1;
                boolean liftSpeed = driver.getRawButton(2);
                boolean liftSpeed1 = driver.getRawButton(3);
                frontLeft.set(leftSpeed);
                backLeft.set(leftSpeed);
                frontRight.set(rightSpeed);
                backRight.set(rightSpeed);
                        boolean push = driver.getRawButton(1);
                        boolean pull = driver.getRawButton(4);
                        if(turns <0){
                                frontLeft.set(turns);
                        }
                        else if(turns>0){
                             
                        }
                        if (liftSpeed == true){
                                lift.set(1);
                        }
                        else if(liftSpeed1 == true) {
                                lift.set(-1);
                        }
                                else {
                     
                                lift.set(0);
                        }
                     
                if (push == true){
                  sol1.set(DoubleSolenoid.Value.kReverse);
             
                }
             
                if (pull == true){
                        sol1.set(DoubleSolenoid.Value.kForward);
                }
             
             
 
        }
    }
    public void operatorControl() {
        NIVision.IMAQdxStartAcquisition(session);
 
        /**
        * grab an image, draw the circle, and provide it for the camera server
        * which will in turn send it to the dashboard.
        */
        NIVision.Rect rect = new NIVision.Rect(10, 10, 100, 100);
 
        while (isOperatorControl() && isEnabled()) {
 
            NIVision.IMAQdxGrab(session, frame, 1);
            NIVision.imaqDrawShapeOnImage(frame, frame, rect,
                    DrawMode.DRAW_VALUE, ShapeMode.SHAPE_OVAL, 0.0f);
         
            CameraServer.getInstance().setImage(frame);
 
            /** robot code here! **/
            Timer.delay(0.005);        // wait for a motor update time
        }
        NIVision.IMAQdxStopAcquisition(session);
    }
 
 
    /**
    * This function is called periodically during test mode
    */
    public void testPeriodic() {
 
    }
 
}


If you have any idea on what we should do please post a reply

codedr 18-02-2015 13:02

Re: How to make the camera work? (Java)
 
We got it to work by getting the cameraServer instance, calling the cameraServer with "cam0" argument and calling the start capture method.

codedr 18-02-2015 13:07

Re: How to make the camera work? (Java)
 
import edu.wpi.first.wpilibj.CameraServer;


CameraServer cams = CameraServer.getInstance();
// set any cam parameters, then start capture
cams.startAutomaticCapture("cam0");

JML 18-02-2015 16:18

Re: How to make the camera work? (Java)
 
Our team had trouble viewing the camera on the default dashboard, viewing it with smart dashboard or another program may be easier. To use smart dashboard, you send the image with the camera server class (we don't use automatic capture so we can set the frame rate)

Code:

int count = 0;

        public void teleopPeriodic() {
                currentTime = Timer.getMatchTime();
                if (count > 3) {
                        NIVision.IMAQdxGrab(session, frame, 1);
                        CameraServer.getInstance().setImage(frame);
                        lastUpdateTime = currentTime;
                        count = 0;

                }
                count += 1;
        }

Edit: You also need to hit the play button for it to play on the Default Dashboard

MaGiC_PiKaChU 18-02-2015 16:45

Re: How to make the camera work? (Java)
 
Our team had to set the quality to 25% for the feed to show

Alan Anderson 19-02-2015 00:18

Re: How to make the camera work? (Java)
 
Quote:

Originally Posted by JML (Post 1446544)
Edit: You also need to hit the play button for it to play on the Default Dashboard

The play button is not associated with the camera image.


All times are GMT -5. The time now is 14:01.

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