全网唯一OpenCyphal/UAVCAN教程DSDL文件的编译

Posted 奇妙之二进制

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了全网唯一OpenCyphal/UAVCAN教程DSDL文件的编译相关的知识,希望对你有一定的参考价值。

如何将Cyphal的dsdl文件编译成C语言源文件呢?

需要用到官方提供的一个python工具nunavut。
https://github.com/OpenCyphal/nunavut

下面介绍ubuntu 22.04下如何使用,其他平台自行摸索。

1、两步完成安装

$  sudo apt install python3-pip
$  pip install -U nunavut

2、准备好DSDL

https://github.com/OpenCyphal/public_regulated_data_types这个仓库存放了Cyphal官方定义的DSDL标准数据类型,我们自己的DSDL最好是基于官方标准数据类型之上设计,因为官方的数据类型比较好用,而且经过了严格设计。

下载:

$ git clone https://github.com/OpenCyphal/public_regulated_data_types

3、编译

先编译官方标准数据类型:

nnvg --target-language c --target-endianness=little --enable-serialization-asserts public_regulated_data_types/uavcan --outdir dsdl_compile

--outdir指定编译后的源文件存放目录。

看下生成的结果:

.
├── diagnostic
│   ├── Record_1_0.h
│   ├── Record_1_1.h
│   └── Severity_1_0.h
├── file
│   ├── Error_1_0.h
│   ├── GetInfo_0_1.h
│   ├── GetInfo_0_2.h
│   ├── List_0_1.h
│   ├── List_0_2.h
│   ├── Modify_1_0.h
│   ├── Modify_1_1.h
│   ├── Path_1_0.h
│   ├── Path_2_0.h
...

假设你自己的dsdl放在public_regulated_data_types/reg下,需要注意的是,加上 --lookup-dir public_regulated_data_types/uavcan,这个路径是官方标准dsdl的存放路径,这个有点类似头文件查找的意思,这样子我们才能在自己的dsdl文件里引用官方标准定义的数据类型。

$ nnvg --target-language c --target-endianness=little --enable-serialization-asserts public_regulated_data_types/reg --lookup-dir public_regulated_data_types/uavcan --outdir dsdl_compile

编译之后我们得到了一堆的头文件,我们随便看一条消息,时钟同步消息:

victory@ubuntu:~/test2/dsdl_compile$ cat ../public_regulated_data_types/uavcan/time/SynchronizedTimestamp.1.0.dsdl 
# Nested data type used for representing a network-wide synchronized timestamp with microsecond resolution.
# This data type is highly recommended for use both in standard and vendor-specific messages alike.

uint56 UNKNOWN = 0  # Zero means that the time is not known.

truncated uint56 microsecond
# The number of microseconds that have passed since some arbitrary moment in the past.
# The moment of origin (i.e., the time base) is defined per-application. The current time base in use
# can be requested from the time synchronization master, see the corresponding service definition.
#
# This value is to never overflow. The value is 56-bit wide because:
#
#   - 2^56 microseconds is about 2285 years, which is plenty. A 64-bit microsecond counter would be
#     unnecessarily wide and its overflow interval of 585 thousand years induces a mild existential crisis.
#
#   - Classic-CAN (not FD) transports carry up to 7 bytes of payload per frame.
#     Time sync messages shall use single-frame transfers, which means that the value can't be wider than 56 bits.

@sealed

生成了uavcan/time/Synchronization_1_0.h,主要内容:

typedef struct

    /// truncated uint56 previous_transmission_timestamp_microsecond
    uint64_t previous_transmission_timestamp_microsecond;
 uavcan_time_Synchronization_1_0;

static inline int8_t uavcan_time_Synchronization_1_0_serialize_(
    const uavcan_time_Synchronization_1_0* const obj, uint8_t* const buffer,  size_t* const inout_buffer_size_bytes)

static inline int8_t uavcan_time_Synchronization_1_0_deserialize_(
    uavcan_time_Synchronization_1_0* const out_obj, const uint8_t* buffer, size_t* const inout_buffer_size_bytes)

以上是关于全网唯一OpenCyphal/UAVCAN教程DSDL文件的编译的主要内容,如果未能解决你的问题,请参考以下文章

全网唯一OpenCyphal/UAVCAN教程libcanard介绍

全网唯一OpenCyphal/UAVCAN教程libcanard介绍

全网唯一OpenCyphal/UAVCAN教程(11)用candump和gawk工具写一个Cyphal协议解析小工具

全网唯一OpenCyphal/UAVCAN教程(11)用candump和gawk工具写一个Cyphal协议解析小工具

全网唯一UAVCAN中文教程(21)libcanard简介

全网唯一UAVCAN中文教程(23)libcanard源码分析