ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

ROS service server and client node to publish on serial port

asked 2022-03-11 01:38:13 -0500

Astronaut gravatar image

updated 2022-03-11 01:40:06 -0500


I need to create ROS SERVICE server and client node that check lets say IMU sensor data and publish the IMU value on serial port. So I though the only way is to create two separates nodes for server and client. I create them but not sure that's the correct way of implementation in python (Im more familiar with C++ but in this case have to use python as my midlleware and firmware are in python and easier ti import all those library in python). Here is the server node python:

#!/usr/bin/env python3

import rospy
from std_msgs.msg import String
import Microcontroller_Manager_Serial as Serial
import IMU_Functions as IMU
from ros_services.srv import RosService,ImuResponse

def handle_ros_service(data_received_imu):
        data_received_imu = IMU.IMU_Get_Values(1, 1)
        Imu = (np.int16((data_received_imu[6]<<24) | (data_received_imu[7]<<16) | (data_received_imu[8]<<8) | (data_received_imu[9])))/10000
        return ImuResponse(data_received_imu.Imu)

def publish_on_serial():
        Imu = data_out
        data_out = Serial.Serial_Port_Send_Data(20,0.2) 
        data_out = rospy.Service('add_two_ints', RosService, handle_ros_service)

if __name__ == "__main__":

And here is the client node python:

#!/usr/bin/env python3

import sys
import rospy
from ros_services.srv import RosService,ImuResponse

# init a node as usual

# wait for this service to be running
def imu_client(imu_data):
    imu_client = rospy.ServiceProxy('imu_service', ImuResponse)

So, Im not sure these is correct. Any help? And also i search online but so far I understand the server and client can not be in the same node , is that correct? Or using service and client in same node is possible?


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-03-13 03:41:49 -0500

miura gravatar image

You seem to have forgotten to call imu_client. Also, the imu_client argument is unused, so it is not needed for the time being. I think the following changes may move the situation forward.

# wait for this service to be running
def imu_client():
    imu_client = rospy.ServiceProxy('imu_service', ImuResponse)

if __name__ == "__main__":
edit flag offensive delete link more


Ok yes good point. I was not sure about it. Thanks. So the last two lines of code will do right? No need anything else? how about from ros_services.srv import RosService,ImuResponse how to define the ros_services.srv file?

Astronaut gravatar image Astronaut  ( 2022-03-14 02:34:57 -0500 )edit

I am glad to see that progress has been made.

I would like you to share what problems you are experiencing. Otherwise, it is difficult to guess what the problem is and what needs to be fixed.

For a definition of services, please refer to the following tutorial to get started.

miura gravatar image miura  ( 2022-03-14 05:52:51 -0500 )edit

i read the tutorial but stiill when create the sercer and client node is not working . I post it separately the question witth the whole code. Please can you help? here the link link text

Astronaut gravatar image Astronaut  ( 2022-03-18 05:40:39 -0500 )edit

I have just checked your question. I think the points already answered by @abhishek47 are improvements.

miura gravatar image miura  ( 2022-03-20 00:24:25 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-03-11 01:38:13 -0500

Seen: 37 times

Last updated: Mar 13 '22