You are on page 1of 31

Programming a Robot

Using C++
Philipp Schrader & Tom Brown
October 27, 2012
Programming in FRC
Introduction
Programming in FRC The robot has mechanical systems and
C++ Overview
Why C++ in FRC? electrical hardware, but needs a program to tell
First Steps
What is programming?
it what to do
Source Control
Windriver
WPILib
The program collects inputs from the drivers
Examples
Simple Robot
Drive with Joystick
and sensors, and uses them to decide what
Sensors
Simple Feedback motor output should be
Pneumatics
Autonomous

Contact Information Different programming “languages”


LabVIEW, C++, Java

Philipp Schrader & Tom Brown - October 27, 2012


C++ Overview
Introduction
Programming in FRC Invented in the 1980’s
C++ Overview
Why C++ in FRC?

First Steps
What is programming?
Built as an extension on top of C
Source Control
Windriver
WPILib
Object-oriented programming language
Examples
Simple Robot
Drive with Joystick
Sensors
Simple Feedback
Pneumatics
Autonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Why C++ in FRC?
Introduction
Programming in FRC Powerful and fast language; used widely in
C++ Overview
Why C++ in FRC? industry
First Steps
What is programming?
Source Control
Windriver Steep learning curve, but after that
WPILib
development is fast
Examples
Simple Robot
Drive with Joystick
Sensors
Simple Feedback Programming tools are less complicated,
Pneumatics
Autonomous smaller, faster than LabVIEW
Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


What is programming?
Introduction
Programming in FRC Writing instructions for machines to perform a
C++ Overview
Why C++ in FRC? set of desired tasks
First Steps
What is programming?
Source Control
Windriver It's about controlling how inputs to a system
WPILib
affect the outputs of the system.
Examples
Simple Robot
Drive with Joystick
Sensors
Simple Feedback
Pneumatics
Autonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Source Control
Introduction
Programming in FRC Centralizing where your source code is stored.
C++ Overview
Why C++ in FRC?

First Steps
What is programming?
Acts as a backup storage, history browser, and
Source Control
Windriver versioning for the code.
WPILib

Examples
Simple Robot
Drive with Joystick
Easily shows differences between two versions
Sensors
Simple Feedback of the code. This includes the "current working
Pneumatics
Autonomous version" with any modifications.
Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Wind River
Introduction
Programming in FRC The Windows program used to compile code
C++ Overview
Why C++ in FRC? into programs for the robot.
First Steps
What is programming?
Source Control
Windriver Primarily serves as a development environment
WPILib
for writing and debugging code.
Examples
Simple Robot
Drive with Joystick
Sensors
Simple Feedback
Pneumatics
Autonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


WPILib
Introduction
Programming in FRC Already-written code provided by FIRST to
C++ Overview
Why C++ in FRC? make robot programming easier
First Steps
What is programming?
Source Control
Windriver Consists of classes that represent all common
WPILib
robot hardware
Examples
Simple Robot
Drive with Joystick
Sensors
Simple Feedback Example: Compressor, DigitalInput, Victor,
Pneumatics
Autonomous DriverStation, Solenoid, Gyro
Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Simple Robot
Introduction
Programming in FRC SimpleRobot is a starting template to help
C++ Overview
Why C++ in FRC? teams quickly get a robot running.
First Steps
What is programming?
Source Control
Windriver There are other templates such as
WPILib
IterativeRobot, but we will focus on
Examples
Simple Robot
Drive with Joystick
SimpleRobot.
Sensors
Simple Feedback
Pneumatics
Autonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Simple Robot Cont'd
Introduction
Programming in FRC
#include "WPILib.h"
C++ Overview
Why C++ in FRC?

First Steps class RobotDemo : public SimpleRobot


What is programming?
Source Control {
Windriver
WPILib public:
Examples RobotDemo(void) { }
Simple Robot
Drive with Joystick
Sensors
void Autonomous(void) { }
Simple Feedback
Pneumatics
void OperatorControl(void) { }
Autonomous
};
Contact Information

START_ROBOT_CLASS(RobotDemo);

