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

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

Hi

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):
        rospy.init_node('imu_server')
        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)
        rospy.spin()

if __name__ == "__main__":
    publish_on_serial()

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
rospy.init_node('imu_service_client')

# wait for this service to be running
def imu_client(imu_data):
    rospy.wait_for_service('imu_service')
    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?

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

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():
    rospy.wait_for_service('imu_service')
    imu_client = rospy.ServiceProxy('imu_service', ImuResponse)

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

Comments

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. http://wiki.ros.org/ROS/Tutorials/Cre...

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

Question Tools

1 follower

Stats

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

Seen: 88 times

Last updated: Mar 13 '22