Apollo Cyber RT Python API
1. 背景
Cyber 核心代码是由 C++ 开发,同时为了方便开发者,提供了 Python 接口。
2. CyberRT Python 接口实现思路
Cyber Python 接口的实现思路是在 Cyber C++ 实现的基础上,做了一层 Python 的封装,由 Python 来调用 C++ 的实现函数。Cyber
Python Wrapper 的实现没有使用 swig 等第三方工具,完全自主实现,以此保证代码的高可维护性和可读性。
3. 主要接口
目前提供的主要接口包括:
- channel 读、写
- server/client 通信
- record 信息查询
- record 文件读、写
- Time/Duration/Rate 时间操作
- Timer
3.1 Channel 读写接口
使用步骤是:
- 首先创建 Node;
- 创建对应的 reader 或 writer;
- 如果是向 channel 写数据,调用 writer 的 write 接口;
- 如果是从 channel 读数据,调用 node 的 spin,对收到的消息进行消费;
接口定义如下:
class Node:
"""
Class for cyber Node wrapper.
"""
def create_writer(self, name, data_type, qos_depth=1):
"""
create a topic writer for send message to topic.
@param self
@param name str: topic name
@param data_type proto: message class for serialization
"""
def create_reader(self, name, data_type, callback, args=None):
"""
create a topic reader for receive message from topic.
@param self
@param name str: topic name
@param data_type proto: message class for serialization
@callback fn: function to call (fn(data)) when data is
received. If args is set, the function must
accept the args as a second argument,
i.e. fn(data, args)
@args any: additional arguments to pass to the callback
"""
def create_client(self, name, request_data_type, response_data_type):
"""
"""
def create_service(self, name, req_data_type, res_data_type, callback, args=None):
def spin(self):
"""
spin in wait and process message.
@param self
"""
class Writer(object):
"""
Class for cyber writer wrapper.
"""
def write(self, data):
"""
writer msg string
"""
3.2 Record 接口
Record 读的操作是:
- 创建一个 RecordReader;
- 对 Record 进行迭代读;
Record 写的操作是:
- 创建一个 RecordWriter
- 对 Record 进行写消息;
接口定义如下:
class RecordReader(object):
"""
Class for cyber RecordReader wrapper.
"""
def read_messages(self, start_time=0, end_time=18446744073709551615):
"""
read message from bag file.
@param self
@param start_time:
@param end_time:
@return: generator of (message, data_type, timestamp)
"""
def get_messagenumber(self, channel_name):
"""
return message count.
"""
def get_messagetype(self, channel_name):
"""
return message type.
"""
def get_protodesc(self, channel_name):
"""
return message protodesc.
"""
def get_headerstring(self):
"""
return message header string.
"""
def reset(self

最低0.47元/天 解锁文章
1389

被折叠的 条评论
为什么被折叠?



