altos: Switch all tick variables to AO_TICK_TYPE/AO_TICK_SIGNED
[fw/altos] / src / kernel / ao_log_motor.c
1 /*
2  * Copyright © 2020 Bdale Garbee <bdale@gag.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         uint16_t        next_sensor;
40
41         ao_storage_setup();
42
43         ao_log_scan();
44
45         while (!ao_log_running)
46                 ao_sleep(&ao_log_running);
47
48         ao_log_data.type = AO_LOG_FLIGHT;
49         ao_log_data.tick = ao_sample_tick;
50         ao_log_data.u.flight.ground_accel = ao_ground_accel;
51         ao_log_data.u.flight.ground_accel_along = ao_ground_accel_along;
52         ao_log_data.u.flight.ground_accel_through = ao_ground_accel_through;
53         ao_log_data.u.flight.ground_motor_pressure = ao_ground_motor_pressure;
54         ao_log_data.u.flight.flight = ao_flight_number;
55         ao_log_write(&ao_log_data);
56
57         /* Write the whole contents of the ring to the log
58          * when starting up.
59          */
60         ao_log_data_pos = ao_data_ring_next(ao_data_head);
61         next_sensor = ao_data_ring[ao_log_data_pos].tick;
62         ao_log_state = ao_flight_startup;
63         for (;;) {
64                 /* Write samples to EEPROM */
65                 while (ao_log_data_pos != ao_data_head) {
66                         AO_TICK_TYPE tick = ao_data_ring[ao_log_data_pos].tick;
67                         ao_log_data.tick = tick;
68                         if ((AO_TICK_SIGNED) (tick - next_sensor) >= 0) {
69                                 ao_log_data.type = AO_LOG_SENSOR;
70                                 ao_log_data.u.sensor.pressure = ao_data_motor_pressure(&ao_data_ring[ao_log_data_pos]);
71                                 ao_log_data.u.sensor.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
72                                 ao_log_data.u.sensor.accel = ao_data_accel(&ao_data_ring[ao_log_data_pos]);
73                                 ao_log_data.u.sensor.accel_across = ao_data_across(&ao_data_ring[ao_log_data_pos]);
74                                 ao_log_data.u.sensor.accel_along = ao_data_along(&ao_data_ring[ao_log_data_pos]);
75                                 ao_log_data.u.sensor.accel_through = ao_data_through(&ao_data_ring[ao_log_data_pos]);
76                                 ao_log_write(&ao_log_data);
77                                 if (ao_log_state <= ao_flight_coast)
78                                         next_sensor = tick + AO_SENSOR_INTERVAL_ASCENT;
79                                 else
80                                         next_sensor = tick + AO_SENSOR_INTERVAL_DESCENT;
81                         }
82                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
83                 }
84 #if HAS_FLIGHT
85                 /* Write state change to EEPROM */
86                 if (ao_flight_state != ao_log_state) {
87                         ao_log_state = ao_flight_state;
88                         ao_log_data.type = AO_LOG_STATE;
89                         ao_log_data.tick = ao_time();
90                         ao_log_data.u.state.state = ao_log_state;
91                         ao_log_data.u.state.reason = 0;
92                         ao_log_write(&ao_log_data);
93
94                         if (ao_log_state == ao_flight_landed)
95                                 ao_log_stop();
96                 }
97 #endif
98
99                 ao_log_flush();
100
101                 /* Wait for a while */
102                 ao_delay(AO_MS_TO_TICKS(100));
103
104                 /* Stop logging when told to */
105                 while (!ao_log_running)
106                         ao_sleep(&ao_log_running);
107         }
108 }
109 #endif /* HAS_FLIGHT */
110