Added sample programs for Solenoids and Relays.

Added a sample program to the eclipse plugin which uses joystick buttons
in Operator Control mode in order to control the output of a single and
a double solenoid.
Also added a sample program which uses joystick buttons to control a
spike/Relay.

Bonus: The title on the window that you get when you are opening a sample in
C++ said "Java" instead of "C++". Fixed now.

Change-Id: I0d01c23003d1fba2dbb08cbe6977ec886d97a22f
This commit is contained in:
James Kuszmaul
2014-10-21 11:18:43 -04:00
parent cd75dc71e8
commit 3abd352be9
4 changed files with 180 additions and 1 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -104,6 +104,39 @@
</files>
</example>
<example>
<name>Relay</name>
<description>Demonstrate controlling a Relay from Joystick buttons.</description>
<tags>
<tag>Actuators</tag>
<tag>Joystick</tag>
<tag>Complete List</tag>
</tags>
<packages>
<package>src</package>
</packages>
<files>
<file source="examples/Relay/src/Robot.cpp" destination="src/Robot.cpp"></file>
</files>
</example>
<example>
<name>Solenoids</name>
<description>Demonstrate controlling a single and double solenoid from Joystick buttons.</description>
<tags>
<tag>Actuators</tag>
<tag>Joystick</tag>
<tag>Pneumatics</tag>
<tag>Complete List</tag>
</tags>
<packages>
<package>src</package>
</packages>
<files>
<file source="examples/Solenoid/src/Robot.cpp" destination="src/Robot.cpp"></file>
</files>
</example>
<example>
<name>Arcade Drive</name>
<description>An example program which the use of Arcade Drive with the RobotDrive class</description>

View File

@@ -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;