Changes between Version 8 and Version 9 of ControlSystems/SoftwareTeam/Training/GettingStarted/ServoMotor
- Timestamp:
- Jan 28, 2020, 10:56:52 PM (5 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/Training/GettingStarted/ServoMotor
v8 v9 10 10 import edu.wpi.first.wpilibj.Servo; 11 11 }}} 12 * 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 class12 * declare variables to hold the Xbox controller and [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599703-repeatable-low-power-movement-controlling-servos-with-wpilib servo motor] objects. The variables should be inside in the Robot class since the robot contains those objects ("hasa" relationship: the robot "has a" Servo motor): 13 13 {{{ 14 14 private XboxController xbox; 15 15 private Servo servo; 16 16 }}} 17 * in robotInit() instantiate the controller and motor objects17 * in robotInit() instantiate (create) the controller and servo motor objects and store them in the variables you created above. 18 18 {{{ 19 19 xbox = new XboxController(0); // Xbox controller on USB port 0 20 20 servo = new Servo(2); // Servo connected to roboRIO PWM 2 21 21 }}} 22 * in teleopPeriodic() read the xbox controller and adjust the servo motor accordingly22 * in teleopPeriodic() read the xbox controller and adjust the servo motor position according to the joystick position. 23 23 {{{ 24 24 // Read xbox controller left joystick x axis … … 33 33 34 34 Connect a Servo motor to PWM port 2 on the roboRIO and run your program. 35 Launch the driver station on your laptop and select Teleop mode (bottom left side of the window). You'll need to press the Enable button on the driver station to put the robot in Teleop mode. When teleop is enabled, teleopInit() will be called once and then teleopPeriodic() is called repeatedly until the robot is disabled. 35 Launch the driver station on your laptop and select Teleop mode (bottom left side of the window). You'll need to press the Enable button on the driver station to put the robot in Teleop mode. When teleop is enabled, teleopInit() will be called once and then teleopPeriodic() is called repeatedly until the robot is disabled. Move the left joystick along the X-axis and observe the servo motor. 36 36 37 37 To see a complete Robot.java that demonstrates controlling the servo motor from an xbox controller using buttons and joystick, click [wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/ServoMotor/Example1 here].