Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ROS 2 from Scratch

You're reading from   ROS 2 from Scratch Get started with ROS 2 and create robotics applications with Python and C++

Arrow left icon
Product type Paperback
Published in Nov 2024
Publisher Packt
ISBN-13 9781835881408
Length 380 pages
Edition 1st Edition
Concepts
Arrow right icon
Author (1):
Arrow left icon
Edouard Renard Edouard Renard
Author Profile Icon Edouard Renard
Edouard Renard
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Part 1:Getting Started with ROS 2
2. Chapter 1: Introduction to ROS 2 – What Is ROS 2? FREE CHAPTER 3. Chapter 2: Installing and Setting Up ROS 2 4. Chapter 3: Uncovering ROS 2 Core Concepts 5. Part 2: Developing with ROS 2 – Python and C++
6. Chapter 4: Writing and Building a ROS 2 Node 7. Chapter 5: Topics – Sending and Receiving Messages between Nodes 8. Chapter 6: Services – Client/Server Interaction between Nodes 9. Chapter 7: Actions – When Services Are Not Enough 10. Chapter 8: Parameters – Making Nodes More Dynamic 11. Chapter 9: Launch Files – Starting All Your Nodes at Once 12. Part 3: Creating and Simulating a Custom Robot with ROS 2
13. Chapter 10: Discovering TFs with RViz 14. Chapter 11: Creating a URDF for a Robot 15. Chapter 12: Publishing TFs and Packaging the URDF 16. Chapter 13: Simulating a Robot in Gazebo 17. Chapter 14: Going Further – What To Do Next 18. Index 19. Other Books You May Enjoy

Creating a C++ node

We are going to do exactly the same thing we did for the Python node: create a file, write the node, build, source, and run.

Make sure you have read the previous Python section as I will not repeat everything here. We will basically just see how to apply the process for a C++ node.

To create a C++ node, we first need a C++ package. We will use the my_cpp_pkg package that we created previously.

Writing a C++ node

Let’s create a file for the node. Go to the src directory inside the my_cpp_pkg package and create a .cpp file:

$ cd ~/ros2_ws/src/my_cpp_pkg/src/
$ touch my_first_node.cpp

You could also create the file directly from your IDE and not use the terminal.

Now, if you haven’t done this previously, open your workspace with VS Code or any other IDE:

$ cd ~/ros2_ws/src/
$ code .

Open my_first_node.cpp. Here is the minimal code to write a C++ node:

#include "rclcpp/rclcpp.hpp"
class MyCustomNode : public rclcpp...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image