当前位置:   article > 正文

ros2中 用python编写action 的service和client_ros2编写action服务器和客户端

ros2编写action服务器和客户端

第一步:定义请求反馈的变量

主要参考官网网站:Writing an action server and client (C++) — ROS 2 Documentation: Rolling documentation

与ros1的对比(74条消息) ROS入门之——action_沐棋的博客-CSDN博客_ros中action

在编写自己的actiion service和client之前,需要先定义自己的action动作类型,这一点和ros1是一样的。参考:Creating an action — ROS 2 Documentation: Rolling documentation 

1.创建工作空间,创建代码包

mkdir -p ros2_ws/src #you can reuse existing workspace with this naming convention
cd ros2_ws/src
ros2 pkg create action_tutorials_interfaces

2.在action_tutorails_interface包中创建一个action文件夹, 并在该文件夹下新建一个脚本,该脚本为发送请求的类型

cd action_tutorials_interfaces
mkdir action

touch Fibonacci.action

3.在这个Fibonacci.action文件中添加如下内容,order为请求时发送的变量,sequence为执行结果,partial_sequence为action执行过程中的反馈结果

int32 order
---
int32[] sequence
---
int32[] partial_sequence 

 4.在action_tutorails_interface文件夹下的CMakeLists.txt文件中添加如下内容,添加在ament_package()语句之前。该段代码会根据我们定义的请求类型Fibonacci在在/opt/ros/foxy/lib/python3.8/site-packages文件夹下生成action_tutorails_interface/action/_fibonacci.py等的文件。

find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "action/Fibonacci.action"
)

5.在package.xml文件中添加如下内容

<buildtool_depend>rosidl_default_generators</buildtool_depend>

<member_of_group>rosidl_interface_packages</member_of_group>

6.编译运行

cd ~/ros2_ws

colcon build

 至此,我们已经在环境变量中申明了请求的变量类型。

source一下 环境变量 . install/setup.bash,执行如下命令就能查看我们自己定义的请求类型了。

ros2 interface show action_tutorials_interfaces/action/Fibonaccis

输出如下:

 第二步:编写action service的服务

1.在 action_tutorails_interface/src文件夹下新建脚本fibonacci_action_server.py

  1. import time
  2. import rclpy
  3. from rclpy.action import ActionServer
  4. from rclpy.node import Node
  5. from action_tutorials_interfaces.action import Fibonacci
  6. class FibonacciActionServer(Node):
  7. def __init__(self):
  8. super().__init__('fibonacci_action_server') #初始话节点名字,action service和client 根据这个节点名字和话题fibonacci通信
  9. self._action_server = ActionServer(
  10. self,
  11. Fibonacci, #自己定义的消息类型
  12. 'fibonacci', #话题
  13. self.execute_callback) #当这个类class:`ServerGoalHandle.execute()被执行的时候,execute_callback将被执行,
  14. def execute_callback(self, goal_handle): #设置目标句柄(goal_handle)当类ServerGoalHandle构造的时候,goal_handle被赋值
  15. self.get_logger().info('Executing goal...')
  16. feedback_msg = Fibonacci.Feedback()
  17. feedback_msg.partial_sequence = [0, 1]
  18. for i in range(1, goal_handle.request.order): #执行斐波那契数列
  19. feedback_msg.partial_sequence.append(
  20. feedback_msg.partial_sequence[i] + feedback_msg.partial_sequence[i-1])
  21. self.get_logger().info('Feedback: {0}'.format(feedback_msg.partial_sequence))
  22. goal_handle.publish_feedback(feedback_msg)#只有在这里发布发聩结果,我们才可以在client里的send_goal_async(goal_msg, feedback_callback=self.feedback_callback) 的形参里有回调
  23. time.sleep(1)
  24. goal_handle.succeed() #使用目标句柄的方法succeed()来表示目标成功了
  25. result = Fibonacci.Result()
  26. result.sequence = feedback_msg.partial_sequence
  27. return result
  28. def main(args=None):
  29. rclpy.init(args=args)
  30. fibonacci_action_server = FibonacciActionServer()
  31. rclpy.spin(fibonacci_action_server)
  32. if __name__ == '__main__':
  33. main()

2.在 action_tutorails_interface/src文件夹下新建脚本fibonacci_action_client.py,起始在哪里新建都行

  1. import rclpy
  2. from rclpy.action import ActionClient
  3. from rclpy.node import Node
  4. from action_tutorials_interfaces.action import Fibonacci
  5. class FibonacciActionClient(Node):
  6. def __init__(self):
  7. super().__init__('fibonacci_action_client')
  8. self._action_client = ActionClient(self, Fibonacci, 'fibonacci')
  9. def send_goal(self, order):
  10. goal_msg = Fibonacci.Goal()
  11. goal_msg.order = order
  12. self._action_client.wait_for_server()
  13. self._send_goal_future = self._action_client.send_goal_async(goal_msg, feedback_callback=self.feedback_callback) #本类的self.feedback_callback作为实参
  14. self._send_goal_future.add_done_callback(self.goal_response_callback)
  15. def goal_response_callback(self, future):
  16. goal_handle = future.result()
  17. if not goal_handle.accepted:
  18. self.get_logger().info('Goal rejected :(')
  19. return
  20. self.get_logger().info('Goal accepted :)')
  21. self._get_result_future = goal_handle.get_result_async()
  22. self._get_result_future.add_done_callback(self.get_result_callback)
  23. def get_result_callback(self, future):
  24. result = future.result().result
  25. self.get_logger().info('Result: {0}'.format(result.sequence))
  26. rclpy.shutdown()
  27. def feedback_callback(self, feedback_msg):
  28. feedback = feedback_msg.feedback
  29. self.get_logger().info('Received feedback: {0}'.format(feedback.partial_sequence))
  30. def main(args=None):
  31. rclpy.init(args=args)
  32. action_client = FibonacciActionClient()
  33. action_client.send_goal(10)
  34. rclpy.spin(action_client)
  35. if __name__ == '__main__':
  36. main()

开启两个终端,在脚本所在的目录执行

一个终端执行

python3 fibonacci_action_server.py

另一个终端执行

python3 fibonacci_action_client.py

 执行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/797842
推荐阅读
相关标签
  

闽ICP备14008679号