altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / kernel / ao_log_mega.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "ao.h"
20 #include <ao_log.h>
21 #include <ao_data.h>
22 #include <ao_flight.h>
23
24 #if HAS_FLIGHT
25 static uint8_t  ao_log_data_pos;
26
27 /* a hack to make sure that ao_log_megas fill the eeprom block in even units */
28 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_mega))] ;
29
30 #ifndef AO_SENSOR_INTERVAL_ASCENT
31 #define AO_SENSOR_INTERVAL_ASCENT       1
32 #define AO_SENSOR_INTERVAL_DESCENT      10
33 #define AO_OTHER_INTERVAL               32
34 #endif
35
36 void
37 ao_log(void)
38 {
39         AO_TICK_TYPE            next_sensor, next_other;
40         uint8_t                 i;
41
42         ao_storage_setup();
43
44         ao_log_scan();
45
46         while (!ao_log_running)
47                 ao_sleep(&ao_log_running);
48
49 #if HAS_FLIGHT
50         ao_log_data.type = AO_LOG_FLIGHT;
51         ao_log_data.tick = (uint16_t) ao_sample_tick;
52 #if HAS_ACCEL
53         ao_log_data.u.flight.ground_accel = ao_ground_accel;
54 #endif
55 #if HAS_GYRO
56         ao_log_data.u.flight.ground_accel_along = ao_ground_accel_along;
57         ao_log_data.u.flight.ground_accel_across = ao_ground_accel_across;
58         ao_log_data.u.flight.ground_accel_through = ao_ground_accel_through;
59         ao_log_data.u.flight.ground_roll = ao_ground_roll;
60         ao_log_data.u.flight.ground_pitch = ao_ground_pitch;
61         ao_log_data.u.flight.ground_yaw = ao_ground_yaw;
62 #endif
63         ao_log_data.u.flight.ground_pres = ao_ground_pres;
64         ao_log_data.u.flight.flight = ao_flight_number;
65         ao_log_write(&ao_log_data);
66 #endif
67
68         /* Write the whole contents of the ring to the log
69          * when starting up.
70          */
71         ao_log_data_pos = ao_data_ring_next(ao_data_head);
72         next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
73         ao_log_state = ao_flight_startup;
74         for (;;) {
75                 /* Write samples to EEPROM */
76                 while (ao_log_data_pos != ao_data_head) {
77                         AO_TICK_TYPE tick = ao_data_ring[ao_log_data_pos].tick;
78                         ao_log_data.tick = (uint16_t) tick;
79                         volatile struct ao_data *d = &ao_data_ring[ao_log_data_pos];
80                         if ((AO_TICK_SIGNED) (tick - next_sensor) >= 0) {
81                                 ao_log_data.type = AO_LOG_SENSOR;
82 #if HAS_MS5607
83                                 ao_log_data.u.sensor.pres = d->ms5607_raw.pres;
84                                 ao_log_data.u.sensor.temp = d->ms5607_raw.temp;
85 #endif
86 #if HAS_MPU6000
87 #ifdef AO_LOG_NORMALIZED
88                                 ao_log_data.u.sensor.accel_along = ao_data_along(d);
89                                 ao_log_data.u.sensor.accel_across = ao_data_across(d);
90                                 ao_log_data.u.sensor.accel_through = ao_data_through(d);
91                                 ao_log_data.u.sensor.gyro_roll = ao_data_roll(d);
92                                 ao_log_data.u.sensor.gyro_pitch = ao_data_pitch(d);
93                                 ao_log_data.u.sensor.gyro_yaw = ao_data_yaw(d);
94 #else
95                                 ao_log_data.u.sensor.accel_x = d->mpu6000.accel_x;
96                                 ao_log_data.u.sensor.accel_y = d->mpu6000.accel_y;
97                                 ao_log_data.u.sensor.accel_z = d->mpu6000.accel_z;
98                                 ao_log_data.u.sensor.gyro_x = d->mpu6000.gyro_x;
99                                 ao_log_data.u.sensor.gyro_y = d->mpu6000.gyro_y;
100                                 ao_log_data.u.sensor.gyro_z = d->mpu6000.gyro_z;
101 #endif
102 #endif
103 #if HAS_HMC5883
104                                 ao_log_data.u.sensor.mag_x = d->hmc5883.x;
105                                 ao_log_data.u.sensor.mag_z = d->hmc5883.z;
106                                 ao_log_data.u.sensor.mag_y = d->hmc5883.y;
107 #endif
108 #ifdef HAS_MMC5983
109                                 ao_log_data.u.sensor.mag_along = ao_data_mag_along(d);
110                                 ao_log_data.u.sensor.mag_across = ao_data_mag_across(d);
111                                 ao_log_data.u.sensor.mag_through = ao_data_mag_through(d);
112 #endif
113 #if HAS_MPU9250
114                                 ao_log_data.u.sensor.accel_x = d->mpu9250.accel_x;
115                                 ao_log_data.u.sensor.accel_y = d->mpu9250.accel_y;
116                                 ao_log_data.u.sensor.accel_z = d->mpu9250.accel_z;
117                                 ao_log_data.u.sensor.gyro_x = d->mpu9250.gyro_x;
118                                 ao_log_data.u.sensor.gyro_y = d->mpu9250.gyro_y;
119                                 ao_log_data.u.sensor.gyro_z = d->mpu9250.gyro_z;
120                                 ao_log_data.u.sensor.mag_x = d->mpu9250.mag_x;
121                                 ao_log_data.u.sensor.mag_z = d->mpu9250.mag_z;
122                                 ao_log_data.u.sensor.mag_y = d->mpu9250.mag_y;
123 #endif
124 #if HAS_BMX160
125                                 ao_log_data.u.sensor.accel_x = d->bmx160.acc_x;
126                                 ao_log_data.u.sensor.accel_y = d->bmx160.acc_y;
127                                 ao_log_data.u.sensor.accel_z = d->bmx160.acc_z;
128                                 ao_log_data.u.sensor.gyro_x = d->bmx160.gyr_x;
129                                 ao_log_data.u.sensor.gyro_y = d->bmx160.gyr_y;
130                                 ao_log_data.u.sensor.gyro_z = d->bmx160.gyr_z;
131                                 ao_log_data.u.sensor.mag_x = d->bmx160.mag_x;
132                                 ao_log_data.u.sensor.mag_z = d->bmx160.mag_z;
133                                 ao_log_data.u.sensor.mag_y = d->bmx160.mag_y;
134 #endif
135                                 ao_log_data.u.sensor.accel = ao_data_accel(&ao_data_ring[ao_log_data_pos]);
136                                 ao_log_write(&ao_log_data);
137                                 if (ao_log_state <= ao_flight_coast)
138                                         next_sensor = tick + AO_SENSOR_INTERVAL_ASCENT;
139                                 else
140                                         next_sensor = tick + AO_SENSOR_INTERVAL_DESCENT;
141                         }
142                         if ((AO_TICK_SIGNED) (tick - next_other) >= 0) {
143                                 ao_log_data.type = AO_LOG_TEMP_VOLT;
144                                 ao_log_data.u.volt.v_batt = d->adc.v_batt;
145                                 ao_log_data.u.volt.v_pbatt = d->adc.v_pbatt;
146                                 ao_log_data.u.volt.n_sense = AO_ADC_NUM_SENSE;
147                                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
148                                         ao_log_data.u.volt.sense[i] = d->adc.sense[i];
149                                 ao_log_data.u.volt.pyro = ao_pyro_fired;
150                                 ao_log_write(&ao_log_data);
151                                 next_other = tick + AO_OTHER_INTERVAL;
152                         }
153                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
154                 }
155 #if HAS_FLIGHT
156                 /* Write state change to EEPROM */
157                 if (ao_flight_state != ao_log_state) {
158                         ao_log_state = ao_flight_state;
159                         ao_log_data.type = AO_LOG_STATE;
160                         ao_log_data.tick = (uint16_t) ao_time();
161                         ao_log_data.u.state.state = ao_log_state;
162                         ao_log_data.u.state.reason = 0;
163                         ao_log_write(&ao_log_data);
164
165                         if (ao_log_state == ao_flight_landed)
166                                 ao_log_stop();
167                 }
168 #endif
169
170                 ao_log_flush();
171
172                 /* Wait for a while */
173                 ao_delay(AO_MS_TO_TICKS(100));
174
175                 /* Stop logging when told to */
176                 while (!ao_log_running)
177                         ao_sleep(&ao_log_running);
178         }
179 }
180 #endif /* HAS_FLIGHT */
181