Skip to content

Commit

Permalink
AP_Periph: clean up node ID parameter handling
Browse files Browse the repository at this point in the history
Properly document the range and behavior.

Rename storage variable as it doesn't connect to the DNA preferred ID
feature.
  • Loading branch information
tpwrules authored and tridge committed Sep 10, 2024
1 parent aca6244 commit dcd34dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Tools/AP_Periph/Parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ const AP_Param::Info AP_Periph_FW::var_info[] = {
GSCALAR(format_version, "FORMAT_VERSION", 0),

// @Param: CAN_NODE
// @DisplayName: UAVCAN node that is used for this network
// @Description: UAVCAN node should be set implicitly or 0 for dynamic node allocation
// @Range: 0 250
// @DisplayName: DroneCAN node ID used by this node on all networks
// @Description: Value of 0 requests any ID from a DNA server, any other value sets that ID ignoring DNA
// @Range: 0 127
// @User: Advanced
// @RebootRequired: True
GSCALAR(can_node, "CAN_NODE", HAL_CAN_DEFAULT_NODE_ID),
Expand Down
13 changes: 5 additions & 8 deletions Tools/AP_Periph/can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ HAL_GPIO_PIN_TERMCAN1
};
#endif // CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS && defined(HAL_GPIO_PIN_TERMCAN1)

#ifndef HAL_CAN_DEFAULT_NODE_ID
#define HAL_CAN_DEFAULT_NODE_ID CANARD_BROADCAST_NODE_ID
#endif
uint8_t PreferredNodeID = HAL_CAN_DEFAULT_NODE_ID;
uint8_t user_set_node_id = HAL_CAN_DEFAULT_NODE_ID;

#ifndef AP_PERIPH_PROBE_CONTINUOUS
#define AP_PERIPH_PROBE_CONTINUOUS 0
Expand Down Expand Up @@ -1550,7 +1547,7 @@ bool AP_Periph_FW::can_do_dna()
// Structure of the request is documented in the DSDL definition
// See http://uavcan.org/Specification/6._Application_level_functions/#dynamic-node-id-allocation
uint8_t allocation_request[CANARD_CAN_FRAME_MAX_DATA_LEN - 1];
allocation_request[0] = (uint8_t)(PreferredNodeID << 1U);
allocation_request[0] = 0; // we are only called if the user has not set an ID, so request any ID

if (dronecan.node_id_allocation_unique_id_offset == 0) {
allocation_request[0] |= 1; // First part of unique ID
Expand Down Expand Up @@ -1590,7 +1587,7 @@ void AP_Periph_FW::can_start()
node_status.uptime_sec = AP_HAL::millis() / 1000U;

if (g.can_node >= 0 && g.can_node < 128) {
PreferredNodeID = g.can_node;
user_set_node_id = g.can_node;
}

#if !defined(HAL_NO_FLASH_SUPPORT) && !defined(HAL_NO_ROMFS_SUPPORT)
Expand Down Expand Up @@ -1662,8 +1659,8 @@ void AP_Periph_FW::can_start()
canardInit(&dronecan.canard, (uint8_t *)dronecan.canard_memory_pool, sizeof(dronecan.canard_memory_pool),
onTransferReceived_trampoline, shouldAcceptTransfer_trampoline, this);

if (PreferredNodeID != CANARD_BROADCAST_NODE_ID) {
canardSetLocalNodeID(&dronecan.canard, PreferredNodeID);
if (user_set_node_id != CANARD_BROADCAST_NODE_ID) {
canardSetLocalNodeID(&dronecan.canard, user_set_node_id);
}
}

Expand Down

0 comments on commit dcd34dd

Please sign in to comment.