From c357d1dcae8ea57eb7dd40c5ae83eee518955fb6 Mon Sep 17 00:00:00 2001 From: Joseph Date: Wed, 24 Jun 2015 16:30:55 -0400 Subject: [PATCH] Added Ultrasonic example program in Java and C++, updated examples XML. Change-Id: I510bef33d8565d124894d926d6634b04b1b6ca28 --- .../examples/Ultrasonic/src/Robot.cpp | 66 +++++++++++++++++++ .../resources/templates/examples/examples.xml | 20 ++++++ .../org/usfirst/frc/team190/robot/Robot.java | 65 ++++++++++++++++++ .../resources/templates/examples/examples.xml | 19 ++++++ 4 files changed, 170 insertions(+) create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Ultrasonic/src/Robot.cpp create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Ultrasonic/src/org/usfirst/frc/team190/robot/Robot.java diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Ultrasonic/src/Robot.cpp b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Ultrasonic/src/Robot.cpp new file mode 100644 index 0000000000..8965ff12ec --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Ultrasonic/src/Robot.cpp @@ -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); diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/examples.xml b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/examples.xml index ae93323e58..ad01afca9d 100644 --- a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/examples.xml +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/examples.xml @@ -261,6 +261,26 @@ + + + + Ultrasonic + Demonstrate maintaining a set distance using an ultrasonic sensor. + + Getting Started with C++ + Robot and Motor + CAN + Complete List + Sensors + + + src + + + + + + diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Ultrasonic/src/org/usfirst/frc/team190/robot/Robot.java b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Ultrasonic/src/org/usfirst/frc/team190/robot/Robot.java new file mode 100644 index 0000000000..99a2cee4b8 --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Ultrasonic/src/org/usfirst/frc/team190/robot/Robot.java @@ -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() { + } +} diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/examples.xml b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/examples.xml index 261b9495ea..71ccd0664d 100755 --- a/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/examples.xml +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/examples.xml @@ -141,6 +141,25 @@ + + + Ultrasonic + Demonstrate maintaining a set distance using an ultrasonic sensor. + + Sensors + Complete List + Robot and Motor + CAN + + + src/$package-dir + + + + + + Motor Controller