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

如下结果:

相关推荐
曹勖之1 天前
在MATLAB中使用自定义的ROS2消息
开发语言·matlab·机器人·ros·simulink·ros2
冀晓武4 天前
Ubuntu22.04 安装 ROS2 Humble
ubuntu·ros
干啥都是小小白5 天前
话题通信之python实现
python·机器人·ros
徐行tag11 天前
ROS架构
架构·ros
Mr.Winter`12 天前
运动规划实战案例 | 图解基于状态晶格(State Lattice)的路径规划(附ROS C++/Python仿真)
c++·人工智能·机器人·自动驾驶·ros·ros2
略无慕艳意14 天前
【教程】Ubuntu 16.04 配置 CLion 开发 ROS Melodic
c++·ros·c·clion
Tipriest_15 天前
足式机器人经典控制常用的ROS库介绍
机器人·ros·介绍·足式机器人
Mr.Winter`18 天前
深度强化学习 | 基于SAC算法的移动机器人路径跟踪(附Pytorch实现)
人工智能·pytorch·深度学习·神经网络·机器人·自动驾驶·ros
白云千载尽21 天前
相机、雷达标定工具,以及雷达自动标定的思路
python·自动驾驶·ros
别让别人觉得你做不到1 个月前
Ros工作空间
ros