Preseason Lesson5

From 1511Wookiee
Revision as of 12:48, 9 December 2019 by Programming (talk | contribs) (Created page with "<div class="mw-parser-output"><div class="mw-parser-output"><span style="font-size: large;">'''Read encoder while motor moves'''</span></div> <div class="mw-parser-output...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
Read encoder while motor moves

The goal is to read an enocder while a motor is turning. Stop the motor after it turns one revolution. You will use a different motor (called a wiper 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 wiper motor. Determine what type of motor controller the wiper motor is using, so you can determine what object/variable type you will need.

 

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.

 

You will print out the encoder position to the console using 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).

 

<span style="color: rgb(231, 76, 60);">get current encoder's initial position</span>
while (IsOperatorControl() && IsEnabled()) {
   if (joystick trigger is pressed) {
       <font color="#e74c3c">assign your variable the encoder's current position
       print the encoder's initial and current positions</font>
       if <span style="color: rgb(231, 76, 60);">(encoder has not turned a full revolution)</span> {
           spin <span style="color: rgb(231, 76, 60);">wiper </span>motor forward<span style="color: rgb(231, 76, 60);"> 0.2</span>
       }
       else {
           stop <span style="color: rgb(231, 76, 60);">wiper </span>motor
           <span style="color: rgb(231, 76, 60);">re-get current </span><font color="#e74c3c">encoder's initial position</font>
       }
   }
   else {
       stop <span style="color: rgb(231, 76, 60);">wiper </span>motor
       <span style="color: rgb(231, 76, 60);">re-get current </span><font color="#e74c3c">encoder's initial position</font>
   }
}

 

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

  1. pull trigger and watch the wiper motor turn
  2. the motor should stop when the motor has turn one full revolution
  3. release the trigger