New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AP_Compass: Add in BMM350 Driver #26987
base: master
Are you sure you want to change the base?
Conversation
c618911
to
654620c
Compare
@tridge Can you check it? It will be used in CUAV's new flight controller. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't checked this against the data sheet, but it looks pretty good structure-wise.
Have you got logs of this with other existing compasses to compare it against? How else have you tested this compass?
AP_HAL::OwnPtr<AP_HAL::Device> _dev; | ||
|
||
/** | ||
* @brief BMM350 magnetometer dut offset coefficient structure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"dut"?
/** | ||
* @brief BMM350 magnetometer cross axis compensation structure | ||
*/ | ||
struct bmm350_cross_axis |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct bmm350_cross_axis | |
struct cross_axis |
bmm350 is redundant here as you're within the class. Similarly elsewhere
@@ -74,6 +74,7 @@ class AP_Compass_Backend | |||
DEVTYPE_AK09918 = 0x14, | |||
DEVTYPE_AK09915 = 0x15, | |||
DEVTYPE_QMC5883P = 0x16, | |||
DEVTYPE_BMM350 = 0x17, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need this number in Tools/scripts/decode_devid.py
*/ | ||
bool AP_Compass_BMM350::read_otp_data() | ||
{ | ||
uint8_t data = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint8_t data = 0; |
uint8_t msb = 0; | ||
uint8_t lsb = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint8_t msb = 0; | |
uint8_t lsb = 0; |
narrow the scope of variables as much as possible.
otp_data[index] = ((uint16_t)(msb << 8) | lsb) & 0xFFFF; | ||
} | ||
|
||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably don't need this level of indent here.
uint8_t chip_id = 0; | ||
uint8_t data = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move closer to where they're used, remove the initialisation as their value should only be checked after a successful read.
|
||
// Configure interrupt settings and enable DRDY | ||
// Set INT mode as PULSED | active polarity | PUSH_PULL | unmap | DRDY interrupt | ||
data = BMM350_INT_MODE_PULSED | BMM350_INT_POL_ACTIVE_HIGH | BMM350_INT_OD_PUSHPULL | BMM350_INT_OUTPUT_DISABLE | BMM350_INT_DRDY_EN; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data = BMM350_INT_MODE_PULSED | BMM350_INT_POL_ACTIVE_HIGH | BMM350_INT_OD_PUSHPULL | BMM350_INT_OUTPUT_DISABLE | BMM350_INT_DRDY_EN; | |
const uint8_t data = BMM350_INT_MODE_PULSED | BMM350_INT_POL_ACTIVE_HIGH | BMM350_INT_OD_PUSHPULL | BMM350_INT_OUTPUT_DISABLE | BMM350_INT_DRDY_EN; |
int32_t magx_raw = 0; | ||
int32_t magy_raw = 0; | ||
int32_t magz_raw = 0; | ||
int32_t temp_raw = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int32_t magx_raw = 0; | |
int32_t magy_raw = 0; | |
int32_t magz_raw = 0; | |
int32_t temp_raw = 0; |
} | ||
|
||
// Converts raw data to signed integer value | ||
magx_raw = fix_sign((data.magx[0] + ((uint32_t)data.magx[1] << 8) + ((uint32_t)data.magx[2] << 16)), BMM350_SIGNED_24_BIT); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
magx_raw = fix_sign((data.magx[0] + ((uint32_t)data.magx[1] << 8) + ((uint32_t)data.magx[2] << 16)), BMM350_SIGNED_24_BIT); | |
const int32_t magx_raw = fix_sign((data.magx[0] + ((uint32_t)data.magx[1] << 8) + ((uint32_t)data.magx[2] << 16)), BMM350_SIGNED_24_BIT); |
etc
@@ -515,7 +516,7 @@ const AP_Param::GroupInfo Compass::var_info[] = { | |||
// @Param: DISBLMSK | |||
// @DisplayName: Compass disable driver type mask | |||
// @Description: This is a bitmask of driver types to disable. If a driver type is set in this mask then that driver will not try to find a sensor at startup | |||
// @Bitmask: 0:HMC5883,1:LSM303D,2:AK8963,3:BMM150,4:LSM9DS1,5:LIS3MDL,6:AK09916,7:IST8310,8:ICM20948,9:MMC3416,11:DroneCAN,12:QMC5883,14:MAG3110,15:IST8308,16:RM3100,17:MSP,18:ExternalAHRS | |||
// @Bitmask: 0:HMC5883,1:LSM303D,2:AK8963,3:BMM150,4:LSM9DS1,5:LIS3MDL,6:AK09916,7:IST8310,8:ICM20948,9:MMC3416,11:DroneCAN,12:QMC5883,14:MAG3110,15:IST8308,16:RM3100,17:MSP,18:ExternalAHRS,21:BMM350 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you use 19 here?
otp_data[index] = ((uint16_t)(msb << 8) | lsb) & 0xFFFF; | ||
} | ||
|
||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think you need this - C++ allows declarations in the middle
return false; | ||
} | ||
|
||
otp_data[index] = ((uint16_t)(msb << 8) | lsb) & 0xFFFF; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to make this explicitly unsigned
mag_comp.dut_offset_coef.z = fix_sign(off_z_lsb_msb, BMM350_SIGNED_12_BIT); | ||
mag_comp.dut_offset_coef.temp = t_off / 5.0f; | ||
|
||
sens_x = (otp_data[BMM350_MAG_SENS_X] & 0xFF00) >> 8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicitly unsigned etc
sens_z = (otp_data[BMM350_MAG_SENS_Z] & 0xFF00) >> 8; | ||
sens_t = (otp_data[BMM350_TEMP_OFF_SENS] & 0xFF00) >> 8; | ||
|
||
mag_comp.dut_sensit_coef.x = sens_x / 256.0f; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
usually our pattern is to multiple 1/constant to avoid slow STM32 instructions
mag_comp.dut_sensit_coef.z = sens_z / 256.0f; | ||
mag_comp.dut_sensit_coef.temp = sens_t / 512.0f; | ||
|
||
tco_x = (otp_data[BMM350_MAG_TCO_X] & 0x00FF); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could probably just cast otp_data as uint8_t* and avoid a lot of this masking.
return false; | ||
} | ||
// Wait for sensor complete bit reset | ||
hal.scheduler->delay(14); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
be nice to have a comment as to whether these delays are mandated by the spec
if (mode == BMM350_POWER_MODE_NORMAL) { | ||
hal.scheduler->delay(38); // Switch from suspend mode to normal mode | ||
} else { | ||
hal.scheduler->delay(20); // AVERAGING_4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear what this comment is about
|
||
void AP_Compass_BMM350::read() | ||
{ | ||
drain_accumulated_samples(_compass_instance); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong whitespace
No description provided.