2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #define GRAVITY 9.80665
25 #define AO_DATA_ADC (1 << 0)
31 #include <ao_ms5607.h>
32 #define AO_DATA_MS5607 (1 << 1)
34 #define AO_DATA_MS5607 0
38 #include <ao_mpu6000.h>
39 #define AO_DATA_MPU6000 (1 << 2)
41 #define AO_DATA_MPU6000 0
45 #include <ao_mpu9250.h>
46 #define AO_DATA_MPU9250 (1 << 2)
48 #define AO_DATA_MPU9250 0
52 #include <ao_hmc5883.h>
53 #define AO_DATA_HMC5883 (1 << 3)
55 #define AO_DATA_HMC5883 0
59 #include <ao_mmc5983.h>
60 #define AO_DATA_MMC5983 (1 << 3)
62 #define AO_DATA_MMC5983 0
66 #include <ao_mma655x.h>
67 #define AO_DATA_MMA655X (1 << 4)
69 #define AO_DATA_MMA655X 0
73 #include <ao_adxl375.h>
74 #define AO_DATA_ADXL375 (1 << 4)
76 #define AO_DATA_ADXL375 0
80 #include <ao_max6691.h>
81 #define AO_DATA_MAX6691 (1 << 4)
83 #define AO_DATA_MAX6691 0
87 #include <ao_bmx160.h>
88 #define AO_DATA_BMX160 (1 << 2)
90 #define AO_DATA_BMX160 0
93 #ifndef HAS_SENSOR_ERRORS
94 #if HAS_IMU || HAS_MMA655X || HAS_MS5607 || HAS_MS5611
95 #define HAS_SENSOR_ERRORS 1
100 extern uint8_t ao_sensor_errors;
105 #define AO_DATA_ALL (AO_DATA_ADC|AO_DATA_MS5607|AO_DATA_MPU6000|AO_DATA_HMC5883|AO_DATA_MMA655X|AO_DATA_MPU9250|AO_DATA_ADXL375|AO_DATA_BMX160|AO_DATA_MMC5983)
113 struct ao_ms5607_sample ms5607_raw;
114 struct ao_ms5607_value ms5607_cooked;
117 struct ao_mpu6000_sample mpu6000;
123 struct ao_mpu9250_sample mpu9250;
129 struct ao_hmc5883_sample hmc5883;
132 struct ao_mmc5983_sample mmc5983;
138 struct ao_adxl375_sample adxl375;
141 struct ao_max6691_sample max6691;
144 struct ao_ads131a0x_sample ads131a0x;
147 struct ao_bmx160_sample bmx160;
154 #define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1))
155 #define ao_data_ring_prev(n) (((n) - 1) & (AO_DATA_RING - 1))
157 /* Get a copy of the last complete sample set */
159 ao_data_get(struct ao_data *packet);
161 extern volatile struct ao_data ao_data_ring[AO_DATA_RING];
162 extern volatile uint8_t ao_data_head;
163 extern volatile uint8_t ao_data_present;
164 extern volatile uint8_t ao_data_count;
167 * Mark a section of data as ready, check for data complete
169 #define AO_DATA_PRESENT(bit) (ao_data_present |= (bit))
172 * Mark sensor failed, and unblock the sample collection code by
173 * marking the data as present
175 #define AO_SENSOR_ERROR(bit) (ao_data_present |= (ao_sensor_errors |= (bit)))
178 * Wait until it is time to write a sensor sample; this is
179 * signaled by the timer tick
181 #define AO_DATA_WAIT() ao_sleep((void *) &ao_data_count)
183 #endif /* AO_DATA_RING */
185 #if !HAS_BARO && HAS_MS5607
187 /* Either an MS5607 or an MS5611 hooked to a SPI port
192 typedef int32_t pres_t;
194 #define AO_ALT_TYPE int32_t
196 typedef AO_ALT_TYPE alt_t;
198 #define ao_data_pres_cook(packet) ao_ms5607_convert(&packet->ms5607_raw, &packet->ms5607_cooked)
200 #define ao_data_pres(packet) ((packet)->ms5607_cooked.pres)
201 #define ao_data_temp(packet) ((int16_t) (packet)->ms5607_cooked.temp)
203 #define pres_to_altitude(p) ao_pa_to_altitude(p)
208 * Need a few macros to pull data from the sensors:
210 * ao_data_accel_raw - pull raw sensor
211 * ao_data_accel_invert - flip rocket ends for positive acceleration
216 /* This section is for an analog accelerometer hooked to one of the ADC pins. As
217 * those are 5V parts, this also requires that the 5V supply be hooked to to anothe ADC
218 * pin so that the both can be measured to correct for changes between the 3.3V and 5V rails
221 typedef int16_t accel_t;
222 #define ao_data_accel_raw(packet) ((packet)->adc.accel)
223 #define ao_data_accel_invert(a) (0x7fff -(a))
226 * Ok, the math here is a bit tricky.
228 * ao_sample_accel: ADC output for acceleration
229 * ao_accel_ref: ADC output for the 5V reference.
230 * ao_cook_accel: Corrected acceleration value
231 * Vcc: 3.3V supply to the CC1111
232 * Vac: 5V supply to the accelerometer
233 * accel: input voltage to accelerometer ADC pin
234 * ref: input voltage to 5V reference ADC pin
237 * Measured acceleration is ratiometric to Vcc:
239 * ao_sample_accel accel
240 * ------------ = -----
243 * Measured 5v reference is also ratiometric to Vcc:
246 * ------------ = -----
250 * ao_accel_ref = 32767 * (ref / Vcc)
252 * Acceleration is measured ratiometric to the 5V supply,
253 * so what we want is:
255 * ao_cook_accel accel
256 * ------------- = -----
264 * ao_sample_accel 32767
265 * = ------------ * ------------
268 * Multiply through by 32767:
270 * ao_sample_accel * 32767
271 * ao_cook_accel = --------------------
274 * Now, the tricky part. Getting this to compile efficiently
275 * and keeping all of the values in-range.
277 * First off, we need to use a shift of 16 instead of * 32767 as SDCC
278 * does the obvious optimizations for byte-granularity shifts:
280 * ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref
282 * Next, lets check our input ranges:
284 * 0 <= ao_sample_accel <= 0x7fff (singled ended ADC conversion)
285 * 0x7000 <= ao_accel_ref <= 0x7fff (the 5V ref value is close to 0x7fff)
287 * Plugging in our input ranges, we get an output range of 0 - 0x12490,
288 * which is 17 bits. That won't work. If we take the accel ref and shift
289 * by a bit, we'll change its range:
291 * 0xe000 <= ao_accel_ref<<1 <= 0xfffe
293 * ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1)
295 * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
296 * is, however, one bit too large for our signed computations. So, we
297 * take the result and shift that by a bit:
299 * ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1
301 * This finally creates an output range of 0 - 0x4924. As the ADC only
302 * provides 11 bits of data, we haven't actually lost any precision,
303 * just dropped a bit of noise off the low end.
308 #define ao_data_accel_raw(packet) \
309 ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->adc.accel_ref << 1))) >> 1)
313 #define ao_data_accel_raw(packet) ((packet)->adc.accel)
315 #endif /* HAS_ACCEL_REF */
317 #endif /* HAS_ACCEL */
319 #if !HAS_ACCEL && HAS_MMA655X
323 typedef int16_t accel_t;
325 /* MMA655X is hooked up so that positive values represent negative acceleration */
327 #define AO_ACCEL_INVERT 4095
329 #ifndef AO_MMA655X_INVERT
330 #error AO_MMA655X_INVERT not defined
333 #if AO_MMA655X_INVERT
334 #define ao_data_accel_raw(packet) ((accel_t) (AO_ACCEL_INVERT - (packet)->mma655x))
336 #define ao_data_accel_raw(packet) ((accel_t) (packet)->mma655x)
338 #define ao_data_accel_invert(accel) (AO_ACCEL_INVERT - (accel))
342 #if !HAS_ACCEL && HAS_ADXL375
346 typedef int16_t accel_t;
348 #ifndef AO_ADXL375_INVERT
349 #error AO_ADXL375_INVERT not defined
352 #if AO_ADXL375_INVERT
353 #define ao_data_accel_raw(packet) (-(packet)->adxl375.AO_ADXL375_AXIS)
355 #define ao_data_accel_raw(packet) ((packet)->adxl375.AO_ADXL375_AXIS)
357 #define ao_data_accel_invert(accel) (-(accel))
360 #define ao_data_along(packet) ((packet)->adxl375.AO_ADXL375_AXIS)
361 #define ao_data_across(packet) ((packet)->adxl375.AO_ADXL375_ACROSS_AXIS)
362 #define ao_data_through(packet) ((packet)->adxl375.z)
363 #define ao_data_accel_to_sample(accel) ao_adxl375_accel_to_sample(accel)
366 #endif /* HAS_ADXL375 */
368 #if !HAS_ACCEL && HAS_MPU6000
372 typedef int16_t accel_t;
374 /* MPU6000 is hooked up so that positive y is positive acceleration */
375 #define ao_data_accel_raw(packet) (-(packet)->mpu6000.accel_y)
376 #define ao_data_accel_invert(a) (-(a))
380 #if !HAS_GYRO && HAS_MPU6000
384 typedef int16_t gyro_t; /* in raw sample units */
385 typedef int16_t angle_t; /* in degrees */
387 /* Y axis is aligned with the direction of motion (along) */
388 /* X axis is aligned in the other board axis (across) */
389 /* Z axis is aligned perpendicular to the board (through) */
391 #ifndef ao_data_along
392 #define ao_data_along(packet) ((packet)->mpu6000.accel_y)
393 #define ao_data_across(packet) ((packet)->mpu6000.accel_x)
394 #define ao_data_through(packet) ((packet)->mpu6000.accel_z)
396 #define ao_data_roll(packet) ((packet)->mpu6000.gyro_y)
397 #define ao_data_pitch(packet) ((packet)->mpu6000.gyro_x)
398 #define ao_data_yaw(packet) ((packet)->mpu6000.gyro_z)
401 static inline float ao_convert_gyro(float sensor)
403 return ao_mpu6000_gyro(sensor);
406 static inline float ao_convert_accel(int16_t sensor)
408 return ao_mpu6000_accel(sensor);
413 #if !HAS_ACCEL && HAS_MPU9250
417 typedef int16_t accel_t;
419 /* MPU9250 is hooked up so that positive y is positive acceleration */
420 #define ao_data_accel_raw(packet) (-(packet)->mpu9250.accel_y)
421 #define ao_data_accel_invert(a) (-(a))
425 #if !HAS_GYRO && HAS_MPU9250
429 typedef int16_t gyro_t; /* in raw sample units */
430 typedef int16_t angle_t; /* in degrees */
432 /* Y axis is aligned with the direction of motion (along) */
433 /* X axis is aligned in the other board axis (across) */
434 /* Z axis is aligned perpendicular to the board (through) */
436 #ifndef ao_data_along
437 #define ao_data_along(packet) ((packet)->mpu9250.accel_y)
438 #define ao_data_across(packet) ((packet)->mpu9250.accel_x)
439 #define ao_data_through(packet) ((packet)->mpu9250.accel_z)
441 #define ao_data_roll(packet) ((packet)->mpu9250.gyro_y)
442 #define ao_data_pitch(packet) ((packet)->mpu9250.gyro_x)
443 #define ao_data_yaw(packet) ((packet)->mpu9250.gyro_z)
446 static inline float ao_convert_gyro(float sensor)
448 return ao_mpu9250_gyro(sensor);
451 static inline float ao_convert_accel(int16_t sensor)
453 return ao_mpu9250_accel(sensor);
458 #if !HAS_ACCEL && HAS_BMX160
462 typedef int16_t accel_t;
464 #define ao_data_accel_raw(packet) -ao_data_along(packet)
465 #define ao_data_accel_invert(a) (-(a))
466 #define ao_data_accel_to_sample(accel) ao_bmx_accel_to_sample(accel)
470 #if !HAS_GYRO && HAS_BMX160
474 typedef int16_t gyro_t; /* in raw sample units */
475 typedef int16_t angle_t; /* in degrees */
477 /* X axis is aligned with the direction of motion (along) */
478 /* Y axis is aligned in the other board axis (across) */
479 /* Z axis is aligned perpendicular to the board (through) */
481 static inline float ao_convert_gyro(float sensor)
483 return ao_bmx160_gyro(sensor);
486 static inline float ao_convert_accel(int16_t sensor)
488 return ao_bmx160_accel(sensor);
493 #if !HAS_MAG && HAS_HMC5883
497 typedef int16_t ao_mag_t; /* in raw sample units */
499 #define ao_data_mag_along(packet) ((packet)->hmc5883.x)
500 #define ao_data_mag_across(packet) ((packet)->hmc5883.y)
501 #define ao_data_mag_through(packet) ((packet)->hmc5883.z)
505 #if !HAS_MAG && HAS_MMC5983
509 typedef int16_t ao_mag_t; /* in raw sample units */
513 #if !HAS_MAG && HAS_MPU9250
517 typedef int16_t ao_mag_t; /* in raw sample units */
519 /* Note that this order is different from the accel and gyro. For some
520 * reason, the mag sensor axes aren't the same as the other two
521 * sensors. Also, the Z axis is flipped in sign.
524 #ifndef ao_data_mag_along
525 #define ao_data_mag_along(packet) ((packet)->mpu9250.mag_x)
526 #define ao_data_mag_across(packet) ((packet)->mpu9250.mag_y)
527 #define ao_data_mag_through(packet) ((packet)->mpu9250.mag_z)
535 ao_data_fill(int head) {
536 if (ao_data_present == AO_DATA_ALL) {
538 ao_data_ring[head].ms5607_raw = ao_ms5607_current;
541 ao_data_ring[head].mma655x = ao_mma655x_current;
544 ao_data_ring[head].hmc5883 = ao_hmc5883_current;
547 ao_data_ring[head].mmc5983 = ao_mmc5983_current;
550 ao_data_ring[head].mpu6000 = ao_mpu6000_current;
553 ao_data_ring[head].mpu9250 = ao_mpu9250_current;
556 ao_data_ring[head].adxl375 = ao_adxl375_current;
559 ao_data_ring[head].max6691 = ao_max6691_current;
562 ao_data_ring[head].ads131a0x = ao_ads131a0x_current;
565 ao_data_ring[head].bmx160 = ao_bmx160_current;
567 ao_data_ring[head].tick = ao_tick_count;
568 ao_data_head = ao_data_ring_next(head);
569 ao_wakeup((void *) &ao_data_head);
577 ao_data_accel(volatile struct ao_data *packet);
580 #endif /* _AO_DATA_H_ */