Changes between Version 5 and Version 6 of ControlSystems/SoftwareTeam/Training/GettingStarted/CameraSwitching
- Timestamp:
- Feb 15, 2020, 12:18:38 PM (5 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/Training/GettingStarted/CameraSwitching
v5 v6 10 10 11 11 import edu.wpi.first.wpilibj.TimedRobot; 12 import edu.wpi.first.wpilibj.XboxController; 12 13 13 14 public class Robot extends TimedRobot { 14 15 Cameras cameras; 16 XboxController xbox; 15 17 16 18 @Override 17 19 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 } 20 32 } 21 33 } … … 29 41 import edu.wpi.first.cameraserver.CameraServer; 30 42 import edu.wpi.cscore.UsbCamera; 31 import edu.wpi.first.wpilibj.XboxController; 43 import edu.wpi.cscore.VideoSource.ConnectionStrategy; 44 32 45 33 46 class Cameras extends Thread { 34 47 CameraServer camServer; 35 48 UsbCamera frontCam, rearCam; 36 XboxController xbox;37 49 38 50 public Cameras() { 39 51 // Camera server manages all cameras and streams 40 52 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); 46 61 } 47 62 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 48 72 public void run() { 49 73 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 58 75 } 59 76 }