2 * Copyright © 2011 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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 #ifndef AO_FLIGHT_TEST
23 * Current sensor values
26 __pdata uint16_t ao_sample_tick; /* time of last data */
27 __pdata int16_t ao_sample_pres;
28 __pdata int16_t ao_sample_alt;
29 __pdata int16_t ao_sample_height;
31 __pdata int16_t ao_sample_accel;
34 __data uint8_t ao_sample_adc;
37 * Sensor calibration values
40 __xdata int16_t ao_ground_pres; /* startup pressure */
41 __xdata int16_t ao_ground_height; /* MSL of ao_ground_pres */
44 __xdata int16_t ao_ground_accel; /* startup acceleration */
45 __xdata int16_t ao_accel_2g; /* factory accel calibration */
46 __xdata int32_t ao_accel_scale; /* sensor to m/s² conversion */
49 static __xdata uint8_t ao_preflight; /* in preflight mode */
51 static __xdata uint16_t nsamples;
52 __xdata int32_t ao_sample_pres_sum;
54 __xdata int32_t ao_sample_accel_sum;
58 ao_sample_preflight(void)
62 * Collect 512 samples of acceleration and pressure
63 * data and average them to find the resting values
67 ao_sample_accel_sum += ao_sample_accel;
69 ao_sample_pres_sum += ao_sample_pres;
75 ao_ground_accel = ao_sample_accel_sum >> 9;
76 ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
77 ao_accel_scale = to_fix32(GRAVITY * 2 * 16) / ao_accel_2g;
79 ao_ground_pres = ao_sample_pres_sum >> 9;
80 ao_ground_height = ao_pres_to_altitude(ao_ground_pres);
86 ao_wakeup(DATA_TO_XDATA(&ao_sample_adc));
87 ao_sleep(DATA_TO_XDATA(&ao_adc_head));
88 while (ao_sample_adc != ao_adc_head) {
89 __xdata struct ao_adc *ao_adc;
91 /* Capture a sample */
92 ao_adc = &ao_adc_ring[ao_sample_adc];
93 ao_sample_tick = ao_adc->tick;
94 ao_sample_pres = ao_adc->pres;
95 ao_sample_alt = ao_pres_to_altitude(ao_sample_pres);
96 ao_sample_height = ao_sample_alt - ao_ground_height;
98 ao_sample_accel = ao_adc->accel;
101 * Ok, the math here is a bit tricky.
103 * ao_sample_accel: ADC output for acceleration
104 * ao_accel_ref: ADC output for the 5V reference.
105 * ao_cook_accel: Corrected acceleration value
106 * Vcc: 3.3V supply to the CC1111
107 * Vac: 5V supply to the accelerometer
108 * accel: input voltage to accelerometer ADC pin
109 * ref: input voltage to 5V reference ADC pin
112 * Measured acceleration is ratiometric to Vcc:
114 * ao_sample_accel accel
115 * ------------ = -----
118 * Measured 5v reference is also ratiometric to Vcc:
121 * ------------ = -----
125 * ao_accel_ref = 32767 * (ref / Vcc)
127 * Acceleration is measured ratiometric to the 5V supply,
128 * so what we want is:
130 * ao_cook_accel accel
131 * ------------- = -----
139 * ao_sample_accel 32767
140 * = ------------ * ------------
143 * Multiply through by 32767:
145 * ao_sample_accel * 32767
146 * ao_cook_accel = --------------------
149 * Now, the tricky part. Getting this to compile efficiently
150 * and keeping all of the values in-range.
152 * First off, we need to use a shift of 16 instead of * 32767 as SDCC
153 * does the obvious optimizations for byte-granularity shifts:
155 * ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref
157 * Next, lets check our input ranges:
159 * 0 <= ao_sample_accel <= 0x7fff (singled ended ADC conversion)
160 * 0x7000 <= ao_accel_ref <= 0x7fff (the 5V ref value is close to 0x7fff)
162 * Plugging in our input ranges, we get an output range of 0 - 0x12490,
163 * which is 17 bits. That won't work. If we take the accel ref and shift
164 * by a bit, we'll change its range:
166 * 0xe000 <= ao_accel_ref<<1 <= 0xfffe
168 * ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1)
170 * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
171 * is, however, one bit too large for our signed computations. So, we
172 * take the result and shift that by a bit:
174 * ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1
176 * This finally creates an output range of 0 - 0x4924. As the ADC only
177 * provides 11 bits of data, we haven't actually lost any precision,
178 * just dropped a bit of noise off the low end.
180 ao_sample_accel = (uint16_t) ((((uint32_t) ao_sample_accel << 16) / (ao_accel_ref[ao_sample_adc] << 1))) >> 1;
181 ao_adc->accel = ao_sample_accel;
186 ao_sample_preflight();
189 ao_sample_adc = ao_adc_ring_next(ao_sample_adc);
191 return !ao_preflight;
198 ao_sample_pres_sum = 0;
201 ao_sample_accel_sum = 0;
204 ao_sample_adc = ao_adc_head;