DC Motor Control
For stronger and faster movement, FRC robots use all sorts of motors. There are large motors that propel the robot, small motors that drive flywheels or manipulators, etc. Most DC motors use more power than can be supplied directly from the !RoboRIO so instead the roboRIO communicates with a dedicated high-power motor controller and tells it how fast and in which direction to move the motor. FRC robots support several types of motor controllers, but there are two main types: simple PWM motor controllers like the Talon SR and intelligent motor controllers such as the Talon SRX (aka CAN Talon).
PWM motor controllers are connected to one of the PWM output ports on the roboRIO. PWM stands for pulse-width modulation and communicates how much battery power to deliver to the motor ranging from -100% (full reverse) to +100% (full forward) or anything in-between. Intelligent motor controllers contain their own microprocessor that can perform sophisticated management of the motor such as keeping it at a particular speed or following a motion profile or keeping a motor in a particular position (e.g. rotated to 30-degrees); we'll talk more about their capabilities later and you can read about them here.
Create another program using the TimedRobot java template and name it MotorTest:
Smart (CAN) Motor Controller Example (Hazelnut, Almond)
This example is for a robot that has a TalonSRX motor controller with its CAN bus address set to 3. A Java Class is available that makes it easy to access the extensive capabilities of the TalonSRX; the class is provided by the manufacturer (Cross The Road Electronics aka CTRE) and must be installed on the laptop and added to the project. Team laptops already have the CTRE framework installed; if your laptop does not, you can download and install it here.
To use this class, you must add it to the MotorTest project:
- WPILib->Manage Vendor Libraries
- -> Install new library (offline)
- -> Select ctre phoneix library (or similar)
Modify the auto-generated template code as follows:
- Import the XboxController, Hand, and all of the CAN motorcontrol classes (we will be using the WPI_TalonSRX class); note the use of '.*' to import all of the classes in a package:
import edu.wpi.first.wpilibj.XboxController; import edu.wpi.first.wpilibj.GenericHID.Hand; import com.ctre.phoenix.motorcontrol.can.*;
- declare variable for the Xbox controller and servo motor in the Robot class
private XboxController xbox; private WPI_TalonSRX m_Left;
- in robotInit() instantiate the controller and motor controller objects
xbox = new XboxController(0); // Xbox controller on port 0 m_Left = new WPI_TalonSRX(4); // CAN Talon ID 4
- in teleopPeriodic() read the xbox controller and adjust the DC motor accordingly
// Read game controller left joystick Y axis // value returned is between -1.0 and 1.0 // For xbox controller uncomment next line double x = xbox.getY(Hand.kLeft); // For F310 controller uncomment next line // double x = -xbox.getRawAxis(1); // DC Motor controllers apply between -1.0 (full reverse) // 0=stop and +1.0 (full forward) power // Set motor speed based on joystick m_Left.set(x);
The complete Robot.java is here
PWM Motor Controller Example (Macadamia)
This example is for a robot with a PWM motor controller such as a TalonSR or Spark connected to the !RoboRIO PWM port 1.
Modify the auto-generated template code as follows:
- Import the XboxController, Hand, and Talon classes:
import edu.wpi.first.wpilibj.XboxController; import edu.wpi.first.wpilibj.GenericHID.Hand; import edu.wpi.first.wpilibj.Talon;
- declare variable for the Xbox controller and servo motor in the Robot class
private XboxController xbox; private Talon m_Left;
- in robotInit() instantiate the controller and motor controller objects
xbox = new XboxController(0); // Xbox controller on USB port 0 m_Left = new Talon(1); // TalonSR connected to roboRIO PWM 1
- in teleopPeriodic() read the xbox controller and adjust the DC motor accordingly
// Read game controller left joystick Y axis // value returned is between -1.0 and 1.0 // For xbox controller uncomment next line double x = xbox.getY(Hand.kLeft); // For F310 controller uncomment next line // double x = -xbox.getRawAxis(1); // DC Motor controllers apply between -1.0 (full reverse) // 0=stop and +1.0 (full forward) power // Set motor speed based on joystick m_Left.set(x);
Running the Program
After you launch the driver station, you'll need to select Teleop mode and then press the Enable button. When you enable teleop mode, teleopInit() is called once and then teleopPeriodic() is called repeatedly until you disable the robot.
Extra Credit
Use the left joystick Y axis to control the left motor power and the right joystick Y axis to control the right motor power...now you have a driveable robot!
NOTE: if you are using a Logitech F310 game controller, the right joystick is read differently; see here