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,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);