ROS开发笔记(3):基于RoboWare Studio 与Python编写动作(action)通讯节点(node)

2023-05-16

ROS的动作非常适合时间不确定,目标导向型的操作接口。原理上用话题实现,其本质是相关于规定了一系列话题(目标、结果、反馈、取消等)的组合使用方法的高层协议。

1、定义动作 与 相关话题说明

选中action目录名,右键AddActionFile,这里定义类似定时器的动作,格式与service类似,分为goal、result、feedback三段,同样由‘---’分隔,文件Timer.action内容:

#the goal
duration time_to_wait  # Specify which dishwasher we want to use
---
#the result
duration timer_elapsed
int32 updates_sent
---
# the feedback message 
duration timer_elapsed
duration timer_remaining

为了帮助理解,我这里先把动作有关的测试结果展示出来,添加并启动动作服务端simple_action_server与客户端程序simple_action_client(后文有详细介绍)以后,新出现了timer动作的五个话题:

wsc@wsc-pc:~/ros1_ws$ rostopic list
/rosout
/rosout_agg
/timer/cancel
/timer/feedback
/timer/goal
/timer/result

其中goal、cancer消息 ,由client端发布,server端订阅,而result、feedback消息 ,由server端发布,client端订阅,话题的详细信息如下:

wsc@wsc-pc:~/ros1_ws$ rostopic  info /timer/goal
Type: ros_dev_test/TimerActionGoal

