Preseason Lesson2: 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"><span style="font-size: large;">'''Control motor direction using the joystick'''</span>  
<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;">'''Control motor direction using the joystick'''</span>  
The goal is to turn the motor at 1/4 speed forward or backward, using the joystick, but only if the joystick trigger is being pressed.
The goal is to turn the motor at 1/4 speed forward or backward, using the joystick, but only if the joystick trigger is being pressed.


Line 32: Line 32:
You will want to first test if the trigger is pressed. If it is pressed, then you will look at the position to see if the position to determine how to turn the motor. Here is the pseudo code (i.e. not actual code, replace the English words with actual code):
You will want to first test if the trigger is pressed. If it is pressed, then you will look at the position to see if the position to determine how to turn the motor. Here is the pseudo code (i.e. not actual code, replace the English words with actual code):


    if (joystick trigger is pressed) {
while (IsOperatorControl() && IsEnabled()) {
<span style="color: rgb(231, 76, 60);">  if (joystick trigger is pressed) {
         if (joystick position is greater than 0.33) {
         if (joystick position is greater than 0.33) {
             spin motor forward 1/4 speed
             spin motor forward 1/4 speed
Line 42: Line 43:
             stop motor
             stop motor
         }
         }
     }
     }</span>
    frc::Wait(0.005);
}


The 0.33 and -0.33 is make sure the joystick has moved away from the center. Ideally, you would get 0 when the joystick is at the center. However, joysticks do not always go back to true 0.
The 0.33 and -0.33 is make sure the joystick has moved away from the center. Ideally, you would get 0 when the joystick is at the center. However, joysticks do not always go back to true 0.
Line 64: Line 67:
#modifiy code so motor does not turn  
#modifiy code so motor does not turn  
#build the project. when there are no error ask instructor to review your code and then ask to test the code  
#build the project. when there are no error ask instructor to review your code and then ask to test the code  
</div> </div> </div> </div> </div> </div> </div> </div> </div>
</div> </div> </div> </div> </div> </div> </div> </div> </div> </div>

Revision as of 11:03, 15 December 2019

Control motor direction using the joystick

The goal is to turn the motor at 1/4 speed forward or backward, using the joystick, but only if the joystick trigger is being pressed.

 

It is expected that you completed Lesson 1 and have the code available.

 

Link to the Joystick documentation: Joystick

 

Part 1

In Robot.h we will need an object/variable that is the Joystick. The class/object type is: frc::Joystick We will name the object: m_joystick In Robot.h, in the 'private:' section add the following line:

   frc::Joystick m_joystick{0};

The '{0}' indicates the contructor will be called and '0' wil be passed in for the port parameter.

 

The frc::Joystick class contains the method 'GetRawButton()', which is what we will use to see if the trigger is being pressed. The trigger is button 1.

The frc:Joystick class contains the method 'GetY()', which will tell us the position of the joystick. The values will range from -1.0 to 1.0.

 

In Robot.cpp, you will add the code in the Robot::OperatorControl() method. You will replace the 'm_motor.Set(0.25)' from lesson 1.

You will want to first test if the trigger is pressed. If it is pressed, then you will look at the position to see if the position to determine how to turn the motor. Here is the pseudo code (i.e. not actual code, replace the English words with actual code):

while (IsOperatorControl() && IsEnabled()) {
   if (joystick trigger is pressed) {
       if (joystick position is greater than 0.33) {
           spin motor forward 1/4 speed
       }
       else if (joystick position is less than -0.33) {
           spin motor reverse 1/4 speed
       }
       else {
           stop motor
       }
   }
   frc::Wait(0.005);
}

The 0.33 and -0.33 is make sure the joystick has moved away from the center. Ideally, you would get 0 when the joystick is at the center. However, joysticks do not always go back to true 0.

To stop a motor you send it a speed of 0.

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. move joystick back and forth - motor should not turn
  2. pull tigger while moving joystick back and forth - motor should turn, and direction of its rotation based on the joystick being forward or backward
  3. pull trigger and move joystick forward (motor will be turning), release trigger and then release joystick. WHY IS MOTOR STILL TURNING?

 

Part 2

  1. modifiy code so motor does not turn
  2. build the project. when there are no error ask instructor to review your code and then ask to test the code