using c++ programming a robot

31
Programming a Robot Using C++ Philipp Schrader & Tom Brown October 27, 2012

Upload: others

Post on 12-Sep-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using C++ Programming a Robot

Programming a RobotUsing C++

Philipp Schrader & Tom BrownOctober 27, 2012

Page 2: Using C++ Programming a Robot

Programming in FRC

The robot has mechanical systems and electrical hardware, but needs a program to tell it what to do

The program collects inputs from the drivers and sensors, and uses them to decide what motor output should be

Different programming “languages”LabVIEW, C++, Java

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 3: Using C++ Programming a Robot

C++ Overview

Invented in the 1980’s

Built as an extension on top of C Object-oriented programming language

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 4: Using C++ Programming a Robot

Why C++ in FRC?

Powerful and fast language; used widely in industry

Steep learning curve, but after that development is fast

Programming tools are less complicated, smaller, faster than LabVIEW

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 5: Using C++ Programming a Robot

What is programming?

Writing instructions for machines to perform a set of desired tasks

It's about controlling how inputs to a system affect the outputs of the system.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 6: Using C++ Programming a Robot

Source Control

Centralizing where your source code is stored.

Acts as a backup storage, history browser, and versioning for the code.

Easily shows differences between two versions of the code. This includes the "current working version" with any modifications.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 7: Using C++ Programming a Robot

Wind River

The Windows program used to compile code into programs for the robot.

Primarily serves as a development environment for writing and debugging code.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 8: Using C++ Programming a Robot

WPILib

Already-written code provided by FIRST to make robot programming easier

Consists of classes that represent all common robot hardware

Example: Compressor, DigitalInput, Victor, DriverStation, Solenoid, Gyro

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 9: Using C++ Programming a Robot

Simple Robot

SimpleRobot is a starting template to help teams quickly get a robot running.

There are other templates such as IterativeRobot, but we will focus on SimpleRobot.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 10: Using C++ Programming a Robot

Simple Robot Cont'd

#include "WPILib.h"

class RobotDemo : public SimpleRobot

{

public:

RobotDemo(void) { }

void Autonomous(void) { }

void OperatorControl(void) { }

};

START_ROBOT_CLASS(RobotDemo);

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 11: Using C++ Programming a Robot

Simple Robot Cont'd

RobotDemo() is the constructor. This is where you initialize all members of the class.

Autonomous() is the function that gets called during the autonomous or hybrid period of a match.

OperatorControl() is the function that gets called during teleop. Function gets called once and needs a loop to continue operator control.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 12: Using C++ Programming a Robot

Drive with Joystick

// Declare drive components

Joystick *stick;

Victor *leftDrive;

Victor *rightDrive;

// Initialize components with port numbers

