Changes between Version 3 and Version 4 of ControlSystems/SoftwareTeam/Training/GettingStarted/Autonomous


Ignore:
Timestamp:
Nov 3, 2019, 8:40:20 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

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

    v3 v4  
    1010import edu.wpi.first.wpilibj.Talon;         // for Macadamia
    1111}}}
     12
    1213*  Declare variables for the Encoders and Motor controllers in the Robot class
    1314{{{
    14   private Encoder leftEncoder, rightEncoder;
    1515
    16   // Uncomment next line for Macademia
     16  // Uncomment next 2 lines for PWM motor controllers (Macademia)
    1717  // 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)
    1921  // private WPI_TalonSRX leftMotor, rightMotor;
    2022}}}
     23
    2124*  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
    2428    leftEncoder = new Encoder(0, 1);
    2529    rightEncoder = new Encoder(2, 3);
     
    2832    leftEncoder.setDistancePerPulse((3.141592*7.5)/360);
    2933    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  }}}
    3038
    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);
    3850  }}}
    3951
    4052* In robotPeriodic() display the encoder values
     53
     54  * For PWM controller robots (Macadamia):
    4155  {{{
    4256    SmartDashboard.putNumber("Left", leftEncoder.getDistance());
     
    4458  }}}
    4559
     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
    4675* In autonomousInit() reset the encoder counts and start the motors:
    4776  {{{
    48     // reset encoder counts to 0
    49     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();
    5180    // start motors turning forward at 20% power
    5281    leftMotor.set(0.20);
     
    5786* In the default section of autonomousPeriodic() stop if we've gone the desired distance.
    5887  Notice our first use of the logical OR operator (!||) to test whether either of two conditions is true
     88  * For PWM motor controllers:
    5989  {{{
    6090        if ((leftEncoder.getDistance() > 36.0) ||
    6191            (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)) {
    62102           leftMotor.stopMotor();
    63103           rightMotor.stopMotor();