Added Ultrasonic example program in Java and C++, updated examples XML.

Change-Id: I510bef33d8565d124894d926d6634b04b1b6ca28
This commit is contained in:
Joseph
2015-06-24 16:30:55 -04:00
parent c6b790cadd
commit c357d1dcae
4 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
#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.
*/
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() :
SampleRobot() {
//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));
}
/**
* 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 motor
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);

View File

@@ -261,6 +261,26 @@
<file source="examples/MecanumDrive/src/Robot.cpp" destination="src/Robot.cpp"></file>
</files>
</example>
<example>
<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>
</tags>
<packages>
<package>src</package>
</packages>
<files>
<file source="examples/Ultrasonic/src/Robot.cpp" destination="src/Robot.cpp"></file>
</files>
</example>
<example>