无法从ROS Services中获取数据,只进入服务器但数据出不来,为什么?
Can not get the data from ROS Services, only entering the server but data is not out, why?
我需要通过客户端请求从串口微控制器读取数据(比如说压力)。我查看了 Python 中的 ROS 服务教程,但我的代码仍然没有将数据值提供给客户端。这里首先是服务服务器Python节点
#!/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")
当我调用服务器时,我得到了这个输出
entering service
Ready to get_value
这里是客户端节点
#!/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")
当我打电话给客户时只得到
entering client
因此意味着服务器永远不会进入 handle_ros_services()
并且客户端永远不会进入 imu_client():
功能。代码有什么问题?
调用wait_for_service
时你的命名有误。您的服务 回调 称为 handle_ros_services
,但服务名称本身是 imu_value
。因此,您的客户将永远等待,因为以前的服务名称实际上从未被提出来。而不是在 imu_client()
里面你想要行
rospy.wait_for_service('imu_value')
我需要通过客户端请求从串口微控制器读取数据(比如说压力)。我查看了 Python 中的 ROS 服务教程,但我的代码仍然没有将数据值提供给客户端。这里首先是服务服务器Python节点
#!/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")
当我调用服务器时,我得到了这个输出
entering service
Ready to get_value
这里是客户端节点
#!/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")
当我打电话给客户时只得到
entering client
因此意味着服务器永远不会进入 handle_ros_services()
并且客户端永远不会进入 imu_client():
功能。代码有什么问题?
调用wait_for_service
时你的命名有误。您的服务 回调 称为 handle_ros_services
,但服务名称本身是 imu_value
。因此,您的客户将永远等待,因为以前的服务名称实际上从未被提出来。而不是在 imu_client()
里面你想要行
rospy.wait_for_service('imu_value')