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


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

--

Legend:

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

    v37 v38  
    1414
    1515
    16 
    17 == Fifth program: Encoder
    18 In the example above, we controlled the amount of power we were delivering the motor (0=none, 1.0=full power), but that doesn't tell us how fast the robot is moving or the wheel is turning.  Just like a car or a bicycle, the speed a robot will go under a given amount of power depends on how heavy the robot is, whether it's going up a hill or down a hill, etc.  To tell how fast a wheel is turning or to determine exactly where it has turned, we use encoders.  An encoder is a device attached to the motor or wheel shaft that generates a pulse with each partial rotation (e.g. every 5-degrees of rotation, another pulse is generated).  By counting these pulses, we can tell how fast the wheel is turning or what position it is in.  You can read more about encoders [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599717-encoders-measuring-rotation-of-a-wheel-or-other-shaft here].
    19 
    20 Create another program using the !TimedRobot java template and name it !EncoderTest. Modify the generated code as follows:
    21 
    22 * Add the following imports:
    23 {{{
    24 import edu.wpi.first.wpilibj.Encoder;
    25 }}}
    26 *  Declare a variable for the Encoder in the Robot class
    27 {{{
    28   private Encoder leftEncoder;
    29 }}}
    30 * •     In robotInit() instantiate the Encoder object and reset it
    31   {{{
    32     // Quadrature encoder has A and B channels connected to DIO0, DIO1
    33     leftEncoder = new Encoder(0, 1);
    34     // Peanut wheels are 7.5" in diameter
    35     // Encoders provide 1 count per degree of rotation
    36     leftEncoder.setDistancePerPulse((3.141592*7.5)/360);
    37     // reset encoder counts to 0
    38     leftEncoder.reset();
    39   }}}
    40 * In robotPeriodic() read and display the encoder valuey
    41   {{{
    42     int encoderValue = leftEncoder.getRaw();
    43     double distance = leftEncoder.getDistance();
    44     SmartDashboard.putNumber("Encoder Value", encoderValue);
    45     SmartDashboard.putNumber("Encoder Distance", distance);
    46   }}}
    47  
    48 Note: you'll need to manually turn the left wheel forward and backward to see the Encoder values change. Try turning the wheel 360 degrees to see what value is equivalent to a full revolution of the wheel.  Notice that the raw count reports the sum of both A and B channels of the quadrature encoder.
    4916
    5017== Sixth program: Autonomous