ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to send rosservice call message from a c++ package?

asked 2021-07-11 10:16:55 -0500

kyubot gravatar image

updated 2022-05-23 08:59:22 -0500

lucasw gravatar image

Hello all,

I am writing a package written in c++ for ros-melodic on ubuntu 18.04.

I need to send service call to a node that takes 3 values as following service call from terminal to change LED color.

$ rosservice call /set_led_color "red: ###
green: ###
blue: ##"

The node have service file definition under "node_name/srv" as Color.srv and its contents as following

int64 red
int64 green
int64 blue
--

And in node.py the service is coded as

from omo_r1mini_bringup.srv import Color, ColorResponse

rospy.Service('set_led_color', Color, self.save_led_color_service_handle)

def save_led_color_service_handle(self, req):
command = "$sCOLOR," + str(req.red) + ',' + str(req.green) + ',' + str(req.blue)
self.ph.write_port(command)
return ColorResponse()

I tried to initialize my service client by

svc_client = n.serviceClient("/set_led_color", true);

however it is not working.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-07-11 11:48:03 -0500

kyubot gravatar image

updated 2021-07-11 11:50:39 -0500

I did not add below lines in package.xml

<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<run_depend>std_msgs</run_depend>

and then it is able to generate service header according to the service files defined in srv folder.

In my node.hpp file I added

#include "r1mini_gui_teleop/Color.h"
...
private:
...
ros::ServiceClient serviceClient; //Client node to call service
my_node::Color serviceSetColor; //Service set color

And in node.cpp file

init(...){
...
serviceClient = n.serviceClient<r1mini_gui_teleop::color>("/set_led_color");
}
some function(...) ...
serviceClient.call(serviceSetColor);

Solved my problem.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-07-11 10:16:55 -0500

Seen: 257 times

Last updated: Jul 11 '21