Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions docs/DroneCAN-Driver.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# DroneCAN Driver Documentation

**Last Updated:** 2026-08-21
**Status:** Complete for the driver core, param-getset, and node table; DNA server, node/battery ID filtering, and actuator control are separate in-flight branches not yet reflected here
**Branch:** feature/dronecan-param-getset
**Last Updated:** 2026-08-28
**Status:** Complete for the driver core, param-getset, node table, and DNA server; node/battery ID filtering and actuator control are separate in-flight branches not yet reflected here
**Branch:** feature/dronecan-dna-server

---

Expand Down Expand Up @@ -116,6 +116,7 @@ CAN Bus ──[CAN interrupt]──> canardSTM32Receive() ──> shouldAcceptTr
| `UAVCAN_EQUIPMENT_GNSS_RTCMSTREAM_ID` | `handle_GNSSRCTMStream()` | RTCM Stream | Broadcast | Variable | RTK correction data |
| `UAVCAN_EQUIPMENT_POWER_BATTERYINFO_ID` | `handle_BatteryInfo()` | Battery Info | Broadcast | 1-10 Hz | Integrated with battery system |
| `UAVCAN_PROTOCOL_GETNODEINFO_ID` | `handle_GetNodeInfo()` | GetNodeInfo | Request/Response | On demand | Responds with FC firmware version |
| `UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID` | `dronecanDnaHandleAllocation()` | Dynamic Node Allocation | Broadcast | On connect | Assigns node IDs to anonymous peripherals via 3-stage UID handshake |

### Handler-Based Architecture

Expand Down Expand Up @@ -289,6 +290,7 @@ The driver uses the following settings from `settings.yaml`:
|---|---|---|---|---|
| `dronecan_node_id` | This FC's CAN node ID. 126/127 reserved for diagnostic tools. | uint8 | 1-127 | 1 |
| `dronecan_bitrate_kbps` | CAN bus bitrate | enum (`dronecan_bitrate_table`) | 125/250/500/1000 | 1000 |
| `dronecan_use_dna_server` | Enable automatic node ID assignment | bool | ON/OFF | ON |