Philipp Schrader & Tom Brown - October 27, 2012


Simple Robot Cont'd
Introduction
Programming in FRC RobotDemo() is the constructor. This is where
C++ Overview
Why C++ in FRC? you initialize all members of the class.
First Steps
What is programming?
Source Control
Windriver Autonomous() is the function that gets called
WPILib
during the autonomous or hybrid period of a
Examples
Simple Robot
Drive with Joystick
match.
Sensors
Simple Feedback
Pneumatics
Autonomous OperatorControl() is the function that gets
Contact Information called during teleop. Function gets called once
and needs a loop to continue operator control.

Philipp Schrader & Tom Brown - October 27, 2012


Drive with Joystick
Introduction
Programming in FRC
// Declare drive components
C++ Overview
Why C++ in FRC?
Joystick *stick;
First Steps Victor *leftDrive;
What is programming?
Source Control Victor *rightDrive;
Windriver
WPILib

Examples // Initialize components with port numbers


Simple Robot
Drive with Joystick
Sensors
RobotDemo(void) {
Simple Feedback
Pneumatics
stick = new Joystick(1);
Autonomous
leftDrive = new Victor(1);
Contact Information
rightDrive = new Victor(2);
}

Philipp Schrader & Tom Brown - October 27, 2012


Drive with Joystick Cont'd
Introduction
Programming in FRC
void OperatorControl(void) {
C++ Overview
Why C++ in FRC?
while (IsOperatorControl()) {
First Steps float leftY = stick->GetRawAxis(2);
What is programming?
Source Control float rightY = stick->GetRawAxis(4);
Windriver
WPILib

Examples // Tank drive


Simple Robot
Drive with Joystick
Sensors
leftDrive->SetSpeed(-leftY);
Simple Feedback
Pneumatics
rightDrive->SetSpeed(rightY);
Autonomous

Contact Information
Wait(0.005);
}
}

Philipp Schrader & Tom Brown - October 27, 2012


Sensors
Introduction
Programming in FRC Sensors help the operators with controlling the
C++ Overview
Why C++ in FRC? robot.
First Steps
What is programming?
Source Control
Windriver
They are crucial in autonomous, but also very
WPILib
valuable for teleop.
Examples
Simple Robot
Drive with Joystick
Sensors
Simple Feedback
For example, appropriate sensors can be used
Pneumatics
Autonomous to precisely position an arm on the robot.
Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Sensors Cont'd
Introduction
Programming in FRC
AnalogChannel *plowPot;
C++ Overview
Why C++ in FRC?

First Steps RobotDemo(void) {


What is programming?
Source Control plowPot = new AnalogChannel(2);
Windriver
WPILib }
Examples
Simple Robot
Drive with Joystick
Sensors
void OperatorControl(void) {
Simple Feedback
Pneumatics
while (IsOperatorControl()) {
Autonomous
printf("%d", plowPot->GetValue());
Contact Information
Wait(0.005);
}
}

Philipp Schrader & Tom Brown - October 27, 2012


Sensors Cont'd
Introduction
Programming in FRC In our example, we are getting a feeling for how
C++ Overview
Why C++ in FRC? the potentiometer behaves.
First Steps
What is programming?
Source Control
Windriver Some sensors have their own classes in WPILib.
WPILib
Use them whenever possible.
Examples
Simple Robot
Drive with Joystick
Sensors
Simple Feedback Base Classes:
Pneumatics
Autonomous AnalogChannel, DigitalInput
Contact Information

Examples:
Encoder, Gyro, UltraSonic

Philipp Schrader & Tom Brown - October 27, 2012


Simple Feedback
Introduction
Programming in FRC We can use sensors to perform more
C++ Overview
Why C++ in FRC? sophisticated maneuvers than a human alone
First Steps
What is programming?
could do.
Source Control
Windriver
WPILib
For example, we can move an arm into the best
Examples
Simple Robot
Drive with Joystick
scoring position consistently on the push of a
Sensors
Simple Feedback button.
Pneumatics
Autonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Simple Feedback Cont'd
Introduction
Programming in FRC
Relay *leftPlow;
C++ Overview
Why C++ in FRC?
Relay *rightPlow;
First Steps
What is programming?
Source Control RobotDemo(void) {
Windriver
WPILib leftPlow = new Relay(1);
Examples rightPlow = new Relay(2);
Simple Robot
Drive with Joystick
Sensors
}
Simple Feedback
Pneumatics
Autonomous
void SetPlow(Relay::Value direction) {
Contact Information
leftPlow->Set(direction);
rightPlow->Set(direction);
}

