109 | | * Coming soon... |
| 109 | Create another program using the !TimedRobot java template and name it !MotorTest. Modify the generated code as follows: |
| 110 | * Add the following imports: |
| 111 | {{{ |
| 112 | import edu.wpi.first.wpilibj.XboxController; |
| 113 | import edu.wpi.first.wpilibj.GenericHID.Hand; |
| 114 | import com.ctre.phoenix.motorcontrol.can.*; |
| 115 | }}} |
| 116 | * 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 |
| 117 | {{{ |
| 118 | private XboxController xbox; |
| 119 | private Servo servo; |
| 120 | }}} |
| 121 | * in robotInit() instantiate the controller and motor controller objects |
| 122 | {{{ |
| 123 | xbox = new XboxController(0); // Xbox controller on port 0 |
| 124 | m_rearLeft = new WPI_TalonSRX(4); // CAN Talon ID 4 |
| 125 | }}} |
| 126 | * in teleopPeriodic() read the xbox controller and adjust the servo motor accordingly |
| 127 | {{{ |
| 128 | // Read xbox controller left joystick x axis |
| 129 | // value returned is between -1.0 and 1.0 |
| 130 | double x = xbox.getX(Hand.kLeft); |
| 131 | // Servo motors are controlled on a scale of 0.0 to 1.0 |
| 132 | // so re-scale the X value to that range (-1.0->0, 0->0.5, 1.0->1.0) |
| 133 | x = (x + 1.0) / 2; |
| 134 | // Set motor speed based on joystick |
| 135 | m_rearLeft.setSpeed(x); |
| 136 | }}} |
| 137 | |
| 138 | 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. |
| 139 | |