Changes between Version 8 and Version 9 of ControlSystems/SoftwareTeam/Training/GettingStarted/IntroRobotJava


Ignore:
Timestamp:
Oct 12, 2019, 11:32:19 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/IntroRobotJava

    v8 v9  
    2424* testPeriodic() - called periodically while robot is in test mode
    2525
    26 You will put your setup code in the Init() functions and the code that should run repeatedly in the Periodic() functions.
     26You will put your setup code in the Init() functions and the code that should run repeatedly in the Periodic() functions.  The code will make use of the pre-existing [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/XboxController.html XboxController] class provided in WPILib that makes it easy to use an Xbox controller.
    2727
    2828Modify the newly created program as follows:
     
    3636* In the function robotPeriodic() addd: {{{ SmartDashboard.putNumber("Left Joystick X", xbox.getX(Hand.kLeft)); }}}
    3737
    38 Your program should look like [wiki://ControlSystems/IntroRobotJava/XboxExample this]
     38Your program should look like [wiki://ControlSystems/SoftwareTeam/Training/IntroRobotJava/XboxExample this]
    3939
    4040* Use the WPILib icon to build your new program: W->Build Robot Code
     
    7070  }}}
    7171
    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.
     72Run 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
     76Create another program using the !TimedRobot java template and name it !ServoTest. Modify the generated code as follows:
     77* Add the following imports:
     78{{{
     79import edu.wpi.first.wpilibj.XboxController;
     80import edu.wpi.first.wpilibj.GenericHID.Hand;
     81import 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
     105Connect 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.
    73106 
    74107== Study some examples