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 @@
#include "WPILib.h"
/**
* 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.
*/
class Robot: public SampleRobot {
const int gyroChannel = 0; //analog input
const int joystickChannel = 0; //usb number in DriverStation
//channels for motors
const int leftMotorChannel = 1;
const int rightMotorChannel = 0;
const int leftRearMotorChannel = 3;
const int rightRearMotorChannel = 2;
double angleSetpoint = 0.0;
const 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
const double voltsPerDegreePerSecond = .0128;
RobotDrive *myRobot;
Gyro *gyro;
Joystick *joystick;
public:
Robot() :
SampleRobot()
{
//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.
*/
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.
*/
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.
*/
void Test()
{
}
};
START_ROBOT_CLASS(Robot);

View File

@@ -0,0 +1,71 @@
#include "WPILib.h"
/**
* This is a sample program that uses mecanum drive with a gyro sensor to maintian
* rotation vectors in 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.
*/
*/
class Robot: public SampleRobot {
Joystick *joystick;
RobotDrive *myRobot;
Gyro *gyro;
//channels for motors
const int leftMotorChannel = 1;
const int rightMotorChannel = 0;
const int leftRearMotorChannel = 3;
const int rightRearMotorChannel = 2;
const int gyroChannel = 0; //analog input
//gyro calibration constant, may need to be adjusted so that a gyro value of 360
//equals 360 degrees
const double voltsPerDegreePerSecond = .0128;
public:
Robot() :
SampleRobot() {
//make objects for drive train, joystick, and gyro
joystick = new Joystick(0);
myRobot = new RobotDrive(new CANTalon(leftMotorChannel),
new CANTalon(leftRearMotorChannel),
new CANTalon(rightMotorChannel),
new CANTalon(rightRearMotorChannel));
myRobot->SetInvertedMotor(RobotDrive::kFrontLeftMotor, true);// invert the left side motors
myRobot->SetInvertedMotor(RobotDrive::kRearLeftMotor, true);// you may need to change or remove this to match your robot
gyro = new Gyro(gyroChannel);
}
/**
* Runs during autonomous.
*/
void Autonomous() {
}
/**
* Runs the motors with arcade steering.
*/
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());
Wait(0.005); // wait 5ms to avoid hogging CPU cycles
}
}
/**
* Runs during test mode.
*/
void Test() {
}
};
START_ROBOT_CLASS(Robot);

View File

@@ -0,0 +1,84 @@
#include "WPILib.h"
/**
* 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.
*/
class Robot: public SampleRobot
{
const int potChannel = 1; //analog input pin
const int motorChannel = 7; //PWM channel
const int joystickChannel = 0; //usb number in DriverStation
const int buttonNumber = 4; //joystick button
const 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() :
SampleRobot()
{
//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.
*/
void Autonomous()
{
}
/**
*
*/
void OperatorControl() {
bool buttonState;
bool prevButtonState = false;
int index = 0; //setpoint array index
double currentSetpoint; //holds desired setpoint
const int size = 3; //number of setpoints
const double setpoints[size] = {1.0, 2.6, 4.3}; //bottom, middle, and top elevator setpoints
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) % size; //increment set point, reset if at maximum
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.
*/
void Test()
{
}
};
START_ROBOT_CLASS(Robot);

View File

@@ -0,0 +1,85 @@
#include "WPILib.h"
/**
* 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.
*/
class Robot: public SampleRobot {
const int potChannel = 1; //analog input pin
const int motorChannel = 7; //PWM channel
const int joystickChannel = 0; //usb number in DriverStation
const int buttonNumber = 4; //button on joystick
const double setPoints[3] = { 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!
const 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() :
SampleRobot()
{
//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.
*/
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.
*/
void OperatorControl() {
pidController->SetInputRange(0, 5); //0 to 5V
pidController->SetSetpoint(setPoints[0]); //set to first setpoint
int index = 0;
bool currentValue;
bool 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) % (sizeof(setPoints)/8); //index of elevator setpoint wraps around
}
previousValue = currentValue;
}
}
/**
* Runs during test mode.
*/
void Test() {
}
};
START_ROBOT_CLASS(Robot);

View File

