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,65 @@
package org.usfirst.frc.team190.robot;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.AnalogInput;
/**
* This is a sample program demonstrating how to use an ultrasonic sensor and proportional
* control to maintain a set distance from an object.
*/
public class Robot extends SampleRobot {
AnalogInput ultrasonic; //ultrasonic sensor
RobotDrive myRobot;
final int ultrasonicChannel = 3; //analog input pin
//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
final double valueToInches = 0.125; //factor to convert sensor values to a distance in inches
final double pGain = 0.05; //proportional speed constant
public Robot() {
//make objects for the sensor and the drive train
ultrasonic = new AnalogInput(ultrasonicChannel);
myRobot = new RobotDrive(new CANTalon(leftMotorChannel), new CANTalon(leftRearMotorChannel),
new CANTalon(rightMotorChannel), new CANTalon(rightRearMotorChannel));
}
/**
* Runs during autonomous.
*/
public void autonomous() {
}
/**
* Tells the robot to drive to a set distance (in inches) from an object using proportional control.
*/
public 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
*/
public void test() {
}
}

View File

@@ -141,6 +141,25 @@
</files>
</example>
<example>
<name>Ultrasonic</name>
<description>Demonstrate maintaining a set distance using an ultrasonic sensor. </description>
<tags>
<tag>Sensors</tag>
<tag>Complete List</tag>
<tag>Robot and Motor</tag>
<tag>CAN</tag>
</tags>
<packages>
<package>src/$package-dir</package>
</packages>
<files>
<file source="examples/Ultrasonic/src/org/usfirst/frc/team190/robot/Robot.java"
destination="src/$package-dir/Robot.java"></file>
</files>
</example>
<example>
<name>Motor Controller</name>