mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-07-27 00:48:23 +08:00
Update the mavlink receiving example to match the current behavior from "b52464059e fix(mavlink): preserve invalid battery values on receive" patch. Signed-off-by: Onur Özkan <work@onurozkan.dev>
87 lines
3.6 KiB
Markdown
87 lines
3.6 KiB
Markdown
# Отримання повідомлень MAVLink
|
||
|
||
This topic explains how to receive a [MAVLink message](../middleware/mavlink.md) and publish it to uORB.
|
||
|
||
## Загальний огляд
|
||
|
||
The topic shows how we would handle a received `BATTERY_STATUS_DEMO` message (as defined in [Streaming MAVLink Messages](../mavlink/streaming_messages.md)) and then update the (existing) [BatteryStatus uORB message](../msg_docs/BatteryStatus.md) with the contained information.
|
||
|
||
Це той тип реалізації, який ви надаєте для підтримки інтеграції батареї MAVLink з PX4.
|
||
|
||
## Кроки
|
||
|
||
Add the headers for the uORB topic to publish to in [mavlink_receiver.h](https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/mavlink/mavlink_receiver.h#L77):
|
||
|
||
```cpp
|
||
#include <uORB/topics/battery_status.h>
|
||
```
|
||
|
||
Add a function signature for a function that handles the incoming MAVLink message in the `MavlinkReceiver` class in
|
||
[mavlink_receiver.h](https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/mavlink/mavlink_receiver.h#L126)
|
||
|
||
```cpp
|
||
void handle_message_battery_status_demo(mavlink_message_t *msg);
|
||
```
|
||
|
||
Normally you would add a uORB publisher for the uORB topic to publish in the `MavlinkReceiver` class in
|
||
[mavlink_receiver.h](https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/mavlink/mavlink_receiver.h#L296).
|
||
In this case the [BatteryStatus](../msg_docs/BatteryStatus.md) uORB topic already exists:
|
||
|
||
```cpp
|
||
uORB::Publication<battery_status_s> _battery_pub{ORB_ID(battery_status)};
|
||
```
|
||
|
||
This creates a publication to a single uORB topic instance, which by default will be the _first_ instance.
|
||
|
||
:::info
|
||
This implementation won't work on multi-battery systems, because several batteries might be publishing data to the first instance of the topic, and there is no way to differentiate them.
|
||
To support multiple batteries we'd need to use `PublicationMulti` and map the MAVLink message instance IDs to specific uORB topic instances.
|
||
:::
|
||
|
||
Implement the `handle_message_battery_status_demo` function in [mavlink_receiver.cpp](https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/mavlink/mavlink_receiver.cpp).
|
||
|
||
```cpp
|
||
void
|
||
MavlinkReceiver::handle_message_battery_status_demo(mavlink_message_t *msg)
|
||
{
|
||
if ((msg->sysid != mavlink_system.sysid) || (msg->compid == mavlink_system.compid)) {
|
||
// ignore battery status coming from other systems or from the autopilot itself
|
||
return;
|
||
}
|
||
|
||
// external battery measurements
|
||
mavlink_battery_status_t battery_mavlink;
|
||
mavlink_msg_battery_status_decode(msg, &battery_mavlink);
|
||
|
||
battery_status_s battery_status{};
|
||
battery_status.timestamp = hrt_absolute_time();
|
||
|
||
battery_status.remaining = (float)battery_mavlink.battery_remaining / 100.0f;
|
||
battery_status.temperature = (battery_mavlink.temperature == INT16_MAX) ?
|
||
NAN : (float)battery_mavlink.temperature / 100.0f;
|
||
battery_status.connected = true;
|
||
|
||
_battery_pub.publish(battery_status);
|
||
}
|
||
```
|
||
|
||
:::info
|
||
Above we only write to the battery fields that are defined in the topic.
|
||
На практиці ви оновлювали б всі поля або з дійсними, або з недійсними значеннями: це було скорочено для стислості.
|
||
:::
|
||
|
||
and finally make sure it is called in [MavlinkReceiver::handle_message()](https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/mavlink/mavlink_receiver.cpp#L228)
|
||
|
||
```cpp
|
||
MavlinkReceiver::handle_message(mavlink_message_t *msg)
|
||
{
|
||
switch (msg->msgid) {
|
||
...
|
||
case MAVLINK_MSG_ID_BATTERY_STATUS_DEMO:
|
||
handle_message_battery_status_demo(msg);
|
||
break;
|
||
...
|
||
}
|
||
}
|
||
```
|