Skip to content

Commit 0f63bc6

Browse files
martinbuddendigitalentity
authored andcommitted
Don't fail HMC5883L initialisation for a single saturated reading (#563)
* Don't fail HMC5883L initialisation for a single saturated reading * Added log message for HMC55883L valid reads in initialisation * Changed delay between reads to 70
1 parent 32eaa7b commit 0f63bc6

File tree

3 files changed

+41
-53
lines changed

3 files changed

+41
-53
lines changed

src/main/drivers/compass_hmc5883l.c

+35-49
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,12 @@ bool hmc5883lDetect(mag_t* mag, const hmc5883Config_t *hmc5883ConfigToUse)
185185
return true;
186186
}
187187

188-
#define INIT_MAX_FAILURES 5
188+
#define INITIALISATION_MAX_READ_FAILURES 5
189189
bool hmc5883lInit(void)
190190
{
191191
int16_t magADC[3];
192192
int32_t xyz_total[3] = { 0, 0, 0 }; // 32 bit totals so they won't overflow.
193193
bool bret = true; // Error indicator
194-
bool error_read = false;
195-
bool error_saturation = false;
196194

197195
delay(50);
198196
i2cWrite(MAG_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR = 0x010 + MS1, MS0 set to pos bias
@@ -202,68 +200,64 @@ bool hmc5883lInit(void)
202200
delay(100);
203201
hmc5883lRead(magADC);
204202

205-
int validSamples = 0;
206-
int failedSamples = 0;
207-
while (validSamples < 10 && failedSamples < INIT_MAX_FAILURES) { // Collect 10 samples
203+
int validSamples1 = 0;
204+
int failedSamples1 = 0;
205+
int saturatedSamples1 = 0;
206+
while (validSamples1 < 10 && failedSamples1 < INITIALISATION_MAX_READ_FAILURES) { // Collect 10 samples
208207
i2cWrite(MAG_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_MODE, 1);
209-
delay(50);
208+
delay(70);
210209
if (hmc5883lRead(magADC)) { // Get the raw values in case the scales have already been changed.
211-
++validSamples;
212-
// Since the measurements are noisy, they should be averaged rather than taking the max.
213-
xyz_total[X] += magADC[X];
214-
xyz_total[Y] += magADC[Y];
215-
xyz_total[Z] += magADC[Z];
216210
// Detect saturation.
217211
if (-4096 >= MIN(magADC[X], MIN(magADC[Y], magADC[Z]))) {
218-
error_saturation = true;
219-
bret = false;
220-
break; // Breaks out of the for loop. No sense in continuing if we saturated.
212+
++saturatedSamples1;
213+
++failedSamples1;
214+
} else {
215+
++validSamples1;
216+
// Since the measurements are noisy, they should be averaged rather than taking the max.
217+
xyz_total[X] += magADC[X];
218+
xyz_total[Y] += magADC[Y];
219+
xyz_total[Z] += magADC[Z];
220+
221221
}
222222
} else {
223-
++failedSamples;
223+
++failedSamples1;
224224
}
225225
LED1_TOGGLE;
226226
}
227227

228-
if (failedSamples >= INIT_MAX_FAILURES) {
229-
bret = false;
230-
}
231-
232-
if (failedSamples > 0) {
233-
error_read = true;
234-
}
235-
236228
// Apply the negative bias. (Same gain)
237229
i2cWrite(MAG_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_CONFA, 0x010 + HMC_NEG_BIAS); // Reg A DOR = 0x010 + MS1, MS0 set to negative bias.
238-
validSamples = 0;
239-
failedSamples = 0;
240-
while (validSamples < 10 && failedSamples < INIT_MAX_FAILURES) { // Collect 10 samples
230+
int validSamples2 = 0;
231+
int failedSamples2 = 0;
232+
int saturatedSamples2 = 0;
233+
while (validSamples2 < 10 && failedSamples2 < INITIALISATION_MAX_READ_FAILURES) { // Collect 10 samples
241234
i2cWrite(MAG_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_MODE, 1);
242-
delay(50);
235+
delay(70);
243236
if (hmc5883lRead(magADC)) { // Get the raw values in case the scales have already been changed.
244-
++validSamples;
245-
// Since the measurements are noisy, they should be averaged.
246-
xyz_total[X] -= magADC[X];
247-
xyz_total[Y] -= magADC[Y];
248-
xyz_total[Z] -= magADC[Z];
249237
// Detect saturation.
250238
if (-4096 >= MIN(magADC[X], MIN(magADC[Y], magADC[Z]))) {
251-
error_saturation = true;
252-
bret = false;
253-
break; // Breaks out of the for loop. No sense in continuing if we saturated.
239+
++saturatedSamples2;
240+
++failedSamples2;
241+
} else {
242+
++validSamples2;
243+
// Since the measurements are noisy, they should be averaged.
244+
xyz_total[X] -= magADC[X];
245+
xyz_total[Y] -= magADC[Y];
246+
xyz_total[Z] -= magADC[Z];
254247
}
255248
} else {
256-
++failedSamples;
249+
++failedSamples2;
257250
}
258251
LED1_TOGGLE;
259252
}
260253

261-
if (failedSamples >= INIT_MAX_FAILURES) {
254+
if (failedSamples1 >= INITIALISATION_MAX_READ_FAILURES || failedSamples2 >= INITIALISATION_MAX_READ_FAILURES) {
255+
addBootlogEvent4(BOOT_EVENT_HMC5883L_READ_OK_COUNT, BOOT_EVENT_FLAGS_NONE, validSamples1, validSamples2);
256+
addBootlogEvent4(BOOT_EVENT_HMC5883L_READ_FAILED, BOOT_EVENT_FLAGS_WARNING, failedSamples1, failedSamples2);
262257
bret = false;
263258
}
264-
265-
if (failedSamples > 0) {
266-
error_read = true;
259+
if (saturatedSamples1 > 0 || saturatedSamples2 > 0) {
260+
addBootlogEvent4(BOOT_EVENT_HMC5883L_SATURATION, BOOT_EVENT_FLAGS_WARNING, saturatedSamples1, saturatedSamples2);
267261
}
268262

269263
if (bret) {
@@ -285,14 +279,6 @@ bool hmc5883lInit(void)
285279

286280
hmc5883lConfigureDataReadyInterruptHandling();
287281

288-
if (error_read) {
289-
addBootlogEvent2(BOOT_EVENT_HMC5883L_READ_FAILED, BOOT_EVENT_FLAGS_WARNING);
290-
}
291-
292-
if (error_saturation) {
293-
addBootlogEvent2(BOOT_EVENT_HMC5883L_SATURATION, BOOT_EVENT_FLAGS_ERROR);
294-
}
295-
296282
return bret;
297283
}
298284

src/main/drivers/logging.c

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static const char * eventDescription[BOOT_EVENT_CODE_COUNT] = {
5151
[BOOT_EVENT_MAG_DETECTION] = "MAG_DETECTION",
5252
[BOOT_EVENT_RANGEFINDER_DETECTION] = "RANGEFINDER_DETECTION",
5353
[BOOT_EVENT_MAG_INIT_FAILED] = "MAG_INIT_FAILED",
54+
[BOOT_EVENT_HMC5883L_READ_OK_COUNT] = "HMC5883L_READ_OK_COUNT",
5455
[BOOT_EVENT_HMC5883L_READ_FAILED] = "HMC5883L_READ_FAILED",
5556
[BOOT_EVENT_HMC5883L_SATURATION] = "HMC5883L_SATURATION",
5657
[BOOT_EVENT_TIMER_CH_SKIPPED] = "TIMER_CHANNEL_SKIPPED",

src/main/drivers/logging_codes.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ typedef enum {
4242
BOOT_EVENT_MAG_DETECTION = 12,
4343
BOOT_EVENT_RANGEFINDER_DETECTION = 13,
4444
BOOT_EVENT_MAG_INIT_FAILED = 14,
45-
BOOT_EVENT_HMC5883L_READ_FAILED = 15,
46-
BOOT_EVENT_HMC5883L_SATURATION = 16,
47-
BOOT_EVENT_TIMER_CH_SKIPPED = 17, // 1 - MAX_MOTORS exceeded, 2 - MAX_SERVOS exceeded, 3 - feature clash
48-
BOOT_EVENT_TIMER_CH_MAPPED = 18, // 0 - PPM, 1 - PWM, 2 - MOTOR, 3 - SERVO
45+
BOOT_EVENT_HMC5883L_READ_OK_COUNT = 15,
46+
BOOT_EVENT_HMC5883L_READ_FAILED = 16,
47+
BOOT_EVENT_HMC5883L_SATURATION = 17,
48+
BOOT_EVENT_TIMER_CH_SKIPPED = 18, // 1 - MAX_MOTORS exceeded, 2 - MAX_SERVOS exceeded, 3 - feature clash
49+
BOOT_EVENT_TIMER_CH_MAPPED = 19, // 0 - PPM, 1 - PWM, 2 - MOTOR, 3 - SERVO
4950

5051
BOOT_EVENT_CODE_COUNT
5152
} bootLogEventCode_e;

0 commit comments

Comments
 (0)