Skip to content
Open
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
12 changes: 6 additions & 6 deletions libraries/AP_Baro/AP_Baro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ bool AP_Baro::_add_backend(AP_Baro_Backend *backend)
if (!backend) {
return false;
}
if (_num_drivers >= BARO_MAX_DRIVERS) {
if (_num_drivers >= ARRAY_SIZE(drivers)) {
AP_HAL::panic("Too many barometer drivers");
}
drivers[_num_drivers++] = backend;
Expand Down Expand Up @@ -520,8 +520,8 @@ bool AP_Baro::_have_i2c_driver(uint8_t bus, uint8_t address) const
*/
#define ADD_BACKEND(backend) \
do { _add_backend(backend); \
if (_num_drivers == BARO_MAX_DRIVERS || \
_num_sensors == BARO_MAX_INSTANCES) { \
if (_num_drivers == ARRAY_SIZE(drivers) || \
_num_sensors == ARRAY_SIZE(sensors)) { \
return; \
} \
} while (0)
Expand All @@ -542,8 +542,8 @@ void AP_Baro::init(void)
}

// zero bus IDs before probing
for (uint8_t i = 0; i < BARO_MAX_INSTANCES; i++) {
sensors[i].bus_id.set(0);
for (auto &sensor : sensors) {
sensor.bus_id.set(0);
}

#if AP_SIM_BARO_ENABLED
Expand Down Expand Up @@ -975,7 +975,7 @@ float AP_Baro::thrust_pressure_correction(uint8_t instance)
*/
uint8_t AP_Baro::register_sensor(void)
{
if (_num_sensors >= BARO_MAX_INSTANCES) {
if (_num_sensors >= ARRAY_SIZE(sensors)) {
AP_HAL::panic("Too many barometers");
}
return _num_sensors++;
Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_Baro/AP_Baro.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class AP_Baro
};
#endif

struct sensor {
struct {
uint32_t last_update_ms; // last update time in ms
uint32_t last_change_ms; // last update time in ms that included a change in reading from previous readings
float pressure; // pressure in Pascal
Expand Down
42 changes: 21 additions & 21 deletions libraries/AP_Baro/AP_Baro_DroneCAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,32 @@ AP_Baro_Backend* AP_Baro_DroneCAN::probe(AP_Baro &baro)
WITH_SEMAPHORE(_sem_registry);

AP_Baro_DroneCAN* backend = nullptr;
for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) {
if (_detected_modules[i].driver == nullptr && _detected_modules[i].ap_dronecan != nullptr) {
for (auto &detected_module : _detected_modules) {
if (detected_module.driver == nullptr && detected_module.ap_dronecan != nullptr) {
backend = NEW_NOTHROW AP_Baro_DroneCAN(baro);
if (backend == nullptr) {
AP::can().log_text(AP_CANManager::LOG_ERROR,
LOG_TAG,
"Failed register DroneCAN Baro Node %d on Bus %d\n",
_detected_modules[i].node_id,
_detected_modules[i].ap_dronecan->get_driver_index());
detected_module.node_id,
detected_module.ap_dronecan->get_driver_index());
} else {
_detected_modules[i].driver = backend;
detected_module.driver = backend;
backend->_pressure = 0;
backend->_pressure_count = 0;
backend->_ap_dronecan = _detected_modules[i].ap_dronecan;
backend->_node_id = _detected_modules[i].node_id;
backend->_ap_dronecan = detected_module.ap_dronecan;
backend->_node_id = detected_module.node_id;

backend->_instance = backend->_frontend.register_sensor();
backend->set_bus_id(backend->_instance, AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_UAVCAN,
_detected_modules[i].ap_dronecan->get_driver_index(),
detected_module.ap_dronecan->get_driver_index(),
backend->_node_id, 0));

AP::can().log_text(AP_CANManager::LOG_INFO,
LOG_TAG,
"Registered DroneCAN Baro Node %d on Bus %d\n",
_detected_modules[i].node_id,
_detected_modules[i].ap_dronecan->get_driver_index());
detected_module.node_id,
detected_module.ap_dronecan->get_driver_index());
}
break;
}
Expand All @@ -73,29 +73,29 @@ AP_Baro_DroneCAN* AP_Baro_DroneCAN::get_dronecan_backend(AP_DroneCAN* ap_droneca
if (ap_dronecan == nullptr) {
return nullptr;
}
for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) {
if (_detected_modules[i].driver != nullptr &&
_detected_modules[i].ap_dronecan == ap_dronecan &&
_detected_modules[i].node_id == node_id) {
return _detected_modules[i].driver;
for (auto &detected_module : _detected_modules) {
if (detected_module.driver != nullptr &&
detected_module.ap_dronecan == ap_dronecan &&
detected_module.node_id == node_id) {
return detected_module.driver;
}
}

if (create_new) {
bool already_detected = false;
//Check if there's an empty spot for possible registration
for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) {
if (_detected_modules[i].ap_dronecan == ap_dronecan && _detected_modules[i].node_id == node_id) {
for (auto &detected_module : _detected_modules) {
if (detected_module.ap_dronecan == ap_dronecan && detected_module.node_id == node_id) {
//Already Detected
already_detected = true;
break;
}
}
if (!already_detected) {
for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) {
if (_detected_modules[i].ap_dronecan == nullptr) {
_detected_modules[i].ap_dronecan = ap_dronecan;
_detected_modules[i].node_id = node_id;
for (auto &detected_module : _detected_modules) {
if (detected_module.ap_dronecan == nullptr) {
detected_module.ap_dronecan = ap_dronecan;
detected_module.node_id = node_id;
break;
}
}
Expand Down
Loading