Jon Cardwell Red Alert Robotics Team 1741 October 24, 2015.
Transcript of 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
Agenda
C++ Overview C++ for FRC
http://wpilib.screenstepslive.com/s/4485Resourceshttp://
www.usfirst.org/roboticsprograms/frc/technical-resources
C++ OVERVIEW
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/…).
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…
Simplest C++ Program
int main(){return 0;
}
First Real Program (code)
# include <iostream>using namespace std;
int main(){cout << “Hello Robot!” << endl;return 0;
}
First Real Program (output)
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;…
}
C++ Assignment
gear_teeth = 32;
gear_ratio = teeth1 / teeth2;
num_rings += 4;
should_deploy = false;
count++;
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”;
}
Tougher Decisions
if (trigger == 1 && target == true){cout << “FIRE!”;
}
if (fwd_limit == true || bkwd_limit == true)
{cout << “Stop the motor!”;
}
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)
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) );
}
Classes
Kicker
Drivetrain
Arm
C++ FOR FRC
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
FRC C++ Architecture
Core C++ LanguageC/C++ Standard LibraryWPILib (FIRST provided library)Individual team’s code
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
So How Do I Use the Joystick? WPILib of course
Class dedicated to the Joystick
FunctionsGetX()GetY()GetZ()GetTrigger()Many, many more
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}
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
}
RESOURCES
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)
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
Additional Questions
Email us: Hugh Meyer [email protected] Jon Cardwell [email protected]