Philipp Schrader & Tom Brown - October 27, 2012


Simple Feedback Cont'd
Introduction
Programming in FRC
void OperatorControl(void) {
C++ Overview
Why C++ in FRC?
while (IsOperatorControl()) {
First Steps if (stick->GetRawButton(2)) {
What is programming?
Source Control SetPlow(Relay::kForward);
Windriver
WPILib }
Examples else if (stick->GetRawButton(3)) {
Simple Robot
Drive with Joystick
Sensors
SetPlow(Relay::kReverse);
Simple Feedback
Pneumatics
}
Autonomous
else {
Contact Information
SetPlow(Relay::kOff);
}
}
}
Philipp Schrader & Tom Brown - October 27, 2012
Simple Feedback Cont'd
Introduction
Programming in FRC The previous slide shows a simple plow control
C++ Overview
Why C++ in FRC? without sensors. Button 2 lowers the plow.
First Steps
What is programming?
Button 3 raises the plow.
Source Control
Windriver
WPILib
If we add sensor feedback to the code, we can
Examples
Simple Robot
Drive with Joystick
prevent the plow from going past mechanical
Sensors
Simple Feedback limits.
Pneumatics
Autonomous

Contact Information The next slide will incorporate feedback to the


plow to prevent breaking itself.

Philipp Schrader & Tom Brown - October 27, 2012


Simple Feedback Cont'd
Introduction
Programming in FRC
// Prevents the plow from going too far
C++ Overview
Why C++ in FRC?
if (stick->GetRawButton(2) &&
First Steps (plowPot->GetValue() > 60)) {
What is programming?
Source Control SetPlow(Relay::kForward);
Windriver
WPILib }
Examples else if (stick->GetRawButton(3) &&
Simple Robot
Drive with Joystick
Sensors
(plowPot->GetValue() < 250)) {
Simple Feedback
Pneumatics
SetPlow(Relay::kReverse);
Autonomous
}
Contact Information
else {
SetPlow(Relay::kOff);
}

Philipp Schrader & Tom Brown - October 27, 2012


Pneumatics
Introduction
Programming in FRC According to the pneumatics setup and electrical
C++ Overview
Why C++ in FRC? rules by FIRST, pneumatics have special
First Steps
What is programming?
programming requirements.
Source Control
Windriver
WPILib
Solenoids and compressors have their own class
Examples
Simple Robot
Drive with Joystick
in WPILib.
Sensors
Simple Feedback
Pneumatics
Autonomous Even if the compressor is not on the robot, it
Contact Information needs to be controlled by the robot itself.

Philipp Schrader & Tom Brown - October 27, 2012


Pneumatics Cont'd
Introduction
Programming in FRC
Solenoid *openGate;
C++ Overview
Why C++ in FRC?
Solenoid *closeGate;
First Steps Compressor *compressor;
What is programming?
Source Control
Windriver
WPILib RobotDemo(void) {
Examples openGate = new Solenoid(1);
Simple Robot
Drive with Joystick
Sensors
closeGate = new Solenoid(2);
Simple Feedback
Pneumatics
compressor = new Compressor (9, 5);
Autonomous
}
Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Pneumatics Cont'd
Introduction void OperatorControl(void) {
Programming in FRC
C++ Overview openGate->Set(false);
Why C++ in FRC?
closeGate->Set(true);
First Steps
What is programming?
Source Control while (IsOperatorControl()) {
Windriver
WPILib if (stick->GetRawButton(1)) {
Examples openGate->Set(true);
Simple Robot
Drive with Joystick
closeGate->Set(false);
Sensors }
Simple Feedback
Pneumatics else if (stick->GetRawButton(4)) {
Autonomous
openGate->Set(false);
Contact Information
closeGate->Set(true);
}
...

Philipp Schrader & Tom Brown - October 27, 2012


Pneumatics Cont'd
Introduction
Programming in FRC
C++ Overview ...
Why C++ in FRC?
// Only runs compressor until 120 psi
First Steps
What is programming?
if (!compressor->GetPressureSwitchValue()) {
Source Control compressor->Start();
Windriver
WPILib }
Examples else {
Simple Robot
Drive with Joystick
compressor->Stop();
Sensors }
Simple Feedback
Pneumatics
Autonomous
Wait(0.005);
Contact Information
}
}

