Changes between Version 38 and Version 39 of ControlSystems/SoftwareTeam/Training/GettingStarted/IntroRobotJava


Ignore:
Timestamp:
Oct 30, 2019, 10:38:31 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

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

    v38 v39  
    1515
    1616
    17 == Sixth program: Autonomous
    18 In the examples above, we learned to apply power to the wheel motors and to see how far the wheel has turned using encoders.  Now we're going to put them together to drive the robot a controlled distance.  We're also going to learn about autonomous mode.  Every FRC match starts with a 15 second autonmous period where the robot drives itself and tries to accomplish some task with no human driver.  For this example, our task is just to drive 3 feet and then stop (harder than it sounds).
    19 
    20 Create another program using the !TimedRobot java template and name it !AutonomousTest1. Modify the generated code as follows:
    21 
    22 * Add the following imports:
    23 {{{
    24 import edu.wpi.first.wpilibj.Encoder;
    25 import com.ctre.phoenix.motorcontrol.can.*; // for Hazelnut
    26 import edu.wpi.first.wpilibj.Talon;         // for Macadamia
    27 }}}
    28 *  Declare variables for the Encoders and Motor controllers in the Robot class
    29 {{{
    30   private Encoder leftEncoder, rightEncoder;
    31 
    32   // Uncomment next line for Macademia
    33   // private Talon leftMotor, rightMotor;
    34   // Uncomment next line for Hazelnut
    35   // private WPI_TalonSRX leftMotor, rightMotor;
    36 }}}
    37 *  In robotInit() instantiate the Encoder and Motor objects
    38   {{{
    39     // Quadrature encoder has A and B channels connected to DIO0, DIO1
    40     leftEncoder = new Encoder(0, 1);
    41     rightEncoder = new Encoder(2, 3);
    42     // Peanut wheels are 7.5" in diameter
    43     // Encoders provide 1 count per degree of rotation
    44     leftEncoder.setDistancePerPulse((3.141592*7.5)/360);
    45     rightEncoder.setDistancePerPulse((3.141592*7.5)/360);
    46 
    47     // Uncomment the next two lines for Macademia
    48     // leftMotor = new Talon(0);
    49     // rightMotor = new Talon(1);
    50 
    51     // uncoment the next two lines for Hazelnut
    52     // leftMotor = new WPI_TalonSRX(3);
    53     // rightMotor = new WPI_TalonSRX(4);
    54   }}}
    55 
    56 * In robotPeriodic() display the encoder values
    57   {{{
    58     SmartDashboard.putNumber("Left", leftEncoder.getDistance());
    59     SmartDashboard.putNumber("Right", rightEncoder.getDistance());
    60   }}}
    61 
    62 * In autonomousInit() reset the encoder counts and start the motors:
    63   {{{
    64     // reset encoder counts to 0
    65     leftEncoder.reset();
    66     rightEncoder.reset();
    67     // start motors turning forward at 20% power
    68     leftMotor.set(0.20);
    69     // to go forward, left motor turns clockwise, right motor counter-clockwise
    70     rightMotor.set(-0.20);
    71   }}}
    72 
    73 * In the default section of autonomousPeriodic() stop if we've gone the desired distance.
    74   Notice our first use of the logical OR operator (!||) to test whether either of two conditions is true
    75   {{{
    76         if ((leftEncoder.getDistance() > 36.0) ||
    77             (rightEncoder.getDistance() > 36.0)) {
    78            leftMotor.stopMotor();
    79            rightMotor.stopMotor();
    80         }
    81    }}}
    82 
    83 When you launch the driver station, you'll need to select "Autonomous" mode (look above the Enable button) and then press the Enable button.  The autonomousInit() method will run first and then the autonomousPeriodic() method will be called repeatedly until the robot is disabled.  If your robot isn't doing what it should be, press the Disable button immediately to stop the robot.
    84 
    85 Why didn't the robot go straight? 
    86 
    87 Extra credit: See if you can make it go straight.
    8817
    8918== Seventh program: Closed Loop Control