0% found this document useful (0 votes)
129 views25 pages

C++ Crash Course For ROS

This document provides a crash course on C++ and building C++ packages in ROS. It begins with an overview of C++ concepts like classes, vectors, namespaces, pointers, references and more. It then demonstrates how to create a ROS package in C++ called cpp_tutorial that subscribes to random number messages and prints them out. It walks through creating the package, editing code to subscribe and print messages, building the package, and iterating on the code to compute an average of the random numbers.

Uploaded by

zakizadeh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views25 pages

C++ Crash Course For ROS

This document provides a crash course on C++ and building C++ packages in ROS. It begins with an overview of C++ concepts like classes, vectors, namespaces, pointers, references and more. It then demonstrates how to create a ROS package in C++ called cpp_tutorial that subscribes to random number messages and prints them out. It walks through creating the package, editing code to subscribe and print messages, building the package, and iterating on the code to compute an average of the random numbers.

Uploaded by

zakizadeh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

C++ crash course

Justin Huang
Jan 12, 2015
Why C++?
Fast
Compile-time checks
Slight improvement over C
Widely used

Why not?
Complicated
Slow compilation
Hello, world
# include < iostream > public class M ain {
public static void m ain(String[] args) {
int m ain(int argc, char** argv) { System .out.println("H ello, w orld!");
std::cout < < "H ello, w orld!" < < std::endl; }
return 0; }
}
Classes (Java)
public class Robot { Robot r = new Robot("Rosie");
private String nam e;

public Robot(String nam e) {


this.nam e = nam e;
}

public void sayN am e() {


System .out.println(nam e);
}
}
Classes (C++)
// robot.h // robot.cpp
# ifndef RO BO T_H _ # include < iostream >
# define RO BO T_H _ # include < string>
# include < string> # include "robot.h"

class Robot { Robot::Robot(): nam e_("") {


private: }
std::string nam e_; Robot::Robot(const std::string& nam e):
public: nam e_(nam e) {
Robot(); }
Robot(const std::string& nam e);
void SayN am e() const; void Robot::SayN am e() const {
}; std::cout < < nam e_ < < std::endl;
# endif }
Classes (C++)
// m ain.cpp

# include < iostream >


# include "robot.h"

int m ain() {
Robot r("Rosie");
r.SayN am e();
Robot r2;
r2.SayN am e();
Robot* r3 = new Robot("Rosie");
r3-> SayN am e();
return 0;
}
Vectors
# include < iostream > for(const Robot& robot : robots) {
# include < vector> robot.SayN am e();
# include "robot.h" }

int m ain() { std::cout < < "There are " < < robots.size()
Robot r("Rosie"); < < " robots" < < std::endl;
Robot r2("M osie"); Robot rosie = robots[0];
return 0;
std::vector< Robot> robots; }
robots.push_back(r);
robots.push_back(*r2);

Continued
Hash tables
# include < iostream > robots["Rosie"].SayN am e();
# include < unordered_m ap> return 0;
}
int m ain() {
Robot r("Rosie");
Robot r2("M osie");

std::unordered_m ap< std::string, Robot>


robots;
robots["Rosie"] = r;
robots["M osie"] = r2;

Continued
Namespaces
// robot.h // m ain.cpp
# ifndef RO BO T_H _
# define RO BO T_H _ nam espace m yproject {
void inside() {
nam espace m yproject { Robot r;
class Robot { r.SayN am e();
... }
}; };
};
int m ain() {
# endif m yproject::Robot r;
r.SayN am e();
m yproject::inside();
}
using-directive
# include < iostream > # include < iostream >
# include < string> # include < string>

using std::string; using nam espace std;

int m ain() { int m ain() {


string nam e = "Rosie"; string nam e = "Rosie";
std::cout < < nam e < < std::endl; cout < < nam e < < endl;
return 0; return 0;
} }
Transitivity of inclusion
// robot.h // m ain.cpp
# ifndef RO BO T_H _
# define RO BO T_H _ # include "robot.h"

# include < iostream > int m ain() {


using nam espace std; cout < < "H ello w orld" < < endl;
}
# endif
Pointers
Code,
0x00000000
int test = 5555; data,
Robot* r = new Robot("Rosie"); BSS
0x0000001
cout < < & test < < endl;
cout < < r < < endl; Heap
0x00001234 Robot {
name = "Rosie"
Robot r; // A Robot object }
Robot* pr; // A pointer to a robot

&r; // The address of r


pr = r; // pr points ro r
*pr; // A Robot, r
0xfffffffe
pr->SayName();
(*pr).SayName(); 0xffffffff Stack
Pass by reference
void D oSom ething( Robot robot1("M osie");
Robot& ref, Robot robot2("M osie");
Robot value, Robot robot3("M osie");
Robot* pointer
){ D oSom ething(robot1, robot2, robot3);
ref.nam e = "Rosie"; robot1.nam e = = "Rosie";
value.nam e = "Rosie"; robot2.nam e = = "M osie";
pointer-> nam e = "Rosie"; robot3.nam e = = "Rosie";
}
Function convention
void D oSom ething( Inputs first, then outputs.
const Robot& input1, Inputs are const references except for
const int input2,
Robot* output1,
primitive types.
int* output2 Outputs are pointers.
){ Function return type is void.
output1-> nam e = input1.nam e;
*output2 = input2;
} Robot r("M osie");
int num 1 = 1;
Robot r2;
int num 2;

D oSom ething(r, num 1, & r2, & num 2);


Further reading
C++ tutorial
Learn C the Hard Way
ROS C++ style guide
Google C++ style guide
Building a C++ package in
ROS
Setup for tutorial
ssh hcrlab@ w alle.cs.w ashington.edu
m kdir {U W N etID }
cd {U W N etID }
m kdir -p catkin_w s/src
cd catkin_w s
catkin_m ake
source devel/setup.bash
cd src
Create the package
catkin_create_pkg cpp_tutorialroscpp std_m sgs
cd cpp_tutorial
ls
Look at random numbers
rostopic info random_numbers
rosmsg show std_msgs/Float64
rostopic echo random_numbers
Get the sample code
Sample code is from the ROS tutorial,
Writing a simple publisher and subscriber (C++)

cp ~/samples/subscriber.cpp src
vim src/subscriber.cpp
Or nano src/subscriber.cpp
Or emacs src/subscriber.cpp
Tasks
1. Change the node's name to be your UW NetID
2. Subscribe to "random_numbers" instead of "chatter"
3. Add #include "std_msgs/Float64.h"
4. Change chatterCallback to be:
void chatterCallback(const std_m sgs::Float64& m sg)
5. Change ROS_INFO to say:
RO S_IN FO ("Iheard: % f", m sg.data);
CMakeLists.txt
vim/nano/emacs CMakeLists.txt
Go to near the bottom, where it says
###########
# # BU ILD # #
###########

Uncomment include_directories(...), add_executable(...),


and target_link_libraries(...)

Change src/cpp_tutorial_node.cpp to src/subscriber.cpp


Build
cd ../.. # You should be in ~/uwnetid/catkin_ws
catkin_make
rosrun cpp_tutorial cpp_tutorial_node
Iterate
Now go back and compute the average of the
random numbers.

You will need global variables to keep track of


the average -- yuck!
Further reading
PCL Sample: a basic example of a C++ class,
used to process the average point in a point
cloud
ROS tutorials (C++)
ROS C++ Overview
ROS C++ Code API (Hydro)

You might also like