Skip to content

Commit b4ebed8

Browse files
committed
support qmc5883
1 parent bcc3846 commit b4ebed8

14 files changed

Lines changed: 956 additions & 11 deletions

File tree

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ endif
152152
export UAVOBJGENERATOR
153153

154154
# Set up default build configurations (debug | release)
155-
GCS_BUILD_CONF := release
155+
# GCS_BUILD_CONF := release
156+
GCS_BUILD_CONF := debug
156157

157158
# Set extra configuration
158159
ifeq ($(GCS_WITH_OSG), 1)

flight/make/apps-defs.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ SRC += $(PIOSCOMMON)/pios_openlrs.c
105105
SRC += $(PIOSCOMMON)/pios_openlrs_rcvr.c
106106
SRC += $(PIOSCOMMON)/pios_board_io.c
107107
SRC += $(PIOSCOMMON)/pios_board_sensors.c
108+
SRC += $(PIOSCOMMON)/pios_qmc5883.c
108109

109110
## Misc library functions
110111
SRC += $(FLIGHTLIB)/sanitycheck.c

flight/modules/Sensors/sensors.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787

8888
static const TickType_t sensor_period_ticks = ((uint32_t)(1000.0f / PIOS_SENSOR_RATE / (float)portTICK_RATE_MS));
8989

90+
// PIOS_SENSOR_RATE 500 HZ
91+
9092
// Interval in number of sample to recalculate temp bias
9193
#define TEMP_CALIB_INTERVAL 30
9294

@@ -310,7 +312,7 @@ static void SensorsTask(__attribute__((unused)) void *parameters)
310312
// we will wait on the sensor that's marked as primary( that means the sensor with higher sample rate)
311313
bool is_primary = (sensor->type & PIOS_SENSORS_TYPE_3AXIS_ACCEL);
312314

