1 | | == DC Motor Control |
2 | | 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 [https://www.andymark.com/products/talon-srx-speed-controller Talon SRX] (aka CAN Talon). |
| 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
10 | | 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 [http://www.ctr-electronics.com/control-system/hro.html#product_tabs_technical_resources here]. |
| 15 | /** |
| 16 | * The VM is configured to automatically run this class, and to call the |
| 17 | * functions corresponding to each mode, as described in the TimedRobot |
| 18 | * documentation. If you change the name of this class or the package after |
| 19 | * creating this project, you must also update the build.gradle file in the |
| 20 | * project. |
| 21 | */ |
| 22 | public class Robot extends TimedRobot { |
| 23 | private XboxController xbox; |
| 24 | private WPI_TalonSRX m_Left; |
12 | | To use this class, you must add it to the !MotorTest project: |
13 | | * WPILib->Manage Vendor Libraries |
14 | | * -> Install new library (offline) |
15 | | * -> Select ctre phoneix library (or similar) |
| 26 | /** |
| 27 | * This function is run when the robot is first started up and should be |
| 28 | * used for any initialization code. |
| 29 | */ |
| 30 | @Override |
| 31 | public void robotInit() { |
| 32 | xbox = new XboxController(0); // Xbox controller on port 0 |
| 33 | m_Left = new WPI_TalonSRX(4); // CAN Talon ID 4 |
| 34 | } |
17 | | Modify the auto-generated template code as follows: |
| 36 | /** |
| 37 | * This function is called every robot packet, no matter the mode. Use |
| 38 | * this for items like diagnostics that you want ran during disabled, |
| 39 | * autonomous, teleoperated and test. |
| 40 | * |
| 41 | * <p>This runs after the mode specific periodic functions, but before |
| 42 | * LiveWindow and SmartDashboard integrated updating. |
| 43 | */ |
| 44 | @Override |
| 45 | public void robotPeriodic() { |
| 46 | } |
19 | | * Import the [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/XboxController.html XboxController], [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/GenericHID.Hand.html Hand], and all of the CAN motorcontrol classes (we will be using the [https://www.ctr-electronics.com/downloads/api/java/html/classcom_1_1ctre_1_1phoenix_1_1motorcontrol_1_1can_1_1_w_p_i___talon_s_r_x.html WPI_TalonSRX] class); note the use of '.*' to import all of the classes in a package: |
20 | | {{{ |
21 | | import edu.wpi.first.wpilibj.XboxController; |
22 | | import edu.wpi.first.wpilibj.GenericHID.Hand; |
23 | | import com.ctre.phoenix.motorcontrol.can.*; |
24 | | }}} |
25 | | * 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 |
26 | | {{{ |
27 | | private XboxController xbox; |
28 | | private WPI_TalonSRX m_rearLeft; |
29 | | }}} |
30 | | * in robotInit() instantiate the controller and motor controller objects |
31 | | {{{ |
32 | | xbox = new XboxController(0); // Xbox controller on port 0 |
33 | | m_rearLeft = new WPI_TalonSRX(4); // CAN Talon ID 4 |
34 | | }}} |
35 | | * in teleopPeriodic() read the xbox controller and adjust the DC motor accordingly |
36 | | {{{ |
37 | | // Read xbox controller left joystick x axis |
38 | | // value returned is between -1.0 and 1.0 |
39 | | double x = xbox.getX(Hand.kLeft); |
40 | | // DC Motor controllers apply between -1.0 (full reverse) |
41 | | // 0=stop and +1.0 (full forward) power |
42 | | // Set motor speed based on joystick |
43 | | m_rearLeft.set(x); |
44 | | }}} |
| 48 | @Override |
| 49 | public void autonomousInit() { |
| 50 | } |
51 | | Modify the auto-generated template code as follows: |
| 67 | // For xbox controller, uncomment the following line |
| 68 | // double x = xbox.getY(Hand.kLeft); |
| 69 | // For Logitech F310 controller, uncomment the following line |
| 70 | double x = -xbox.getRawAxis(1); |
| 71 | |
| 72 | // DC Motor controllers apply between -1.0 (full reverse) |
| 73 | // 0=stop and +1.0 (full forward) power |
| 74 | // Set motor speed based on joystick |
| 75 | m_Left.set(x); |
| 76 | } |
53 | | * Import the [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/XboxController.html XboxController], [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/GenericHID.Hand.html Hand], and [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/Talon.html Talon] classes: |
54 | | {{{ |
55 | | import edu.wpi.first.wpilibj.XboxController; |
56 | | import edu.wpi.first.wpilibj.GenericHID.Hand; |
57 | | import edu.wpi.first.wpilibj.Talon; |
58 | | }}} |
59 | | * 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 |
60 | | {{{ |
61 | | private XboxController xbox; |
62 | | private Talon m_rearLeft; |
63 | | }}} |
64 | | * in robotInit() instantiate the controller and motor controller objects |
65 | | {{{ |
66 | | xbox = new XboxController(0); // Xbox controller on USB port 0 |
67 | | m_rearLeft = new Talon(1); // TalonSR connected to roboRIO PWM 1 |
68 | | }}} |
69 | | * in teleopPeriodic() read the xbox controller and adjust the DC motor accordingly |
70 | | {{{ |
71 | | // Read xbox controller left joystick x axis |
72 | | // value returned is between -1.0 and 1.0 |
73 | | double x = xbox.getX(Hand.kLeft); |
74 | | // DC Motor controllers apply between -1.0 (full reverse) |
75 | | // 0=stop and +1.0 (full forward) power |
76 | | // Set motor speed based on joystick |
77 | | m_rearLeft.set(x); |
78 | | }}} |
79 | | |
80 | | |
81 | | === Running the Program |
82 | | |
83 | | 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. |
84 | | |
85 | | === Extra Credit === |
86 | | 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! [[BR]] |
87 | | NOTE: if you are using a Logitech F310 game controller, the right joystick is read differently; see [wiki:/ControlSystems/GameControllers/LogitechF310 here] |
| 78 | /** |
| 79 | * This function is called periodically during test mode. |
| 80 | */ |
| 81 | @Override |
| 82 | public void testPeriodic() { |
| 83 | } |
| 84 | } |