!REDIRECT “https://docs.px4.io/master/zh/middleware/mavlink.html

MAVLink 通讯

MAVLink 是一个针对无人机生态系统设计的非常轻量化的消息传递协议。

PX4 使用 MAVLink 实现与 QGroundControl (或者其它地面站软件)的通讯交流,同时也将其用于整合飞控板与飞控板之外的无人机部件:伴随计算机、支持 MAVLink 的摄像头等。

该协议定义了许多用于交换数据的标准 消息微型服务(microservices)(PX4 中用到了许多消息/服务,但不是全部)。

本教程介绍了如何为你自己新 “自定义” 的报文添加 PX4 支持。

Note 本教程假定你在 msg/ca_trajectory.msg 文件中定义了一个名为 ca_trajectory自定义 uORB 消息,以及在 mavlink/include/mavlink/v2.0/custom_messages/mavlink_msg_ca_trajectory.h 文件中定义了一个名为 ca_trajectory的 自定义 MAVLink 消息。

创建自定义 MAVLink 消息

MAVlink 开发者指南介绍了如何定义新的消息并将其构建成指定的编程语言的库文件:

你需要为你的消息生成适用于 MAVLink 2 的 C 语言库文件。 只要你 安装好了 MAVLink ,你可以使用如下命令执行此操作:

  1. python -m pymavlink.tools.mavgen --lang=C --wire-protocol=2.0 --output=generated/include/mavlink/v2.0 message_definitions/v1.0/custom_messages.xml

For your own use/testing you can just copy the generated headers into PX4-Autopilot/mavlink/include/mavlink/v2.0.

如果想让其他人可以更简单地测试你的修改,更好的做法则是将你生成的头文件加入 https://github.com/mavlink/c_library_v2 的一个分支中, PX4 developers can then update the submodule to your fork in the PX4-Autopilot repo before building.

发送自定义MAVLink消息

此章节旨在说明:如何使用一条自定义uORB消息,并将其作为一条MAVLink消息发送出去。

Add the headers of the MAVLink and uORB messages to mavlink_messages.cpp

  1. #include <uORB/topics/ca_trajectory.h>
  2. #include <v2.0/custom_messages/mavlink.h>

Create a new class in mavlink_messages.cpp

  1. class MavlinkStreamCaTrajectory : public MavlinkStream
  2. {
  3. public:
  4. const char *get_name() const
  5. {
  6. return MavlinkStreamCaTrajectory::get_name_static();
  7. }
  8. static const char *get_name_static()
  9. {
  10. return "CA_TRAJECTORY";
  11. }
  12. static uint16_t get_id_static()
  13. {
  14. return MAVLINK_MSG_ID_CA_TRAJECTORY;
  15. }
  16. uint16_t get_id()
  17. {
  18. return get_id_static();
  19. }
  20. static MavlinkStream *new_instance(Mavlink *mavlink)
  21. {
  22. return new MavlinkStreamCaTrajectory(mavlink);
  23. }
  24. unsigned get_size()
  25. {
  26. return MAVLINK_MSG_ID_CA_TRAJECTORY_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES;
  27. }
  28. private:
  29. MavlinkOrbSubscription *_sub;
  30. uint64_t _ca_traj_time;
  31. /* do not allow top copying this class */
  32. MavlinkStreamCaTrajectory(MavlinkStreamCaTrajectory &);
  33. MavlinkStreamCaTrajectory& operator = (const MavlinkStreamCaTrajectory &);
  34. protected:
  35. explicit MavlinkStreamCaTrajectory(Mavlink *mavlink) : MavlinkStream(mavlink),
  36. _sub(_mavlink->add_orb_subscription(ORB_ID(ca_trajectory))), // make sure you enter the name of your uORB topic here
  37. _ca_traj_time(0)
  38. {}
  39. bool send(const hrt_abstime t)
  40. {
  41. struct ca_traj_struct_s _ca_trajectory; //make sure ca_traj_struct_s is the definition of your uORB topic
  42. if (_sub->update(&_ca_traj_time, &_ca_trajectory)) {
  43. mavlink_ca_trajectory_t _msg_ca_trajectory; //make sure mavlink_ca_trajectory_t is the definition of your custom MAVLink message
  44. _msg_ca_trajectory.timestamp = _ca_trajectory.timestamp;
  45. _msg_ca_trajectory.time_start_usec = _ca_trajectory.time_start_usec;
  46. _msg_ca_trajectory.time_stop_usec = _ca_trajectory.time_stop_usec;
  47. _msg_ca_trajectory.coefficients =_ca_trajectory.coefficients;
  48. _msg_ca_trajectory.seq_id = _ca_trajectory.seq_id;
  49. mavlink_msg_ca_trajectory_send_struct(_mavlink->get_channel(), &_msg_ca_trajectory)
  50. }
  51. return true;
  52. }
  53. };

