原文: https://mavlink.io/en/mavgen_c/
Using C MAVLink Libraries (mavgen) - C MAVLinkライブラリ (mavgen)の使い方
The MAVLink C library generated by mavgen is a header-only implementation that is highly optimized for resource-constrained systems with limited RAM and flash memory. It is field-proven and deployed in many products where it serves as interoperability interface between components of different manufacturers.
mavgenによって生成されたMAVLink Cライブラリは、RAMとフラッシュメモリが限られているリソース制約のあるシステム用に高度に最適化されたヘッダのみの実装です。 さまざまなメーカーのコンポーネント間の相互運用性インターフェースとして機能する多くの製品で、現場で実証され、展開されています。
This topic explains how to get and use the library.
このトピックでは、ライブラリの入手方法と使用方法について説明します。
Getting Libraries - ライブラリの入手
If you are using a standard dialect then download the MAVLink 2 library from Github: c_library_v2.
標準の方言を使っているなら、GithubからMAVLink 2ライブラリをダウンロードしてください: c_library_v2
The MAVLink 2 library supports both MAVLink 2 and MAVLink 1, and has been built with all standard dialects in the mavlink/mavlink repo. It supersedes the MAVLink 1 library (c_library_v1), and should be used by preference.
MAVLink 2ライブラリは、MAVLink 2とMAVLink 1の両方をサポートしており、mavlink/mavlinkリポジトリのすべての標準的な方言で構築されています。これはMAVLink 1ライブラリ(c_library_v1)に取って代わり、優先的に使われるべきです。
If you need libraries for a custom dialect then you will need to install mavgen and generate them yourself. These can then be used in the same way as the pre-generated libraries.
カスタムの方言用のライブラリが必要な場合は、mavgenをインストールして自分で生成する必要があります。 これらは事前に生成されたライブラリと同じ方法で使用できます。
The libraries can be placed/generated anywhere in your project tree. The example below shows them located in: generated/include/mavlink/v2.0/.
ライブラリはプロジェクトツリーのどこにでも配置/生成できます。 以下の例では、generated/include/mavlink/v2.0/
にあります。
Upgrading Library from MAVLink 1 - MAVLink 1 からのアップグレード
※略
Adding Libraries - ライブラリの追加
To use MAVLink in your C project, include the mavlink.h header file for your dialect:
CプロジェクトでMAVLinkを使用するには、次のようにして方言用のmavlink.hヘッダファイルをインクルードします:
# include <your_dialect/mavlink.h>
これにより、方言内のすべてのメッセージ、およびそれに含まれる方言ファイルのヘッダーファイルが自動的に追加されます。
If you support multiple independent dialects you can include these separately.
複数の独立した方言をサポートしている場合は、次のようにして、それらを別々に含めることができます。
# include <common/mavlink.h>
# include <your_dialect/mavlink.h>
# include <another_dialect/mavlink.h>
Avoid including header files of dependent dialects (those included by your dialect). If you include the headers in the wrong order the enums/messages of the parent dialect may not be available to your code.
依存している方言(あなたの方言に含まれているもの)のヘッダファイルを含めることを避けてください。間違った順序でヘッダを含めると、親方言のenum/messagesはあなたのコードで利用できないかもしれません。
Do not include the individual message files. If you generate your own headers, you will have to add their output location to your C compiler's search path.
個々のメッセージファイルを含めないでください。 あなたがあなた自身のヘッダを生成するならば、Cコンパイラの検索パスにそれらの出力位置を追加しなければならないでしょう。
When compiling the project, we recommend that you specify the top-level output directory AND all generated dialects and versions (this will give the greatest compatibility with existing code and examples):
プロジェクトをコンパイルするときは、最上位の出力ディレクトリと生成されたすべての方言およびバージョンを指定することをお勧めします(これにより、既存のコードとサンプルコードとの最大の互換性が得られます)。
$ gcc ... -I generated/include -I generated/include/common ...
Multiple Streams ("channels") - 複数のストリーム(チャンネル)
The C MAVLink library utilizes a "channel" metaphor to allow for simultaneous processing of multiple, independent MAVLink streams in the same program. It is therefore important to use the correct channel for each operation as all receiving and transmitting functions provided by MAVLink require a channel.
C MAVLinkライブラリは、同じプログラム内で複数の独立したMAVLinkストリームを同時に処理することを可能にするために「チャンネル」メタファを利用します。したがって、MAVLinkによって提供されるすべての受信および送信機能はチャネルを必要とするため、各操作に正しいチャネルを使用することが重要です。
If only one MAVLink stream exists, channel 0 should be used by specifying the
MAVLINK_COMM_0
constant.
MAVLinkストリームが1つしか存在しない場合は、MAVLINK_COMM_0
定数を指定してチャネル0を使用します。
Receiving - 受信
MAVLink reception is then done using
mavlink_helpers.h:mavlink_parse_char()
.
MAVLinkの受信は mavlink_helpers.h:mavlink_parse_char()
関数を使って行われます。
Transmitting - 送信
Transmitting messages can be done by using the
mavlink_msg_*_pack()
function, where one is defined for every message. The packed message can then be serialized withmavlink_helpers.h:mavlink_msg_to_send_buffer()
and then writing the resultant byte array out over the appropriate serial interface.
メッセージを送信するには、mavlink_msg_*_pack()
関数を使用します。この場合、メッセージごとにメッセージが定義されます。 パックされたメッセージは、mavlink_helpers.h:mavlink_msg_to_send_buffer()でシリアル化してから、適切なシリアルインタフェースを介して結果のバイト配列を書き出すことができます。
It is possible to simplify the above by writing wrappers around the transmitting/receiving code. A multi-byte writing macro can be defined,
MAVLINK_SEND_UART_BYTES()
, or a single-byte function can be defined,comm_send_ch()
, that wrap the low-level driver for transmitting the data. If this is done,MAVLINK_USE_CONVENIENCE_FUNCTIONS
must be defined.
送受信コードの周りにラッパーを書くことで上記を単純化することが可能です。マルチバイト書き込みマクロをMAVLINK_SEND_UART_BYTES()
として定義するか、またはデータを転送するための低レベルドライバをラップするシングルバイト関数をcomm_send_ch()
として定義することができます。これが行われた場合、MAVLINK_USE_CONVENIENCE_FUNCTIONS
を定義しなければなりません。
Message Signing - メッセージの署名
Message Signing (authentication) allows a MAVLink 2 system to verify that messages originate from a trusted source.
メッセージ署名(認証)を使用すると、MAVLink 2システムはメッセージが信頼できる送信元から送信されたものであることを確認できます。
The C libraries generated by mavgen provide almost everything needed to support message signing in your MAVLink system. The topic C Message Signing explains the remaining code that a system must implement to enable signing using the C library.
mavgenによって生成されたCライブラリは、あなたのMAVLinkシステムでメッセージ署名をサポートするのに必要なほとんどすべてを提供します。 トピック「Cメッセージ署名」では、Cライブラリを使用して署名を有効にするためにシステムが実装する必要がある残りのコードについて説明します。
Working with MAVLink 1
※略