diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Relay/src/Robot.cpp b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Relay/src/Robot.cpp new file mode 100644 index 0000000000..c7c83ed795 --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Relay/src/Robot.cpp @@ -0,0 +1,69 @@ +#include "WPILib.h" + +/** + * This is a sample program which uses joystick buttons to control a relay. + * A Relay (generally a spike) has two outputs, each of which can be at either + * 0V or 12V and so can be used for actions such as turning a motor off, + * full forwards, or full reverse, and is generally used on the compressor. + * This program uses two buttons on a joystick and each button corresponds to + * one output; pressing the button sets the output to 12V and releasing sets + * it to 0V. + * During Operator Control, the loop waits for a brief time before continuing + * in order to allow other threads to run. This is generally a good idea, + * especially as joystick values are only received every 20ms. + */ +class Robot: public SampleRobot +{ + // Joystick with which to control the relay. + Joystick m_stick; + + // Relay to use for the + Relay m_relay; + + // Numbers of the buttons to be used for controlling the Relay. + const int kRelayForwardButton = 1; + const int kRelayReverseButton = 2; + + // Update every 5milliseconds/0.005 seconds. + const double kUpdatePeriod = 0.005; + +public: + Robot() : + m_stick(0), // Use joystick on port 0. + m_relay(0) // Relay on port 0. + { + } + + /** + * Control a Relay using Joystick buttons. + */ + void OperatorControl() + { + while (IsOperatorControl()) + { + // Retrieve the button values. GetRawButton will return + // true if the button is pressed and false if not. + bool forward = m_stick.GetRawButton(kRelayForwardButton); + bool reverse = m_stick.GetRawButton(kRelayReverseButton); + + // Depending on the button values, we want to use one of + // kOn, kOff, kForward, or kReverse. + // kOn sets both outputs to 12V, kOff sets both to 0V, + // kForward sets forward to 12V and reverse to 0V, and + // kReverse sets reverse to 12V and forward to 0V. + if (forward && reverse) + m_relay.Set(Relay::kOn); + else if (forward) + m_relay.Set(Relay::kForward); + else if (reverse) + m_relay.Set(Relay::kReverse); + else + m_relay.Set(Relay::kOff); + + // Insert 5ms delay in loop. + Wait(kUpdatePeriod); + } + } +}; + +START_ROBOT_CLASS(Robot); diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Solenoid/src/Robot.cpp b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Solenoid/src/Robot.cpp new file mode 100644 index 0000000000..c8f91e7406 --- /dev/null +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/resources/templates/examples/Solenoid/src/Robot.cpp @@ -0,0 +1,77 @@ +#include "WPILib.h" + +/** + * This is a sample program showing the use of the solenoid classes during + * operator control. + * Three buttons from a joystick will be used to control two solenoids: + * One button to control the position of a single solenoid and the other + * two buttons to control a double solenoid. + * Single solenoids can either be on or off, such that the air diverted through + * them goes through either one channel or the other. + * Double solenoids have three states: Off, Forward, and Reverse. Forward and + * Reverse divert the air through the two channels and correspond to the + * on and off of a single solenoid, but a double solenoid can also be "off", + * where both channels are diverted to exhaust such that there is no pressure + * in either channel. + * Additionally, double solenoids take up two channels on your PCM whereas + * single solenoids only take a single channel. + * During Operator Control, the loop waits for a brief time before continuing + * in order to allow other threads to run. This is generally a good idea, + * especially as joystick values are only received every 20ms. + */ +class Robot: public SampleRobot +{ + // Joystick with buttons to control solenoids with. + Joystick m_stick; + // Solenoids to control with the joystick. + // Solenoid corresponds to a single solenoid. + Solenoid m_solenoid; + // DoubleSolenoid corresponds to a double solenoid. + DoubleSolenoid m_doubleSolenoid; + + // Update every 5milliseconds/0.005 seconds. + const double kUpdatePeriod = 0.005; + + // Numbers of the buttons to use for triggering the solenoids. + const int kSolenoidButton = 1; + const int kDoubleSolenoidForward = 2; + const int kDoubleSolenoidReverse = 3; + +public: + Robot() : + m_stick(0), // Use joystick on port 0. + m_solenoid(0), // Use solenoid on channel 0. + // Use double solenoid with Forward Channel of 1 and Reverse of 2. + m_doubleSolenoid(1, 2) + { + } + + /** + * Sets the solenoids from the position of joystick buttons. + */ + void OperatorControl() + { + while (IsOperatorControl()) + { + // The output of GetRawButton is true/false depending on whether + // the button is pressed; Set takes a boolean for for whether to + // use the default (false) channel or the other (true). + m_solenoid.Set(m_stick.GetRawButton(kSolenoidButton)); + + // In order to set the double solenoid, we will say that if neither + // button is pressed, it is off, if just one button is pressed, + // set the solenoid to correspond to that button, and if both + // are pressed, set the solenoid to Forwards. + if (m_stick.GetRawButton(kDoubleSolenoidForward)) + m_doubleSolenoid.Set(DoubleSolenoid::kForward); + else if (m_stick.GetRawButton(kDoubleSolenoidReverse)) + m_doubleSolenoid.Set(DoubleSolenoid::kReverse); + else + m_doubleSolenoid.Set(DoubleSolenoid::kOff); + + Wait(kUpdatePeriod); // wait for a motor update time + } + } +}; + +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 0ecad12a48..de77ba9069 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 @@ -104,6 +104,39 @@ + + Relay + Demonstrate controlling a Relay from Joystick buttons. + + Actuators + Joystick + Complete List + + + src + + + + + + + + Solenoids + Demonstrate controlling a single and double solenoid from Joystick buttons. + + Actuators + Joystick + Pneumatics + Complete List + + + src + + + + + + Arcade Drive An example program which the use of Arcade Drive with the RobotDrive class diff --git a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/src/main/java/edu/wpi/first/wpilib/plugins/cpp/wizards/examples/ExampleCPPWizard.java b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/src/main/java/edu/wpi/first/wpilib/plugins/cpp/wizards/examples/ExampleCPPWizard.java index 4d0bb67488..9798ce5e53 100644 --- a/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/src/main/java/edu/wpi/first/wpilib/plugins/cpp/wizards/examples/ExampleCPPWizard.java +++ b/eclipse-plugins/edu.wpi.first.wpilib.plugins.cpp/src/main/java/edu/wpi/first/wpilib/plugins/cpp/wizards/examples/ExampleCPPWizard.java @@ -41,7 +41,7 @@ public class ExampleCPPWizard extends ExampleWizard { protected IWizardPage getDetailsPage() { if (detailsPage != null) return detailsPage; detailsPage = new NewProjectMainPage(selection, getTeamNumberPage()); - detailsPage.setTitle("Create Example Robot Java Project"); + detailsPage.setTitle("Create Example Robot C++ Project"); detailsPage.setDescription("This wizard creates a new example project based on your selection."); detailsPage.setShowPackage(false); return detailsPage;