313-
if (!sensor->driver->is_polled) {
315+
if (!sensor->driver->is_polled) { // is_polled false mpu6k, qmc5883
314316
const QueueHandle_t queue = PIOS_SENSORS_GetQueue(sensor);
315317
while (xQueueReceive(queue,
316318
(void *)source_data,
@@ -326,7 +328,7 @@ static void SensorsTask(__attribute__((unused)) void *parameters)
326328
PERF_TRACK_VALUE(counterSensorResets, reset_counter);
327329
error = true;
328330
}
329-
} else {
331+
} else { // poll manually
330332
if (PIOS_SENSORS_Poll(sensor)) {
331333
PIOS_SENSOR_Fetch(sensor, (void *)source_data, MAX_SENSORS_PER_INSTANCE);
332334
if (sensor->type & PIOS_SENSORS_TYPE_3D) {
@@ -494,6 +496,18 @@ static void handleMag(float *samples, float temperature)
494496

495497
rot_mult(mag_transform, mags, samples);
496498

499+
/*
500+
// Debug
501+
static uint32_t debug_ticks_pre = 0;
502+
static uint32_t debug_ticks_now = 0;
503+
debug_ticks_now++;
504+
if (debug_ticks_now >= debug_ticks_pre + 2500) {
505+
debug_ticks_pre = 0;
506+
debug_ticks_now = 0;
507+
DEBUG_PRINTF(0, "handle mag: %f,%f,%f\r\n", mags[0],mags[1],mags[2]);
508+
}
509+
*/
510+
497511
mag.x = samples[0];
498512
mag.y = samples[1];
499513
mag.z = samples[2];

flight/pios/common/pios_board_sensors.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@
5353
# include <pios_mpu9250_config.h>
5454
#endif
5555

56+
#ifdef PIOS_INCLUDE_QMC5883
57+
#include "pios_qmc5883.h"
58+
# ifdef PIOS_QMC5883_HAS_GPIOS
59+
pios_qmc5883_dev_t pios_qmc5883_internal_id;
60+
# endif
61+
#endif
62+
5663
#ifdef PIOS_INCLUDE_HMC5X83
5764
# include "pios_hmc5x83.h"
5865
# ifdef PIOS_HMC5X83_HAS_GPIOS
@@ -123,6 +130,69 @@ void PIOS_BOARD_Sensors_Configure()
123130
}
124131
#endif
125132

133+
// internal QMC5883 mag
134+
# ifdef PIOS_INCLUDE_QMC5883_INTERNAL
135+
const struct pios_qmc5883_cfg *qmc5883_internal_cfg = PIOS_BOARD_HW_DEFS_GetInternalQMC5883Cfg(pios_board_info_blob.board_rev);
136+
137+
if (qmc5883_internal_cfg) {
138+
// attach the 5883 mag to internal i2c bus
139+
140+
pios_qmc5883_dev_t internal_mag = PIOS_QMC5883_Init(qmc5883_internal_cfg, PIOS_I2C_QMC5883_INTERNAL_ADAPTER, 0);
141+
# ifdef PIOS_INCLUDE_WDG
142+
// give QMC5883 on I2C some extra time to allow for reset, etc. if needed
143+
// this is not in a loop, so it is safe
144+
PIOS_WDG_Clear();
145+
# endif /* PIOS_INCLUDE_WDG */
146+
147+
#ifdef PIOS_QMC5883_HAS_GPIOS
148+
pios_qmc5883_internal_id = internal_mag;
149+
#endif
150+
// add this sensor to the sensor task's list
151+
PIOS_QMC5883_Register(internal_mag, PIOS_SENSORS_TYPE_3AXIS_MAG);
152+
}
153+
154+
# endif /* PIOS_INCLUDE_QMC5883_INTERNAL */
155+
156+
# ifdef PIOS_INCLUDE_QMC5883
157+
158+
AuxMagSettingsTypeOptions option;
159+
AuxMagSettingsTypeGet(&option);
160+
161+
const struct pios_qmc5883_cfg *qmc5883_external_cfg = PIOS_BOARD_HW_DEFS_GetExternalQMC5883Cfg(pios_board_info_blob.board_rev);
162+
163+
if (qmc5883_external_cfg) {
164+
uint32_t i2c_id = 0;
165+
166+
if (option == AUXMAGSETTINGS_TYPE_FLEXI) {
167+
// i2c_external
168+
#ifdef PIOS_I2C_FLEXI_ADAPTER
169+
i2c_id = PIOS_I2C_FLEXI_ADAPTER;
170+
#endif
171+
} else if (option == AUXMAGSETTINGS_TYPE_I2C) {
172+
// i2c_internal (or Sparky2/F3 dedicated I2C port)
173+
#ifdef PIOS_I2C_EXTERNAL_ADAPTER
174+
i2c_id = PIOS_I2C_EXTERNAL_ADAPTER;
175+
#endif
176+
}
177+
178+
if (i2c_id) {
179+
uint32_t external_mag = PIOS_QMC5883_Init(qmc5883_external_cfg, i2c_id, 0);
180+
# ifdef PIOS_INCLUDE_WDG
181+
// give QMC5883 on I2C some extra time to allow for reset, etc. if needed
182+
// this is not in a loop, so it is safe
183+
PIOS_WDG_Clear();
184+
# endif /* PIOS_INCLUDE_WDG */
185+
// add this sensor to the sensor task's list
186+
// be careful that you don't register a slow, unimportant sensor after registering the fastest sensor
187+
// and before registering some other fast and important sensor
188+
// as that would cause delay and time jitter for the second fast sensor
189+
PIOS_QMC5883_Register(external_mag, PIOS_SENSORS_TYPE_3AXIS_AUXMAG);
190+
// mag alarm is cleared later, so use I2C
191+
AlarmsSet(SYSTEMALARMS_ALARM_I2C, (external_mag) ? SYSTEMALARMS_ALARM_OK : SYSTEMALARMS_ALARM_WARNING);
192+
}
193+
}
194+
# endif /* PIOS_INCLUDE_QMC5883 */
195+
126196
// internal HMC5x83 mag
127197
# ifdef PIOS_INCLUDE_HMC5X83_INTERNAL
128198
const struct pios_hmc5x83_cfg *hmc5x83_internal_cfg = PIOS_BOARD_HW_DEFS_GetInternalHMC5x83Cfg(pios_board_info_blob.board_rev);

flight/pios/common/pios_hmc5x83.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pios_hmc5x83_dev_t PIOS_HMC5x83_Init(const struct pios_hmc5x83_cfg *cfg, uint32_
149149
break;
150150
}
151151
// if the application sensor rate is fast enough to warrant skipping some slow hardware sensor reads
152-
if ((PIOS_SENSOR_RATE * 100.0f / 3.0f) > rate100) {
152+
if ((PIOS_SENSOR_RATE * 100.0f / 3.0f/*3-AXIS*/) > rate100) {
153153
// count the number of "return false" up to this number
154154
dev->magCountMax = ((uint16_t)PIOS_SENSOR_RATE * 100 / rate100) + 1;
155155
} else {

0 commit comments

Comments
 (0)