mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
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:
@@ -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);
|
||||||
@@ -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);
|
||||||
@@ -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);
|
||||||
@@ -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);
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
#include "WPILib.h"
|
#include "WPILib.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a sample program demonstrating how to use an ultrasonic sensor and proportional control to
|
* This is a sample program demonstrating how to use an ultrasonic sensor and proportional
|
||||||
* maintain a set distance from an object.
|
* 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 {
|
class Robot: public SampleRobot {
|
||||||
AnalogInput *ultrasonic; //ultrasonic sensor
|
AnalogInput *ultrasonic; //ultrasonic sensor
|
||||||
@@ -46,7 +50,7 @@ public:
|
|||||||
void OperatorControl() {
|
void OperatorControl() {
|
||||||
|
|
||||||
double currentDistance; //distance measured from the ultrasonic sensor values
|
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()) {
|
while (IsOperatorControl() && IsEnabled()) {
|
||||||
currentDistance = ultrasonic->GetValue() * valueToInches; //sensor returns a value from 0-4095 that is scaled to inches
|
currentDistance = ultrasonic->GetValue() * valueToInches; //sensor returns a value from 0-4095 that is scaled to inches
|
||||||
|
|||||||
@@ -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);
|
||||||
@@ -267,11 +267,11 @@
|
|||||||
<name>Ultrasonic</name>
|
<name>Ultrasonic</name>
|
||||||
<description>Demonstrate maintaining a set distance using an ultrasonic sensor.</description>
|
<description>Demonstrate maintaining a set distance using an ultrasonic sensor.</description>
|
||||||
<tags>
|
<tags>
|
||||||
<tag>Getting Started with C++</tag>
|
|
||||||
<tag>Robot and Motor</tag>
|
<tag>Robot and Motor</tag>
|
||||||
<tag>CAN</tag>
|
<tag>CAN</tag>
|
||||||
<tag>Complete List</tag>
|
<tag>Complete List</tag>
|
||||||
<tag>Sensors</tag>
|
<tag>Sensors</tag>
|
||||||
|
<tag>Analog</tag>
|
||||||
</tags>
|
</tags>
|
||||||
<packages>
|
<packages>
|
||||||
<package>src</package>
|
<package>src</package>
|
||||||
@@ -282,7 +282,103 @@
|
|||||||
</example>
|
</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>
|
<example>
|
||||||
<name>Getting Started</name>
|
<name>Getting Started</name>
|
||||||
<description>An example program which demonstrates the simplest autonomous and
|
<description>An example program which demonstrates the simplest autonomous and
|
||||||
|
|||||||
@@ -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(){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
package org.usfirst.frc.team190.robot;
|
package $package;
|
||||||
|
|
||||||
import edu.wpi.first.wpilibj.CANTalon;
|
import edu.wpi.first.wpilibj.CANTalon;
|
||||||
import edu.wpi.first.wpilibj.SampleRobot;
|
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
|
* This is a sample program demonstrating how to use an ultrasonic sensor and proportional
|
||||||
* control to maintain a set distance from an object.
|
* 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 {
|
public class Robot extends SampleRobot {
|
||||||
@@ -48,7 +52,7 @@ public class Robot extends SampleRobot {
|
|||||||
public void operatorControl() {
|
public void operatorControl() {
|
||||||
|
|
||||||
double currentDistance; //distance measured from the ultrasonic sensor values
|
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()) {
|
while (isOperatorControl() && isEnabled()) {
|
||||||
currentDistance = ultrasonic.getValue()*valueToInches; //sensor returns a value from 0-4095 that is scaled to inches
|
currentDistance = ultrasonic.getValue()*valueToInches; //sensor returns a value from 0-4095 that is scaled to inches
|
||||||
|
|||||||
@@ -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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -150,6 +150,7 @@
|
|||||||
<tag>Complete List</tag>
|
<tag>Complete List</tag>
|
||||||
<tag>Robot and Motor</tag>
|
<tag>Robot and Motor</tag>
|
||||||
<tag>CAN</tag>
|
<tag>CAN</tag>
|
||||||
|
<tag>Analog</tag>
|
||||||
</tags>
|
</tags>
|
||||||
<packages>
|
<packages>
|
||||||
<package>src/$package-dir</package>
|
<package>src/$package-dir</package>
|
||||||
@@ -161,6 +162,108 @@
|
|||||||
</example>
|
</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>
|
<example>
|
||||||
<name>Motor Controller</name>
|
<name>Motor Controller</name>
|
||||||
<description>Demonstrate controlling a single motor with a joystick</description>
|
<description>Demonstrate controlling a single motor with a joystick</description>
|
||||||
|
|||||||
Reference in New Issue
Block a user