• ros自定义action记录


    自定义action

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

    |-- test
    |   |-- CMakeLists.txt
    |   |-- action
    |   |   `--Timer.action
    |   |-- package.xml
    |   |-- scripts
    |   |   |-- simple_action_server.py
    |   |   `-- simple_action_server.py
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    动作定义文件后缀 .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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2. 修改 package.xml

    向其中添加如下信息:

      <build_depend>actionlib_msgs</build_depend>
      <build_export_depend>actionlib_msgs</build_export_depend>
    
    • 1
    • 2

    3. 修改 CMakeLists.txt

    find_package(catkin REQUIRED COMPONENTS
      rospy
      roscpp
      std_msgs
      message_generation
      actionlib_msgs
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    ## Generate actions in the 'action' folder
    add_action_files(
      FILES
      Timer.action
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    ## Generate added messages and services with any dependencies listed here
    generate_messages(
      DEPENDENCIES
      actionlib_msgs
      std_msgs  # Or other packages containing msgs
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    catkin_package(
    #   INCLUDE_DIRS include
    #   LIBRARIES test
       CATKIN_DEPENDS rospy message_runtime std_msgs roscpp actionlib_msgs
    #   DEPENDS system_lib
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    
    ## 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}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    chmod +x  simple_action_server.py
    
    • 1

    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()))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    chmod +x  simple_action_client.py
    
    • 1

    测试

    roscore
    
    • 1
    rosrun test simple_action_client.py
    
    • 1
    rosrun test simple_action_client.py
    
    • 1

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

  • 相关阅读:
    云原生之k8s】k8s 亲和、反亲和、污点、容忍
    C++ 智能指针
    【LeetCode:2095. 删除链表的中间节点 + 链表】
    Flutter应用框架搭建之屏幕适配
    JAVA【JDBC】
    JavaScript从入门到精通系列第三十篇:详解JavaScript中的正则表达式语法
    (第24天)【leetcode题解】二叉树的层序遍历
    用git stash暂存修改
    Ubuntu 20.04编译GPMP2过程记录
    C语言第十二课(上):操作符详解【算数、移位、位、赋值操作符】
  • 原文地址:https://blog.csdn.net/weixin_45654152/article/details/136222651