Preseason Lesson4
Introduction
This lesson will be split in two parts. Complete and test out part 1 before moving to part 2!
Overall, this lesson will use most of what you have already learned to do similar, but still different, things. Our previous lessons have focused on control that mimics some simple appendages. This lesson will focus on control that might be more appropriate for a drive system.
We will be using a different motor and controller for this lesson and all new code you write will not directly interact with your existing code from previous lessons (as in, you won't be changing your existing code, just adding new/additional things to it).
Before starting this lesson, you should have completed the prior 3 lessons. Start with your code from the end of Lesson 3
Part 1: Control Speed of Motor
In this part, you will use the Y-axis of a new Joystick to directly control the speed of a new motor. We will use a dead-band as we did in prior lessons to prevent accidental driving of the motor from small movement of the joystick.
Part 1 Detailed Goals
- Use the Y-axis of the new Joystick on the driver station to control a new motor
- If the stick is moved forward, the motor should go forward. If moved down/toward you, it should drive the motor backward.
- The motor to control is the one immediately to the right of the one you have been using until now.
- This motor is also controlled by a TalonSRX motor controller
- You will need to look at the labels on the test board to know what the motor controller's ID/address number is
- Use a deadband of +/- 1/4 of the Y axis range (so, motor should not move unless the stick is moved more than 1/4 of the travel)
- The speed of the motor should directly match the amount that the stick is moved
Part 1 Guided Help
This lesson will have significantly less help than prior ones. Most of what you need to do is using things you have already done! Here are some ordered hints to get you there:
- You will need to make a new Joystick object to represent the new Joystick. Look at the driver station to get the address/ID number!
- You will need to make a new motor controller (TalonSRX) object to control the new motor. The ID of the new motor's controller is labelled on it on the test board.
- In prior lessons you made a deadband of 1/3rd the motion of the controller. Remember that for this lesson, we are using 1/4 the range of motion of the joystick for the deadband!
- Once you have handled the deadband, the speed of the motor can be simply set to the value read from the Joystick Y axis when you are outside of the deadband.
Part 1 Test
As always, coordinate with your teammates to use the test board to test. Test the following:
- The new motor should not move if the joystick is left stationary.
- Move the joystick a small amount off center. The motor should not move!
- Move the joystick forward more than 1/4 off center, slowly, toward fully forward. You should see the motor move slowly once you pass 1/4, then speed up and be going full speed when all the way forward. The LED on the motor controller should be flashing green while moving until you are fully forward. Verify that at full forward the light is solid green.
- Repeat the test for pulling back on the joystick. This time the controller should flash red until fully pulled back, then turn solid red.
- Verify that the motor moving is the correct one (the one next to Motor 5 from the previous lessons)
- Notice that when you pass the 1/4 mark, the motor jumps from stopped/off to a fairly substantial speed. It doesn't scale from 0 smoothly. For Part 2 we will address this.
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.