| 1 | {{{ |
| 2 | package frc.robot; |
| 3 | |
| 4 | import edu.wpi.first.wpilibj.TimedRobot; |
| 5 | import edu.wpi.first.wpilibj.XboxController; |
| 6 | import edu.wpi.first.wpilibj.Servo; |
| 7 | |
| 8 | public class Robot extends TimedRobot { |
| 9 | // variables to hold the objects used |
| 10 | private XboxController xbox; |
| 11 | private Servo servo; |
| 12 | |
| 13 | /** |
| 14 | * This function is run when the robot is first started up and should be |
| 15 | * used for any initialization code. Create (instantiate) your objects here. |
| 16 | */ |
| 17 | @Override |
| 18 | public void robotInit() { |
| 19 | // instantiate (create) the contained objects and store them in the variables |
| 20 | xbox = new XboxController(0); // First Xbox/game controller |
| 21 | servo = new Servo(2); // Servo connected to PWM2 |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * This function is called every robot packet, no matter the mode. Use |
| 26 | * this for items like diagnostics that you want ran during disabled, |
| 27 | * autonomous, teleoperated and test. |
| 28 | * |
| 29 | * <p>This runs after the mode specific periodic functions, but before |
| 30 | * LiveWindow and SmartDashboard integrated updating. |
| 31 | */ |
| 32 | @Override |
| 33 | public void robotPeriodic() { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | */ |
| 38 | @Override |
| 39 | public void autonomousInit() { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * This function is called periodically during autonomous. |
| 44 | */ |
| 45 | @Override |
| 46 | public void autonomousPeriodic() { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * This function is called periodically during operator control. |
| 51 | */ |
| 52 | @Override |
| 53 | public void teleopPeriodic() { |
| 54 | if (xbox.getXButton()) { |
| 55 | // X button moves servo all the way left |
| 56 | servo.set(-1.0); |
| 57 | } else if (xbox.getYButton()) { |
| 58 | // Y button moves servo all the way right |
| 59 | servo.set(1.0); |
| 60 | } else { |
| 61 | // If neither X nor Y are pressed, servo position is |
| 62 | // controlled by the left joystick X-axis position |
| 63 | // Read xbox controller left joystick x axis (axis 0) |
| 64 | // value returned is between -1.0 and 1.0 |
| 65 | double x = xbox.getRawAxis(0); |
| 66 | // Servo motors are controlled on a scale of 0.0 to 1.0 |
| 67 | // so re-scale the X value to that range (-1.0->0, 0->0.5, 1.0->1.0) |
| 68 | x = (x + 1.0) / 2; |
| 69 | // Set servo motor position based on joystick |
| 70 | servo.set(x); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * This function is called periodically during test mode. |
| 76 | */ |
| 77 | @Override |
| 78 | public void testPeriodic() { |
| 79 | } |
| 80 | } |
| 81 | }}} |