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

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

asked 2022-03-18 05:21:25 -0500

Astronaut gravatar image

updated 2022-03-21 02:31:56 -0500

Hi

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 Microcontroller_Manager_Serial as Serial
import Pressure_Functions as Pressure
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?

Here is my ImuValue.srv file

float64 current_x_orientation_c
---
float64 current_x_orientation_s
bool success
edit retag flag offensive close merge delete

Comments

Follow-up to (or duplicate of) #q397753

abhishek47 gravatar image abhishek47  ( 2022-03-20 01:03:29 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-03-19 05:17:23 -0500

abhishek47 gravatar image

updated 2022-03-19 05:18:13 -0500

Client node

You wait for a service named 'handle_ros_services' which is not advertised in the provided code. As explained in the documentation, rospy.wait_for_service() is blocking, and without a specified timeout it's gonna block indefinitely.

The server advertises a service with the name "imu_value", which you're actually correctly using with rospy.ServiceProxy()

Server node

while not rospy.is_shutdown() is not needed, simply call ros_serice_server(). It helps to understand the difference between while not rospy.is_shutdown() and rospy.spin() so you know when to use which.

The service server must return a response.

edit flag offensive delete link more

Comments

So when do the changes i can enter the client but in server node never enter the handle_ros_services() function so Im not able to get the data. Is that because server didn't return a response? How to make server return response?

Astronaut gravatar image Astronaut  ( 2022-03-21 01:21:12 -0500 )edit

never enter the handle_ros_services() function

How did you verify this? Is everything alright with Pressure.Pressure_Get_Final_Values(1,1)?

so Im not able to get the data

Might have to do with the commented out return statement (btw ImuValue isn't the correct return type, it should be ImuValueResponse)

How to make server return response?

See Writing a Service Node, note the line return AddTwoIntsResponse(req.a + req.b)

abhishek47 gravatar image abhishek47  ( 2022-03-21 01:48:05 -0500 )edit

I verify with print("Server Read Data:") in the function. It never print it. Yes all good with Pressure.Pressure_Get_Final_Values(1,1) . I only have one srv file and that is the ImuValue. Understand? I don't have ImuValueResponse

Astronaut gravatar image Astronaut  ( 2022-03-21 02:26:30 -0500 )edit

I edit in the question the ImuValue srv file. I dont know what ImuValueResponse srv file should contain. Any help?

Astronaut gravatar image Astronaut  ( 2022-03-21 02:30:33 -0500 )edit

only have one srv file and that is the ImuValue. Understand? I don't have ImuValueResponse

ImuValueResponse is auto-generated :) See here

abhishek47 gravatar image abhishek47  ( 2022-03-21 02:32:03 -0500 )edit

ok when do that with from demo_teleop.srv import ImuValue,ImuResponse i got error ImportError: cannot import name 'ImuResponse' from

Astronaut gravatar image Astronaut  ( 2022-03-21 02:41:17 -0500 )edit

Still not able to enter the handle_ros_services(). I generated ImuValueResponse but still no data returned and not entering handle_ros_services()

Astronaut gravatar image Astronaut  ( 2022-03-21 03:23:26 -0500 )edit

The problem remain Im not able to enter the function handle_ros_services. Why?

Astronaut gravatar image Astronaut  ( 2022-03-21 03:47:52 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-03-18 05:21:25 -0500

Seen: 385 times

Last updated: Mar 21 '22