Preseason Lesson4: Difference between revisions

From 1511Wookiee
Jump to navigationJump to search
(Created page with "<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-out...")
 
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"><div class="mw-parser-output"><div class="mw-parser-output"><span style="font-size: large;">'''Change motor speed using joystick'''</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"><div class="mw-parser-output"><div class="mw-parser-output"><span style="font-size: large;">'''Change motor speed using joystick'''</span></div> <div class="mw-parser-output">
The goal is to make the motor's speed change based on the position of the joystick. When the joystick is all the way forward, the motor will move at full forward speed. When the joystick is all the way back, the motor will move at full reverse speed.
The goal is to make the motor's speed change based on the position of the joystick. When the joystick is all the way forward, the motor will move at full forward speed. When the joystick is all the way back, the motor will move at full reverse speed.


Line 50: Line 50:


Did you notice that the motor speed just jumps to a speed, and does not ramp up from a stop?
Did you notice that the motor speed just jumps to a speed, and does not ramp up from a stop?
&nbsp;


<u><span style="font-size: large;">Part 2</span></u>
<u><span style="font-size: large;">Part 2</span></u>


Update the code so the motor speed starts at 0 when the joystick is at the end of the dead band. That is, when the stick is at -0.25 the motor will move 0 and when the stick is at -1 the motor will move +1. You will need to use your math skills.
Update the code so the motor speed starts at 0 when the joystick is at the end of the dead band. That is, when the stick is at -0.25 the motor will move 0 and when the stick is at -1 the motor will move +1. You will need to use your math skills.
</div> <div class="mw-parser-output">&nbsp;</div> <div class="mw-parser-output">&nbsp;</div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
</div> <div class="mw-parser-output">&nbsp;</div> <div class="mw-parser-output">&nbsp;</div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>

Revision as of 13:38, 1 December 2019

Change motor speed using joystick

The goal is to make the motor's speed change based on the position of the joystick. When the joystick is all the way forward, the motor will move at full forward speed. When the joystick is all the way back, the motor will move at full reverse speed.

 

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

 

Link to the Joystick documentation: Joystick

 

You will not need to make any changes to Robot.h.

 

In Robot.cpp, you will want to get the joystick 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 joystick documentation to see what data type GetY() returns. You will call GetY() and save what it returned into your varaible.

Let's also adjust the dead band (the area in which the joystick has no affect) to be -0.25 to +0.25.

   if (joystick trigger is pressed) {
       assign your variable the joystick position
       if ((your variable is less than -0.25) and (switch A is not tripped)) {
           spin motor forward the same speed as the joystick
       }
       else if ((your variable is greater than 0.25) and (switch B is not tripped)) {
           spin motor reverse the same speed as the joystick
       }
       else {
           stop motor
       }
   }
   else {
       stop motor
   }

NOTE: If you recall from the previous lessons, the joystick ranges from -1 to 1 and the motor also goes from -1 to 1. Also notice the changes to handle the fact that the joystick outputs negative values when the stick is moved forward.

 

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 move joystick forward - motor should turn faster the more forward the joystick is moved
  2. with the trigger pressed, pull the joystick backward - motor should turn, in opposite direction, faster the more backward the joystick is moved

Did you notice that the motor speed just jumps to a speed, and does not ramp up from a stop?

 

Part 2

Update the code so the motor speed starts at 0 when the joystick is at the end of the dead band. That is, when the stick is at -0.25 the motor will move 0 and when the stick is at -1 the motor will move +1. You will need to use your math skills.