There is no separate enable/disable setting — DroneCAN support is a compile-time feature (`USE_DRONECAN`);
whether it's active at runtime is determined by whether the target was built with that flag.
Expand Down Expand Up @@ -663,6 +665,67 @@ unique ID; PARAM_GETSET: name + value + min/max; EXECUTE_OPCODE/RESTART_NODE: `o

---

## DNA Server

The DNA server (`dronecan_dna_server.c`) implements a non-redundant (single-master) Dynamic Node Allocation allocator per UAVCAN Specification §6.4.9. It is guarded by `dronecanConfig()->dronecanUseDNAServer` and can be disabled via `set dronecan_use_dna_server = OFF`.

### Three-stage handshake

A peripheral that has no node ID broadcasts anonymous Allocation messages carrying up to 6 bytes of its 16-byte hardware unique ID per stage:

| Stage | `first_part_of_unique_id` | Bytes sent |
|-------|--------------------------|------------|
| 1 | `true` | bytes 0–5 (+ optional preferred node ID) |
| 2 | `false` | bytes 6–11 |
| 3 | `false` | bytes 12–15 |

Each stage must arrive within 500 ms (`FOLLOWUP_TIMEOUT_MS`) of the previous one, or the accumulator resets and the peripheral must start over from Stage 1.

A transport capable of a larger single frame (e.g. CAN-FD) may instead send the full 16-byte UID in one message with `first_part_of_unique_id = true` and `unique_id.len = 16`; the server accepts this as Stage 1 and completes the allocation immediately without waiting for Stage 2/3.

After the full 16-byte UID has been assembled — whether over three stages or in one single-frame message — the server calls `dnaLookupOrAssignNode()`.

### Node ID assignment

`dnaLookupOrAssignNode()` applies the following priority order:

1. **Existing entry** — if the UID is already in the allocation table and the stored node ID isn't currently claimed by another live node, return the previously assigned node ID. If it *is* claimed by someone else, fall through to reassignment below.
2. **Preferred ID** — if the peripheral included a preferred node ID in Stage 1 (range 1–125; 126–127 are reserved for network maintenance tools), search upward from that ID first, then downward from `preferred - 1`, assigning the first available ID found either way.
3. **Top-down fallback** — if there's no preference, or the preferred-ID search found nothing free, scan from node ID 125 downward and assign the first available ID.

The FC's own node ID is never assigned to a peripheral. If all 32 table slots are full and no existing entry matches, the allocation fails silently.

### Allocation table persistence

The allocation table (`dnaServerData_t`) is stored in a Parameter Group (`PG_DRONECAN_DNA_SERVER`) backed by flash. A new entry calls `saveConfig()` (deferred, silent, only while disarmed) so the same peripheral receives the same node ID on every boot without re-negotiating.

The table holds up to `DRONECAN_MAX_NODES` (32) entries, each storing:

```c
typedef struct {
uint8_t uniqueId[16]; // Full 16-byte hardware UID
uint8_t nodeId; // Assigned CAN node ID (1-127)
} dnaAllocationEntry_t;
```

### Entry point

`dronecanDnaHandleAllocation()` is called from `onTransferReceived()` when a `UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID` broadcast arrives, but only while `dronecanUseDNAServer` is true:

```c
case UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID:
if (dronecanConfig()->dronecanUseDNAServer) {
dronecanDnaHandleAllocation(ins, transfer);
}
break;
```

### Unit tests

Sixteen tests in `src/test/unit/dronecan_dna_server_unittest.cc` cover the full handshake, UID re-use, table-full rejection, non-broadcast source rejection, stage ordering, timeout reset, FC node ID exclusion, preferred node ID honouring, top-down sequential assignment, reserved-range and taken-preferred-ID fallback, live-network reassignment of a stored ID, single-frame (e.g. CAN-FD) full-UID delivery, and rejection of a malformed 4-byte stage-1 message without poisoning a legitimate follow-up handshake (DNA-1 through DNA-16).

---

## Adding New Message Types

To add support for a new DroneCAN message type:
Expand Down Expand Up @@ -1023,7 +1086,9 @@ void broadcastNodeStatus(void) {

| Date | Version | Changes |
|---|---|---|
| 2026-08-28 | 1.4 | Rebased onto maintenance-10.x; DNA server is now part of this branch (settings table now includes `dronecan_use_dna_server`); prior 1.3 history preserved below |
| 2026-08-21 | 1.3 | Documented the on-demand async service client (`dronecan_async.c`): GetNodeInfo/ParamGetSet/ExecuteOpcode/RestartNode requests, the shared slot state machine, and the new `MSP2_INAV_DRONECAN_ASYNC_REQUEST`/`ASYNC_RESULT` messages that replace 0x2043's old NODE_INFO meaning. Corrected `dronecanNodeInfo_t` (name/name_len fields removed), the `MSP2_INAV_DRONECAN_NODES` reply layout (13 bytes/node, not 30; `last_seen_ms` is an elapsed delta, not an absolute timestamp), and the Settings table (real setting names are `dronecan_node_id`/`dronecan_bitrate_kbps`, not `dronecan_mode`/`dronecan_baudrate`). Scoped to the param-getset feature — this pass did not re-verify sections describing DNA server, node/battery ID filtering, or actuator control, which may have their own drift from other in-flight branches. |
| 2026-06-06 | 1.3 | Added DNA server section; corrected settings table (`dronecan_bitrate_kbps`, `dronecan_use_dna_server`); added DNA allocation to message type table |
| 2026-04-30 | 1.2 | Added node table (`dronecanNodeInfo_t`), accessor functions, CLI status output, and MSP commands (0x2042/0x2043) |
| 2026-02-18 | 1.1 | Added error recovery, graceful disable behavior, and safe initialization documentation |
| 2026-02-16 | 1.0 | Initial version - handler-based architecture documentation |
Expand Down
59 changes: 49 additions & 10 deletions docs/DroneCAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DroneCAN (formerly UAVCAN v0) is a lightweight protocol designed for reliable co
| Battery Current | Supported | Current sensing from DroneCAN battery monitors |
| Parameter Get/Set | Supported | Read/write DroneCAN peripheral parameters via the configurator's DroneCAN tab |
| ESC Control | Planned | Motor control via DroneCAN ESCs |
| Dynamic Node Assignment | Planned | Manage node IDs dynamically to minimize first time configuration |
| Dynamic Node Assignment | Supported | Automatically assign CAN node IDs to plug-and-play DroneCAN peripherals |

## Supported Hardware

Expand Down Expand Up @@ -40,8 +40,9 @@ save
|---------|--------|---------|-------------|
| `dronecan_node_id` | 1-127 (126/127 reserved for diagnostic tools) | 1 | CAN node ID for the flight controller |
| `dronecan_bitrate_kbps` | 125, 250, 500, 1000 | 1000 | CAN bus bitrate in kbps |
| `dronecan_use_dna_server` | ON, OFF | ON | Enable automatic node ID assignment for plug-and-play peripherals |

All peripherals need to have the node ID and the bitrate set manually through the dronecan_gui for now. You can use your flight controller as a CAN interface by loading an Ardupilot image on it. Once the set up is complete, you can reflash it to Inav.
With `dronecan_use_dna_server = ON` (the default), peripherals that support Dynamic Node Allocation and have their node ID set to 0 (anonymous/unset) negotiate their node IDs automatically at power-up. Only the CAN bitrate needs to match across the bus. Peripherals that do not support DNA, or that already have a static node ID configured, will not send Allocation requests and must have their node ID set manually via a tool such as dronecan_gui. You can use your flight controller as a CAN interface by loading an ArduPilot image on it; once configuration is complete, reflash to INAV.

### GPS via DroneCAN

Expand Down Expand Up @@ -173,24 +174,28 @@ Setting up multiple DroneCAN peripherals on a single CAN bus:

```
# Flight Controller Configuration
set dronecan_node_id = 10 # Flight controller = node 10
set dronecan_bitrate = 1000KBPS
set dronecan_node_id = 1 # Flight controller = node 1
set dronecan_bitrate_kbps = 1000
set dronecan_use_dna_server = ON # Enable plug-and-play node assignment (default)

