[ROS in 5 mins] 030 – How to compile ROS Service messages

[ROS in 5 mins] 030 - How to compile ROS Service messages

Written by Ruben Alves

27/08/2018

 

Hello ROS Developers!

In today’s video we are going learn how to create a ROS Service file, how to compile and how to import it.

For that we are going to use Robot Ignite Academy, which is the best tool if you want to Learn ROS Fast.

Before we start, if you are new to ROS, I highly recommend you to take any of the following courses on Robot Ignite Academy:

We love feedback, so, whether you like the video or not, please share your thoughts on the comments section below:)

Step1. Create a project in Robot Ignite Academy(RIA)

We have the best online ROS course available in RIA. It helps you learn ROS in the easiest way without setting up ROS environment locally. The only thing you need is a browser! Create an account here and start to browse the trial course for free now! We’ll use the ROS Basics in 5 Days course unit3 as an example today.

Step2. Create a package

We’ll put our new service message in a new package called services_tutorial

catkin_create_pkg services_tutorial rospy

Then we create the definition of the message called AddTwoNumbers.srv and put it into a folder called srv with the following content.

float32 a
float32 b
---
float32 sum

In the package.xml file, you have to uncomment the following part.

...
<build_depend>message_generation</build_depend>
...
<run_depend>message_runtime</run_depend>
...

You’ll also need to change the following part in the CMakeLists.txt

...
find_package(catkin REQUIRED COMPONENTS
  rospy
  std_msgs
  message_generation
)
...
catkin_package(
  DEPENDS message_runtime
)
...
add_service_files(
  FILES
  AddTwoNumbers.srv
)
...
generate_messages(
  DEPENDENCIES
  std_msgs
)
...

Then we can finally compile the package.

cd ~/catkin_ws
catkin_make
source devel/setup.bash

You can now use your new message type in your code. For example, you can do the following to import it in a python script.

from services_tutorial.srv import AddTwoNumbers

Want to learn more?

You can learn more about ROS topic, service and action in our ROS Basics in 5 Days course.

 

Edit by: Tony Huang

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This