Lesson 3: Using Motor Controllers
In order to make our robot move, we are going to need to be able to control the motors. We do this with a piece of hardware called a motor controller. Each motor needs its own motor controller. Motor controllers can operate with one of two protocols, but we are going to focus on only one in this tutorial: PWM. PWM is a way for us to tell the motor controllers what we want them to do.
The list of permitted motor controllers for the 2016 season is:
- Jaguar Motor Controller
- Talon Motor Controller
- Talon SRX Motor Controller
- Victor 884 Motor Controller
- Victor 888 Motor Controller
- Victor SP Motor Controller
- SD540 Motor Controller
- Spark Motor Controller
Why use Victor
when uncertain?
PWM works the same for all the controllers, so it doesn't matter what object
is used unless you are using additional features of the motor controller.
Each type of motor controller comes with their own class in the WPI Java
library. (The Jaguar uses the Jaguar
object, the Talon SRX uses the
TalonSRX
object, and so on.) If you are uncertain on which class
to use, the safest bet would be to use the Victor
object.
Enough talk, let's write some code. I am going to be using a
Talon
, but you don't have to. Simply replace all my
Talon
objects with the class of your choice. Let's make a robot
with two motor controllers: left and right. At the top of your main robot class,
declare your motor controllers:
Now we need to instantiate them. Do this in your robotInit()
method. The constructor of a motor controller object takes one integer: the port
on the roboRIO that the motor controller is plugged into. I am going to plug
the left motor controller into port 0, and the right into port 1.
Now let's make them move! You only need to know one method: the
set()
method. It takes one double as a parameter: the speed that
the motor should go. The speed can be anywhere from -1 to 1, where -1 is full
speed backwards, 1 is full speed forwards, and 0 is stop. To make both left and
right motors move forward at 20% speed, we can add this code to our
teleopPeriodc()
method:
It is important to note that the motor will continue to run
at that speed until set()
is called again with a different value.
Now when we enable the robot, we will see the left and right motors move forward at 20% speed. If you have deployed this code to your robot, one of the motors may have gone backwards. This is due to motors being mounted in the opposite orientation, and thus forward in code becomes backwards on the physical motor. To fix this issue, simply add a negative sign where it is needed. (For example, let's say your left motor is moving backwards):
should fix it up.
Using Code Red's Robot Library
An alternative way to control motors is using Code Red's Robot Library. Make
sure that you have the library setup in your environment before continuing. When
using the Robot Library, all motor controllers use the
PWMController
object. This object does much more than simply set
motor speeds, but that is beyond the scope of this lesson and will be covered
later. Here is the same code we just wrote, but using the
PWMController
object:
Notice that the constructor for the PWMController
object takes
an additional boolean. The boolean indicates whether the motor is mounted on
the robot backwards or not. If it is mounted backwards, simply pass
true
into the constructor of the motor controller, and you will not
need to worry about using any negatives later on in your code.
That's it for this lesson! In the next lesson we are going to learn about getting joystick input.