Search code examples
pythonros

Can not get the data from ROS Services, only entering the server but data is not out, why?


I need to read data ( lets say pressure) from serial Port Microcontroller by Client Request. I check the tutorial for ROS Services in Python but still my code is not giving the data value to to client. Here first the Service Server Python node

#!/usr/bin/env python3

from __future__ import print_function
import rospy
import numpy as np
from os import system
import time
import threading
import Microcontroller_Manager_Serial as Serial
import IMU_Functions as IMU
import Motors_Functions as Motor
import Pressure_Functions as Pressure
from geometry_msgs.msg import Vector3
import Modem_Functions as Modem
import threading 
import time
import serial
import serial.tools.list_ports

from time import sleep
from std_msgs.msg import Float32
from std_msgs.msg import String

from demo_teleop.srv import ImuValue
Communication_Mode_ = 0

def handle_ros_services():
    global P0
    data_received = Pressure.Pressure_Get_Final_Values(1,1)
    print("Server Read Data:")
    P0 = (np.int16((data_received[6]<<24) | (data_received[7]<<16) | (data_received[8]<<8) | (data_received[9])))/10000
    P=P0
    pressure = P/9.81
    current_x_orientation_s=pressure
    print("Returning ", current_x_orientation_s)
    #return ImuValue(current_x_orientation_s)


def ros_serice_server():
    #rospy.init_node('ros_serice_server')
    s = rospy.Service('imu_value', ImuValue, handle_ros_services)
    print("Ready to get_value")
    rospy.spin()


if __name__ == '__main__':
    rospy.init_node('server_node_f')
    Serial.Serial_Port_Standard()
    while not rospy.is_shutdown():
        try:
            print("entering service")
            ros_serice_server()
        except:
            print("pass")

When I call the server I got this output

entering service 
Ready to get_value

And here the client node

#!/usr/bin/env python3

from __future__ import print_function
import rospy
import sys
import numpy as np
from os import system
import time
import threading
import Microcontroller_Manager_Serial as Serial
import IMU_Functions as IMU
import Pressure_Functions as Pressure
import time
import serial
import serial.tools.list_ports
from time import sleep
from std_msgs.msg import Float32
from std_msgs.msg import String

from demo_teleop.srv import ImuValue

Communication_Mode_ = 0

def imu_client():
    rospy.wait_for_service('handle_ros_services')
    print("Request call send")
    imu_value = rospy.ServiceProxy('imu_value', ImuValue)
    #resp1 = imu_value
    #return imu_value.current_x_orientation_s

if __name__ == "__main__":
    rospy.init_node('client_node_f')
    while not rospy.is_shutdown():
        try:
            print("entering client")
            imu_client()
        except:
            print("pass")

When i call the client only got

entering client

So means the server never enter the handle_ros_services()and the client never enter imu_client(): functions. What is wrong with the code?


Solution

  • You have the naming wrong when calling wait_for_service. Your service callback is called handle_ros_services but the service name itself is imu_value. Because of this your client will wait forever, because the former service name never actually gets brought up. Instead inside imu_client() you want the line

    rospy.wait_for_service('imu_value')