Files
James Kuszmaul e017f93f16 Fixed examples to build/run with new WPILib versions.
Also added some references/smart pointers to a couple places
that seemed convenient to the user.

I haven't updated the constructors for RobotDrive() related
examples, pending the results of gerrit change https://usfirst.collab.net/gerrit/#/c/960/

A few things that we are noticing:
--It might be nice if ReturnPIDInput() didn't have to be const;
  when people try to override it, they have to remember to put
  the const in and if they don't, then the compiler error isn't the
  most obvious (especially since this is a change). This would also
  apply to PIDGet() in the PIDSource interface.
--SendableChooser still takes raw pointers. This could lead to an
  issue I had to debug briefly where you accidentally call
  GetSelected() on autoChooser and put the resulting raw pointer
  into a unique_ptr, which destroys the pointer when it goes out of
  scope. Specifically, I was testing the PacGoat example and
  I ended up with a situation where if auto mode was run once, it
  was fine, but if it was run twice, the selected command would
  have been destroyed by the unique_ptr. I believe that this
  just requires updating SendableChosser to take shared_ptr.
--When the samples are compiled with -pedantic, it points out that
  START_ROBOT_CLASS macro expansion results in a redundant semicolon.

Change-Id: Ib4c025a61263d0d2780d4253faa31713e15333a5
2015-08-13 11:26:28 -07:00

66 lines
1.9 KiB
C++

#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.
*
* 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;
public:
const int ultrasonicChannel = 3; //analog input pin
//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
const double valueToInches = 0.125; //factor to convert sensor values to a distance in inches
const double pGain = 0.05; //proportional speed constant
Robot() :
ultrasonic(ultrasonicChannel),
myRobot(new CANTalon(leftMotorChannel), new CANTalon(leftRearMotorChannel),
new CANTalon(rightMotorChannel), new CANTalon(rightRearMotorChannel)) {}
/**
* Runs during autonomous.
*/
void Autonomous() {
}
/**
* Tells the robot to drive to a set distance (in inches) from an object using
* proportional control.
*/
void OperatorControl() {
double currentDistance; //distance measured from the ultrasonic sensor values
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
currentSpeed = (holdDistance - currentDistance) * pGain; //convert distance error to a motor speed
myRobot.Drive(currentSpeed, 0); //drive robot
}
}
/**
* Runs during test mode
*/
void Test() {
}
};
START_ROBOT_CLASS(Robot)