ros自定义action记录

文章目录

  • 自定义action
    • [1. 定义action文件](#1. 定义action文件)
    • [2. 修改 package.xml](#2. 修改 package.xml)
    • [3. 修改 CMakeLists.txt](#3. 修改 CMakeLists.txt)
    • [4. 运行 `catkin build`](#4. 运行 catkin build)
    • [5. simple_action_server.py](#5. simple_action_server.py)
    • [6. simple_action_client.py](#6. simple_action_client.py)
  • 测试

自定义action

ros 版本:kinetic

自定义test包的文件结构如下

bash 复制代码
|-- test
|   |-- CMakeLists.txt
|   |-- action
|   |   `--Timer.action
|   |-- package.xml
|   |-- scripts
|   |   |-- simple_action_server.py
|   |   `-- simple_action_server.py

动作定义文件后缀 .action。其组成和srv非常相似。有关自定义srv链接

1. 定义action文件

bash 复制代码
# the goal: to be sent by the client
duration time_to_wait
---

# the result: to be sent by the server upon completion
duration time_elapsed
uint32 updates_sent
---

# the feedback: to be sent periodically by the server during execution
duration time_elapsed
duration time_remaining

2. 修改 package.xml

向其中添加如下信息:

bash 复制代码
  <build_depend>actionlib_msgs</build_depend>
  <build_export_depend>actionlib_msgs</build_export_depend>

3. 修改 CMakeLists.txt

bash 复制代码
find_package(catkin REQUIRED COMPONENTS
  rospy
  roscpp
  std_msgs
  message_generation
  actionlib_msgs
)
bash 复制代码
## Generate actions in the 'action' folder
add_action_files(
  FILES
  Timer.action
)
bash 复制代码
## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  actionlib_msgs
  std_msgs  # Or other packages containing msgs
)
bash 复制代码
catkin_package(
#   INCLUDE_DIRS include
#   LIBRARIES test
   CATKIN_DEPENDS rospy message_runtime std_msgs roscpp actionlib_msgs
#   DEPENDS system_lib
)
bash 复制代码
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
catkin_install_python(PROGRAMS
  scripts/simple_action_server.py
  scripts/simple_action_client.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

4. 运行 catkin build

动作被正确编译后会生成6个msgTimerGoal.msg, TimerResult.msg, TimerFeedback.msg, TimerActionFeedback.msg, TimerActionGoal.msg, TimerActionResult.msg

5. simple_action_server.py

python 复制代码
#!/usr/bin/env python

import rospy
import time
import actionlib
from test.msg import TimerAction, TimerGoal, TimerResult


def do_timer(goal):
    start_time = time.time()
    time.sleep(goal.time_to_wait.to_sec())
    result = TimerResult()
    result.time_elapsed = rospy.Duration.from_sec(time.time() -start_time)
    result.updates_sent = 0
    server.set_succeeded(result)


rospy.init_node("timer_action_server")
server = actionlib.SimpleActionServer("timer", TimerAction, do_timer, False) #记得写False
server.start()
rospy.spin()
chmod +x  simple_action_server.py

6. simple_action_client.py

python 复制代码
#!/usr/bin/env python
import rospy

import actionlib
from test.msg import TimerAction, TimerGoal, TimerResult

rospy.init_node("timer_action_client")
client = actionlib.SimpleActionClient("timer", TimerAction)
client.wait_for_server()
goal = TimerGoal()
goal.time_to_wait = rospy.Duration.from_sec(5.0)
client.send_goal(goal)
client.wait_for_result()
print("Time elapsed: %f" % (client.get_result().time_elapsed.to_sec()))
chmod +x  simple_action_client.py

测试

bash 复制代码
roscore
bash 复制代码
rosrun test simple_action_client.py
bash 复制代码
rosrun test simple_action_client.py

如下结果:

相关推荐
AliCloudROS6 天前
ROS CDK魔法书:建立你的游戏王国(Csharp篇)
游戏·c#·云计算·ros
不知道是谁26 天前
百度Apollo打通与ROS的通信,扩展自动驾驶系统生态
机器人·自动驾驶·ros·apollo
—你的鼬先生6 天前
基于树莓派ubuntu20.04的ros-noetic小车
python·嵌入式·ros·树莓派项目
AliCloudROS9 天前
基于 ROS 的Terraform托管服务轻松部署Stable Diffusion
云原生·stable diffusion·ros·terraform·iac
AliCloudROS10 天前
ROS CDK魔法书:建立你的游戏王国(TypeScript篇)
游戏·阿里云·typescript·云计算·ros·资源编排·cdk
机器人梦想家13 天前
ROS2 2D相机基于AprilTag实现3D空间定位最简流程
计算机视觉·机器人·ros
乱七八糟喜欢就学先生18 天前
跟自动驾驶之心仿真课程问题操作与解决
人工智能·自动驾驶·ros·pygame
月照银海似蛟龙21 天前
无人机 PX4 飞控 | ROS应用层开发:基础代码框架构建
ros·无人机·px4·mavros
大象机器人23 天前
使用myAGV、Jetson Nano主板和3D摄像头,实现了RTAB-Map的三维建图功能!
人工智能·python·科技·机器人·开源·ros·具身智能
月照银海似蛟龙24 天前
无人机 PX4 飞控 | ROS应用层开发:指令(字符串)订阅功能
ros·无人机·topic·px4·mavros