RobotDemo(void) {

stick = new Joystick(1);

leftDrive = new Victor(1);

rightDrive = new Victor(2);

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 13: Using C++ Programming a Robot

Drive with Joystick Cont'd

void OperatorControl(void) {

while (IsOperatorControl()) {

float leftY = stick->GetRawAxis(2);

float rightY = stick->GetRawAxis(4);

// Tank drive

leftDrive->SetSpeed(-leftY);

rightDrive->SetSpeed(rightY);

Wait(0.005);

}

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 14: Using C++ Programming a Robot

Sensors

Sensors help the operators with controlling the robot.

They are crucial in autonomous, but also very valuable for teleop.

For example, appropriate sensors can be used to precisely position an arm on the robot.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 15: Using C++ Programming a Robot

Sensors Cont'd

AnalogChannel *plowPot;

RobotDemo(void) {

plowPot = new AnalogChannel(2);

}

void OperatorControl(void) {

while (IsOperatorControl()) {

printf("%d", plowPot->GetValue());

Wait(0.005);

}

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 16: Using C++ Programming a Robot

Sensors Cont'd

In our example, we are getting a feeling for how the potentiometer behaves.

Some sensors have their own classes in WPILib. Use them whenever possible.

Base Classes:AnalogChannel, DigitalInput

Examples:Encoder, Gyro, UltraSonic

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 17: Using C++ Programming a Robot

Simple Feedback

We can use sensors to perform more sophisticated maneuvers than a human alone could do.

For example, we can move an arm into the best scoring position consistently on the push of a button.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 18: Using C++ Programming a Robot

Simple Feedback Cont'd

Relay *leftPlow;

Relay *rightPlow;

RobotDemo(void) {

leftPlow = new Relay(1);

rightPlow = new Relay(2);

}

void SetPlow(Relay::Value direction) {

leftPlow->Set(direction);

rightPlow->Set(direction);

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 19: Using C++ Programming a Robot

Simple Feedback Cont'd

void OperatorControl(void) {

while (IsOperatorControl()) {

if (stick->GetRawButton(2)) {

SetPlow(Relay::kForward);

}

else if (stick->GetRawButton(3)) {

SetPlow(Relay::kReverse);

}

else {

SetPlow(Relay::kOff);

}

}

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 20: Using C++ Programming a Robot

Simple Feedback Cont'd

The previous slide shows a simple plow control without sensors. Button 2 lowers the plow. Button 3 raises the plow.

If we add sensor feedback to the code, we can prevent the plow from going past mechanical limits.

The next slide will incorporate feedback to the plow to prevent breaking itself.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 21: Using C++ Programming a Robot

Simple Feedback Cont'd

// Prevents the plow from going too far

if (stick->GetRawButton(2) &&

(plowPot->GetValue() > 60)) {

SetPlow(Relay::kForward);

}

else if (stick->GetRawButton(3) &&

(plowPot->GetValue() < 250)) {

SetPlow(Relay::kReverse);

}

else {

SetPlow(Relay::kOff);

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 22: Using C++ Programming a Robot

Pneumatics

According to the pneumatics setup and electrical rules by FIRST, pneumatics have special programming requirements.

Solenoids and compressors have their own class in WPILib.

Even if the compressor is not on the robot, it needs to be controlled by the robot itself.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 23: Using C++ Programming a Robot

Pneumatics Cont'd

Solenoid *openGate;

Solenoid *closeGate;

Compressor *compressor;

RobotDemo(void) {

openGate = new Solenoid(1);

closeGate = new Solenoid(2);

compressor = new Compressor (9, 5);

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 24: Using C++ Programming a Robot

Pneumatics Cont'dvoid OperatorControl(void) {

openGate->Set(false);

closeGate->Set(true);

while (IsOperatorControl()) {

if (stick->GetRawButton(1)) {

openGate->Set(true);

closeGate->Set(false);

}

else if (stick->GetRawButton(4)) {

openGate->Set(false);

closeGate->Set(true);

}

...

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 25: Using C++ Programming a Robot

Pneumatics Cont'd

...

// Only runs compressor until 120 psi

if (!compressor->GetPressureSwitchValue()) {

compressor->Start();

}

else {

compressor->Stop();

}

Wait(0.005);

}

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 26: Using C++ Programming a Robot

Autonomous

The autonomous code works similarly to the teleop code.

void Autonomous(void) {

while (IsAutonomous()) {

// Your code here

...

}

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 27: Using C++ Programming a Robot

Autonomous Cont'd

For this example we will make the robot lower and raise its plow until the end of the autonomous period.

We will make use of a simple state machine that cycles between a state that raises the plow and a state that lowers the plow. We shall modify our SetPlow function as well.

Switching between the states is done through the potentiometer.

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 28: Using C++ Programming a Robot

Autonomous Cont'dbool SetPlow(Relay::Value direction) {

if ((direction == Relay::kForward &&

plowPot->GetValue() > 60) ||

(direction == Relay::kReverse &&

plowPot->GetValue() < 260)) {

leftPlow->Set(direction);

rightPlow->Set(direction);

return false;

}

leftPlow->Set(Relay::kOff);

rightPlow->Set(Relay::kOff);

return true;

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 29: Using C++ Programming a Robot

Autonomous Cont'd

void Autonomous(void) {

int state = 1;

while (IsAutonomous()) {

switch (state) {

case 1:

if (SetPlow(Relay::kForward)) {

state = 2;

}

break;

...

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 30: Using C++ Programming a Robot

Autonomous Cont'd

...

case 2:

if (SetPlow(Relay::kReverse)) {

case = 1;

}

break;

}

}

}

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012

Page 31: Using C++ Programming a Robot

Contact Information

Philipp SchraderFirmware Developer, Nuvation [email protected]

Tom BrownSenior Software Developer, F.A. [email protected]

IntroductionProgramming in FRCC++ OverviewWhy C++ in FRC?

First StepsWhat is programming?Source ControlWindriverWPILib

ExamplesSimple RobotDrive with JoystickSensorsSimple FeedbackPneumaticsAutonomous

Contact Information

Philipp Schrader & Tom Brown - October 27, 2012