@@ -1,8 +1,12 @@
#include "WPILib.h"
/**
* This is a sample program demonstrating how to use an ultrasonic sensor and proportional control to
* maintain a set distance from an object.
* 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.
*/
class Robot: public SampleRobot {
AnalogInput *ultrasonic; //ultrasonic sensor
@@ -46,7 +50,7 @@ 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,99 @@
#include "WPILib.h"
/**
* 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.
*/
class Robot: public SampleRobot {
AnalogInput *ultrasonic; //ultrasonic sensor
RobotDrive *myRobot;
PIDController *pidController;
public:
const int ultrasonicChannel = 3; //analog input
//channels for motors
const int leftMotorChannel = 1;
const int rightMotorChannel = 0;
const int leftRearMotorChannel = 3;
const 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!
const 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.
const double VoltsToInches = 0.0098;
//internal class to write to myRobot (a RobotDrive object) using a PIDOutput
class MyPIDOutput: public PIDOutput {
public:
RobotDrive* rd;
MyPIDOutput(RobotDrive *r)
{
rd = r;
rd->SetSafetyEnabled(false);
}
void PIDWrite(float output) {
rd->Drive(output, 0); //write to myRobot (RobotDrive) by reference
}
};
Robot() :
SampleRobot() {
//make objects for 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(myRobot));
}
/**
* Runs during autonomous.
*/
void Autonomous()
{
}
/**
* Drives robot to set distance from an object using PID control and the ultrasonic
* sensor.
*/
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.
*/
void Test()
{
}
};
START_ROBOT_CLASS(Robot);

View File

@@ -267,11 +267,11 @@
<name>Ultrasonic</name>
<description>Demonstrate maintaining a set distance using an ultrasonic sensor.</description>
<tags>
<tag>Getting Started with C++</tag>
<tag>Robot and Motor</tag>
<tag>CAN</tag>
<tag>Complete List</tag>
<tag>Sensors</tag>
<tag>Analog</tag>
</tags>
<packages>
<package>src</package>
@@ -282,7 +282,103 @@
</example>
<example>
<name>UltrasonicPID</name>
<description>Demonstrate maintaining a set distance using an ultrasonic sensor and PID control.</description>
<tags>
<tag>Robot and Motor</tag>
<tag>CAN</tag>
<tag>Complete List</tag>
<tag>Sensors</tag>
<tag>Analog</tag>
</tags>
<packages>
<package>src</package>
</packages>
<files>
<file source="examples/UltrasonicPID/src/Robot.cpp" destination="src/Robot.cpp"></file>
</files>
</example>
<example>
<name>Gyro</name>
<description>An example program showing how to drive straight with using a gyro sensor.</description>
<tags>
<tag>Robot and Motor</tag>
<tag>CAN</tag>
<tag>Complete List</tag>
<tag>Sensors</tag>
<tag>Analog</tag>
<tag>Joystick</tag>
</tags>
<packages>
<package>src</package>
</packages>
<files>
<file source="examples/Gyro/src/Robot.cpp" destination="src/Robot.cpp"></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>Robot and Motor</tag>
<tag>CAN</tag>
<tag>Complete List</tag>
<tag>Sensors</tag>
<tag>Analog</tag>
<tag>Joysitck</tag>
</tags>
<packages>
<package>src</package>
</packages>
<files>
<file source="examples/GyroMecanum/src/Robot.cpp" destination="src/Robot.cpp"></file>
</files>
</example>
<example>
<name>PotentiometerPID</name>
<description>An example to demonstrate the use of a potentiometer and PID control to reach elevator position setpoints.</description>
<tags>
<tag>Joystick</tag>
<tag>Actuators</tag>
<tag>Complete List</tag>
<tag>Sensors</tag>
<tag>Analog</tag>
</tags>
<packages>
<package>src</package>
</packages>
<files>
<file source="examples/PotentiometerPID/src/Robot.cpp" destination="src/Robot.cpp"></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>Joystick</tag>
<tag>Actuators</tag>
<tag>Complete List</tag>
<tag>Sensors</tag>
<tag>Analog</tag>
</tags>
<packages>
<package>src</package>
</packages>
<files>
<file source="examples/Potentiometer/src/Robot.cpp" destination="src/Robot.cpp"></file>
</files>
</example>
<example>
<name>Getting Started</name>
<description>An example program which demonstrates the simplest autonomous and