Added new sample programs to the eclipse plugins for C++ and Java.

Modified examples.xml for the corresponding added programs.

Change-Id: I3d86c570f446ec8cf3013c58dd94215f2907bbb1
This commit is contained in:
Joseph
2015-06-25 14:31:51 -04:00
parent c357d1dcae
commit ae90e5f6d3
14 changed files with 1053 additions and 7 deletions

View File

@@ -0,0 +1,86 @@
package $package;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.Gyro;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Joystick;
/**
* This is a sample program to demonstrate how to use a gyro sensor to make a robot drive
* straight. This program uses a joystick to drive forwards and backwards while the gyro
* is used for direction keeping.
*
* WARNING: While it may look like a good choice to use for your code if you're inexperienced,
* don't. Unless you know what you are doing, complex code will be much more difficult under
* this system. Use IterativeRobot or Command-Based instead if you're new.
*/
public class Robot extends SampleRobot {
final int gyroChannel = 0; //analog input
final int joystickChannel = 0; //usb number in DriverStation
//channels for motors
final int leftMotorChannel = 1;
final int rightMotorChannel = 0;
final int leftRearMotorChannel = 3;
final int rightRearMotorChannel = 2;
double angleSetpoint = 0.0;
final double pGain = .005; //propotional turning constant
//gyro calibration constant, may need to be adjusted;
//gyro value of 360 is set to correspond to one full revolution
final double voltsPerDegreePerSecond = .0128;
RobotDrive myRobot;
Gyro gyro;
Joystick joystick;
public Robot()
{
//make objects for the drive train, gyro, and joystick
myRobot = new RobotDrive(new CANTalon(leftMotorChannel), new CANTalon(
leftRearMotorChannel), new CANTalon(rightMotorChannel),
new CANTalon(rightRearMotorChannel));
gyro = new Gyro(gyroChannel);
joystick = new Joystick(joystickChannel);
}
/**
* Runs during autonomous.
*/
public void autonomous() {
}
/**
* Sets the gyro sensitivity and drives the robot when the joystick is pushed. The
* motor speed is set from the joystick while the RobotDrive turning value is assigned
* from the error between the setpoint and the gyro angle.
*/
public void operatorControl() {
double turningValue;
gyro.setSensitivity(voltsPerDegreePerSecond); //calibrates gyro values to equal degrees
while (isOperatorControl() && isEnabled()) {
turningValue = (angleSetpoint - gyro.getAngle())*pGain;
if(joystick.getY() <= 0)
{
//forwards
myRobot.drive(joystick.getY(), turningValue);
} else {
//backwards
myRobot.drive(joystick.getY(), -turningValue);
}
}
}
/**
* Runs during test mode.
*/
public void test(){
}
}

View File

@@ -0,0 +1,71 @@
package $package;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.Gyro;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.RobotDrive.MotorType;
/**
* This is a sample program that uses mecanum drive with a gyro sensor to maintian
* rotation vectorsin relation to the starting orientation of the robot (field-oriented controls).
*
* WARNING: While it may look like a good choice to use for your code if you're inexperienced,
* don't. Unless you know what you are doing, complex code will be much more difficult under
* this system. Use IterativeRobot or Command-Based instead if you're new.
*/
public class Robot extends SampleRobot {
RobotDrive myRobot;
Joystick joystick;
Gyro gyro;
//channels for motors
final int leftMotorChannel = 1;
final int rightMotorChannel = 0;
final int leftRearMotorChannel = 3;
final int rightRearMotorChannel = 2;
final int gyroChannel = 0; //analog input
//gyro calibration constant, may need to be adjusted so that a gyro value of 360
//equals 360 degrees
final double voltsPerDegreePerSecond = .0128;
public Robot() {
//make objects for drive train, joystick, and gyro
myRobot = new RobotDrive(new CANTalon(leftMotorChannel), new CANTalon(leftRearMotorChannel),
new CANTalon(rightMotorChannel), new CANTalon(rightRearMotorChannel));
myRobot.setInvertedMotor(MotorType.kFrontLeft, true); // invert the left side motors
myRobot.setInvertedMotor(MotorType.kRearLeft, true); // you may need to change or remove this to match your robot
joystick = new Joystick(0);
gyro = new Gyro(gyroChannel);
}
/**
* Runs during autonomous.
*/
public void autonomous() {
}
/**
* Gyro sensitivity is set and mecanum drive is used with the gyro angle as an input.
*/
public void operatorControl() {
gyro.setSensitivity(voltsPerDegreePerSecond); //calibrate gyro to have the value equal to degrees
while (isOperatorControl() && isEnabled()) {
myRobot.mecanumDrive_Cartesian(joystick.getX(), joystick.getY(), joystick.getZ(), gyro.getAngle());
Timer.delay(0.005); // wait 5ms to avoid hogging CPU cycles
}
}
/**
* Runs during test mode
*/
public void test() {
}
}

