Welcome to Level 2! In this level, you'll create a ROS package to control a virtual rover using a simple publisher-subscriber communication pattern.
This will help you understand the core communication mechanisms in ROS, which are essential for building modular and scalable robotic applications.
- Create a package inside the Docker image
- Create a simple publisher-subscriber system
Imagine you are tasked with developing the command system for our rover.
Your job is to create a control interface that sends movement commands to the rover, and a system that interprets these commands to move the rover accordingly.
You will create a publisher that sends Twist messages (a translation and rotation vector, see doc) based on user input, and a subscriber that receives these messages and interprets them as movement commands.
The publisher will send a Twist message, which the subscriber will interpret as a trajectory command. The user will input letters to indicate the trajectory to send:
w: Move Forward (+x)a: Slide Left (-z)s: Move Backward (-x)d: Slide Right (+z)t: Rotate Left (+ry)y: Rotate Right (-ry)
The subscriber will receive the data and transform it into a coherent string, filtering out impossible commands:
Tx axis: “Go [Forward / Backward]”Tx axisandRy axis: “Go [Left / Right]”Tz axis: “Slide [Left / Right]”Ry axis: “Rotating on itself to the [Left / Right]”Tz axisandRy axis,Tx axisandTz axis: “Forbidden move”
It will also print the position: “New Position: [x, z, orientation (Ry)]”, where translation is updated after rotation if both are present.
-
Open the Docker container:
If the Docker container is not already running, start it using the
run.shscript (for Mac or Linux) orrun.bat(for Windows):./run.sh (Mac or Linux) run.bat (Windows)
-
Create a new ROS package:
Inside the Docker container, create a new workspace and a package:
cd src ros2 pkg create --build-type ament_cmake rover_commandsThis command creates a new ROS package named
rover_commandsusing C++. The--build-type ament_cmakespecifies that we are using CMake (a C++ build tool) for this package. -
Navigate to the package directory:
cd rover_commands
-
Open a Text Editor/IDE:
Open the assignment in your favorite text editor or IDE.
We recommend VS Code for its ease of use and installation.
Once this is done, you can edit the files directly on your computer, they will also be updated in real-time in Docker!
You will see that in the assignment a new folder appeared. It's your package.
It should have the following content:
rover_commands/ ├── CMakeLists.txt ├── include │ └── rover_commands ├── package.xml └── src -
Create the publisher script:
Inside the folder
rover_commandsof the package, there is asrcfolder.We will place all the source code of our package in this
srcfolder. Create a file namedpublisher.cppin your editor.Check that it also appears in Docker using:
ls ~/dev_ws/src/rover_commands/src -
Edit the publisher script:
Open
publisher.cppwith a text editor and add the following code:#include <iostream> #include <memory> #include <string> #include "rclcpp/rclcpp.hpp" class TrajectoryPublisher : public rclcpp::Node { public: TrajectoryPublisher() : Node("trajectory_publisher") { // TODO: Create a publisher of type Twist // Your code here... RCLCPP_INFO(this->get_logger(), "Publisher node has been started"); // TODO: Create a loop here to ask users a prompt and send messages accordingly // Your code here... } private: void cmd_acquisition() { std::string command; std::cout << "Enter command (w/a/s/d/t/y): "; std::cin >> command; // TODO: Complete the function to transform the input into the right command. // Your code here... } private: // TODO: Add private members here (publisher instance?) }; int main(int argc, char* argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<TrajectoryPublisher>()); rclcpp::shutdown(); return 0; }
This code defines a
TrajectoryPublishernode that waits for user input and publishes aTwistmessage based on the command. Thecmd_acquisitionfunction is called indefinitely to prompt for user input.Now your turn to complete it! Use online resources such as ROS doc.
Important
At multiple points in the C++ ROS docs, they use the following syntax: std::bind(&MinimalSubscriber::topic_callback, this, _1)
I would much rather use [this](const std_msgs::msg::String::SharedPtr msg) { this->topic_callback(); }.
For more information, you can look at W3Schools C++ Lambda Documentation.
-
Update the
package.xml: In therover_commandsdirectory, open thepackage.xmlfile and add the following after the<builtdtool_depend>ament_cmake</builtdtool_depend>:<depend>rclcpp</depend> <depend>geometry_msgs</depend>
This tells ROS to add
rclcppandgeometry_msgsas dependencies for your script. -
Update the
CMakeLists.txt:In the
rover_commandsdirectory, open theCMakeLists.txtfile and add the following after thefind_package(ament_cmake REQUIRED):find_package(rclcpp REQUIRED) find_package(geometry_msgs REQUIRED)
Then, add the executable, and name it
publisher:add_executable(publisher src/publisher.cpp) ament_target_dependencies(publisher rclcpp geometry_msgs)
Finally, add the
installdirective so thatros2 runwill be able to find our executable:install(TARGETS talker DESTINATION lib/${PROJECT_NAME})
-
Create the subscriber script:
Again, inside the
rover_commands/srcfolder of the package, create a file namedsubscriber.cppin your editor.Check that it also appears in Docker using:
ls ~/dev_ws/src/rover_commands/src -
Edit the subscriber script:
Open
subscriber.cppwith a text editor and add the following code:#include <memory> #include "rclcpp/rclcpp.hpp" #include "geometry_msgs/msg/twist.hpp" class TrajectorySubscriber : public rclcpp::Node { public: TrajectorySubscriber() : Node("trajectory_subscriber"), x(0.0), z(0.0), ry(0.0) { // TODO: Create a subscriber of type Twist, that calls listener_callback // Your code here... RCLCPP_INFO(this->get_logger(), "Subscriber node has been started."); } private: void listener_callback(const geometry_msgs::msg::Twist& msg) { // TODO: Interpret the received commands and log the result using RCLCPP_INFO // Your code here... RCLCPP_INFO( this->get_logger(), "New Position: { x: %.2f, z: %.2f, ry: %.2f }", this->x, this->z, this->ry ); } private: // TODO: Add private members here (subscriber instance?) float x; float z; float ry; }; int main(int argc, char* argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<TrajectorySubscriber>()); rclcpp::shutdown(); return 0; }
This script defines a
TrajectorySubscribernode that listens forTwistmessages on thetrajectorytopic. It interprets the commands and prints the corresponding action and updated position.Now your turn to complete it! Use online resources such as ROS doc.
-
Update the
CMakeLists.txt:Reopen `CMakeLists.txt`` and add the executable and target for the subscriber node below the publisher’s entries:
add_executable(subscriber src/subscriber.cpp) ament_target_dependencies(subscriber rclcpp geometry_msgs) install(TARGETS publisher subscriber DESTINATION lib/${PROJECT_NAME})
-
Build the package:
In the
dev_wsdirectory, build the package:cd ~/dev_ws/ colcon build
This command compiles the package and sets up the necessary environment.
-
Source the setup file:
After building, source the setup file to overlay the workspace on your environment:
. install/setup.bashThis command sets up the environment variables needed to run the nodes. Now, your terminal is aware of the existing nodes. You will have to execute this command on every terminal you open to help it find your nodes.
-
Run the publisher node:
In one terminal inside the Docker container, run the publisher node:
ros2 run rover_commands publisher
This starts the
TrajectoryPublishernode, which waits for user input and publishes the correspondingTwistmessage. -
Run the subscriber node:
Open a new terminal inside the Docker container and run the subscriber node:
docker exec -it base_humble_desktop bash . install/setup.bash ros2 run rover_commands subscriber
This starts the
TrajectorySubscribernode, which listens forTwistmessages on thetrajectorytopic and prints the interpreted commands and updated position.
-
Start the Publisher Node:
In the first terminal, you will be prompted to enter commands (e.g.,
w,a,s,d,t,y). -
Monitor the Subscriber Node:
In the second terminal, you will see the subscriber node interpreting the commands and printing the corresponding actions and updated position.
Example Output:
Enter command (w/a/s/d/t/y): w [INFO] [publisher]: Published: linear: x: 1.0 y: 0.0 z: 0.0 angular: x: 0.0 y: 0.0 z: 0.0 --- [INFO] [subscriber]: Go Forward [INFO] [subscriber]: New Position: {'x': 1.0, 'z': 0.0, 'ry': 0.0}
By completing these steps, you have created a ROS package with a simple publisher-subscriber system to control the rover. The publisher sends trajectory commands, and the subscriber interprets and prints the trajectory.
Congratulations on completing Level 2! You are now ready to move on to more complex interactions in Level 3.
