In this post, you’ll learn how to launch RViz using a configuration file. You will learn how to close and open RViz as many times you need without having to configure or customize it all over again.
Step 1: Fire up a system with ROS installation
“Hey, do you mean I have to install ROS first?” Absolutely not! Just log in to The Construct to get access to virtual machines pre-installed with ROS.
Once logged in, click here to copy the ROS project (rosject) we’ll use for this post. Once copied, click the Run button to open the rosject.
You might also want to try this on a local PC if you have ROS installed. However, please note that we don’t support local PCs and you will have to fix any errors you run into on your own. The rest of the instruction assumes that you are working on The Construct; please adapt them to your local PC and ROS installation.
PS: we are using ROS Kinetic. You should be able to replicate the same on any ROS 1 distro, with minor changes.
Step 2: Run RViz without loading a configuration file
Open the Code Editor and create a new file rviz_plain.launch in the location shown in the image below:
Next, launch the rviz_plain.launch file. Open a Web Shell and type the command that follows.
roslaunch my_robot_description rviz_plain.launch
Now you should see the rviz window, with no display added. We would need to add the display one by one and then configure them. But this is not necessary, since we have a configuration file saved already. Let’s load it next.
Step 3: Run RViz in a launch file and pass the configuration
Stop the current program in the web shell using Ctrl + C and run the following command instead.
roslaunch my_robot_description rviz.launch
Now you should see a different rviz screen, generated from the configuration!
Can you spot some differences in the two launch files? Here is the rviz.launch file.
Do you now understand how to launch rviz using a configuration file? If not, please go over the post again, more carefully this time.
You can create a configuration file by using the File -> Save Config As... command on the rviz window. Use File -> Save Config to update the current config.
Step 5 (optional): watch the video
Feedback
Did you like this video? Whatever the case, please leave a comment in the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS topics, please let us know on the comments area, and we will do a video about it.
In this post, you will learn how to publish and subscribe to a topic from a launch file. This post is a response to this question posted on Gazebo Answers.
Step 1: Fire up a system with ROS installation
“Hey, do you mean I have to install ROS first?” Absolutely not! Just log in to The Construct to get access to virtual machines pre-installed with ROS.
Once logged in, click on My Rosjects, then Create a New Rosject, supply the information as shown in the image below, and click Create. Then RUN the rosject.
You might also want to try this on a local PC if you have ROS installed. However, please note that we don’t support local PCs and you will have to fix any errors you run into on your own. The rest of the instruction assumes that you are working on The Construct; please adapt them to your local PC and ROS installation.
PS: we are using ROS Noetic. You should be able to replicate the same on any ROS 1 distro.
Step 2: Create a package to demonstrate how to publish and subscribe to a topic from a launch file
Open a web shell and run the following commands to create the package.
cd catkin_ws/src
source /opt/ros/noetic/setup.bash
catkin_create_pkg test_rostopic
Next, we create the launch directory and the launch files.
cd test_rostopic
mkdir launch
cd launch
touch test_pub.launch
touch test_echo.launch
Nice done! Now head over to the Code Editor to make changes to the launch files. Check the image below for how to open the Code Editor.
Locate the launch folder in the code editor: catkin_ws > src > test_rostopic > launch and open the two launch files.
Paste the following lines into the test_pub.launch file. This is equivalent to rostopic pub /test_topic std_msgs/String "data: 'Hello there!'"
cd ~/catkin_ws
catkin_make
source devel/setup.bash
Success! We’ll test it in the next step.
PS: if your code did not compile correctly, please go over the instructions and ensure you have created the files in the exact locations specified.
Step 3: Test the package to demonstrate how to publish and subscribe to a topic from a launch file
Start the test_pub.launch file in the current terminal.
roslaunch test_rostopic test_pub.launch
When done, open another terminal and run the test_echo.launch file.
roslaunch test_rostopic test_echo.launch
Now you should see something similar to the following on the terminal where test_echo.launch is running:
process[rostopic_echo_node-1]: started with pid [xxxx]
data: "Hello there!"
---
Sweet! So we are able to publish with one launch file and echo what we publish in another launch file. And…we are done here, congrats!
Take home: try combining both into a single launch file. After trying, check the solution in this gist.
Step 4: Check your learning
Do you understand how to publish and subscribe to a topic from a launch file? If you don’t know it yet, please go over the post again, more carefully this time.
(Extra) Step 5: Watch the video to understand how to publish and subscribe to a topic from a launch file
Here you go:
Feedback
Did you like this post? Do you have any questions about how to publish and subscribe to a topic from a launch file? Please leave a comment in the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS topics, please let us know in the comments area and we will do a video or post about it.
“Hey, do you mean I have to install ROS first?” Absolutely not! Just log in to The Construct to get access to virtual machines pre-installed with ROS.
Once logged in, click on My Rosjects, then Create a New Rosject, supply the information as shown in the image below, and click Create. Then RUN the rosject.
You might also want to try this on a local PC if you have ROS installed. However, please note that we don’t support local PCs and you will have to fix any errors you run into on your own. The rest of the instruction assumes that you are working on The Construct; please adapt them to your local PC and ROS installation.
PS:
We are using ROS Kinetic here because that’s what the user on ROS Answers used.
We could put the publisher and the subscriber in a single package, but we’ll create two packages as the user did.
We could put the custom message in any of the two packages, but we’ll put it in the subscriber, just like the user.
Step 2: Create the first package that contains the subscriber and the custom message
Create the package and custom message
Open a web shell and run the following commands to create the package. When creating a custom message, you need extra dependencies message_generation and message_runtime, in addition to the main ROS API, rospy (for Python) in this case, and the types needed for the message, in this case std_msgs.
cd catkin_ws/src
source /opt/ros/kinetic/setup.bash
catkin_create_pkg quadrotor_receive rospy std_msgs message_generation message_runtime
Next, we create the message file, Person.msg. Run the following command in the same terminal you just used.
cd quadrotor_receive
mkdir msg
cd msg
touch Person.msg
Nice done! Now head over to the Code Editor to make changes to the Person.msg file. Check the image below for how to open the Code Editor.
Locate the file in the code editor: catkin_ws > src > quadrotor_receive > msg > Person.msg and paste in the following code.
string name
int32 age
Edit package files
Replace CMakeLists.txt with the one below (where the following changes have been made).
Removed message_runtime from the find_package function.
Uncommented the add_message_files function, and added Person.msg below FILES.
Uncommented the generate_messages function, and left it intact.
Uncommented the line CATKIN_DEPENDS in the catkin_package function.
Removed all commented lines to keep the file short and sweet.
cmake_minimum_required(VERSION 3.0.2)
project(quadrotor_receive)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
message_runtime
rospy
std_msgs
)
## Generate messages in the 'msg' folder
add_message_files(
FILES
Person.msg
# Message2.msg
)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
)
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES quadrotor_receive
CATKIN_DEPENDS message_generation message_runtime rospy std_msgs
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
Next, add a single line to package.xml so that it looks like the one below. Can you identify the line that was added?
<?xml version="1.0"?>
<package format="2">
<name>quadrotor_receive</name>
<version>0.0.0</version>
<description>The quadrotor_receive package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<maintainer email="user@todo.todo">user</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>message_generation</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<build_export_depend>message_generation</build_export_depend>
<exec_depend>message_runtime</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
Finally, let’s create the receiver Python node, just as the ROS Answers user created it. Run the following in the same terminal as before. Notice that we’re making the Python script executable.
cd ~/catkin_ws/src/quadrotor_receive/src
touch custom_listener.py
chmod +x custom_listener.py
Open custom_listener.py in the IDE and paste in the following code.
#!/usr/bin/env python
import rospy
from quadrotor_receive.msg import Person
def callback(data):
rospy.loginfo("%s is age: %d" % (data.name, data.age))
def listener():
rospy.init_node('custom_listener', anonymous=True)
rospy.Subscriber("custom_chatter", Person , callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__=='__main__':
listener()
So much for all the hard work – let’s see if it works. Time to compile the code. In the same web shell, run the following commands:
Compile and test
cd ~/catkin_ws
catkin_make
source devel/setup.bash
Success! We have successfully created a package with a custom message. Let’s see if we can find the message. You should get an output similar to the one below.
user:~/catkin_ws$ rosmsg show quadrotor_receive/Person
string name
int32 age
Let’s also test receiver node at this point. Though there’s nothing to receive yet, it should not throw any error. First, we need to start roscore.
roscore
Then try to run your subscriber in another terminal. Leave it running!
rosrun quadrotor_receive custom_listener.py
PS: if your code did not compile correctly, please go over the instructions and ensure you have created the files in the exact locations specified.
Step 3: Create the second package that contains the publisher
Create a new folder package. This should be easy-peasy, as most of the work has been done in the previous step. Do the following in a new terminal.
cd ~/catkin_ws/src/
catkin_create_pkg transmit_thrust rospy
Next, create the custom_talker.py node.
cd transmit_thrust/src
touch custom_talker.py
chmod +x custom_talker.py
Open up custom_talker.py in the IDE and paste in the following.
#!/usr/bin/env python
import rospy
from quadrotor_receive.msg import Person
def talker():
pub = rospy.Publisher('custom_chatter', Person)
rospy.init_node('custom_talker', anonymous=True)
r = rospy.Rate(10) #10hz
msg = Person()
msg.name = "ROS User"
msg.age = 4
while not rospy.is_shutdown():
rospy.loginfo(msg)
pub.publish(msg)
r.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException: pass
Please note that:
We are importing the custom message from the quadrotor_receive package.
We are creating a publisher for the same topic we created a subscriber for in the quadrotor_receive package.
Nothing more is needed. Let’s compile our new package.
cd ~/catkin_ws
catkin_make
source devel/setup.bash
That done, let’s start the custom talker node.
rosrun transmit_thrust custom_talker.py
You should see some output similar to these on the terminal.
And…on the terminal where you have the listener tunning, you should see something similar to:
[INFO] [1680728823.956347]: ROS User is age: 4
[INFO] [1680728824.056833]: ROS User is age: 4
And we are done here!
Step 4: Check your learning
Do you understand how to write a ROS publisher and subscriber with a custom message? If you don’t know it yet, please go over the post again, more carefully this time.
(Extra) Step 5: Watch the video to understand how to write a ROS publisher and subscriber with a custom message
Here you go:
Feedback
Did you like this post? Do you have any questions about how to write a ROS publisher and subscriber with a custom message? Please leave a comment in the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS topics, please let us know in the comments area and we will do a video or post about it.
In this episode of ROS2 Tutorials Series, you will learn how to create a simple ROS2 custom message for Topics.
We are going to use a ROS2 (Dashing) installation for this, but you don’t need to install ROS, as we will use the ROS Development Studio (ROSDS), an online platform that provides access to ROS2 and ROS2 computers and other powerful ROS tools within a browser!
Head to http://rosds.online and create a project with a similar configuration as the one shown below. You can change the details as you like, but please ensure you select “Ubuntu 18.04 + ROS2 Dashing” under “Configuration”.
Once done with that, open your ROSject. This might take a few moments, please be patient.
Step 2: Source the ROS2 workspace
Once the ROSject is open, head to the Tools menu and pick the Shell tool (a terminal) and run the following command to source the workspace:
user:~$ source /opt/ros/dashing/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$
If you get that ROS_DISTRO warning, just ignore it.
Step 3: Create a ROS2 C++ package in your ROS2 workspace
We are creating a C++ package (ament_cmake) and adding dependencies rclcpp (ROS C++ client) and std_msgs (in-built standard messages type, on which our new message will be based).
Add the following function to CMakeList.txt.This function tells the ROS system to generate a new message from the specified message definition, using the specified interface.
Whew, a couple of ingredients had to be added there; now it is time to compile the message and taste the “soup”!
Get back to the terminal and run the following commands:
user:~$ cd ~/ros2_ws
user:~/ros2_ws$ colcon build --symlink-install
Starting >>> ros2_msg
Finished <<< ros2_msg [14.1s]
Summary: 1 package finished [14.3s]
user:~/ros2_ws$ source install/setup.bash
ROS_DISTRO was set to 'dashing' before. Please make sure that the environment does not mix paths from different distributions.
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~/ros2_ws$
Pick another Shell from the Tools menu and check the message being published (PS: you need to source ROS Dashing and the workspace first):
user:~$ source /opt/ros/dashing/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$ source ros2_ws/install/setup.bash
ROS_DISTRO was set to 'dashing' before. Please make sure that the environment does not mix paths from different distributions.
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$ ros2 topic list
/parameter_events
/rosout
/test_topic
user:~$ ros2 topic echo /test_topic
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
day: 7
month: October
year: 2019
---
^Cuser:~$
Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it 🙂
In this post, you will learn how to access and output odometry data programmatically. More specifically, you will learn how to create a C++ program for subscribing to and printing different parts of the odometry message. This is useful for cases where you need to make a decision based on the robot’s current position.
The command line output above shows the parts the make up an odometry message. The hottest part is pose, which tells us the current position of the robot. twist is also important, because it tells us the current message being published to move the robot.
Now let’s see how to access and output odometry data, considering the hottest part.
Step 4: See how to access and output odometry data in code
Switch to the IDE tool and locate the catkin_ws directory. You should find a C++ file in a package odom_subscriber. The full path to the file is catkin_ws/src/odom_subscriber/src/odom_sub.cpp.
Let’s see the content of this file:
#include <nav_msgs/Odometry.h> // Needed for accessing Odometry data
#include <ros/ros.h> // Needed for creating the node, etc
// This functions get called when a new odometry message gets to the subscriber
// It automatically gets the odometry message as a parameter
// It prints out various parts of the message
void counterCallback(const nav_msgs::Odometry::ConstPtr &msg) {
// ROS_INFO("%s", msg->header.frame_id.c_str());
// ROS_INFO("%f", msg->twist.twist.linear.x);
ROS_INFO("%f", msg->pose.pose.position.x);
}
int main(int argc, char **argv) {
// Create a node to run the code
ros::init(argc, argv, "odom_sub_node");
ros::NodeHandle nh;
// create a subscriber to the "/odom" topic so we can get the odometry message
ros::Subscriber sub = nh.subscribe("odom", 1000, counterCallback);
// Run program until manually stopped
ros::spin();
return 0;
}
I have added comments to the code to explain what each line or code block does, for better clarity.
The `counterCallback` function prints out the current x position of the robot. Observe how parts of the message are accessed in hierarchical format; using the structure in Step 3 as a guide, you can access and output any part of the Odometry message.
You can uncomment the commented parts to print out parts of the header and twist messages or even write other statements that print out other parts of the message!
Step 5: Move the robot and see the odometry data printed in real-time!
Run the C++ program in the shell, to print out the current x position of the robot. We can see the robot is basically at the “origin” and is stationary. Leave this program running.
Now you should see the robot moving. You should also see that the Odometry data being output in the other shell is changing.
In the second Shell, press Ctrl + C to stop the current program. Then publish the following message to stop the robot. After this, you should see the odometry position data become static again.
Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS topics, please let us know in the comments area and we will do a video or post about it.