ros自定义action记录

本文详细介绍了如何在ROSKinetic环境中自定义action,包括定义action文件、修改package.xml和CMakeLists.txt,以及编写和运行简单的action_server和action_client。通过实例展示了如何创建动作定义、依赖管理和编译测试过程。
摘要由CSDN通过智能技术生成

自定义action

ros 版本:kinetic
自定义test包的文件结构如下

|-- test
|   |-- CMakeLists.txt
|   |-- action
|   |   `--Timer.action
|   |-- package.xml
|   |-- scripts
|   |   |-- simple_action_server.py
|   |   `-- simple_action_server.py

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

1. 定义action文件

# 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

向其中添加如下信息:

  <build_depend>actionlib_msgs</build_depend>
  <build_export_depend>actionlib_msgs</build_export_depend>

3. 修改 CMakeLists.txt

find_package(catkin REQUIRED COMPONENTS
  rospy
  roscpp
  std_msgs
  message_generation
  actionlib_msgs
)
## Generate actions in the 'action' folder
add_action_files(
  FILES
  Timer.action
)
## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  actionlib_msgs
  std_msgs  # Or other packages containing msgs
)
catkin_package(
#   INCLUDE_DIRS include
#   LIBRARIES test
   CATKIN_DEPENDS rospy message_runtime std_msgs roscpp actionlib_msgs
#   DEPENDS system_lib
)

## 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

#!/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

#!/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

测试

roscore
rosrun test simple_action_client.py
rosrun test simple_action_client.py

如下结果:
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值