Skip to content

Commit

Permalink
AP_Baro: optimize DroneCAN subscription process
Browse files Browse the repository at this point in the history
* remove unnecessary nullptr check, these are always called from an
  initialized AP_DroneCAN so if it's nullptr something has gone
  horrifically wrong

* pass in driver index instead of repeatedly calling function to get it

* simplify error handling; knowing exactly which allocation failed is not
  super helpful and one failing likely means subsequent ones will too,
  as it can only fail due to being out of memory
  • Loading branch information
tpwrules authored and tridge committed Nov 17, 2024
1 parent cd1118a commit def199e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
15 changes: 5 additions & 10 deletions libraries/AP_Baro/AP_Baro_DroneCAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ AP_Baro_DroneCAN::AP_Baro_DroneCAN(AP_Baro &baro) :
AP_Baro_Backend(baro)
{}

void AP_Baro_DroneCAN::subscribe_msgs(AP_DroneCAN* ap_dronecan)
bool AP_Baro_DroneCAN::subscribe_msgs(AP_DroneCAN* ap_dronecan)
{
if (ap_dronecan == nullptr) {
return;
}
if (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_pressure, ap_dronecan->get_driver_index()) == nullptr) {
AP_BoardConfig::allocation_error("pressure_sub");
}
const auto driver_index = ap_dronecan->get_driver_index();

if (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_temperature, ap_dronecan->get_driver_index()) == nullptr) {
AP_BoardConfig::allocation_error("temperature_sub");
}
return (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_pressure, driver_index) != nullptr)
&& (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_temperature, driver_index) != nullptr)
;
}

AP_Baro_Backend* AP_Baro_DroneCAN::probe(AP_Baro &baro)
Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_Baro/AP_Baro_DroneCAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AP_Baro_DroneCAN : public AP_Baro_Backend {

void update() override;

static void subscribe_msgs(AP_DroneCAN* ap_dronecan);
static bool subscribe_msgs(AP_DroneCAN* ap_dronecan);
static AP_Baro_DroneCAN* get_dronecan_backend(AP_DroneCAN* ap_dronecan, uint8_t node_id, bool create_new);
static AP_Baro_Backend* probe(AP_Baro &baro);

Expand Down

0 comments on commit def199e

Please sign in to comment.