Philipp Schrader & Tom Brown - October 27, 2012


Autonomous
Introduction
Programming in FRC The autonomous code works similarly to the
C++ Overview
Why C++ in FRC? teleop code.
First Steps
What is programming?
Source Control
Windriver
void Autonomous(void) {
WPILib
while (IsAutonomous()) {
Examples
Simple Robot // Your code here
Drive with Joystick
Sensors ...
Simple Feedback
Pneumatics }
Autonomous
}
Contact Information

Philipp Schrader & Tom Brown - October 27, 2012


Autonomous Cont'd
Introduction
Programming in FRC For this example we will make the robot lower
C++ Overview
Why C++ in FRC? and raise its plow until the end of the
First Steps
What is programming?
autonomous period.
Source Control
Windriver
WPILib
We will make use of a simple state machine that
Examples
Simple Robot
Drive with Joystick
cycles between a state that raises the plow and
Sensors
Simple Feedback a state that lowers the plow. We shall modify our
Pneumatics
Autonomous SetPlow function as well.
Contact Information

Switching between the states is done through


the potentiometer.

Philipp Schrader & Tom Brown - October 27, 2012


Autonomous Cont'd
Introduction bool SetPlow(Relay::Value direction) {
Programming in FRC
C++ Overview if ((direction == Relay::kForward &&
Why C++ in FRC?
plowPot->GetValue() > 60) ||
First Steps
What is programming?
(direction == Relay::kReverse &&
Source Control plowPot->GetValue() < 260)) {
Windriver
WPILib leftPlow->Set(direction);
Examples rightPlow->Set(direction);
Simple Robot
Drive with Joystick
return false;
Sensors }
Simple Feedback
Pneumatics
Autonomous
leftPlow->Set(Relay::kOff);
Contact Information
rightPlow->Set(Relay::kOff);
return true;
}

Philipp Schrader & Tom Brown - October 27, 2012


Autonomous Cont'd
Introduction
Programming in FRC
void Autonomous(void) {
C++ Overview
Why C++ in FRC?
int state = 1;
First Steps
What is programming?
Source Control while (IsAutonomous()) {
Windriver
WPILib switch (state) {
Examples case 1:
Simple Robot
Drive with Joystick
Sensors
if (SetPlow(Relay::kForward)) {
Simple Feedback
Pneumatics
state = 2;
Autonomous
}
Contact Information
break;
...

Philipp Schrader & Tom Brown - October 27, 2012


Autonomous Cont'd
Introduction
Programming in FRC
C++ Overview
Why C++ in FRC?
...
First Steps case 2:
What is programming?
Source Control if (SetPlow(Relay::kReverse)) {
Windriver
WPILib case = 1;
Examples }
Simple Robot
Drive with Joystick
Sensors
break;
Simple Feedback
Pneumatics
}
Autonomous
}
Contact Information
}

Philipp Schrader & Tom Brown - October 27, 2012


Contact Information
Introduction
Programming in FRC Philipp Schrader
C++ Overview
Why C++ in FRC? Firmware Developer, Nuvation Research
First Steps
phil@spartonics.org
What is programming?
Source Control
Windriver
WPILib Tom Brown
Examples Senior Software Developer, F.A. Servers
Simple Robot
Drive with Joystick
tom.brown@spartonics.org
Sensors
Simple Feedback
Pneumatics
Autonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

You might also like