From ae90e5f6d3239f08d5259c7c0b6aa3a4eac8fc79 Mon Sep 17 00:00:00 2001 From: Joseph Date: Thu, 25 Jun 2015 14:31:51 -0400 Subject: [PATCH] Added new sample programs to the eclipse plugins for C++ and Java. Modified examples.xml for the corresponding added programs. Change-Id: I3d86c570f446ec8cf3013c58dd94215f2907bbb1 --- .../templates/examples/Gyro/src/Robot.cpp | 86 +++++++++++++++ .../examples/GyroMecanum/src/Robot.cpp | 71 ++++++++++++ .../examples/Potentiometer/src/Robot.cpp | 84 ++++++++++++++ .../examples/PotentiometerPID/src/Robot.cpp | 85 +++++++++++++++ .../examples/Ultrasonic/src/Robot.cpp | 10 +- .../examples/UltrasonicPID/src/Robot.cpp | 99 +++++++++++++++++ .../resources/templates/examples/examples.xml | 100 ++++++++++++++++- .../org/usfirst/frc/team190/robot/Robot.java | 86 +++++++++++++++ .../org/usfirst/frc/team190/robot/Robot.java | 71 ++++++++++++ .../org/usfirst/frc/team190/robot/Robot.java | 83 ++++++++++++++ .../org/usfirst/frc/team190/robot/Robot.java | 85 +++++++++++++++ .../org/usfirst/frc/team190/robot/Robot.java | 8 +- .../org/usfirst/frc/team190/robot/Robot.java | 89 +++++++++++++++ .../resources/templates/examples/examples.xml | 103 ++++++++++++++++++ 14 files changed, 1053 insertions(+), 7 deletions(-) create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Gyro/src/Robot.cpp create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/GyroMecanum/src/Robot.cpp create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Potentiometer/src/Robot.cpp create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/PotentiometerPID/src/Robot.cpp create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/UltrasonicPID/src/Robot.cpp create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Gyro/src/org/usfirst/frc/team190/robot/Robot.java create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/GyroMecanum/src/org/usfirst/frc/team190/robot/Robot.java create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Potentiometer/src/org/usfirst/frc/team190/robot/Robot.java create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/PotentiometerPID/src/org/usfirst/frc/team190/robot/Robot.java create mode 100644 eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/UltrasonicPID/src/org/usfirst/frc/team190/robot/Robot.java diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Gyro/src/Robot.cpp b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Gyro/src/Robot.cpp new file mode 100644 index 0000000000..3667c4e691 --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Gyro/src/Robot.cpp @@ -0,0 +1,86 @@ +#include "WPILib.h" + +/** + * This is a sample program to demonstrate how to use a gyro sensor to make a robot drive + * straight. This program uses a joystick to drive forwards and backwards while the gyro + * is used for direction keeping. + * + * 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 { + const int gyroChannel = 0; //analog input + const int joystickChannel = 0; //usb number in DriverStation + + //channels for motors + const int leftMotorChannel = 1; + const int rightMotorChannel = 0; + const int leftRearMotorChannel = 3; + const int rightRearMotorChannel = 2; + + double angleSetpoint = 0.0; + const double pGain = .005; //propotional turning constant + + //gyro calibration constant, may need to be adjusted + //gyro value of 360 is set to correspond to one full revolution + const double voltsPerDegreePerSecond = .0128; + + RobotDrive *myRobot; + Gyro *gyro; + Joystick *joystick; + +public: + Robot() : + SampleRobot() + { + //make objects for the drive train, gyro, and joystick + myRobot = new RobotDrive(new CANTalon(leftMotorChannel), + new CANTalon(leftRearMotorChannel), + new CANTalon(rightMotorChannel), + new CANTalon(rightRearMotorChannel)); + gyro = new Gyro(gyroChannel); + joystick = new Joystick(joystickChannel); + } + + /** + * Runs during autonomous. + */ + void Autonomous() + { + + } + + /** + * Sets the gyro sensitivity and drives the robot when the joystick is pushed. The + * motor speed is set from the joystick while the RobotDrive turning value is + * assigned from the error between the setpoint and the gyro angle. + */ + void OperatorControl() + { + double turningValue; + gyro->SetSensitivity(voltsPerDegreePerSecond); //calibrates gyro values to equal degrees + + while (IsOperatorControl() && IsEnabled()) + { + turningValue = (angleSetpoint - gyro->GetAngle()) * pGain; + if (joystick->GetY() <= 0) { + //forwards + myRobot->Drive(joystick->GetY(), turningValue); + } else { + //backwards + myRobot->Drive(joystick->GetY(), -turningValue); + } + } + } + + /** + * Runs during test mode. + */ + void Test() + { + + } +}; + +START_ROBOT_CLASS(Robot); diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/GyroMecanum/src/Robot.cpp b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/GyroMecanum/src/Robot.cpp new file mode 100644 index 0000000000..23259d591a --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/GyroMecanum/src/Robot.cpp @@ -0,0 +1,71 @@ +#include "WPILib.h" + +/** + * This is a sample program that uses mecanum drive with a gyro sensor to maintian + * rotation vectors in relation to the starting orientation of the robot (field-oriented controls). + * + * 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 { + Joystick *joystick; + RobotDrive *myRobot; + Gyro *gyro; + + //channels for motors + const int leftMotorChannel = 1; + const int rightMotorChannel = 0; + const int leftRearMotorChannel = 3; + const int rightRearMotorChannel = 2; + + const int gyroChannel = 0; //analog input + + //gyro calibration constant, may need to be adjusted so that a gyro value of 360 + //equals 360 degrees + const double voltsPerDegreePerSecond = .0128; + +public: + Robot() : + SampleRobot() { + //make objects for drive train, joystick, and gyro + joystick = new Joystick(0); + myRobot = new RobotDrive(new CANTalon(leftMotorChannel), + new CANTalon(leftRearMotorChannel), + new CANTalon(rightMotorChannel), + new CANTalon(rightRearMotorChannel)); + myRobot->SetInvertedMotor(RobotDrive::kFrontLeftMotor, true);// invert the left side motors + myRobot->SetInvertedMotor(RobotDrive::kRearLeftMotor, true);// you may need to change or remove this to match your robot + + gyro = new Gyro(gyroChannel); + } + + /** + * Runs during autonomous. + */ + void Autonomous() { + + } + + /** + * Runs the motors with arcade steering. + */ + void OperatorControl() { + gyro->SetSensitivity(voltsPerDegreePerSecond); //calibrate gyro to have the value equal to degrees + while (IsOperatorControl() && IsEnabled()) { + myRobot->MecanumDrive_Cartesian(joystick->GetX(), joystick->GetY(), + joystick->GetZ(), gyro->GetAngle()); + Wait(0.005); // wait 5ms to avoid hogging CPU cycles + } + } + + /** + * Runs during test mode. + */ + void Test() { + + } +}; + +START_ROBOT_CLASS(Robot); diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Potentiometer/src/Robot.cpp b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Potentiometer/src/Robot.cpp new file mode 100644 index 0000000000..0076ae00a5 --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Potentiometer/src/Robot.cpp @@ -0,0 +1,84 @@ +#include "WPILib.h" + +/** + * This is a sample program to demonstrate the use of a soft potentiometer and proportional + * control to reach and maintain position setpoints on an elevator mechanism. A joystick + * button is used to switch elevator setpoints. + * + * 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 +{ + + const int potChannel = 1; //analog input pin + const int motorChannel = 7; //PWM channel + const int joystickChannel = 0; //usb number in DriverStation + const int buttonNumber = 4; //joystick button + + const double pGain = 1.0; //proportional speed constant + double motorSpeed; + double currentPosition; //sensor voltage reading corresponding to current elevator position + + AnalogInput *potentiometer; + Victor *elevatorMotor; + Joystick *joystick; + +public: + Robot() : + SampleRobot() + { + //make objects for the potentiometer, elevator motor controller, and joystick + potentiometer = new AnalogInput(potChannel); + elevatorMotor = new Victor(motorChannel); + joystick = new Joystick(joystickChannel); + } + + /** + * Runs during autonomous. + */ + void Autonomous() + { + + } + + /** + * + */ + void OperatorControl() { + bool buttonState; + bool prevButtonState = false; + + int index = 0; //setpoint array index + double currentSetpoint; //holds desired setpoint + const int size = 3; //number of setpoints + const double setpoints[size] = {1.0, 2.6, 4.3}; //bottom, middle, and top elevator setpoints + currentSetpoint = setpoints[0]; //set to first setpoint + + while (IsOperatorControl() && IsEnabled()) { + buttonState = joystick->GetRawButton(buttonNumber); //check if button is pressed + + //if button has been pressed and released once + if (buttonState && !prevButtonState) { + index = (index + 1) % size; //increment set point, reset if at maximum + currentSetpoint = setpoints[index]; //set setpoint + } + prevButtonState = buttonState; //record previous button state + + currentPosition = potentiometer->GetAverageVoltage(); //get position value + motorSpeed = (currentPosition - currentSetpoint)*pGain; //convert position error to speed + elevatorMotor->Set(motorSpeed); //drive elevator motor + } + } + + /** + * Runs during test mode. + */ + void Test() + { + + } +}; + +START_ROBOT_CLASS(Robot); diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/PotentiometerPID/src/Robot.cpp b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/PotentiometerPID/src/Robot.cpp new file mode 100644 index 0000000000..14a6d31abe --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/PotentiometerPID/src/Robot.cpp @@ -0,0 +1,85 @@ +#include "WPILib.h" + +/** + * This is a sample program to demonstrate how to use a soft potentiometer and a PID + * Controller to reach and maintain position setpoints on an elevator mechanism. + * + * 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 { + const int potChannel = 1; //analog input pin + const int motorChannel = 7; //PWM channel + const int joystickChannel = 0; //usb number in DriverStation + const int buttonNumber = 4; //button on joystick + + const double setPoints[3] = { 1.0, 2.6, 4.3 }; //bottom, middle, and top elevator setpoints + + //proportional, integral, and derivative speed constants; motor inverted + //DANGER: when tuning PID constants, high/inappropriate values for pGain, iGain, + //and dGain may cause dangerous, uncontrollable, or undesired behavior! + const double pGain = -5.0, iGain = -0.02, dGain = -2.0; //these may need to be positive for a non-inverted motor + + PIDController *pidController; + AnalogInput *potentiometer; + Victor *elevatorMotor; + Joystick *joystick; + +public: + Robot() : + SampleRobot() + { + //make objects for potentiometer, the elevator motor controller, and the joystick + potentiometer = new AnalogInput(potChannel); + elevatorMotor = new Victor(motorChannel); + joystick = new Joystick(joystickChannel); + + //potentiometer (AnalogInput) and elevatorMotor (Victor) can be used as a + //PIDSource and PIDOutput respectively + pidController = new PIDController(pGain, iGain, dGain, potentiometer, + elevatorMotor); + } + + /** + * Runs during autonomous. + */ + void Autonomous() { + + } + + /** + * Uses a PIDController and an array of setpoints to switch and maintain elevator positions. + * The elevator setpoint is selected by a joystick button. + */ + void OperatorControl() { + pidController->SetInputRange(0, 5); //0 to 5V + pidController->SetSetpoint(setPoints[0]); //set to first setpoint + + int index = 0; + bool currentValue; + bool previousValue = false; + + while (IsOperatorControl() && IsEnabled()) { + pidController->Enable(); //begin PID control + + //when the button is pressed once, the selected elevator setpoint is incremented + currentValue = joystick->GetRawButton(buttonNumber); + if (currentValue && !previousValue) { + pidController->SetSetpoint(setPoints[index]); + index = (index + 1) % (sizeof(setPoints)/8); //index of elevator setpoint wraps around + } + previousValue = currentValue; + } + } + + /** + * Runs during test mode. + */ + void Test() { + + } +}; + +START_ROBOT_CLASS(Robot); 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 index 8965ff12ec..eacd2e90a8 100644 --- 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 @@ -1,8 +1,12 @@ #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. + * 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 @@ -46,7 +50,7 @@ public: void OperatorControl() { double currentDistance; //distance measured from the ultrasonic sensor values - double currentSpeed; //speed to set the motor + 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 diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/UltrasonicPID/src/Robot.cpp b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/UltrasonicPID/src/Robot.cpp new file mode 100644 index 0000000000..ec8ce13d5d --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/UltrasonicPID/src/Robot.cpp @@ -0,0 +1,99 @@ +#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 { + AnalogInput *ultrasonic; //ultrasonic sensor + RobotDrive *myRobot; + PIDController *pidController; + +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: + RobotDrive* rd; + MyPIDOutput(RobotDrive *r) + { + rd = r; + rd->SetSafetyEnabled(false); + } + void PIDWrite(float output) { + rd->Drive(output, 0); //write to myRobot (RobotDrive) by reference + } + }; + + Robot() : + SampleRobot() { + //make objects for sensor and drive train + ultrasonic = new AnalogInput(ultrasonicChannel); + myRobot = new RobotDrive(new CANTalon(leftMotorChannel), + new CANTalon(leftRearMotorChannel), + new CANTalon(rightMotorChannel), + new CANTalon(rightRearMotorChannel)); + + //ultrasonic (AnalogInput) can be used as a PIDSource without modification, + //PIDOutput is an instance of the internal class MyPIDOutput made earlier + pidController = new PIDController(pGain, iGain, dGain, ultrasonic, + new MyPIDOutput(myRobot)); + + } + + /** + * Runs during autonomous. + */ + void Autonomous() + { + + } + + /** + * Drives robot to set distance from an object using PID control and the ultrasonic + * sensor. + */ + void OperatorControl() { + pidController->SetSetpoint(holdDistance * VoltsToInches); //set setpoint to 12 inches + + //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. + pidController->SetInputRange(0, 24 * VoltsToInches); + + while (IsOperatorControl() && IsEnabled()) { + pidController->Enable(); //begin PID control + } + } + + /** + * 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 ad01afca9d..a0d0a07f33 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 @@ -267,11 +267,11 @@ Ultrasonic Demonstrate maintaining a set distance using an ultrasonic sensor. - Getting Started with C++ Robot and Motor CAN Complete List Sensors + Analog src @@ -282,7 +282,103 @@ - + + UltrasonicPID + Demonstrate maintaining a set distance using an ultrasonic sensor and PID control. + + Robot and Motor + CAN + Complete List + Sensors + Analog + + + src + + + + + + + + + Gyro + An example program showing how to drive straight with using a gyro sensor. + + Robot and Motor + CAN + Complete List + Sensors + Analog + Joystick + + + src + + + + + + + + + Gyro Mecanum + An example program showing how to perform mecanum drive with field oriented controls. + + Robot and Motor + CAN + Complete List + Sensors + Analog + Joysitck + + + src + + + + + + + + + PotentiometerPID + An example to demonstrate the use of a potentiometer and PID control to reach elevator position setpoints. + + Joystick + Actuators + Complete List + Sensors + Analog + + + src + + + + + + + + + Potentiometer + An example to demonstrate the use of a potentiometer and basic proportional control to reach elevator position setpoints. + + Joystick + Actuators + Complete List + Sensors + Analog + + + src + + + + + + + Getting Started An example program which demonstrates the simplest autonomous and diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Gyro/src/org/usfirst/frc/team190/robot/Robot.java b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Gyro/src/org/usfirst/frc/team190/robot/Robot.java new file mode 100644 index 0000000000..eab760a45e --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Gyro/src/org/usfirst/frc/team190/robot/Robot.java @@ -0,0 +1,86 @@ + +package $package; + +import edu.wpi.first.wpilibj.CANTalon; +import edu.wpi.first.wpilibj.Gyro; +import edu.wpi.first.wpilibj.SampleRobot; +import edu.wpi.first.wpilibj.RobotDrive; +import edu.wpi.first.wpilibj.Joystick; + +/** + * This is a sample program to demonstrate how to use a gyro sensor to make a robot drive + * straight. This program uses a joystick to drive forwards and backwards while the gyro + * is used for direction keeping. + * + * 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. + */ +public class Robot extends SampleRobot { + + final int gyroChannel = 0; //analog input + final int joystickChannel = 0; //usb number in DriverStation + + //channels for motors + final int leftMotorChannel = 1; + final int rightMotorChannel = 0; + final int leftRearMotorChannel = 3; + final int rightRearMotorChannel = 2; + + double angleSetpoint = 0.0; + final double pGain = .005; //propotional turning constant + + //gyro calibration constant, may need to be adjusted; + //gyro value of 360 is set to correspond to one full revolution + final double voltsPerDegreePerSecond = .0128; + + RobotDrive myRobot; + Gyro gyro; + Joystick joystick; + + public Robot() + { + //make objects for the drive train, gyro, and joystick + myRobot = new RobotDrive(new CANTalon(leftMotorChannel), new CANTalon( + leftRearMotorChannel), new CANTalon(rightMotorChannel), + new CANTalon(rightRearMotorChannel)); + gyro = new Gyro(gyroChannel); + joystick = new Joystick(joystickChannel); + } + + /** + * Runs during autonomous. + */ + public void autonomous() { + + } + + /** + * Sets the gyro sensitivity and drives the robot when the joystick is pushed. The + * motor speed is set from the joystick while the RobotDrive turning value is assigned + * from the error between the setpoint and the gyro angle. + */ + public void operatorControl() { + double turningValue; + gyro.setSensitivity(voltsPerDegreePerSecond); //calibrates gyro values to equal degrees + while (isOperatorControl() && isEnabled()) { + + turningValue = (angleSetpoint - gyro.getAngle())*pGain; + if(joystick.getY() <= 0) + { + //forwards + myRobot.drive(joystick.getY(), turningValue); + } else { + //backwards + myRobot.drive(joystick.getY(), -turningValue); + } + } + } + + /** + * Runs during test mode. + */ + public void test(){ + + } +} diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/GyroMecanum/src/org/usfirst/frc/team190/robot/Robot.java b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/GyroMecanum/src/org/usfirst/frc/team190/robot/Robot.java new file mode 100644 index 0000000000..dbe0ab0822 --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/GyroMecanum/src/org/usfirst/frc/team190/robot/Robot.java @@ -0,0 +1,71 @@ + +package $package; + +import edu.wpi.first.wpilibj.CANTalon; +import edu.wpi.first.wpilibj.Gyro; +import edu.wpi.first.wpilibj.SampleRobot; +import edu.wpi.first.wpilibj.RobotDrive; +import edu.wpi.first.wpilibj.Joystick; +import edu.wpi.first.wpilibj.Timer; +import edu.wpi.first.wpilibj.RobotDrive.MotorType; + +/** + * This is a sample program that uses mecanum drive with a gyro sensor to maintian + * rotation vectorsin relation to the starting orientation of the robot (field-oriented controls). + * + * 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. + */ +public class Robot extends SampleRobot { + RobotDrive myRobot; + Joystick joystick; + Gyro gyro; + + //channels for motors + final int leftMotorChannel = 1; + final int rightMotorChannel = 0; + final int leftRearMotorChannel = 3; + final int rightRearMotorChannel = 2; + + final int gyroChannel = 0; //analog input + + //gyro calibration constant, may need to be adjusted so that a gyro value of 360 + //equals 360 degrees + final double voltsPerDegreePerSecond = .0128; + + public Robot() { + //make objects for drive train, joystick, and gyro + myRobot = new RobotDrive(new CANTalon(leftMotorChannel), new CANTalon(leftRearMotorChannel), + new CANTalon(rightMotorChannel), new CANTalon(rightRearMotorChannel)); + myRobot.setInvertedMotor(MotorType.kFrontLeft, true); // invert the left side motors + myRobot.setInvertedMotor(MotorType.kRearLeft, true); // you may need to change or remove this to match your robot + + joystick = new Joystick(0); + gyro = new Gyro(gyroChannel); + } + + /** + * Runs during autonomous. + */ + public void autonomous() { + + } + + /** + * Gyro sensitivity is set and mecanum drive is used with the gyro angle as an input. + */ + public void operatorControl() { + gyro.setSensitivity(voltsPerDegreePerSecond); //calibrate gyro to have the value equal to degrees + while (isOperatorControl() && isEnabled()) { + myRobot.mecanumDrive_Cartesian(joystick.getX(), joystick.getY(), joystick.getZ(), gyro.getAngle()); + Timer.delay(0.005); // wait 5ms to avoid hogging CPU cycles + } + } + + /** + * Runs during test mode + */ + public void test() { + } +} diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Potentiometer/src/org/usfirst/frc/team190/robot/Robot.java b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Potentiometer/src/org/usfirst/frc/team190/robot/Robot.java new file mode 100644 index 0000000000..c95388981a --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/Potentiometer/src/org/usfirst/frc/team190/robot/Robot.java @@ -0,0 +1,83 @@ + +package $package; + +import edu.wpi.first.wpilibj.AnalogInput; +import edu.wpi.first.wpilibj.SampleRobot; +import edu.wpi.first.wpilibj.Joystick; +import edu.wpi.first.wpilibj.Victor; + +/** + * This is a sample program to demonstrate the use of a soft potentiometer and proportional + * control to reach and maintain position setpoints on an elevator mechanism. A joystick + * button is used to switch elevator setpoints. + * + * 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. + */ +public class Robot extends SampleRobot { + final int potChannel = 1; //analog input pin + final int motorChannel = 7; //PWM channel + final int joystickChannel = 0; //usb number in DriverStation + final int buttonNumber = 4; //joystick button + + final double setpoints[] = {1.0, 2.6, 4.3}; //bottom, middle, and top elevator setpoints + + final double pGain = 1.0; //proportional speed constant + double motorSpeed; + double currentPosition; //sensor voltage reading corresponding to current elevator position + + AnalogInput potentiometer; + Victor elevatorMotor; + Joystick joystick; + + public Robot() { + //make objects for the potentiometer, elevator motor controller, and joystick + potentiometer = new AnalogInput(potChannel); + elevatorMotor = new Victor(motorChannel); + joystick = new Joystick(joystickChannel); + } + + /** + * Runs during autonomous. + */ + public void autonomous() { + + } + + /** + * Moves elevator to a selectable setpoint that can be changed by pressing a button on + * the joystick. Proportional control is used to reach and maintain the desired setpoint + * by obtaining values from the potentiometer and comparing them to the setpoint value. + */ + public void operatorControl() { + boolean buttonState; + boolean prevButtonState = false; + + int index = 0; //setpoint array index + double currentSetpoint; //holds desired setpoint + currentSetpoint = setpoints[0]; //set to first setpoint + + while (isOperatorControl() && isEnabled()) { + buttonState = joystick.getRawButton(buttonNumber); //check if button is pressed + + //if button has been pressed and released once + if(buttonState && !prevButtonState) { + index = (index + 1) % setpoints.length; //increment set point, reset if at end of array + currentSetpoint = setpoints[index]; //set setpoint + } + prevButtonState = buttonState; //record previous button state + + currentPosition = potentiometer.getAverageVoltage(); //get position value + motorSpeed = (currentPosition - currentSetpoint)*pGain; //convert position error to speed + elevatorMotor.set(motorSpeed); //drive elevator motor + } + } + + /** + * Runs during test mode + */ + public void test() { + + } +} diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/PotentiometerPID/src/org/usfirst/frc/team190/robot/Robot.java b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/PotentiometerPID/src/org/usfirst/frc/team190/robot/Robot.java new file mode 100644 index 0000000000..b45781c67f --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/PotentiometerPID/src/org/usfirst/frc/team190/robot/Robot.java @@ -0,0 +1,85 @@ + +package $package; + +import edu.wpi.first.wpilibj.AnalogInput; +import edu.wpi.first.wpilibj.PIDController; +import edu.wpi.first.wpilibj.SampleRobot; +import edu.wpi.first.wpilibj.Joystick; +import edu.wpi.first.wpilibj.Victor; + +/** + * This is a sample program to demonstrate how to use a soft potentiometer and a PID + * controller to reach and maintain position setpoints on an elevator mechanism. + * + * 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. + */ +public class Robot extends SampleRobot { + final int potChannel = 1; //analog input pin + final int motorChannel = 7; //PWM channel + final int joystickChannel = 0; //usb number in DriverStation + final int buttonNumber = 4; //button on joystick + + final double setPoints[] = {1.0, 2.6, 4.3}; //bottom, middle, and top elevator setpoints + + //proportional, integral, and derivative speed constants; motor inverted + //DANGER: when tuning PID constants, high/inappropriate values for pGain, iGain, + //and dGain may cause dangerous, uncontrollable, or undesired behavior! + final double pGain = -5.0, iGain = -0.02, dGain = -2.0; //these may need to be positive for a non-inverted motor + + PIDController pidController; + AnalogInput potentiometer; + Victor elevatorMotor; + Joystick joystick; + + public Robot() { + //make objects for potentiometer, the elevator motor controller, and the joystick + potentiometer = new AnalogInput(potChannel); + elevatorMotor = new Victor(motorChannel); + joystick = new Joystick(joystickChannel); + + //potentiometer (AnalogInput) and elevatorMotor (Victor) can be used as a + //PIDSource and PIDOutput respectively + pidController = new PIDController(pGain, iGain, dGain, potentiometer, elevatorMotor); + } + + /** + * Runs during autonomous. + */ + public void autonomous() { + + } + + /** + * Uses a PIDController and an array of setpoints to switch and maintain elevator + * positions. The elevator setpoint is selected by a joystick button. + */ + public void operatorControl() { + pidController.setInputRange(0, 5); //0 to 5V + pidController.setSetpoint(setPoints[0]); //set to first setpoint + + int index = 0; + boolean currentValue; + boolean previousValue = false; + + while (isOperatorControl() && isEnabled()) { + pidController.enable(); //begin PID control + + //when the button is pressed once, the selected elevator setpoint is incremented + currentValue = joystick.getRawButton(buttonNumber); + if(currentValue && !previousValue){ + pidController.setSetpoint(setPoints[index]); + index = (index + 1) % setPoints.length; //index of elevator setpoint wraps around + } + previousValue = currentValue; + } + } + + /** + * Runs during test mode. + */ + public void test() { + + } +} 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 index 99a2cee4b8..79940f5501 100644 --- 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 @@ -1,5 +1,5 @@ -package org.usfirst.frc.team190.robot; +package $package; import edu.wpi.first.wpilibj.CANTalon; import edu.wpi.first.wpilibj.SampleRobot; @@ -9,6 +9,10 @@ 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. + * + * 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. */ public class Robot extends SampleRobot { @@ -48,7 +52,7 @@ public class Robot extends SampleRobot { public void operatorControl() { double currentDistance; //distance measured from the ultrasonic sensor values - double currentSpeed; //speed to set the motor + 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 diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/UltrasonicPID/src/org/usfirst/frc/team190/robot/Robot.java b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/UltrasonicPID/src/org/usfirst/frc/team190/robot/Robot.java new file mode 100644 index 0000000000..45f39ea185 --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.java/resources/templates/examples/UltrasonicPID/src/org/usfirst/frc/team190/robot/Robot.java @@ -0,0 +1,89 @@ + +package $package; + +import edu.wpi.first.wpilibj.AnalogInput; +import edu.wpi.first.wpilibj.CANTalon; +import edu.wpi.first.wpilibj.PIDOutput; +import edu.wpi.first.wpilibj.SampleRobot; +import edu.wpi.first.wpilibj.RobotDrive; +import edu.wpi.first.wpilibj.PIDController; + +/** + * 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. + */ +public class Robot extends SampleRobot { + AnalogInput ultrasonic; //ultrasonic sensor + RobotDrive myRobot; + PIDController pidController; + + final int ultrasonicChannel = 3; //analog input + + //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 + + //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! + final 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. + final double VoltsToInches = 0.0098; + + //internal class to write to myRobot (a RobotDrive object) using a PIDOutput + public class MyPIDOutput implements PIDOutput { + @Override + public void pidWrite(double output) { + myRobot.drive(output, 0); //drive robot from PID output + } + } + + public Robot() { + //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)); + + //ultrasonic (AnalogInput) can be used as a PIDSource without modification, + //PIDOutput is an instance of the internal class MyPIDOutput made earlier + pidController = new PIDController(pGain, iGain, dGain, ultrasonic, new MyPIDOutput()); + } + + /** + * Runs during autonomous. + */ + public void autonomous() { + } + + /** + * Drives the robot a set distance from an object using PID control and the + * ultrasonic sensor. + */ + public void operatorControl() { + pidController.setSetpoint(holdDistance*VoltsToInches); //set setpoint to 12 inches + + //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. + pidController.setInputRange(0, 24*VoltsToInches); + + while (isOperatorControl() && isEnabled()) { + pidController.enable(); //begin PID control + } + } + + /** + * 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 71ccd0664d..ebe02c943b 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 @@ -150,6 +150,7 @@ Complete List Robot and Motor CAN + Analog src/$package-dir @@ -161,6 +162,108 @@ + + Ultrasonic PID + Demonstrate maintaining a set distance using an ultrasonic sensor and PID Control. + + Sensors + Complete List + Robot and Motor + CAN + Analog + + + src/$package-dir + + + + + + + + + Potentiometer PID + An example to demonstrate the use of a potentiometer and PID control to reach elevator position setpoints. + + Sensors + Complete List + Actuators + Analog + Joystick + + + src/$package-dir + + + + + + + + + Potentiometer + An example to demonstrate the use of a potentiometer and basic proportional control to reach elevator position setpoints. + + Sensors + Complete List + Actuators + Analog + Joystick + + + src/$package-dir + + + + + + + + + Gyro + An example program showing how to drive straight with using a gyro sensor. + + Sensors + Complete List + Robot and Motor + CAN + Analog + Joystick + + + src/$package-dir + + + + + + + + + Gyro Mecanum + An example program showing how to perform mecanum drive with field oriented controls. + + Sensors + Complete List + Robot and Motor + CAN + Analog + Joystick + + + src/$package-dir + + + + + + + Motor Controller Demonstrate controlling a single motor with a joystick