View File

@@ -0,0 +1,83 @@
package $package;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Victor;
/**
* This is a sample program to demonstrate the use of a soft potentiometer and proportional
* control to reach and maintain position setpoints on an elevator mechanism. A joystick
* button is used to switch elevator setpoints.
*
* WARNING: While it may look like a good choice to use for your code if you're inexperienced,
* don't. Unless you know what you are doing, complex code will be much more difficult under
* this system. Use IterativeRobot or Command-Based instead if you're new.
*/
public class Robot extends SampleRobot {
final int potChannel = 1; //analog input pin
final int motorChannel = 7; //PWM channel
final int joystickChannel = 0; //usb number in DriverStation
final int buttonNumber = 4; //joystick button
final double setpoints[] = {1.0, 2.6, 4.3}; //bottom, middle, and top elevator setpoints
final double pGain = 1.0; //proportional speed constant
double motorSpeed;
double currentPosition; //sensor voltage reading corresponding to current elevator position
AnalogInput potentiometer;
Victor elevatorMotor;
Joystick joystick;
public Robot() {
//make objects for the potentiometer, elevator motor controller, and joystick
potentiometer = new AnalogInput(potChannel);
elevatorMotor = new Victor(motorChannel);
joystick = new Joystick(joystickChannel);
}
/**
* Runs during autonomous.
*/
public void autonomous() {
}
/**
* Moves elevator to a selectable setpoint that can be changed by pressing a button on
* the joystick. Proportional control is used to reach and maintain the desired setpoint
* by obtaining values from the potentiometer and comparing them to the setpoint value.
*/
public void operatorControl() {
boolean buttonState;
boolean prevButtonState = false;
int index = 0; //setpoint array index
double currentSetpoint; //holds desired setpoint
currentSetpoint = setpoints[0]; //set to first setpoint
while (isOperatorControl() && isEnabled()) {
buttonState = joystick.getRawButton(buttonNumber); //check if button is pressed
//if button has been pressed and released once
if(buttonState && !prevButtonState) {
index = (index + 1) % setpoints.length; //increment set point, reset if at end of array
currentSetpoint = setpoints[index]; //set setpoint
}
prevButtonState = buttonState; //record previous button state
currentPosition = potentiometer.getAverageVoltage(); //get position value
motorSpeed = (currentPosition - currentSetpoint)*pGain; //convert position error to speed
elevatorMotor.set(motorSpeed); //drive elevator motor
}
}
/**
* Runs during test mode
*/
public void test() {
}
}

View File

