16 | | == Fourth program: DC Motor Control |
17 | | For stronger and faster movement, FRC robots use all sorts of motors. There are large motors that propel the robot, small motors that drive flywheel 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; a common and powerful controller is the [https://www.andymark.com/products/talon-srx-speed-controller Talon SRX] often called a CAN Talon. |
18 | | |
19 | | Create another program using the !TimedRobot java template and name it !MotorTest. |
20 | | |
21 | | The motor controller Class is provided by a third party (Cross The Road Electronics aka CTRE); the team laptops already have the CTRE framework installed; if your laptop does not, you can download and install it [http://www.ctr-electronics.com/control-system/hro.html#product_tabs_technical_resources here]. To use the library, you need to add it to the project: |
22 | | * WPILib->Manage Vendor Libraries |
23 | | * -> Install new library (offline) |
24 | | * -> Select ctre phoneix library (or similar) |
25 | | |
26 | | Modify the generated code as follows: |
27 | | |
28 | | * Add the following imports: |
29 | | {{{ |
30 | | import edu.wpi.first.wpilibj.XboxController; |
31 | | import edu.wpi.first.wpilibj.GenericHID.Hand; |
32 | | import com.ctre.phoenix.motorcontrol.can.*; |
33 | | }}} |
34 | | * declare variable for the Xbox controller and [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599703-repeatable-low-power-movement-controlling-servos-with-wpilib servo motor] in the Robot class |
35 | | {{{ |
36 | | private XboxController xbox; |
37 | | private WPI_TalonSRX m_rearLeft; |
38 | | }}} |
39 | | * in robotInit() instantiate the controller and motor controller objects |
40 | | {{{ |
41 | | xbox = new XboxController(0); // Xbox controller on port 0 |
42 | | m_rearLeft = new WPI_TalonSRX(4); // CAN Talon ID 4 |
43 | | }}} |
44 | | * in teleopPeriodic() read the xbox controller and adjust the DC motor accordingly |
45 | | {{{ |
46 | | // Read xbox controller left joystick x axis |
47 | | // value returned is between -1.0 and 1.0 |
48 | | double x = xbox.getX(Hand.kLeft); |
49 | | // DC Motor controllers apply between -1.0 (full reverse) |
50 | | // 0=stop and +1.0 (full forward) power |
51 | | // Set motor speed based on joystick |
52 | | m_rearLeft.set(x); |
53 | | }}} |
54 | | |
55 | | Note: you'll need to press the Enable button on the driver station to put the robot in Teleop mode so that teleopPeriodic() is called repeatedly. |
56 | | |
57 | | * 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! |