Changes between Version 10 and Version 11 of ControlSystems/SoftwareTeam/Training/GettingStarted/IntroRobotJava


Ignore:
Timestamp:
Oct 15, 2019, 6:37:57 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

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

    v10 v11  
    107107== Fourth program: DC Motor Control
    108108
    109 * Coming soon...
     109Create another program using the !TimedRobot java template and name it !MotorTest. Modify the generated code as follows:
     110* Add the following imports:
     111{{{
     112import edu.wpi.first.wpilibj.XboxController;
     113import edu.wpi.first.wpilibj.GenericHID.Hand;
     114import 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
     138You'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
    110140 
    111141== Study some examples