Finally append the stream class to the streams_list at the bottom of mavlink_messages.cpp

  1. StreamListItem *streams_list[] = {
  2. ...
  3. new StreamListItem(&MavlinkStreamCaTrajectory::new_instance, &MavlinkStreamCaTrajectory::get_name_static, &MavlinkStreamCaTrajectory::get_id_static),
  4. nullptr
  5. };

Then make sure to enable the stream, for example by adding the following line to the startup script (e.g. /ROMFS/px4fmu_common/init.d-posix/rcS on NuttX or ROMFS/px4fmu_common/init.d-posix/rcS) on SITL. Note that -r configures the streaming rate and -u identifies the MAVLink channel on UDP port 14556).

  1. mavlink stream -r 50 -s CA_TRAJECTORY -u 14556

Tip 你可以使用 uorb top [&lt;message_name&gt;] 命令来实时验证你的消息是否被发布及消息的发布频率(详情请参阅: uORB Messaging)。 这个方法还可以用来测试发布 uORB 主题的传入消息(对于其它类型的消息你可以在代码中使用 printf 然后再 SITL 仿真中进行测试)。

要在 QGroundControl 中查看自定义消息,你需要 使用你自己的 MAVLink 库重新构建该消息,然后使用 MAVLink Inspector Widget (或者其它 MAVLink 工具) 验证是否收到该消息。

接收自定义MAVLink消息

此章节旨在说明:如何接收一条MAVLink消息,并将其发布至uORB。

Add a function that handles the incoming MAVLink message in mavlink_receiver.h

  1. #include <uORB/topics/ca_trajectory.h>
  2. #include <v2.0/custom_messages/mavlink_msg_ca_trajectory.h>

Add a function that handles the incoming MAVLink message in the MavlinkReceiver class in mavlink_receiver.h

  1. void handle_message_ca_trajectory_msg(mavlink_message_t *msg);

Add an uORB publisher in the MavlinkReceiver class in mavlink_receiver.h

  1. orb_advert_t _ca_traj_msg_pub;

Implement the handle_message_ca_trajectory_msg function in mavlink_receiver.cpp

  1. void MavlinkReceiver::handle_message_ca_trajectory_msg(mavlink_message_t *msg)
  2. {
  3. mavlink_ca_trajectory_t traj;
  4. mavlink_msg_ca_trajectory_decode(msg, &traj);
  5. struct ca_traj_struct_s f;
  6. memset(&f, 0, sizeof(f));
  7. f.timestamp = hrt_absolute_time();
  8. f.seq_id = traj.seq_id;
  9. f.time_start_usec = traj.time_start_usec;
  10. f.time_stop_usec = traj.time_stop_usec;
  11. for(int i=0;i<28;i++)
  12. f.coefficients[i] = traj.coefficients[i];
  13. if (_ca_traj_msg_pub == nullptr) {
  14. _ca_traj_msg_pub = orb_advertise(ORB_ID(ca_trajectory), &f);
  15. } else {
  16. orb_publish(ORB_ID(ca_trajectory), _ca_traj_msg_pub, &f);
  17. }
  18. }

and finally make sure it is called in MavlinkReceiver::handle_message()

  1. MavlinkReceiver::handle_message(mavlink_message_t *msg)
  2. {
  3. switch (msg->msgid) {
  4. ...
  5. case MAVLINK_MSG_ID_CA_TRAJECTORY:
  6. handle_message_ca_trajectory_msg(msg);
  7. break;
  8. ...
  9. }

创建自定义MAVlink消息的备选方法

有时候需要创建一个内容尚未完全定义的自定义 MAVlink 消息。

例如,当使用 MAVlink 构建一个嵌入式设备与 PX4 的通讯接口时,设备与自动驾驶仪之间交换的消息的内容可能需要经过数次迭代之后才能稳定下来不发生变化。 在这种情况下重新生成 MAVlink 头文件并保证两个设备均使用同版本的协议会非常耗时且容易出错。

一个备选 - 也是临时的- 解决方案是重新使用(re-purpose)调试消息(debug messages)。 你可以发送一个以 CA_TRAJ 为字符串键,在x, yz 字段中存放数据的 DEBUG_VECT 消息,而不需要创建一个自定义 MAVLink 消息 CA_TRAJECTORY 。 参阅 这篇教程 以获取调试信息的更详细的使用方法。

Note 此解决方案由于通过网络发送字符串并涉及字符串的比较,所以效率并不高。 此方法应仅用于开发!

常规信息

设置流速率(streaming rate)

有时候提高单个主题的流速率非常有用(例如在 QGC 中进行检查时)。 这可以通过在 shell 中输入如下命令实现:

  1. mavlink stream -u <port number> -s <mavlink topic name> -r <rate>

你可以使用 mavlink status 来获取上述命令中的端口号,该指令的输出结果会包含 transport protocol: UDP (&lt;port number&gt;) 。 一个例子是:

  1. mavlink stream -u 14556 -s OPTICAL_FLOW_RAD -r 300