Changes between Initial Version and Version 1 of ControlSystems/SoftwareTeam/Training/GettingStarted/Autonomous/PWM


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

--

Legend:

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

    v1 v1  
     1Modify the generated code as follows:
     2
     3* Add the following imports:
     4{{{
     5import edu.wpi.first.wpilibj.Encoder;
     6import com.ctre.phoenix.motorcontrol.can.*; // for Hazelnut
     7import edu.wpi.first.wpilibj.Talon;         // for Macadamia
     8}}}
     9
     10*  Declare variables for the Encoders and Motor controllers in the Robot class
     11{{{
     12
     13  // Uncomment next 2 lines for PWM motor controllers (Macademia)
     14  // private Talon leftMotor, rightMotor;
     15  // private Encoder leftEncoder, rightEncoder;
     16
     17  // Uncomment next line for CAN motor controllers (Hazelnut)
     18  // private WPI_TalonSRX leftMotor, rightMotor;
     19}}}
     20
     21*  In robotInit() instantiate the Encoder and Motor objects
     22  * For PWM Motor Controller robots (Macadamia):
     23  {{{   
     24    // Quadrature encoder has A and B channels connected to DIO0,1, DIO2,3
     25    leftEncoder = new Encoder(0, 1);
     26    rightEncoder = new Encoder(2, 3);
     27    // Peanut wheels are 7.5" in diameter
     28    // Encoders provide 1 count per degree of rotation
     29    leftEncoder.setDistancePerPulse((3.141592*7.5)/360);
     30    rightEncoder.setDistancePerPulse((3.141592*7.5)/360);
     31    // PWM motor controllers connected to PWM0, PWM1
     32    leftMotor = new Talon(0);
     33    rightMotor = new Talon(1);
     34  }}}
     35  * For CAN Motor Controller robots (Hazelnut):
     36  {{{
     37    // CAN motor controllers connected to CAN bus (addrs 3,4)
     38    leftMotor = new WPI_TalonSRX(3);
     39    rightMotor = new WPI_TalonSRX(4);
     40    // clear any previously configured settings
     41    leftMotor.configFactoryDefault();
     42    rightMotor.configFactoryDefault();
     43    // Encoders are connected directly to the motor controllers
     44    leftMotor.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder);
     45    rightMotor.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder);
     46  }}}
     47
     48* For PWM controller robots (Macadamia),[[BR]]
     49  in robotPeriodic() display the encoder values:
     50  {{{
     51    SmartDashboard.putNumber("Left", leftEncoder.getDistance());
     52    SmartDashboard.putNumber("Right", rightEncoder.getDistance());
     53  }}}
     54* For CAN motor controllers (Hazelnut): 
     55     * First add the following new method to the Robot class:
     56     {{{
     57        double raw2inches(int raw_count, int counts_per_rotation, double wheel_diameter) {
     58           return (raw_count / counts_per_rotation) * (3.141492 * wheel_diameter);
     59        }
     60     }}}
     61     * Add the following to the robotPeriodic() method;  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:
     62     {{{
     63        // Encoders connected to CAN controller return raw counts
     64        int l_raw = leftMotor.getSelectedSensorPosition();
     65        int r_raw = rightMotor.getSelectedSensorPosition();
     66        // display distances on smart dashboard
     67        SmartDashboard.putNumber("Left", raw2inches(l_raw, 360*2, 7.5));
     68        SmartDashboard.putNumber("Right", raw2inches(r_raw, 360*2, 7.5));
     69     }}}
     70
     71* In autonomousInit() reset the encoder counts and start the motors:
     72  {{{
     73    // For PWM controllers (Macadamia) uncomment the next 2 lines to reset encoder counts to 0
     74    // leftEncoder.reset();
     75    // rightEncoder.reset();
     76    // start motors turning forward at 20% power
     77    leftMotor.set(0.20);
     78    // to go forward, left motor turns clockwise, right motor counter-clockwise
     79    rightMotor.set(-0.20);
     80  }}}
     81
     82* In the default section of autonomousPeriodic() stop if we've gone the desired distance.
     83  Notice our first use of the logical OR operator (!||) to test whether either of two conditions is true
     84  * For PWM motor controllers:
     85  {{{
     86        if ((leftEncoder.getDistance() > 36.0) ||
     87            (rightEncoder.getDistance() > 36.0)) {
     88           leftMotor.stopMotor();
     89           rightMotor.stopMotor();
     90        }
     91  }}}
     92  * For CAN motor controllers:
     93  {{{
     94        int l_raw = leftMotor.getSelectedSensorPosition();
     95        int r_raw = rightMotor.getSelectedSensorPosition();
     96        if ((raw2inches(l_raw, 360*2, 7.5) > 36.0) ||
     97            (raw2inches(r_raw, 360*2, 7.5) > 36.0)) {
     98           leftMotor.stopMotor();
     99           rightMotor.stopMotor();
     100        }
     101  }}}