Add a Motor Controller example java program

Change-Id: I7b0a7622fbf4cbe2ef541088bbe521d4cac9aa22
This commit is contained in:
Brad Miller
2014-10-02 13:56:15 -04:00
parent 88bf4ee567
commit ba5111b994
2 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
package org.usfirst.frc.team190.robot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Timer;
/**
* This sample program shows how to control a motor using a joystick. In the operator
* control part of the program, the joystick is read and the value is written to the motor.
*
* Joystick analog values range from -1 to 1 and speed controller inputs also range from -1
* to 1 making it easy to work together. The program also delays a short time in the loop
* to allow other threads to run. This is generally a good idea, especially since the joystick
* values are only transmitted from the Driver Station once every 20ms.
*/
public class Robot extends SampleRobot {
private SpeedController motor; // the motor to directly control with a joystick
private Joystick stick;
private final double k_updatePeriod = 0.005; // update every 0.005 seconds/5 milliseconds (200Hz)
public Robot() {
motor = new Talon(0); // initialize the motor as a Talon on channel 0
stick = new Joystick(0); // initialize the joystick on port 0
}
/**
* Runs the motor from a joystick.
*/
public void operatorControl() {
while (isOperatorControl()) {
// Set the motor's output.
// This takes a number from -1 (100% speed in reverse) to +1 (100% speed going forward)
motor.set(stick.getY());
Timer.delay(k_updatePeriod); // wait 5ms to the next update
}
}
}

View File

@@ -116,6 +116,26 @@
</files>
</example>
<example>
<name>Motor Controller</name>
<description>Demonstrate controlling a single motor with a joystick</description>
<tags>
<tag>Actuators</tag>
<tag>Complete List</tag>
<tag>Joystick</tag>
<tag>Robot and Motor</tag>
</tags>
<packages>
<package>src/$package-dir</package>
</packages>
<files>
<file source="examples/MotorControl/src/org/usfirst/frc/team190/robot/Robot.java"
destination="src/$package-dir/Robot.java"></file>
</files>
</example>
<tagDescription>
<name>CommandBased Robot</name>
<description>Examples for CommandBased robot programs.</description>