Changes between Version 3 and Version 4 of ControlSystems/SoftwareTeam/Training/GettingStarted/Autonomous
- Timestamp:
- Nov 3, 2019, 8:40:20 PM (6 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/Training/GettingStarted/Autonomous
v3 v4 10 10 import edu.wpi.first.wpilibj.Talon; // for Macadamia 11 11 }}} 12 12 13 * Declare variables for the Encoders and Motor controllers in the Robot class 13 14 {{{ 14 private Encoder leftEncoder, rightEncoder;15 15 16 // Uncomment next line for Macademia16 // Uncomment next 2 lines for PWM motor controllers (Macademia) 17 17 // private Talon leftMotor, rightMotor; 18 // Uncomment next line for Hazelnut 18 // private Encoder leftEncoder, rightEncoder; 19 20 // Uncomment next line for CAN motor controllers (Hazelnut) 19 21 // private WPI_TalonSRX leftMotor, rightMotor; 20 22 }}} 23 21 24 * In robotInit() instantiate the Encoder and Motor objects 22 {{{ 23 // Quadrature encoder has A and B channels connected to DIO0, DIO1 25 For PWM Motor Controller robots (Macadamia): 26 {{{ 27 // Quadrature encoder has A and B channels connected to DIO0,1, DIO2,3 24 28 leftEncoder = new Encoder(0, 1); 25 29 rightEncoder = new Encoder(2, 3); … … 28 32 leftEncoder.setDistancePerPulse((3.141592*7.5)/360); 29 33 rightEncoder.setDistancePerPulse((3.141592*7.5)/360); 34 // PWM motor controllers connected to PWM0, PWM1 35 leftMotor = new Talon(0); 36 rightMotor = new Talon(1); 37 }}} 30 38 31 // Uncomment the next two lines for Macademia 32 // leftMotor = new Talon(0); 33 // rightMotor = new Talon(1); 34 35 // uncoment the next two lines for Hazelnut 36 // leftMotor = new WPI_TalonSRX(3); 37 // rightMotor = new WPI_TalonSRX(4); 39 For CAN Motor Controller robots (Hazelnut): 40 {{{ 41 // CAN motor controllers connected to CAN bus (addrs 3,4) 42 leftMotor = new WPI_TalonSRX(3); 43 rightMotor = new WPI_TalonSRX(4); 44 // clear any previously configured settings 45 leftMotor.configFactoryDefault(); 46 rightMotor.configFactoryDefault(); 47 // Encoders are connected directly to the motor controllers 48 leftMotor.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder); 49 rightMotor.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder); 38 50 }}} 39 51 40 52 * In robotPeriodic() display the encoder values 53 54 * For PWM controller robots (Macadamia): 41 55 {{{ 42 56 SmartDashboard.putNumber("Left", leftEncoder.getDistance()); … … 44 58 }}} 45 59 60 * For CAN motor controllers (Hazelnut) note the use of the [https://www.ctr-electronics.com/downloads/api/java/html/classcom_1_1ctre_1_1phoenix_1_1motorcontrol_1_1can_1_1_base_motor_controller.html#afb934a11682e710df123eec35aba1ba9 getSelectedFeedbackSensor()] method of the motor controller object which returns raw encoder counts. 61 Add the following function to the Robot class: 62 {{{ 63 double raw2inches(int raw_count, int counts_per_rotation, double wheel_diameter) { 64 return (raw_count / counts_per_rotation) * (3.141492 * wheel_diameter); 65 {{{ 66 Add the following to the robotPeriodic() method: 67 // Encoders connected to CAN controller return raw counts 68 double l_raw = leftMotor.getSelectedSensorPosition(); 69 double r_raw = rightMotor.getSelectedSensorPosition(); 70 // display distances on smart dashboard 71 SmartDashboard.putNumber("Left", raw2inches(l_raw, 360*2, 7.5)); 72 SmartDashboard.putNumber("Right", raw2inches(r_raw, 360*2, 7.5)); 73 }}} 74 46 75 * In autonomousInit() reset the encoder counts and start the motors: 47 76 {{{ 48 // reset encoder counts to 049 leftEncoder.reset();50 rightEncoder.reset();77 // For PWM controllers (Macadamia) uncomment the next 2 lines to reset encoder counts to 0 78 // leftEncoder.reset(); 79 // rightEncoder.reset(); 51 80 // start motors turning forward at 20% power 52 81 leftMotor.set(0.20); … … 57 86 * In the default section of autonomousPeriodic() stop if we've gone the desired distance. 58 87 Notice our first use of the logical OR operator (!||) to test whether either of two conditions is true 88 * For PWM motor controllers: 59 89 {{{ 60 90 if ((leftEncoder.getDistance() > 36.0) || 61 91 (rightEncoder.getDistance() > 36.0)) { 92 leftMotor.stopMotor(); 93 rightMotor.stopMotor(); 94 } 95 }}} 96 * For CAN motor controllers: 97 {{{ 98 double l_raw = leftMotor.getSelectedSensorPosition(); 99 double r_raw = rightMotor.getSelectedSensorPosition(); 100 if ((raw2inches(l_raw, 360*2, 7.5) > 36.0) || 101 (raw2inches(r_raw, 360*2, 7.5) > 36.0)) { 62 102 leftMotor.stopMotor(); 63 103 rightMotor.stopMotor();