From 83929cd290279963b01b2ccd52c70d61bdeff6b0 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 22 Oct 2017 15:44:32 -0500 Subject: altos: Share common logging code. Deal with corrupt initial flight records Move common logging APIs from per-format files into ao_log.c. Then, change that code to check the first log record in a slot (containing the flight number) to see if it's invalid and deal with it. That involves not re-using that slot, and allowing it to be erased. Corrupted log blocks are reported with a negative flight number. Signed-off-by: Keith Packard --- src/kernel/ao_log_mega.c | 64 +++--------------------------------------------- 1 file changed, 4 insertions(+), 60 deletions(-) (limited to 'src/kernel/ao_log_mega.c') diff --git a/src/kernel/ao_log_mega.c b/src/kernel/ao_log_mega.c index b86abe7a..d1cf4f13 100644 --- a/src/kernel/ao_log_mega.c +++ b/src/kernel/ao_log_mega.c @@ -21,50 +21,6 @@ #include #include -static __xdata struct ao_log_mega log; - -__code uint8_t ao_log_format = AO_LOG_FORMAT_TELEMEGA; - -static uint8_t -ao_log_csum(__xdata uint8_t *b) __reentrant -{ - uint8_t sum = 0x5a; - uint8_t i; - - for (i = 0; i < sizeof (struct ao_log_mega); i++) - sum += *b++; - return -sum; -} - -uint8_t -ao_log_mega(__xdata struct ao_log_mega *log) __reentrant -{ - uint8_t wrote = 0; - /* set checksum */ - log->csum = 0; - log->csum = ao_log_csum((__xdata uint8_t *) log); - ao_mutex_get(&ao_log_mutex); { - if (ao_log_current_pos >= ao_log_end_pos && ao_log_running) - ao_log_stop(); - if (ao_log_running) { - wrote = 1; - ao_storage_write(ao_log_current_pos, - log, - sizeof (struct ao_log_mega)); - ao_log_current_pos += sizeof (struct ao_log_mega); - } - } ao_mutex_put(&ao_log_mutex); - return wrote; -} - -static uint8_t -ao_log_dump_check_data(void) -{ - if (ao_log_csum((uint8_t *) &log) != 0) - return 0; - return 1; -} - #if HAS_FLIGHT static __data uint8_t ao_log_data_pos; @@ -106,7 +62,7 @@ ao_log(void) #endif log.u.flight.ground_pres = ao_ground_pres; log.u.flight.flight = ao_flight_number; - ao_log_mega(&log); + ao_log_write(&log); #endif /* Write the whole contents of the ring to the log @@ -139,7 +95,7 @@ ao_log(void) log.u.sensor.mag_y = ao_data_ring[ao_log_data_pos].hmc5883.y; #endif log.u.sensor.accel = ao_data_accel(&ao_data_ring[ao_log_data_pos]); - ao_log_mega(&log); + ao_log_write(&log); if (ao_log_state <= ao_flight_coast) next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT; else @@ -153,7 +109,7 @@ ao_log(void) for (i = 0; i < AO_ADC_NUM_SENSE; i++) log.u.volt.sense[i] = ao_data_ring[ao_log_data_pos].adc.sense[i]; log.u.volt.pyro = ao_pyro_fired; - ao_log_mega(&log); + ao_log_write(&log); next_other = log.tick + AO_OTHER_INTERVAL; } ao_log_data_pos = ao_data_ring_next(ao_log_data_pos); @@ -166,7 +122,7 @@ ao_log(void) log.tick = ao_time(); log.u.state.state = ao_log_state; log.u.state.reason = 0; - ao_log_mega(&log); + ao_log_write(&log); if (ao_log_state == ao_flight_landed) ao_log_stop(); @@ -185,15 +141,3 @@ ao_log(void) } #endif /* HAS_FLIGHT */ -uint16_t -ao_log_flight(uint8_t slot) -{ - if (!ao_storage_read(ao_log_pos(slot), - &log, - sizeof (struct ao_log_mega))) - return 0; - - if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT) - return log.u.flight.flight; - return 0; -} -- cgit v1.2.3 From f0068719b17019c5cd7ed318364a0581caf64e1a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 2 Dec 2017 15:32:38 -0600 Subject: altos/kernel: MPU9250 support Use MPU9250 for accel, gyro and mag data in logging, telemetry and flight status computations. Signed-off-by: Keith Packard --- src/kernel/ao_data.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/ao_flight.c | 2 +- src/kernel/ao_log.h | 3 ++- src/kernel/ao_log_mega.c | 11 +++++++++ src/kernel/ao_sample.c | 6 ++--- src/kernel/ao_telemetry.c | 16 ++++++++++++- 6 files changed, 90 insertions(+), 6 deletions(-) (limited to 'src/kernel/ao_log_mega.c') diff --git a/src/kernel/ao_data.h b/src/kernel/ao_data.h index 9a3b389c..88d0e916 100644 --- a/src/kernel/ao_data.h +++ b/src/kernel/ao_data.h @@ -330,6 +330,47 @@ typedef int16_t angle_t; /* in degrees */ #define ao_data_pitch(packet) ((packet)->mpu6000.gyro_x) #define ao_data_yaw(packet) ((packet)->mpu6000.gyro_z) +static inline float ao_convert_gyro(float sensor) +{ + return ao_mpu6000_gyro(sensor); +} + +static inline float ao_convert_accel(int16_t sensor) +{ + return ao_mpu6000_accel(sensor); +} + +#endif + +#if !HAS_GYRO && HAS_MPU9250 + +#define HAS_GYRO 1 + +typedef int16_t gyro_t; /* in raw sample units */ +typedef int16_t angle_t; /* in degrees */ + +/* Y axis is aligned with the direction of motion (along) */ +/* X axis is aligned in the other board axis (across) */ +/* Z axis is aligned perpendicular to the board (through) */ + +#define ao_data_along(packet) ((packet)->mpu9250.accel_y) +#define ao_data_across(packet) ((packet)->mpu9250.accel_x) +#define ao_data_through(packet) ((packet)->mpu9250.accel_z) + +#define ao_data_roll(packet) ((packet)->mpu9250.gyro_y) +#define ao_data_pitch(packet) ((packet)->mpu9250.gyro_x) +#define ao_data_yaw(packet) ((packet)->mpu9250.gyro_z) + +static inline float ao_convert_gyro(float sensor) +{ + return ao_mpu9250_gyro(sensor); +} + +static inline float ao_convert_accel(int16_t sensor) +{ + return ao_mpu9250_accel(sensor); +} + #endif #if !HAS_MAG && HAS_HMC5883 @@ -344,4 +385,21 @@ typedef int16_t ao_mag_t; /* in raw sample units */ #endif +#if !HAS_MAG && HAS_MPU9250 + +#define HAS_MAG 1 + +typedef int16_t ao_mag_t; /* in raw sample units */ + +/* Note that this order is different from the accel and gyro. For some + * reason, the mag sensor axes aren't the same as the other two + * sensors. Also, the Z axis is flipped in sign. + */ + +#define ao_data_mag_along(packet) ((packet)->mpu9250.mag_x) +#define ao_data_mag_across(packet) ((packet)->mpu9250.mag_y) +#define ao_data_mag_through(packet) ((packet)->mpu9250.mag_z) + +#endif + #endif /* _AO_DATA_H_ */ diff --git a/src/kernel/ao_flight.c b/src/kernel/ao_flight.c index f06125cd..cb02c454 100644 --- a/src/kernel/ao_flight.c +++ b/src/kernel/ao_flight.c @@ -21,7 +21,7 @@ #include #endif -#if HAS_MPU6000 +#if HAS_MPU6000 || HAS_MPU9250 #include #endif diff --git a/src/kernel/ao_log.h b/src/kernel/ao_log.h index 1c186364..5f04ef9a 100644 --- a/src/kernel/ao_log.h +++ b/src/kernel/ao_log.h @@ -54,6 +54,7 @@ extern __pdata enum ao_flight_state ao_log_state; #define AO_LOG_FORMAT_TELEMINI3 12 /* 16-byte MS5607 baro only, 3.3V supply, stm32f042 SoC */ #define AO_LOG_FORMAT_TELEFIRETWO 13 /* 32-byte test stand data */ #define AO_LOG_FORMAT_EASYMINI2 14 /* 16-byte MS5607 baro only, 3.3V supply, stm32f042 SoC */ +#define AO_LOG_FORMAT_TELEMEGA_3 15 /* 32 byte typed telemega records with 32 bit gyro cal and mpu9250 */ #define AO_LOG_FORMAT_NONE 127 /* No log at all */ /* Return the flight number from the given log slot, 0 if none, -slot on failure */ @@ -473,7 +474,7 @@ struct ao_log_gps { } u; }; -#if AO_LOG_FORMAT == AO_LOG_FOMAT_TELEMEGA_OLD || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA +#if AO_LOG_FORMAT == AO_LOG_FOMAT_TELEMEGA_OLD || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA_3 typedef struct ao_log_mega ao_log_type; #endif diff --git a/src/kernel/ao_log_mega.c b/src/kernel/ao_log_mega.c index d1cf4f13..c6bdf1e2 100644 --- a/src/kernel/ao_log_mega.c +++ b/src/kernel/ao_log_mega.c @@ -93,6 +93,17 @@ ao_log(void) log.u.sensor.mag_x = ao_data_ring[ao_log_data_pos].hmc5883.x; log.u.sensor.mag_z = ao_data_ring[ao_log_data_pos].hmc5883.z; log.u.sensor.mag_y = ao_data_ring[ao_log_data_pos].hmc5883.y; +#endif +#if HAS_MPU9250 + log.u.sensor.accel_x = ao_data_ring[ao_log_data_pos].mpu9250.accel_x; + log.u.sensor.accel_y = ao_data_ring[ao_log_data_pos].mpu9250.accel_y; + log.u.sensor.accel_z = ao_data_ring[ao_log_data_pos].mpu9250.accel_z; + log.u.sensor.gyro_x = ao_data_ring[ao_log_data_pos].mpu9250.gyro_x; + log.u.sensor.gyro_y = ao_data_ring[ao_log_data_pos].mpu9250.gyro_y; + log.u.sensor.gyro_z = ao_data_ring[ao_log_data_pos].mpu9250.gyro_z; + log.u.sensor.mag_x = ao_data_ring[ao_log_data_pos].mpu9250.mag_x; + log.u.sensor.mag_z = ao_data_ring[ao_log_data_pos].mpu9250.mag_z; + log.u.sensor.mag_y = ao_data_ring[ao_log_data_pos].mpu9250.mag_y; #endif log.u.sensor.accel = ao_data_accel(&ao_data_ring[ao_log_data_pos]); ao_log_write(&log); diff --git a/src/kernel/ao_sample.c b/src/kernel/ao_sample.c index f0ab0169..61519478 100644 --- a/src/kernel/ao_sample.c +++ b/src/kernel/ao_sample.c @@ -184,9 +184,9 @@ ao_sample_rotate(void) #else static const float dt = 1/TIME_DIV; #endif - float x = ao_mpu6000_gyro((float) ((ao_sample_pitch << 9) - ao_ground_pitch) / 512.0f) * dt; - float y = ao_mpu6000_gyro((float) ((ao_sample_yaw << 9) - ao_ground_yaw) / 512.0f) * dt; - float z = ao_mpu6000_gyro((float) ((ao_sample_roll << 9) - ao_ground_roll) / 512.0f) * dt; + float x = ao_convert_gyro((float) ((ao_sample_pitch << 9) - ao_ground_pitch) / 512.0f) * dt; + float y = ao_convert_gyro((float) ((ao_sample_yaw << 9) - ao_ground_yaw) / 512.0f) * dt; + float z = ao_convert_gyro((float) ((ao_sample_roll << 9) - ao_ground_roll) / 512.0f) * dt; struct ao_quaternion rot; ao_quaternion_init_half_euler(&rot, x, y, z); diff --git a/src/kernel/ao_telemetry.c b/src/kernel/ao_telemetry.c index 2ae1e41b..9ed612ce 100644 --- a/src/kernel/ao_telemetry.c +++ b/src/kernel/ao_telemetry.c @@ -141,7 +141,7 @@ ao_send_mega_sensor(void) telemetry.generic.tick = packet->tick; telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR; -#if HAS_MPU6000 +#if HAS_MPU6000 || HAS_MPU9250 telemetry.mega_sensor.orient = ao_sample_orient; #endif telemetry.mega_sensor.accel = ao_data_accel(packet); @@ -164,6 +164,20 @@ ao_send_mega_sensor(void) telemetry.mega_sensor.mag_y = packet->hmc5883.y; #endif +#if HAS_MPU9250 + telemetry.mega_sensor.accel_x = packet->mpu9250.accel_x; + telemetry.mega_sensor.accel_y = packet->mpu9250.accel_y; + telemetry.mega_sensor.accel_z = packet->mpu9250.accel_z; + + telemetry.mega_sensor.gyro_x = packet->mpu9250.gyro_x; + telemetry.mega_sensor.gyro_y = packet->mpu9250.gyro_y; + telemetry.mega_sensor.gyro_z = packet->mpu9250.gyro_z; + + telemetry.mega_sensor.mag_x = packet->mpu9250.mag_x; + telemetry.mega_sensor.mag_z = packet->mpu9250.mag_z; + telemetry.mega_sensor.mag_y = packet->mpu9250.mag_y; +#endif + ao_telemetry_send(); } -- cgit v1.2.3