# Configure GPS (from node 1)
# Configure GPS
set gps_provider = DRONECAN

# Configure battery monitor (from node 2)
# Configure battery monitor
set bat_voltage_src = CAN
feature CURRENT_METER
set current_meter_type = CAN

save
```

**Peripheral Configuration (using dronecan_gui or similar tool):**
- **GPS Receiver:** Node ID = 1, Bitrate = 1000 KBPS
- **Battery Monitor:** Node ID = 2, Bitrate = 1000 KBPS
- **Potential Future Peripheral:** Node ID = 3, etc.
**Peripheral Configuration:**

With `dronecan_use_dna_server = ON`, peripherals that support Dynamic Node Allocation negotiate their node IDs automatically — no manual per-device configuration needed. Just set the CAN bitrate to match on every device.

For peripherals that do not support DNA, use dronecan_gui to assign a static node ID:
- **GPS Receiver:** Node ID = 2, Bitrate = 1000 KBPS
- **Battery Monitor:** Node ID = 3, Bitrate = 1000 KBPS

**CAN Bus Layout:**
```
Expand Down Expand Up @@ -314,6 +319,40 @@ bat_voltage_src = CAN
current_meter_type = CAN
```

## Dynamic Node Allocation (Plug and Play)

DroneCAN supports a three-stage handshake protocol (defined in UAVCAN Specification §6.4.9) that lets peripherals negotiate a unique node ID at power-up without any manual configuration. INAV implements a non-redundant (single-master) DNA server.

### How it works

1. The peripheral powers on without a node ID (anonymous mode).
2. It broadcasts an Allocation request containing the first 6 bytes of its 16-byte hardware unique ID, optionally including a preferred node ID.
3. INAV responds with an echo of the bytes received so far, prompting the peripheral to send the next 6 bytes.
4. After three stages (6 + 6 + 4 bytes), INAV has the full UID and assigns a node ID.
5. INAV stores the UID→node ID mapping in flash so the same peripheral receives the same ID on every power cycle.

All three stages must arrive within 500 ms of each other, or the handshake resets.

### Preferred node IDs

A peripheral may include a preferred node ID in its Stage 1 request. INAV honours the request if the ID is in the valid range (1–127) and not already assigned. If the requested ID is unavailable, INAV falls back to the next free ID in sequence.

### Allocation table

INAV stores up to 32 UID→node ID mappings in persistent flash storage. The table survives power cycles, so peripherals receive consistent node IDs across reboots. The table is managed automatically and is not directly user-configurable.

The flight controller's own node ID is never assigned to a peripheral.

### Enabling / disabling

```
set dronecan_use_dna_server = ON # default — plug-and-play enabled
set dronecan_use_dna_server = OFF # static node IDs only
save
```

---

## Hardware Setup

### Wiring
Expand Down
30 changes: 30 additions & 0 deletions docs/Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,16 @@ Re-purpose the craft name field for messages.

---

### dronecan_battery_id

Only accept BatteryInfo messages whose battery_id field (battery slot) matches this value. Every value 0-255 is a valid battery_id on the wire, so there is no wildcard; the default (0) matches the conventional primary-battery ID used by most peripherals.

| Default | Min | Max |
| --- | --- | --- |
| 0 | 0 | 255 |

---

### dronecan_bitrate_kbps

The speed of the CANbus network in kbps. Set all devices to the same speed.
Expand All @@ -899,6 +909,16 @@ The speed of the CANbus network in kbps. Set all devices to the same speed.

---

### dronecan_gps_node_id

Filter GPS messages by source Node ID. 0 = use any node.

| Default | Min | Max |
| --- | --- | --- |
| 0 | 0 | 127 |

---

### dronecan_node_id

Unique identifier for this device. Valid values are 1 to 127. 126 and 127 are reserved for diagnostic tools.
Expand All @@ -909,6 +929,16 @@ Unique identifier for this device. Valid values are 1 to 127. 126 and 127 are re

---

### dronecan_use_dna_server

Enable the DNA server to manage plug and play dronecan devices

| Default | Min | Max |
| --- | --- | --- |
| ON | OFF | ON |

---

### dshot_beeper_enabled

Whether using DShot motors as beepers is enabled
Expand Down
5 changes: 5 additions & 0 deletions src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ main_sources(COMMON_SRC
drivers/dronecan/dronecan.c
drivers/dronecan/dronecan_async.c
drivers/dronecan/dronecan_async.h
drivers/dronecan/dronecan_dna_server.c
drivers/dronecan/dronecan_dna_server.h
drivers/dronecan/dronecan_node_status.c
drivers/dronecan/dronecan_node_status.h
drivers/dronecan/dronecan.h

drivers/display.c
Expand Down Expand Up @@ -578,6 +582,7 @@ main_sources(COMMON_SRC
io/gps_msp.c
io/gps_crsf.c
io/gps_dronecan.c
io/gps_dronecan.h
io/gps_fake.c
io/gps_private.h
io/ledstrip.c
Expand Down
2 changes: 1 addition & 1 deletion src/main/config/config_eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <stddef.h>
#include <stdint.h>

#define EEPROM_CONF_VERSION 126
#define EEPROM_CONF_VERSION 127

bool isEEPROMContentValid(void);
bool loadEEPROM(void);
Expand Down
3 changes: 2 additions & 1 deletion src/main/config/parameter_group_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
#define PG_GEOZONES 1043
#define PG_GEOZONE_VERTICES 1044
#define PG_DRONECAN_CONFIG 1045
#define PG_INAV_END PG_DRONECAN_CONFIG
#define PG_DRONECAN_DNA_SERVER 1046 // Separate PG so we don't wipe user settings if the allocation table changes
#define PG_INAV_END PG_DRONECAN_DNA_SERVER

// OSD configuration (subject to change)
//#define PG_OSD_FONT_CONFIG 2047
Expand Down
Loading
Loading