Preseason Lesson5
The goal is to read an enocder while a motor is turning. Stop the motor after it turns one revolution. You will also print the encoder's value to the Driver Station.
You will use a different motor (called a window motor), because that one has the encoder attached to it.
It is expected that you completed Lesson 4 and have the code available.
Link to the encoder documentation: Encoder
An encoder is a counter that increments. The counter goes up the same amount for 1 revolution. Our test board has a 256 count encoder, which means it will count up by 256 for each full revolution. When the robot is powered up, the encoder is not at a know value. You will need to read the value to know where it is starting from.
In Robot.h you will need an object/variable that is the encoder. The class/object type is: frc::Encoder. You will name the objects: m_encoder. Add this to the 'private:' section as you have done for other objects/variables. Pay attention to the parameters for the constructor.
In Robot.h you will need an object/variable for the window motor. The motor is connected to a Talon, but for 2019 preseason we just move the connector so we could use a Jaguar (like the previous lessons). If the Talon is used, then the class/object type is: TalonSRX. Notice, there is no frc:: in front of it. API documentation is here: TalonSRX. You will need to add an include for this at the top of the file (after any existing includes):
#include <ctre/Phoenix.h>
If it doesn't compile, then you will need to install the library for it:
- Download from here: CTRE Phoenix Toolsuite
- click the newest ZIP next to "Installer"
- run the exe that is in the ZIP, and follow the prompts
- TBD: need to add this to the VS IDE
In Robot.cpp, you will want to get the encoder position and store it in a variable. The variable can be declared at the top of the Robot::OperatorControl() method (i.e. the first line). Refer to the encoder documentation to see what data type Get() returns. You will call Get() and save what it returned into your varaible. You will need two variables: one to store the initial value and one to store the current value.
To print out the encoder position to the Driver Station's console, you will use the method 'printf()'.
For example: printf("Encoder: %d %d\n", (int)initialposition, (int)currentposition);
The text in the double quotes is want is printed. The first '%d' indicates the first variable parameter (in this example initialposition) is an integer, so it will format the value in initialposition and print that. The second '%d' indicates the second variable parameter (in this example currentposition) is an integer. The '\n' indicates to print a newline (i.e. like hitting the Enter key when typing to put the cursor on a new line).
declare initial position variable declare current position variable get current encoder's initial position while (IsOperatorControl() && IsEnabled()) { if (joystick trigger is pressed) { assign your variable the encoder's current position print the encoder's initial and current positions if (encoder has not turned a full revolution) { spin window motor forward 0.2 } else { stop window motor re-get current encoder's initial position } } else { stop window motor re-get current encoder's initial position } frc::Wait(0.005); }
After you have modified Robot.h and Robot.cpp, build the project. Once project builds without errors, let instructor know you are ready to try to delpoy and test. Do not deploy until told to.
Test
- pull trigger and watch the window motor turn
- the motor should stop when the motor has turn one full revolution
- release the trigger