altos: struct ao_log_mega doesn't have a ground temp value
[fw/altos] / src / core / 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19 #include <ao_log.h>
20 #include <ao_data.h>
21 #include <ao_flight.h>
22
23 static __xdata uint8_t  ao_log_mutex;
24 static __xdata struct ao_log_mega log;
25
26 __code uint8_t ao_log_format = AO_LOG_FORMAT_MEGAMETRUM;
27
28 static uint8_t
29 ao_log_csum(__xdata uint8_t *b) __reentrant
30 {
31         uint8_t sum = 0x5a;
32         uint8_t i;
33
34         for (i = 0; i < sizeof (struct ao_log_mega); i++)
35                 sum += *b++;
36         return -sum;
37 }
38
39 uint8_t
40 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant
41 {
42         uint8_t wrote = 0;
43         /* set checksum */
44         log->csum = 0;
45         log->csum = ao_log_csum((__xdata uint8_t *) log);
46         ao_mutex_get(&ao_log_mutex); {
47                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
48                         ao_log_stop();
49                 if (ao_log_running) {
50                         wrote = 1;
51                         ao_storage_write(ao_log_current_pos,
52                                          log,
53                                          sizeof (struct ao_log_mega));
54                         ao_log_current_pos += sizeof (struct ao_log_mega);
55                 }
56         } ao_mutex_put(&ao_log_mutex);
57         return wrote;
58 }
59
60 static uint8_t
61 ao_log_dump_check_data(void)
62 {
63         if (ao_log_csum((uint8_t *) &log) != 0)
64                 return 0;
65         return 1;
66 }
67
68 static __data uint8_t   ao_log_data_pos;
69
70 /* a hack to make sure that ao_log_megas fill the eeprom block in even units */
71 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_mega))] ;
72
73 #ifndef AO_SENSOR_INTERVAL_ASCENT
74 #define AO_SENSOR_INTERVAL_ASCENT       1
75 #define AO_SENSOR_INTERVAL_DESCENT      10
76 #define AO_OTHER_INTERVAL               32
77 #endif
78
79 void
80 ao_log(void)
81 {
82         __pdata uint16_t        next_sensor, next_other;
83         uint8_t                 i;
84
85         ao_storage_setup();
86
87         ao_log_scan();
88
89         while (!ao_log_running)
90                 ao_sleep(&ao_log_running);
91
92 #if HAS_FLIGHT
93         log.type = AO_LOG_FLIGHT;
94         log.tick = ao_sample_tick;
95 #if HAS_ACCEL
96         log.u.flight.ground_accel = ao_ground_accel;
97 #endif
98         log.u.flight.ground_pres = ao_ground_pres;
99         log.u.flight.flight = ao_flight_number;
100         ao_log_mega(&log);
101 #endif
102
103         /* Write the whole contents of the ring to the log
104          * when starting up.
105          */
106         ao_log_data_pos = ao_data_ring_next(ao_data_head);
107         next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
108         ao_log_state = ao_flight_startup;
109         for (;;) {
110                 /* Write samples to EEPROM */
111                 while (ao_log_data_pos != ao_data_head) {
112                         log.tick = ao_data_ring[ao_log_data_pos].tick;
113                         if ((int16_t) (log.tick - next_sensor) >= 0) {
114                                 log.type = AO_LOG_SENSOR;
115 #if HAS_MS5607
116                                 log.u.sensor.pres = ao_data_ring[ao_log_data_pos].ms5607_raw.pres;
117                                 log.u.sensor.temp = ao_data_ring[ao_log_data_pos].ms5607_raw.temp;
118 #endif
119 #if HAS_MPU6000
120                                 log.u.sensor.accel_x = ao_data_ring[ao_log_data_pos].mpu6000.accel_x;
121                                 log.u.sensor.accel_y = ao_data_ring[ao_log_data_pos].mpu6000.accel_y;
122                                 log.u.sensor.accel_z = ao_data_ring[ao_log_data_pos].mpu6000.accel_z;
123                                 log.u.sensor.gyro_x = ao_data_ring[ao_log_data_pos].mpu6000.gyro_x;
124                                 log.u.sensor.gyro_y = ao_data_ring[ao_log_data_pos].mpu6000.gyro_y;
125                                 log.u.sensor.gyro_z = ao_data_ring[ao_log_data_pos].mpu6000.gyro_z;
126 #endif
127 #if HAS_HMC5883
128                                 log.u.sensor.mag_x = ao_data_ring[ao_log_data_pos].hmc5883.x;
129                                 log.u.sensor.mag_y = ao_data_ring[ao_log_data_pos].hmc5883.y;
130                                 log.u.sensor.mag_z = ao_data_ring[ao_log_data_pos].hmc5883.z;
131 #endif
132                                 log.u.sensor.accel = ao_data_accel(&ao_data_ring[ao_log_data_pos]);
133                                 ao_log_mega(&log);
134                                 if (ao_log_state <= ao_flight_coast)
135                                         next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT;
136                                 else
137                                         next_sensor = log.tick + AO_SENSOR_INTERVAL_DESCENT;
138                         }
139                         if ((int16_t) (log.tick - next_other) >= 0) {
140                                 log.type = AO_LOG_TEMP_VOLT;
141                                 log.u.volt.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
142                                 log.u.volt.v_pbatt = ao_data_ring[ao_log_data_pos].adc.v_pbatt;
143                                 log.u.volt.n_sense = AO_ADC_NUM_SENSE;
144                                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
145                                         log.u.volt.sense[i] = ao_data_ring[ao_log_data_pos].adc.sense[i];
146                                 ao_log_mega(&log);
147                                 next_other = log.tick + AO_OTHER_INTERVAL;
148                         }
149                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
150                 }
151 #if HAS_FLIGHT
152                 /* Write state change to EEPROM */
153                 if (ao_flight_state != ao_log_state) {
154                         ao_log_state = ao_flight_state;
155                         log.type = AO_LOG_STATE;
156                         log.tick = ao_time();
157                         log.u.state.state = ao_log_state;
158                         log.u.state.reason = 0;
159                         ao_log_mega(&log);
160
161                         if (ao_log_state == ao_flight_landed)
162                                 ao_log_stop();
163                 }
164 #endif
165
166                 /* Wait for a while */
167                 ao_delay(AO_MS_TO_TICKS(100));
168
169                 /* Stop logging when told to */
170                 while (!ao_log_running)
171                         ao_sleep(&ao_log_running);
172         }
173 }
174
175 uint16_t
176 ao_log_flight(uint8_t slot)
177 {
178         if (!ao_storage_read(ao_log_pos(slot),
179                              &log,
180                              sizeof (struct ao_log_mega)))
181                 return 0;
182
183         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
184                 return log.u.flight.flight;
185         return 0;
186 }