ROS服务通信,返回多数据
问题产生
学习完ROS.org wiki上的关于服务的示例之后,在实际应用上因各人理解差异,可能出现一些疑惑。这里是对于服务通信,返回值为多数据类型时的实现。
相关实现代码
服务消息定义
说明:请求为空,响应数据为多个。
---
float32 j4
float32 j5
float32 j6
float32 j1
float32 j2
float32 j3
float64 x
float64 y
float64 z
float64 roll
float64 pitch
float64 yaw
bool relative
bool res
服务发布
self.getCurrentStatusService=rospy.Service("/current_status",CurrentStatus,self.getCurrentStatus)
服务回调函数(handler)
说明:该函数定义于一个类中,该类为全部的ROS服务。
def getCurrentStatus(self,_):
try:
currentpose=self.robot.get_status()
response=CurrentStatusResponse()
response.j4=currentpose.angle.joint1
response.j5=currentpose.angle.joint2
response.j6=currentpose.angle.joint3
response.j1=currentpose.angle.joint4
response.j2=currentpose.angle.joint5
response.j3=currentpose.angle.joint6
response.x=currentpose.cartesian.x
response.y=currentpose.cartesian.y
response.z=currentpose.cartesian.z
response.roll=currentpose.cartesian.roll
response.pitch=currentpose.cartesian.pitch
response.yaw=currentpose.cartesian.yaw
response.relative=currentpose.motion_mode ##false 笛卡尔坐标系;true 关节运动模式
response.res=True
except:
rospy.loginfo("未能获取当前六轴姿态信息")
return False
return response
发起请求(request)
可以在客户端程序写入相关函数,也可以直接通过命令行发起请求。
rosservice call /robot_drive /current_status