72 | | Run the program and observe the "Front range" value in the Smart Dashboard as you move the robot towards and away from the wall. |
| 72 | Run the program and observe the "Front range" value in the [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599724-displaying-data-on-the-ds-dashboard-overview Smart Dashboard] as you move the robot towards and away from the wall. |
| 73 | |
| 74 | == Third program: Servo Motor Control |
| 75 | |
| 76 | Create another program using the !TimedRobot java template and name it !ServoTest. Modify the generated code as follows: |
| 77 | * Add the following imports: |
| 78 | {{{ |
| 79 | import edu.wpi.first.wpilibj.XboxController; |
| 80 | import edu.wpi.first.wpilibj.GenericHID.Hand; |
| 81 | import edu.wpi.first.wpilibj.Servo; |
| 82 | }}} |
| 83 | * 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 |
| 84 | {{{ |
| 85 | private XboxController xbox; |
| 86 | private Servo servo; |
| 87 | }}} |
| 88 | * in robotInit() instantiate the controller and motor objects |
| 89 | {{{ |
| 90 | xbox = new XboxController(0); // Xbox controller on port 0 |
| 91 | servo = new Servo(1); // Servo connected to PWM 1 |
| 92 | }}} |
| 93 | * in teleopPeriodic() read the xbox controller and adjust the servo motor accordingly |
| 94 | {{{ |
| 95 | // Read xbox controller left joystick x axis |
| 96 | // value returned is between -1.0 and 1.0 |
| 97 | double x = xbox.getX(Hand.kLeft); |
| 98 | // Servo motors are controlled on a scale of 0.0 to 1.0 |
| 99 | // so re-scale the X value to that range (-1.0->0, 0->0.5, 1.0->1.0) |
| 100 | x = (x + 1.0) / 2; |
| 101 | // Set servo motor position based on joystick |
| 102 | servo.set(x); |
| 103 | }}} |
| 104 | |
| 105 | Connect a Servo motor to PWM port 1 on the roboRIO and run your program. 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. |