前言
- ROS程序与非ROS程序沟通
目录
- 参考
- 学习记录
参考
rosbridge_suit
rosbridge v2.0 Protocol Specification
WebSocket 特点与应用
LabVIEW TCP Write JSON
LabVIEW websocket client
client
rosbridge 原理及应用
学习记录
rosbridge_suit
- 这个工具包提供JSON API供非ROS程序使用,有很多前端可以使用,包括WebSocket Server。
- 任何环境,只要能够发送JSON协议数据且符合rosbridge传输协议
- 注意,JSON协议和TCP,HTTP不在一个层次上,JSON是一种数据规约,规定发送的数据应该具有何种格式,可以说,JSON处在OSI七层标准协议的表示层。
WebSocket Json API是啥
- websocket是一种在单个TCP连接上进行全双工通讯的协议。将套接字引入网络。
- 通俗的讲,就是通过浏览器运行程序,在B/S架构下与本地运行的软件进行通讯,完成套接字的相关功能。
rosbridge2.0协议规约
- rosbridge消息的传输层是JSON对象,仅仅需要的字段是’op’字段,表示消息的操作(operation),而整个协议就是’op’codes的集合,再加上每一个操作的语义。
传输层
- 一个消息就是一个JSON对象,类似于下面的键值对:
1 | {“op”: "Example" } |
- 请注意,依然能够提供任意的字符串键值对,如
1 | {"op": “Example” |
但是请注意,”id”键可能并没有任何释义,但是在服务端可以被设计为一个特定的含义。
协议规约主要分类
- 消息压缩和转换
- fragment - 分段消息的一个部分
- png - 图片分段消息的一个部分
- 状态消息
- set_status_level - 设置状态消息报告等级的一个请求消息
- status - 一个状态消息
- 确认消息
- auth - 确认或者授权客户端的连接
- ROS消息
ROS消息详细介绍
- 注册(Advertise)
3.4.1 Advertise ( advertise )
If you wish to advertise that you are or will be publishing a topic, then use the advertise command.
1 | { "op": "advertise", (optional) "id": <string>, "topic": <string>, "type": <string> } |
- topic – the string name of the topic being unadvertised
- If the topic does not exist, a warning status message is sent and this message is dropped
- If the topic exists and there are still clients left advertising it, rosbridge will continue to advertise it until all of them have unadvertised
- If the topic exists but rosbridge is not advertising it, a warning status message is sent and this message is dropped
3.4.3 Publish ( publish )
The publish message is used to send data on a topic.
1 | { "op": "publish", (optional) "id": <string>, "topic": <string>, "msg": <json> } |
The publish command publishes a message on a topic.
- topic - the string name of the topic to publish to
- msg - the message to publish on the topic
- If the topic does not exist, then an error status message is sent and this message is dropped
- If the msg does not conform to the type of the topic, then an error status message is sent and this message is dropped
- If the msg is a subset of the type of the topic, then a warning status message is sent and the unspecified fields are filled in with defaults Special case: if the type being published has a ‘header’ field, then the client can optionally omit the header from the msg. If this happens, rosbridge will automatically populate the header with a frame id of “” and the timestamp as the current time. Alternatively, just the timestamp field can be omitted, and then the current time will be automatically inserted.
3.4.4 Subscribe
1 | { "op": "subscribe", (optional) "id": <string>, "topic": <string>, (optional) "type": <string>, (optional) "throttle_rate": <int>, (optional) "queue_length": <int>, (optional) "fragment_size": <int>, (optional) "compression": <string> } |
- topic – the name of the topic to unsubscribe from
- id – an id of the subscription to unsubscribe If an id is provided, then only the corresponding subscription is unsubscribed. If no ID is provided, then all subscriptions are unsubscribed.
3.4.6 Call Service
1 | { "op": "call_service", (optional) "id": <string>, "service": <string>, (optional) "args": <list<json>>, (optional) "fragment_size": <int>, (optional) "compression": <string> } |
Calls a ROS service
- service – the name of the service to call
- args – if the service has no args, then args does not have to be provided, though an empty list is equally acceptable. Args should be a list of json objects representing the arguments to the service
- id – an optional id to distinguish this service call
- fragment_size – the maximum size that the response message can take before it is fragmented
- compression – an optional string to specify the compression scheme to be used on messages. Valid values are “none” and “png”
3.4.7 Advertise Service
1 | { "op": "advertise_service", "type": <string>, "service": <string> } |
Advertises an external ROS service server. Requests come to the client via Call Service.
- service – the name of the service to advertise
- type – the advertised service message type
3.4.8 Unadvertise Service
1 | { "op": "unadvertise_service", "service": <string> } |
Stops advertising an external ROS service server
- service – the name of the service to unadvertise
3.4.9 Service Response
1 | { "op": "service_response", (optional) "id": <string>, "service": <string>, (optional) "values": <list<json>>, "result": <boolean> } |
A response to a ROS service call
- service – the name of the service that was called
- values – the return values. If the service had no return values, then this field can be omitted (and will be by the rosbridge server)
- id – if an ID was provided to the service request, then the service response will contain the ID
- result - return value of service callback. true means success, false failure.
rosbridge server implementation
- 给出服务端的实现细节是为了方便修改协议
LabVIEW JSON TCP
- LabVIEW2014拥有“簇平化至JSON字符串”以及“从JSON恢复”vi函数,可以完成上述需求,我虽然也找到一些JSON插件,但是貌似没有使用的必要,都是比较老的库,新版本的LV已经包含了这一功能
rosbridge-suit使用
安装
1 | sudo apt-get install ros-indigo-rosbridge-suit |
运行tcp服务端
1 | rosrun rosbridge_server rosbridge_tcp |