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


Ignore:
Timestamp:
Nov 12, 2019, 6:44:26 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

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

    v1 v2  
    1010
    1111import edu.wpi.first.wpilibj.TimedRobot;
     12import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
    1213import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
    1314import edu.wpi.first.wpilibj.Encoder;
     
    2223 */
    2324public 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<>();
    2429  private Talon leftMotor, rightMotor;
    2530  private Encoder leftEncoder, rightEncoder;
     
    3136  @Override
    3237  public void robotInit() {
     38    m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
     39    m_chooser.addOption("My Auto", kCustomAuto);
     40    SmartDashboard.putData("Auto choices", m_chooser);
    3341   // Quadrature encoder has A and B channels connected to DIO0,1, DIO2,3
    3442   leftEncoder = new Encoder(0, 1);
    35    rightEncoder = new Encoder(2, 3);
     43   rightEncoder = new Encoder(2, 3, true); // right side direction is reversed
    3644   // Peanut wheels are 7.5" in diameter
    3745   // Encoders provide 1 count per degree of rotation
     
    4149   leftMotor = new Talon(0);
    4250   rightMotor = new Talon(1);
     51   // right side must turn in opposite direction
     52   rightMotor.setInverted(true);
    4353  }
    4454
     
    5868
    5969  /**
     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.
    6079   */
    6180  @Override
    6281  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);
    6385    // Reset encoder counts to 0
    6486    leftEncoder.reset();
     
    6688    // start motors turning forward at 20% power
    6789    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);
    7091  }
    7192
     
    7596  @Override
    7697  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    }
    82111  }
    83112