Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

26
PROGRAMMING FOR FRC IN C++ Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015

Transcript of Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Page 1: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

PROGRAMMING FOR FRC IN C++

Jon Cardwell

Red Alert Robotics Team 1741

October 24, 2015

Page 3: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

C++ OVERVIEW

Page 4: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Why use C++?

C++ is powerful and flexible C++ is object oriented (classes) All source code is plain text Programming only requires a text editor

and a compilerFor example: vi/notepad++ and g++/gccWindRiver IDE is the best for FRC.Use Source-Code revision/repository

(SVN/Git/…).

Page 5: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

C++ Basics

The main() functionStart and end for every program

Basic Data TypesIntegers, float, doubleReal numbersCharacters 1,2,3…

2.71828, 3.14159…a,B,c…

Page 6: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Simplest C++ Program

int main(){return 0;

}

Page 7: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

First Real Program (code)

# include <iostream>using namespace std;

int main(){cout << “Hello Robot!” << endl;return 0;

}

Page 8: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

First Real Program (output)

Page 9: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

C++ Datatypes

int main(){// 32-bit integer

int gear_teeth=16;// 32-bit real number float sensor_volts=11.6;// true/falsebool target_acquired=true;…

}

Page 10: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

C++ Assignment

gear_teeth = 32;

gear_ratio = teeth1 / teeth2;

num_rings += 4;

should_deploy = false;

count++;

Page 11: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

C++ Decisionsif (count == 13) {

cout << “Count is 13”;}

if (count == 14) {cout << “Count is 14”;

} else {cout << “Count is NOT 14”;

}

if (current > 90) {cout << “Magic smoke may appear”;

} else if (current > 60) {cout << “Motors are getting hot”;

} else {cout << “Working as expected”;

}

Page 12: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Tougher Decisions

if (trigger == 1 && target == true){cout << “FIRE!”;

}

if (fwd_limit == true || bkwd_limit == true)

{cout << “Stop the motor!”;

}

Page 13: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Relational OperatorsOperator Meaning

== Equals (not the same as =)

> Greater than

>= Greater than or equal

< Less than

<= Less than or equal

!= Not equal

Operator Meaning

&& and (both must be true)

|| or (either must be true)

Page 14: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Functions

void drive_fwd() {drive_motor = 0.8;

}

float get_drive_cmd() {return joystick.GetY();

}

float get_distance(float x, float y) {return sqrt( pow(targetX-x,2) + pow(targetY-y,2) );

}

Page 15: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Classes

Kicker

Drivetrain

Arm

Page 16: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

C++ FOR FRC

Page 17: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

FRC Programming Process

Drive the robot

Determine something

needs to change

Change the C++ source

code

Compile the C++ source

code

Use the FIRST undeploy option

to remove the old code

Use the FIRST download option to download the

new code

Restart the robot

Page 18: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

FRC C++ Architecture

Core C++ LanguageC/C++ Standard LibraryWPILib (FIRST provided library)Individual team’s code

Page 19: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

WPI Lib

WPI = Worchester Polytechnic Institute WPILib = Helpful library that makes FRC

programming much easier WPILib

Fully open and documentedRich set of classes to represent control of

the robot

Page 20: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

So How Do I Use the Joystick? WPILib of course

Class dedicated to the Joystick

FunctionsGetX()GetY()GetZ()GetTrigger()Many, many more

Page 21: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Robot Class Layout

Initialization Routinesvoid RobotInit(void) {

//Runs once and only once when the robot is initially turned on

}void DisabledInit(void {

//Runs when the robot enters disabled mode}void AutonomousInit(void) {

//Runs when the robot enters autonomous mode}void TeleopInit(void) {

//Runs when the robot enters teleoperated mode}

Page 22: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Robot Class Layout

Periodic Routinesvoid DisabledPeriodic(void {

//Runs in a loop while in disabled mode}

void AutonomousPeriodic(void) {//Runs in a loop when in autonomous mode

}

void TeleopPeriodic(void) {//Runs in a loop when in teleoperated mode

}

Page 23: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

RESOURCES

Page 24: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

WPILib Documentation

http://first.wpi.edu/FRC/frcupdates.html

<WindRiver install dir>\docs\extensions\FRC\Starting a new robot project

○ GettingStartedWithC.pdfWPI Overview

○ WPI Robotics Library User’s Guide.pdfClass Documentation and Functions

○ WPILib C++ Reference.chm (Bible for FRC)

Page 25: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Teaching New Students C++ “C++ for Everyone”, by Cay Horstmann,

2nd Ed, 2010, Wiley. ISBN 978-0-470-92713-7.

C++ Tutorial: http://www.tutorialspoint.com/cplusplus/index.htm

http://www.learncpp.com

Page 26: Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.

Additional Questions

Email us: Hugh Meyer [email protected] Jon Cardwell [email protected]