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.
34 * For 32-bit computed values, use 64-bit intermediates.
36 typedef int64_t ao_k_t;
37 typedef int32_t ao_v_t;
40 * For 16-bit computed values, use 32-bit intermediates.
42 typedef int32_t ao_k_t;
43 typedef int16_t ao_v_t;
47 * Barometer calibration
49 * We directly sample the barometer. The specs say:
51 * Pressure range: 15-115 kPa
52 * Voltage at 115kPa: 2.82
53 * Output scale: 27mV/kPa
55 * If we want to detect launch with the barometer, we need
56 * a large enough bump to not be fooled by noise. At typical
57 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
58 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
59 * As all of our calculations are done in 16 bits, we'll actually see a change
60 * of 16 times this though
62 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
65 /* Accelerometer calibration
67 * We're sampling the accelerometer through a resistor divider which
68 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
69 * That goes into the cc1111 A/D converter, which is running at 11 bits
70 * of precision with the bits in the MSB of the 16 bit value. Only positive
71 * values are used, so values should range from 0-32752 for 0-3.3V. The
72 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
73 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
74 * for a final computation of:
76 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
78 * Zero g was measured at 16000 (we would expect 16384).
79 * Note that this value is only require to tell if the
80 * rocket is standing upright. Once that is determined,
81 * the value of the accelerometer is averaged for 100 samples
82 * to find the resting accelerometer value, which is used
83 * for all further flight computations
87 * Above this height, the baro sensor doesn't work
90 #define AO_MAX_BARO_HEIGHT 30000
92 #define AO_MAX_BARO_HEIGHT 12000
96 * Above this speed, baro measurements are unreliable
98 #define AO_MAX_BARO_SPEED 248
100 /* The maximum amount (in a range of 0-256) to de-rate the
101 * baro sensor data based on speed.
103 #define AO_MAX_SPEED_DISTRUST 160
105 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
108 * Speed and acceleration are scaled by 16 to provide a bit more
109 * resolution while still having reasonable range. Note that this
110 * limits speed to 2047m/s (around mach 6) and acceleration to
111 * 2047m/s² (over 200g)
114 #define AO_M_TO_HEIGHT(m) ((ao_v_t) (m))
115 #define AO_MS_TO_SPEED(ms) ((ao_v_t) ((ms) * 16))
116 #define AO_MSS_TO_ACCEL(mss) ((ao_v_t) ((mss) * 16))
118 extern uint16_t ao_sample_tick; /* time of last data */
119 extern uint8_t ao_sample_adc; /* Ring position of last processed sample */
120 extern uint8_t ao_sample_data; /* Ring position of last processed sample */
123 extern pres_t ao_sample_pres; /* most recent pressure sensor reading */
124 extern alt_t ao_sample_alt; /* MSL of ao_sample_pres */
125 extern alt_t ao_sample_height; /* AGL of ao_sample_pres */
126 extern pres_t ao_ground_pres; /* startup pressure */
127 extern alt_t ao_ground_height; /* MSL of ao_ground_pres */
131 extern accel_t ao_sample_accel; /* most recent accel sensor reading */
132 extern accel_t ao_ground_accel; /* startup acceleration */
133 extern accel_t ao_accel_2g; /* factory accel calibration */
134 extern int32_t ao_accel_scale; /* sensor to m/s² conversion */
137 extern accel_t ao_ground_accel_along;
138 extern accel_t ao_ground_accel_across;
139 extern accel_t ao_ground_accel_through;
140 extern accel_t ao_sample_accel_along;
141 extern accel_t ao_sample_accel_across;
142 extern accel_t ao_sample_accel_through;
148 extern int32_t ao_ground_pitch; /* * 512 */
149 extern int32_t ao_ground_yaw; /* * 512 */
150 extern int32_t ao_ground_roll; /* * 512 */
151 extern gyro_t ao_sample_roll;
152 extern gyro_t ao_sample_pitch;
153 extern gyro_t ao_sample_yaw;
154 #define AO_NUM_ORIENT 64
155 extern angle_t ao_sample_orient;
156 extern angle_t ao_sample_orients[AO_NUM_ORIENT];
157 extern uint8_t ao_sample_orient_pos;
159 #if HAS_MOTOR_PRESSURE
160 extern motor_pressure_t ao_ground_motor_pressure;
161 extern motor_pressure_t ao_sample_motor_pressure;
164 void ao_sample_init(void);
166 /* returns false in preflight mode, true in flight mode */
167 uint8_t ao_sample(void);
173 #define to_fix_16(x) ((int16_t) ((x) * 65536.0 + 0.5))
174 #define to_fix_32(x) ((int32_t) ((x) * 65536.0 + 0.5))
175 #define to_fix_64(x) ((int64_t) ((x) * 65536.0 + 0.5))
179 #define to_fix_v(x) to_fix_32(x)
180 #define to_fix_k(x) to_fix_64(x)
182 #define to_fix_v(x) to_fix_16(x)
183 #define to_fix_k(x) to_fix_32(x)
186 #define from_fix(x) ((x) >> 16)
188 extern ao_v_t ao_height; /* meters */
189 extern ao_v_t ao_speed; /* m/s * 16 */
190 extern ao_v_t ao_accel; /* m/s² * 16 */
191 extern ao_v_t ao_max_height; /* max of ao_height */
192 extern ao_v_t ao_avg_height; /* running average of height */
194 extern ao_v_t ao_error_h;
196 extern ao_v_t ao_error_h_sq_avg;
200 extern ao_v_t ao_error_a;
204 void ao_kalman(void);
207 void ao_kalman_reset_accumulate(void);
210 #endif /* _AO_SAMPLE_H_ */