Preseason Lesson4
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.
declare your variable while (IsOperatorControl() && IsEnabled()) { 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 } frc::Wait(0.005); }
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
- pull trigger and move joystick forward - motor should turn faster the more forward the joystick is moved
- 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 to map one range of numbers to another range.