@@ -0,0 +1,85 @@
package $package;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.PIDController;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Victor;
/**
* This is a sample program to demonstrate how to use a soft potentiometer and a PID
* controller to reach and maintain position setpoints on an elevator mechanism.
*
* WARNING: While it may look like a good choice to use for your code if you're inexperienced,
* don't. Unless you know what you are doing, complex code will be much more difficult under
* this system. Use IterativeRobot or Command-Based instead if you're new.
*/
public class Robot extends SampleRobot {
final int potChannel = 1; //analog input pin
final int motorChannel = 7; //PWM channel
final int joystickChannel = 0; //usb number in DriverStation
final int buttonNumber = 4; //button on joystick
final double setPoints[] = {1.0, 2.6, 4.3}; //bottom, middle, and top elevator setpoints
//proportional, integral, and derivative speed constants; motor inverted
//DANGER: when tuning PID constants, high/inappropriate values for pGain, iGain,
//and dGain may cause dangerous, uncontrollable, or undesired behavior!
final double pGain = -5.0, iGain = -0.02, dGain = -2.0; //these may need to be positive for a non-inverted motor
PIDController pidController;
AnalogInput potentiometer;
Victor elevatorMotor;
Joystick joystick;
public Robot() {
//make objects for potentiometer, the elevator motor controller, and the joystick
potentiometer = new AnalogInput(potChannel);
elevatorMotor = new Victor(motorChannel);
joystick = new Joystick(joystickChannel);
//potentiometer (AnalogInput) and elevatorMotor (Victor) can be used as a
//PIDSource and PIDOutput respectively
pidController = new PIDController(pGain, iGain, dGain, potentiometer, elevatorMotor);
}
/**
* Runs during autonomous.
*/
public void autonomous() {
}
/**
* Uses a PIDController and an array of setpoints to switch and maintain elevator
* positions. The elevator setpoint is selected by a joystick button.
*/
public void operatorControl() {
pidController.setInputRange(0, 5); //0 to 5V
pidController.setSetpoint(setPoints[0]); //set to first setpoint
int index = 0;
boolean currentValue;
boolean previousValue = false;
while (isOperatorControl() && isEnabled()) {
pidController.enable(); //begin PID control
//when the button is pressed once, the selected elevator setpoint is incremented
currentValue = joystick.getRawButton(buttonNumber);
if(currentValue && !previousValue){
pidController.setSetpoint(setPoints[index]);
index = (index + 1) % setPoints.length; //index of elevator setpoint wraps around
}
previousValue = currentValue;
}
}
/**
* Runs during test mode.
*/
public void test() {
}
}

View File

