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


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

--

Legend:

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

    v1 v2  
    11== Encoders
    2 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].
     2In 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, how good the traction is, etc.  To determine how fast a wheel is turning or exactly how far it has turned, we use encoders. 
    33
     4An encoder is a device attached to the motor or wheel shaft that generates a pulse with each partial rotation (e.g. every 1-degree or 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.  We can use this information to do things like make a robot travel a fixed distance or at a set speed.  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].
     5
     6We connect encoders either directly to !RoboRio DIO ports or to an intelligent motor controller such as the !TalonSRX.  The intelligent motor controllers can make use of the encoder data independently which offloads work from the !RoboRIO; in general, if you have smart motor controllers, the encoders should connect to them.  We'll provide examples both ways:
     7
     8=== Encoder connected to RoboRIO DIO (Macademia)
    49Create another program using the !TimedRobot java template and name it !EncoderTest. Modify the generated code as follows:
    510
     
    2934    SmartDashboard.putNumber("Encoder Distance", distance);
    3035  }}}
     36
     37=== Encoder connected to a TalonSRX smart motor controller (Hazelnut)
     38Create another program using the !TimedRobot java template and name it !EncoderTest2. Modify the generated code as follows:
     39
     40* Add the following imports:
     41{{{
     42   import com.ctre.phoenix.motorcontrol.can*;
     43   import com.ctre.phoenix.motorcontrol.FeedbackDevice;
     44}}}
     45*  Declare a variable for the smart motor controller with connected encoder in the Robot class
     46{{{
     47   private WPI_TalonSRX leftMotor;
     48}}}
     49* •     In robotInit() instantiate the motor controller object and reset it
     50{{{
     51   // TalonSRX is configured with CAN bus address 3
     52   leftMotor = new WPI_TalonSRX(3);
     53   // Clear any non default configuration/settings
     54   leftMotor.configFactoryDefaults();
     55   // A quadrature encoder is connected to the TalonSRX
     56   leftMotor.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder);
     57  }}}
     58* In robotPeriodic() read and display the encoder valuey
     59  {{{
     60    SmartDashboard.putNumber("Encoder value", leftMotor.getSelectedSensorPosition());
     61  }}}
    3162 
    3263Note: 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.