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


Ignore:
Timestamp:
Nov 3, 2019, 9:04:31 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

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

    v1 v2  
    33* Add the following imports:
    44{{{
    5 import edu.wpi.first.wpilibj.Encoder;
    6 import com.ctre.phoenix.motorcontrol.can.*; // for Hazelnut
    7 import edu.wpi.first.wpilibj.Talon;         // for Macadamia
     5   import edu.wpi.first.wpilibj.Encoder;
     6   import edu.wpi.first.wpilibj.Talon;
    87}}}
    98
    109*  Declare variables for the Encoders and Motor controllers in the Robot class
    1110{{{
    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;
     11   private Talon leftMotor, rightMotor;
     12   private Encoder leftEncoder, rightEncoder;
    1913}}}
    2014
    2115*  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   }}}
     16{{{   
     17   // Quadrature encoder has A and B channels connected to DIO0,1, DIO2,3
     18   leftEncoder = new Encoder(0, 1);
     19   rightEncoder = new Encoder(2, 3);
     20   // Peanut wheels are 7.5" in diameter
     21   // Encoders provide 1 count per degree of rotation
     22   leftEncoder.setDistancePerPulse((3.141592*7.5)/360);
     23   rightEncoder.setDistancePerPulse((3.141592*7.5)/360);
     24   // PWM motor controllers connected to PWM0, PWM1
     25   leftMotor = new Talon(0);
     26   rightMotor = new Talon(1);
     27}}}
    4728
    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      }}}
     29* In robotPeriodic() display the encoder values:
     30{{{
     31  SmartDashboard.putNumber("Left", leftEncoder.getDistance());
     32  SmartDashboard.putNumber("Right", rightEncoder.getDistance());
     33}}}
    7034
    7135* 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   }}}
     36{{{
     37  // Rreset encoder counts to 0
     38  leftEncoder.reset();
     39  rightEncoder.reset();
     40  // start motors turning forward at 20% power
     41  leftMotor.set(0.20);
     42  // to go forward, left motor turns clockwise, right motor counter-clockwise
     43  rightMotor.set(-0.20);
     44}}}
    8145
    8246* In the default section of autonomousPeriodic() stop if we've gone the desired distance.
    8347  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   }}}
     48{{{
     49  if ((leftEncoder.getDistance() > 36.0) ||
     50      (rightEncoder.getDistance() > 36.0)) {
     51      leftMotor.stopMotor();
     52      rightMotor.stopMotor();
     53  }
     54}}}