First time here? Check out the FAQ!


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 Jul 11 '21

kyubot gravatar image

updated May 23 '22

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered Jul 11 '21

kyubot gravatar image

updated Jul 11 '21

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.

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Jul 11 '21

Seen: 352 times

Last updated: Jul 11 '21