Changes between Version 5 and Version 6 of ControlSystems/SoftwareTeam/Training/GettingStarted/CameraSwitching


Ignore:
Timestamp:
Feb 15, 2020, 12:18:38 PM (5 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/CameraSwitching

    v5 v6  
    1010
    1111import edu.wpi.first.wpilibj.TimedRobot;
     12import edu.wpi.first.wpilibj.XboxController;
    1213
    1314public class Robot extends TimedRobot {
    1415  Cameras cameras;
     16  XboxController xbox;
    1517
    1618  @Override
    1719  public void robotInit() {
    18     cameras = new Cameras(); // create the Cameras thread
    19     cameras.start();         // start thread and launch its run() method
     20    xbox = new XboxController(0); // create xbox controller object
     21    cameras = new Cameras();      // create the Cameras thread
     22    cameras.start();              // start thread and launch its run() method
     23  }
     24
     25  @Override
     26  public void robotPeriodic() {
     27    if (xbox.getAButton()) {
     28      cameras.selectFront();
     29    } else if (xbox.getBButton()) {
     30      cameras.selectRear();
     31    }
    2032  }
    2133}
     
    2941import edu.wpi.first.cameraserver.CameraServer;
    3042import edu.wpi.cscore.UsbCamera;
    31 import edu.wpi.first.wpilibj.XboxController;
     43import edu.wpi.cscore.VideoSource.ConnectionStrategy;
     44
    3245
    3346class Cameras extends Thread {
    3447    CameraServer   camServer;
    3548    UsbCamera      frontCam, rearCam;
    36     XboxController xbox;
    3749
    3850    public Cameras() {
    3951        // Camera server manages all cameras and streams
    4052        camServer = CameraServer.getInstance();
    41         // Create and start front and rear cameras
    42         frontCam  = camServer.startAutomaticCapture("front", 0);
    43         rearCam   = camServer.startAutomaticCapture("rear", 1);
    44         // Use Xbox controller to select camera stream
    45         xbox      = new XboxController(0);
     53        // Start serving front camera (USB port 0)
     54        frontCam = camServer.startAutomaticCapture("cameras", 0);
     55        // Create additional camera(s)
     56        rearCam   = new UsbCamera("rear", 1);
     57
     58        // keep video streams open for fast switching
     59        //frontCam.setConnectionStrategy(ConnectionStrategy.kKeepOpen);
     60        //rearCam.setConnectionStrategy(ConnectionStrategy.kKeepOpen);
    4661    }
    4762
     63    public void selectFront() {
     64        camServer.getServer().setSource(frontCam);
     65    }
     66   
     67    public void selectRear() {
     68        camServer.getServer().setSource(rearCam);
     69    }
     70
     71    // Run method is invoked when start method is called
    4872    public void run() {
    4973        while (true) {
    50             if (xbox.getAButton()) {
    51                 // A button selects front camera
    52                 camServer.getServer().setSource(frontCam);
    53             }
    54             if (xbox.getBButton()) {
    55                 // B button selects rear camera
    56                 camServer.getServer().setSource(rearCam);
    57             }
     74            // run forever
    5875        }
    5976    }