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


Ignore:
Timestamp:
Nov 6, 2019, 1:15:30 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

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

    v1 v1  
     1You should review the Java State Machine example before exploring this code.
     2
     3== State Machine for PWM robot
     4{{{
     5/*----------------------------------------------------------------------------*/
     6/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
     7/* Open Source Software - may be modified and shared by FRC teams. The code   */
     8/* must be accompanied by the FIRST BSD license file in the root directory of */
     9/* the project.                                                               */
     10/*----------------------------------------------------------------------------*/
     11
     12package frc.robot;
     13
     14import edu.wpi.first.wpilibj.TimedRobot;
     15import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
     16import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
     17import edu.wpi.first.wpilibj.Talon;
     18import edu.wpi.first.wpilibj.Encoder;
     19
     20/**
     21 * The VM is configured to automatically run this class, and to call the
     22 * functions corresponding to each mode, as described in the TimedRobot
     23 * documentation. If you change the name of this class or the package after
     24 * creating this project, you must also update the build.gradle file in the
     25 * project.
     26 */
     27public class Robot extends TimedRobot {
     28  private static final String kDefaultAuto = "Default";
     29  private static final String kCustomAuto = "My Auto";
     30  private String m_autoSelected;
     31  private final SendableChooser<String> m_chooser = new SendableChooser<>();
     32  private Talon l_motor, r_motor;
     33  private Encoder l_encoder, r_encoder;
     34
     35  private enum AutoState { STARTING, DRIVING, TURNING, STOPPED };
     36  private AutoState state;
     37 
     38  private void driveForward() {
     39    // to go forward, left and right motors turn in opposite directions
     40    // so wheels turn in same direction
     41    l_motor.set(0.20);
     42    r_motor.set(-0.20);
     43    // reset encoders so we can tell how far we've driven
     44    l_encoder.reset();
     45    r_encoder.reset();
     46  }
     47
     48  private void turnRight() {
     49    // to turn, both motors turn in the same direction
     50    // so wheels turn in opposite directions
     51    l_motor.set(0.20);
     52    r_motor.set(0.20);
     53    // reset encoders so we can tell how far we've driven
     54    l_encoder.reset();
     55    r_encoder.reset();
     56  }
     57
     58  private void stop() {
     59    l_motor.stopMotor();
     60    r_motor.stopMotor();
     61  }
     62
     63  /**
     64   * This function is run when the robot is first started up and should be
     65   * used for any initialization code.
     66   */
     67  @Override
     68  public void robotInit() {
     69    m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
     70    m_chooser.addOption("My Auto", kCustomAuto);
     71    SmartDashboard.putData("Auto choices", m_chooser);
     72    // Talon motor controllers connected to PWM ports 0,1
     73    l_motor = new Talon(0);
     74    r_motor = new Talon(1);
     75    // Quadrature encoders connected to DIO ports 0,1 and 2,3
     76    l_encoder = new Encoder(0,1);
     77    r_encoder = new Encoder(2,3);
     78    // 7.5" wheels with 360-pulse-per-rotation encoders
     79    l_encoder.setDistancePerPulse((7.5 * 3.141592) / 360);
     80    r_encoder.setDistancePerPulse((7.5 * 3.141592) / 360);
     81  }
     82
     83  /**
     84   * This function is called every robot packet, no matter the mode. Use
     85   * this for items like diagnostics that you want ran during disabled,
     86   * autonomous, teleoperated and test.
     87   *
     88   * <p>This runs after the mode specific periodic functions, but before
     89   * LiveWindow and SmartDashboard integrated updating.
     90   */
     91  @Override
     92  public void robotPeriodic() {
     93  }
     94
     95  /**
     96   * This autonomous (along with the chooser code above) shows how to select
     97   * between different autonomous modes using the dashboard. The sendable
     98   * chooser code works with the Java SmartDashboard. If you prefer the
     99   * LabVIEW Dashboard, remove all of the chooser code and uncomment the
     100   * getString line to get the auto name from the text box below the Gyro
     101   *
     102   * <p>You can add additional auto modes by adding additional comparisons to
     103   * the switch structure below with additional strings. If using the
     104   * SendableChooser make sure to add them to the chooser code above as well.
     105   */
     106  @Override
     107  public void autonomousInit() {
     108    m_autoSelected = m_chooser.getSelected();
     109    // m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto);
     110    System.out.println("Auto selected: " + m_autoSelected);
     111    state = AutoState.STARTING;
     112  }
     113
     114  /**
     115   * This function is called periodically during autonomous.
     116   */
     117  @Override
     118  public void autonomousPeriodic() {
     119    switch (state) {
     120      case STARTING:
     121        // transition to driving state
     122        driveForward();
     123        state = AutoState.DRIVING;
     124        break;
     125      case DRIVING:
     126        if ((l_encoder.getDistance() >= 36.0) &&
     127            (r_encoder.getDistance() >= 36.0)) {
     128            // if driven desired distance, transition to turning state
     129            turnRight();
     130            state = AutoState.TURNING;
     131        }
     132        break;
     133      case TURNING:
     134        if ((l_encoder.getDistance() >= 3.0) &&
     135            (r_encoder.getDistance() <= -3.0)) {
     136            // if turned desired distance, transition to stopped state
     137            stop();
     138            state = AutoState.STOPPED;
     139        }
     140        break;
     141      case STOPPED:
     142        // stay in this state until we exit autonomous
     143        break;
     144    }
     145  }
     146
     147  /**
     148   * This function is called periodically during operator control.
     149   */
     150  @Override
     151  public void teleopPeriodic() {
     152  }
     153
     154  /**
     155   * This function is called periodically during test mode.
     156   */
     157  @Override
     158  public void testPeriodic() {
     159  }
     160}
     161}}}