Publishers:
 * /simple_action_client (http://wsc-pc:35747/)

Subscribers:
 * /simple_action_server (http://wsc-pc:41467/)

wsc@wsc-pc:~/ros1_ws$ rostopic  info /timer/result
Type: ros_dev_test/TimerActionResult

Publishers:
 * /simple_action_server (http://wsc-pc:41467/)

Subscribers:
 * /simple_action_client (http://wsc-pc:34939/)

wsc@wsc-pc:~/ros1_ws$ rostopic  info /timer/feedback
Type: ros_dev_test/TimerActionFeedback

Publishers:
 * /simple_action_server (http://wsc-pc:41467/)

Subscribers:
 * /simple_action_client (http://wsc-pc:42565/)

wsc@wsc-pc:~/ros1_ws$ rostopic  info /timer/cancel
Type: actionlib_msgs/GoalID

Publishers:
 * /simple_action_client (http://wsc-pc:40621/)

Subscribers:
 * /simple_action_server (http://wsc-pc:41467/)

以/timer/goal为例,其消息类型为TimerActionGoal,我们来看看其详细信息:

wsc@wsc-pc:~/ros1_ws$ rosmsg  show  TimerActionGoal
[ros_dev_test/TimerActionGoal]:
std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
actionlib_msgs/GoalID goal_id
  time stamp
  string id
ros_dev_test/TimerGoal goal
  duration time_to_wait

可以看出其是一个组合消息,其中与我们自己定义相关的是ros_dev_test/TimerGoal,其详细信息如下:

wsc@wsc-pc:~/ros1_ws$ rosmsg  show  TimerGoal
[ros_dev_test/TimerGoal]:
duration time_to_wait

 

可以看出这个是和action文件的goal段对应可以来的,另外还有:

wsc@wsc-pc:~/ros1_ws$ rosmsg  show  TimerResult
[ros_dev_test/TimerResult]:
duration timer_elapsed
int32 updates_sent


wsc@wsc-pc:~/ros1_ws$ rosmsg  show  TimerFeedback
[ros_dev_test/TimerFeedback]:
duration timer_elapsed
duration timer_remaining

以上的 TimerAction、TimerActionGoal、TimerActionFeedback、TimerActionResult、TimerGoal、TimerFeedback、TimerResult在加入action动作文件后由ROS编译系统自动生成,同样,添加action文件需要对CMakeLists.txt、package.xml 进行的修改RoboWare Studio也替我们自动完成了,CMakeLists.txt主要修改了以下几处:

2、实现动作server端

代码及注释如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
import time

#actionlib包含了了SimpleActionServer
import actionlib

# 导入action文件自动生成的消息类
from ros_dev_test.msg import TimerAction,TimerGoal,TimerResult

#goal处理函数并返回处理结果
def do_timer(goal):
    start_time=time.time()
    time.sleep(goal.time_to_wait.to_sec())
    result=TimerResult()
    result.timer_elapsed=rospy.Duration.from_sec(time.time()-start_time)
    result.updates_sent=0
    action_server.set_succeeded(result)


rospy.init_node('simple_action_server')
action_server=actionlib.SimpleActionServer('timer',TimerAction,do_timer,False)
action_server.start()
rospy.spin()

注意运行前先要通过chmod u+x  命令添加运行权限。

3、client端使用动作

使用服务的client端代码及注释如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
import time

#actionlib包含了了SimpleActionServer
import actionlib

# 导入action文件自动生成的消息类
from ros_dev_test.msg import TimerAction,TimerGoal,TimerResult


rospy.init_node('simple_action_client')
action_client=actionlib.SimpleActionClient('timer',TimerAction)

#等待服务启动
action_client.wait_for_server()

#发送goal消息
goal=TimerGoal()
goal.time_to_wait=rospy.Duration.from_sec(5.0)
action_client.send_goal(goal)

action_client.wait_for_result()
print('Time elapsed: %f' %(action_client.get_result().timer_elapsed.to_sec()))

注意运行前先要通过chmod u+x  命令添加运行权限。

3、完整一点的server端与client端代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
import time

#actionlib包含了了SimpleActionServer
import actionlib

# 导入action文件自动生成的消息类
from ros_dev_test.msg import TimerAction,TimerGoal,TimerResult,TimerFeedback

#goal处理函数并返回处理结果
def do_timer(goal):
    start_time=time.time()
    update_count=0
    #异常处理
    if goal.time_to_wait.to_sec()>60.0:
        result=TimerResult()
        result.timer_elapsed=rospy.Duration.from_sec(time.time()-start_time)
        result.updates_sent=update_count
        #动作异常退出
        action_server.set_aborted(result,'Timer aborted due to too-long wait')
        return
    
    while((time.time()-start_time)<goal.time_to_wait.to_sec()):
        
        #判断是否发生中断,比如收到新的目标或者客户端取消目标等
        if action_server.is_preempt_requested():
            result=TimerResult()
            result.timer_elapsed=rospy.Duration.from_sec(time.time()-start_time)
            result.updates_sent=update_count
            action_server.set_preempted(result,'Timer preempted')
            return

        feedback=TimerFeedback()
        feedback.timer_elapsed=rospy.Duration.from_sec(time.time()-start_time)
        feedback.timer_remaining=goal.time_to_wait-feedback.timer_elapsed
        action_server.publish_feedback(feedback)
        update_count+=1
        time.sleep(1)

    result=TimerResult()
    result.timer_elapsed=rospy.Duration.from_sec(time.time()-start_time)
    result.updates_sent=update_count
    action_server.set_succeeded(result,'Timer completed successfully')    



rospy.init_node('simple_action_server')
action_server=actionlib.SimpleActionServer('timer',TimerAction,do_timer,False)
action_server.start()
print 'start server'
rospy.spin()
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
import time

#actionlib包含了了SimpleActionServer
import actionlib

# 导入action文件自动生成的消息类
from ros_dev_test.msg import TimerAction,TimerGoal,TimerResult,TimerFeedback

def feedback_callback(feedback):
    print('[feedback] timer_elapsed %f'% (feedback.timer_elapsed.to_sec()))
    print('[feedback] timer_remaining %f'% (feedback.timer_remaining.to_sec()))
    
rospy.init_node('simple_action_client')
action_client=actionlib.SimpleActionClient('timer',TimerAction)

#等待服务启动
action_client.wait_for_server()

#发送goal消息
goal=TimerGoal()

goal.time_to_wait=rospy.Duration.from_sec(5.0)
action_client.send_goal(goal,feedback_cb=feedback_callback)

#测试时间超长异常
# goal.time_to_wait=rospy.Duration.from_sec(70.0)
# action_client.send_goal(goal)

#测试中途取消终止目标
# time.sleep(3.0)
# action_client.cancel_goal()


action_client.wait_for_result()
print('[Result] state: %d' %(action_client.get_state()))
print('[Result] status: %s' %(action_client.get_goal_status_text()))

print('[Result] Updates_sent: %f' %(action_client.get_result().updates_sent))
print('[Result] Time elapsed: %f' %(action_client.get_result().timer_elapsed.to_sec()))

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ROS开发笔记(3):基于RoboWare Studio 与Python编写动作(action)通讯节点(node) 的相关文章

  • docker命令中的/bin/bash

    docker run i t tomcat bin bash 中的 bin bash的作用是因为docker后台必须运行一个进程 xff0c 否则容器就会退出 xff0c 在这里表示启动容器后启动bash
  • 如何将文件从本机上传到docker容器

    1 如何从docker容器中下载文件 xff1a docker span class token function cp span container created path span class token operator lt sp
  • libjpeg.so.8: cannot open shared object file: No such file or directory.

    在docker容器里执行carla的PythonAPI报错 xff1a libjpeg so 8 cannot span class token function open span shared object file No such s
  • Docker容器图形界面显示的配置方法

    参考博客 0 环境说明 Ubuntu 16 04 docker 19 03 12 因为要在docker中用pygame xff0c 要用到显示器 xff0c 这个时候需要解决这个Docker 可视化 的问题 原理简介 原理上可以把docke
  • 在docker中运行carla

    参考carla文档 ubuntu18 04 carla0 9 9 docker19 03 12 Docker Installation Docker CE For our tests we used the Docker CE versio
  • Pyglet设置窗口标题

    Pyglet教程 Caption The window s caption appears in its title bar and task bar icon on Windows and some Linux window manage
  • Linux 解决远程连接的 “Gtk-WARNING **: cannot open display;”

    转发 ssh Y username 64 ip 使用 Y 参数实际上是授权了 X11 转发 xff0c 这样就可以看到来自远端的 gtk 图形窗口了 span class token function man span span class
  • android jni调试 - 堆栈分析

    一 环境 xff1a windows rk3399 android 7 1 二 奔溃信息 pid 13544 tid 13639 name no localmeeting gt gt gt com sino localmeeting lt
  • CFileDialog的使用[转]

    由于项目需要 xff0c 查阅了一下CFileDialog类 xff0c 以满足程序自动读取配置文件的需求 xff0c 现在小小记录一下 xff01 CFileDialog类封装了Windows常用的文件对话框 xff0c 提供个一种简单的
  • 记录一下c/c++的几种计时方式

    include lt iostream gt include lt string gt include lt chrono gt void Run for int i 61 0 i lt 10000000 i 43 43 void time
  • [转载] 强化学习开源框架整理

    转载 https zhuanlan zhihu com p 582396276 本篇主要是介绍了不同的 RL 开源工作 xff0c 包括环境开源工作和算法开源工作 xff0c 同时关注这些开源工作对于多机多卡并行分布式训练的支持 算法框架
  • tf模型在C++部署

    tensorflow训练好的模型使用ONNX Runtime在C 43 43 部署 tf模型转onnx使用tf2onnx 在前面的文章有讲到c 43 43 调用tf keras的模型 环境 ubuntu20 04cuda 11 6cudnn
  • Google Breakpad 之一,跨平台crash 处理上报系统简介

    C C 43 43 程序最棘手的时候就是一个字 挂 xff0c 总是经常和不经常的挂掉 xff0c 尤其是那些线上的不经常挂的情况 xff0c 光看日志定位问题真的很难 为解决C挂挂的问题 xff0c 有必要提供一个跨平台的crash处理系
  • ubuntu14.04更换内核为3.14

    查看ubuntu14 04支持的内核版本的命令 xff1a atp cache showpkg linux headers 现在Ubuntu14 04安装完成后为4 4 0的内核 xff0c 若要降低内核版本 xff0c 操作方法如下 xf
  • 回首2013,这一年的坚持

    2013年 xff0c 眨眼间已悄悄流逝 这一年回味起来 xff0c 总是那么美 xff0c 充满快乐 xff0c 令人陶醉 一 生活篇 2013年对我来说 xff0c 是快乐的一年 在这一年里 xff0c 遇见了很多美丽的景色 xff0c
  • 使用Cropper进行图片剪裁上传

    在项目中 xff0c 需要多上传的图片按照比例和尺寸进行裁剪 xff0c 这类场景在一些CMS系统中是比较常见的 xff0c 尤其是大部分的文章现在要适配PC Mobile两种平台 xff0c 文章的封面图等便需要按照尺寸做裁剪 xff0c
  • Element-UI消息提示组件Message在Vuex中的调用实现

    在最近的项目开发中 xff0c 前端部分使用 Vue 开发 xff0c 整个页面基于 Element UI 实现 由于是单页面多组件应用 xff0c 使用了 Vuex 做状态管理 为了页面交互的友好和风格的统一 xff0c 消息提醒使用 E
  • Fork原项目新增分支的同步和推送

    在 Github 或者 Gitlab Fork 项目以后 xff0c 原项目增加了新的分支 xff0c 我们可以通过以下流程将分支同步下来 本项目前提假设我们设置了 remote 的名称为 up 与源项目关联 git remote v or
  • Elasticsearch文档版本冲突原理与解决

    一般我们在更新文档时 xff0c 主要的操作流程时 xff1a 读取文档 gt 修改 gt 提交保存 数据中心等保存的都是最新一次提交的内容 大部分时候 xff0c 这都没有什么问题 但是如果两个或更多的请求同时修改一个文档时 xff0c
  • Protobuf3 使用笔记

    一 和protobuf2比 xff0c 更新的内容 xff1a 1 字段前取消了required和optional两个关键字 xff0c 目前可用的只有repeated关键字 2 不可以现设置默认值了 a string默认为空串 b 枚举默

随机推荐

  • 随笔

    沟通 跨部门沟通 xff0c 首先应确立沟通的目的是双方的有效配合和问题解决的方向 方法 xff0c 去除程序员们自带的问题责任论 xff0c 不能按照 不是我的问题 xff0c 我这OK xff0c 跟我无关 这样的思路去讨论和解决问题
  • Linux内存消耗

    原文 xff1a https web archive org web 20120520221529 http emilics com blog article mconsumption html 本文主要描述如何通过一个合理的方法来测量li
  • Linux安装Docker完整详细教程

    目录 Docker及系统版本 Docker的自动化安装 Docker的手动安装 xff08 CentOS7 xff09 1 1 卸载历史版本的Docker 1 2 安装依赖包 1 3 更新本地镜像源 也可以叫做 xff1a 设置源仓库 1
  • 匿名飞控笔记(一)

    四轴飞行器的控制原理 四轴飞行器的结构 xff08 待补 xff09 四轴飞行器的运动控制方法 xff08 待补 xff09 四轴飞行器各部分工作原理 飞行姿态与升力关系 1 绕y轴旋转 角度 2 绕x轴旋转 角度 3 绕z轴旋转 角度 飞
  • 匿名飞控笔记(三)

    姿态解算 四元数表示方向余弦矩阵误差的求解PI误差补偿四元数的求解欧拉角的求解 以下参考 xff1a https blog csdn net zhiyu buliang article details 88936541 1 明确一个概念 x
  • Eigen初始化及基本操作大全

    Eigen常用操作 Eigen3安装 ubuntu如何安装Eigen include directories span class token punctuation span span class token operator span
  • 关于c++多文件编程中遇到class has no member named

    问题描述是关于自己已经在类中定义了该函数或者变量但是却无法识别 这是为什么 xff1f 也许你在使用c 43 43 的时候会出现问题 has no member named 意思就是类没有成员变量XXA 实际上类是具有成员变量XX的 问题的
  • VSCode C++ :ERROR: Unable to start debugging. GDB exited unexpectedly.

    问题描述 在VSCode中运行C 43 43 程序时 xff0c 出现了报错ERROR Unable to start debugging GDB exited unexpectedly 或者Unexpected GDB output fr
  • VMware 克隆centos7虚拟机

    由于要创建多台Linux虚拟机来搭建K8s集群 xff0c 一台一台的安装非常的费时 所以记录如何通过克隆的方式来快速克隆多台虚拟机 一 首先克隆的虚拟机状态必须为关机 二 克隆完成之后 xff0c 选中新克隆的虚拟机右击 xff0c 点击
  • MySql 修改 root 密码

    整理了以下四种在MySQL中修改root密码的方法 可能对大家有所帮助 方法1 xff1a 用SET PASSWORD命令 mysql u root mysql gt SET PASSWORD FOR 39 root 39 64 39 lo
  • Ubuntu16.04:安装python3.6及遗留问题解决

    文章目录 Ubuntu16 04 xff1a 安装python3 6引发问题 xff1a 打开terminal没反应安装pip和python3 6 devpip install加速 Ubuntu16 04 xff1a 安装python3 6
  • 【论文阅读】【综述】3D Object Detection 3D目标检测综述

    目录 写在开头3D Object Detection相关博客 xff1a Sliding windowVote3Deep Fast Object Detection in 3D Point Clouds Using Efficien xff
  • OpenStack历史知识

    翻译自 xff1a https docs openstack org project team guide introduction html OpenStack历史 起源 OpenStack项目在2010年前几个月创建 Rackspace
  • 元学习——meta-learning

    前言 称不上多熟悉这个领域 xff0c 毕竟还没有系统调研 最近读了两篇论文 xff0c 权当总结 学习优化器 简介 在这种方法中 xff0c 一个网络 xff08 元学习器meta learner xff09 学习更新另一个网络 xff0
  • PHP下载docx文档打开时显示 word2007无法打开Office OpenXML 文件

    症状 我自己的PHP xff0c 把word文档存入数据库后 xff0c 再从数据库下载成docx文件 xff1b 当打开该文件时 Word2007 xff0c 出现错误提示 xff1a 无法打开Office OpenXML 文件 文件名
  • 认真聊聊中断(软中断)

    文章目录 软中断与硬中断很像软中断的守护进程注册软中断向量表触发一次软中断总结 前面认认真真聊聊中断 xff0c 其实讲的都是硬中断 xff0c 注意是硬中断不是硬件中断哦 xff0c 硬中断的概念更大 硬中断包括中断 异常以及 INT 指
  • Pycharm 调用sklearn时出现Process finished with exit code -1066598274 (0xC06D007E)

    问题描述 跑代码的时候发现总是莫名奇妙的中断程序 xff0c 出现代码 Process finished with exit code 1066598274 0xC06D007E xff0c 网上搜半天也能用的解决方案 xff0c 后面逐句
  • 如何用C语言写一个服务器和客户端(TCP)

    如果想要自己写一个服务器和客户端 xff0c 我们需要掌握一定的网络编程技术 xff0c 个人认为 xff0c 网络编程中最关键的就是这个东西 socket 套接字 socket 套接字 xff1a socket 的原意是 插座 xff0c
  • CSS三种样式

    CSS样式分为 xff1a 内联式css样式 嵌入式css样式 外部式css样式 1 内联式css样式 内联式css样式表就是把css代码直接写在现有的HTML标签中 lt p style 61 34 color 000099 34 gt
  • ROS开发笔记(3):基于RoboWare Studio 与Python编写动作(action)通讯节点(node)

    ROS的动作非常适合时间不确定 xff0c 目标导向型的操作接口 原理上用话题实现 xff0c 其本质是相关于规定了一系列话题 xff08 目标 结果 反馈 取消等 xff09 的组合使用方法的高层协议 1 定义动作 与 相关话题说明 选中