@@ -1,5 +1,5 @@
package org.usfirst.frc.team190.robot;
package $package;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.SampleRobot;
@@ -9,6 +9,10 @@ import edu.wpi.first.wpilibj.AnalogInput;
/**
* This is a sample program demonstrating how to use an ultrasonic sensor and proportional
* control to maintain a set distance from an object.
*
* WARNING: While it may look like a good choice to use for your code if you're inexperienced,
* don't. Unless you know what you are doing, complex code will be much more difficult under
* this system. Use IterativeRobot or Command-Based instead if you're new.
*/
public class Robot extends SampleRobot {
@@ -48,7 +52,7 @@ public class Robot extends SampleRobot {
public void operatorControl() {
double currentDistance; //distance measured from the ultrasonic sensor values
double currentSpeed; //speed to set the motor
double currentSpeed; //speed to set the drive train motors
while (isOperatorControl() && isEnabled()) {
currentDistance = ultrasonic.getValue()*valueToInches; //sensor returns a value from 0-4095 that is scaled to inches

View File

@@ -0,0 +1,89 @@
package $package;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.PIDOutput;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.PIDController;
/**
* This is a sample program to demonstrate the use of a PID Controller with an ultrasonic
* sensor to reach and maintain a set distance from an object.
*
* WARNING: While it may look like a good choice to use for your code if you're inexperienced,
* don't. Unless you know what you are doing, complex code will be much more difficult under
* this system. Use IterativeRobot or Command-Based instead if you're new.
*/
public class Robot extends SampleRobot {
AnalogInput ultrasonic; //ultrasonic sensor
RobotDrive myRobot;
PIDController pidController;
final int ultrasonicChannel = 3; //analog input
//channels for motors
final int leftMotorChannel = 1;
final int rightMotorChannel = 0;
final int leftRearMotorChannel = 3;
final int rightRearMotorChannel = 2;
int holdDistance = 12; //distance in inches the robot wants to stay from an object
//proportional, integral, and derivative speed constants
//DANGER: when tuning PID constants, high/inappropriate values for pGain, iGain,
//and dGain may cause dangerous, uncontrollable, or undesired behavior!
final double pGain = 7, iGain = .018, dGain = 1.5;
//conversion factor specific to the sensor being used. For this sensor,
//the sensor returned values from 0.0V to 5.0V with a resolution of 9.8mV/in.
final double VoltsToInches = 0.0098;
//internal class to write to myRobot (a RobotDrive object) using a PIDOutput
public class MyPIDOutput implements PIDOutput {
@Override
public void pidWrite(double output) {
myRobot.drive(output, 0); //drive robot from PID output
}
}
public Robot() {
//make objects for the sensor and drive train
ultrasonic = new AnalogInput(ultrasonicChannel);
myRobot = new RobotDrive(new CANTalon(leftMotorChannel), new CANTalon(leftRearMotorChannel),
new CANTalon(rightMotorChannel), new CANTalon(rightRearMotorChannel));
//ultrasonic (AnalogInput) can be used as a PIDSource without modification,
//PIDOutput is an instance of the internal class MyPIDOutput made earlier
pidController = new PIDController(pGain, iGain, dGain, ultrasonic, new MyPIDOutput());
}
/**
* Runs during autonomous.
*/
public void autonomous() {
}
/**
* Drives the robot a set distance from an object using PID control and the
* ultrasonic sensor.
*/
public void operatorControl() {
pidController.setSetpoint(holdDistance*VoltsToInches); //set setpoint to 12 inches
//Set expected range to 0-24 inches; e.g. at 24 inches from object go
//full forward, at 0 inches from object go full backward.
pidController.setInputRange(0, 24*VoltsToInches);
while (isOperatorControl() && isEnabled()) {
pidController.enable(); //begin PID control
}
}
/**
* Runs during test mode
*/
public void test() {
}
}

View File

@@ -150,6 +150,7 @@
<tag>Complete List</tag>
<tag>Robot and Motor</tag>
<tag>CAN</tag>
<tag>Analog</tag>
</tags>
<packages>
<package>src/$package-dir</package>
@@ -161,6 +162,108 @@
</example>
<example>
<name>Ultrasonic PID</name>
<description>Demonstrate maintaining a set distance using an ultrasonic sensor and PID Control. </description>
<tags>
<tag>Sensors</tag>
<tag>Complete List</tag>
<tag>Robot and Motor</tag>
<tag>CAN</tag>
<tag>Analog</tag>
</tags>
<packages>
<package>src/$package-dir</package>
</packages>
<files>
<file source="examples/UltrasonicPID/src/org/usfirst/frc/team190/robot/Robot.java"
destination="src/$package-dir/Robot.java"></file>
</files>
</example>
<example>
<name>Potentiometer PID</name>
<description>An example to demonstrate the use of a potentiometer and PID control to reach elevator position setpoints.</description>
<tags>
<tag>Sensors</tag>
<tag>Complete List</tag>
<tag>Actuators</tag>
<tag>Analog</tag>
<tag>Joystick</tag>
</tags>
<packages>
<package>src/$package-dir</package>
</packages>
<files>
<file source="examples/PotentiometerPID/src/org/usfirst/frc/team190/robot/Robot.java"
destination="src/$package-dir/Robot.java"></file>
</files>
</example>
<example>
<name>Potentiometer</name>
<description>An example to demonstrate the use of a potentiometer and basic proportional control to reach elevator position setpoints.</description>
<tags>
<tag>Sensors</tag>
<tag>Complete List</tag>
<tag>Actuators</tag>
<tag>Analog</tag>
<tag>Joystick</tag>
</tags>
<packages>
<package>src/$package-dir</package>
</packages>
<files>
<file source="examples/Potentiometer/src/org/usfirst/frc/team190/robot/Robot.java"
destination="src/$package-dir/Robot.java"></file>
</files>
</example>
<example>
<name>Gyro</name>
<description>An example program showing how to drive straight with using a gyro sensor.</description>
<tags>
<tag>Sensors</tag>
<tag>Complete List</tag>
<tag>Robot and Motor</tag>
<tag>CAN</tag>
<tag>Analog</tag>
<tag>Joystick</tag>
</tags>
<packages>
<package>src/$package-dir</package>
</packages>
<files>
<file source="examples/Gyro/src/org/usfirst/frc/team190/robot/Robot.java"
destination="src/$package-dir/Robot.java"></file>
</files>
</example>
<example>
<name>Gyro Mecanum</name>
<description>An example program showing how to perform mecanum drive with field oriented controls.</description>
<tags>
<tag>Sensors</tag>
<tag>Complete List</tag>
<tag>Robot and Motor</tag>
<tag>CAN</tag>
<tag>Analog</tag>
<tag>Joystick</tag>
</tags>
<packages>
<package>src/$package-dir</package>
</packages>
<files>
<file source="examples/GyroMecanum/src/org/usfirst/frc/team190/robot/Robot.java"
destination="src/$package-dir/Robot.java"></file>
</files>
</example>
<example>
<name>Motor Controller</name>
<description>Demonstrate controlling a single motor with a joystick</description>