Preseason Lesson5: Difference between revisions

From 1511Wookiee
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
<div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><span style="font-size: large;">'''Read encoder while&nbsp;motor moves'''</span></div> <div class="mw-parser-output">
<div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><span style="font-size: large;">'''Read encoder while&nbsp;motor moves'''</span></div> <div class="mw-parser-output">
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 window&nbsp;motor), because that one has the encoder attached to it.
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 window&nbsp;motor), because that one has the encoder attached to it.


Line 18: Line 18:
In Robot.h you will need an object/variable that is the encoder. The class/object type is: frc::Encoder.&nbsp;You will name the objects: m_encoder.&nbsp;Add this to the&nbsp;'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 that is the encoder. The class/object type is: frc::Encoder.&nbsp;You will name the objects: m_encoder.&nbsp;Add this to the&nbsp;'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&nbsp;motor. The class/object type is: TalonSRX. Notice, there is no frc:: in front of it.
In Robot.h you will need an object/variable for the window&nbsp;motor. The class/object type is: TalonSRX. Notice, there is no frc:: in front of it. You will need to add an include for this at the top of the file (after any existing includes):
<pre>#include <ctre/Phoenix.h></pre>


&nbsp;
&nbsp;
Line 68: Line 69:


&nbsp;
&nbsp;
</div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
</div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>

Revision as of 16:13, 9 December 2019

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 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 class/object type is: TalonSRX. Notice, there is no frc:: in front of it. You will need to add an include for this at the top of the file (after any existing includes):

#include <ctre/Phoenix.h>

 

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).

 

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 wiper motor
      re-get current encoder's initial position
  }
}

 

 

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