2015-06-25 14:31:51 -04:00
|
|
|
#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 {
|
2015-07-24 19:19:40 -04:00
|
|
|
AnalogInput ultrasonic; //ultrasonic sensor
|
|
|
|
|
RobotDrive myRobot;
|
|
|
|
|
PIDController pidController;
|
2015-06-25 14:31:51 -04:00
|
|
|
|
|
|
|
|
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:
|
2015-07-24 19:19:40 -04:00
|
|
|
RobotDrive &rd;
|
|
|
|
|
MyPIDOutput(RobotDrive &r) : rd(r)
|
2015-06-25 14:31:51 -04:00
|
|
|
{
|
2015-07-24 19:19:40 -04:00
|
|
|
rd.SetSafetyEnabled(false);
|
2015-06-25 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
void PIDWrite(float output) {
|
2015-07-24 19:19:40 -04:00
|
|
|
rd.Drive(output, 0); //write to myRobot (RobotDrive) by reference
|
2015-06-25 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Robot() :
|
|
|
|
|
//make objects for sensor and drive train
|
2015-07-24 19:19:40 -04:00
|
|
|
ultrasonic(ultrasonicChannel),
|
|
|
|
|
myRobot(new CANTalon(leftMotorChannel), new CANTalon(leftRearMotorChannel),
|
|
|
|
|
new CANTalon(rightMotorChannel), new CANTalon(rightRearMotorChannel)),
|
2015-06-25 14:31:51 -04:00
|
|
|
//ultrasonic (AnalogInput) can be used as a PIDSource without modification,
|
|
|
|
|
//PIDOutput is an instance of the internal class MyPIDOutput made earlier
|
2015-07-24 19:19:40 -04:00
|
|
|
pidController(pGain, iGain, dGain, &ultrasonic, new MyPIDOutput(myRobot)) {}
|
2015-06-25 14:31:51 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Runs during autonomous.
|
|
|
|
|
*/
|
|
|
|
|
void Autonomous()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Drives robot to set distance from an object using PID control and the ultrasonic
|
|
|
|
|
* sensor.
|
|
|
|
|
*/
|
|
|
|
|
void OperatorControl() {
|
2015-07-24 19:19:40 -04:00
|
|
|
pidController.SetSetpoint(holdDistance * VoltsToInches); //set setpoint to 12 inches
|
2015-06-25 14:31:51 -04:00
|
|
|
|
|
|
|
|
//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.
|
2015-07-24 19:19:40 -04:00
|
|
|
pidController.SetInputRange(0, 24 * VoltsToInches);
|
2015-06-25 14:31:51 -04:00
|
|
|
|
|
|
|
|
while (IsOperatorControl() && IsEnabled()) {
|
2015-07-24 19:19:40 -04:00
|
|
|
pidController.Enable(); //begin PID control
|
2015-06-25 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Runs during test mode.
|
|
|
|
|
*/
|
|
|
|
|
void Test()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-24 19:19:40 -04:00
|
|
|
START_ROBOT_CLASS(Robot)
|