Changes between Version 1 and Version 2 of ControlSystems/SoftwareTeam/Training/GettingStarted/Autonomous/PWMCode
- Timestamp:
- Nov 12, 2019, 6:44:26 PM (6 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/Training/GettingStarted/Autonomous/PWMCode
v1 v2 10 10 11 11 import edu.wpi.first.wpilibj.TimedRobot; 12 import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; 12 13 import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; 13 14 import edu.wpi.first.wpilibj.Encoder; … … 22 23 */ 23 24 public class Robot extends TimedRobot { 25 private static final String kDefaultAuto = "Default"; 26 private static final String kCustomAuto = "My Auto"; 27 private String m_autoSelected; 28 private final SendableChooser<String> m_chooser = new SendableChooser<>(); 24 29 private Talon leftMotor, rightMotor; 25 30 private Encoder leftEncoder, rightEncoder; … … 31 36 @Override 32 37 public void robotInit() { 38 m_chooser.setDefaultOption("Default Auto", kDefaultAuto); 39 m_chooser.addOption("My Auto", kCustomAuto); 40 SmartDashboard.putData("Auto choices", m_chooser); 33 41 // Quadrature encoder has A and B channels connected to DIO0,1, DIO2,3 34 42 leftEncoder = new Encoder(0, 1); 35 rightEncoder = new Encoder(2, 3 );43 rightEncoder = new Encoder(2, 3, true); // right side direction is reversed 36 44 // Peanut wheels are 7.5" in diameter 37 45 // Encoders provide 1 count per degree of rotation … … 41 49 leftMotor = new Talon(0); 42 50 rightMotor = new Talon(1); 51 // right side must turn in opposite direction 52 rightMotor.setInverted(true); 43 53 } 44 54 … … 58 68 59 69 /** 70 * This autonomous (along with the chooser code above) shows how to select 71 * between different autonomous modes using the dashboard. The sendable 72 * chooser code works with the Java SmartDashboard. If you prefer the 73 * LabVIEW Dashboard, remove all of the chooser code and uncomment the 74 * getString line to get the auto name from the text box below the Gyro 75 * 76 * <p>You can add additional auto modes by adding additional comparisons to 77 * the switch structure below with additional strings. If using the 78 * SendableChooser make sure to add them to the chooser code above as well. 60 79 */ 61 80 @Override 62 81 public void autonomousInit() { 82 m_autoSelected = m_chooser.getSelected(); 83 // m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto); 84 System.out.println("Auto selected: " + m_autoSelected); 63 85 // Reset encoder counts to 0 64 86 leftEncoder.reset(); … … 66 88 // start motors turning forward at 20% power 67 89 leftMotor.set(0.20); 68 // to go forward, left motor turns clockwise, right motor counter-clockwise 69 rightMotor.set(-0.20); 90 rightMotor.set(0.20); 70 91 } 71 92 … … 75 96 @Override 76 97 public void autonomousPeriodic() { 77 if ((leftEncoder.getDistance() > 36.0) || 78 (rightEncoder.getDistance() > 36.0)) { 79 leftMotor.stopMotor(); 80 rightMotor.stopMotor(); 81 } 98 switch (m_autoSelected) { 99 case kCustomAuto: 100 // Put custom auto code here 101 break; 102 case kDefaultAuto: 103 default: 104 if ((leftEncoder.getDistance() > 36.0) || 105 (rightEncoder.getDistance() > 36.0)) { 106 leftMotor.stopMotor(); 107 rightMotor.stopMotor(); 108 } 109 break; 110 } 82 111 } 83 112