first cut at turnon scripts for EasyTimer v2
[fw/altos] / altoslib / AltosEepromRecordTimer.java
1 /*
2  * Copyright © 2024 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
15 package org.altusmetrum.altoslib_14;
16
17 public class AltosEepromRecordTimer extends AltosEepromRecord {
18         public static final int record_length = 32;
19
20         private int log_format;
21
22         private int imu_type() {
23                 switch (log_format) {
24                 case AltosLib.AO_LOG_FORMAT_EASYTIMER_2:
25                         return AltosIMU.imu_type_easytimer_v2;
26                 default:
27                         return AltosLib.MISSING;
28                 }
29         }
30
31         private int imu_model() {
32                 switch (log_format) {
33                 case AltosLib.AO_LOG_FORMAT_EASYTIMER_2:
34                         return AltosLib.model_bmi088;
35                 }
36                 return AltosLib.MISSING;
37         }
38
39         private boolean sensor_normalized() {
40                 switch (log_format) {
41                 case AltosLib.AO_LOG_FORMAT_EASYTIMER_2:
42                         return true;
43                 }
44                 return false;
45         }
46
47         private int mag_model() {
48                 switch (log_format) {
49                 case AltosLib.AO_LOG_FORMAT_EASYTIMER_2:
50                         return AltosLib.model_mmc5983;
51                 }
52                 return AltosLib.MISSING;
53         }
54
55         /* AO_LOG_FLIGHT elements */
56         private int flight() { return data16(0); }
57         private int ground_accel() { return data16(2); }
58         private int ground_pres() { return AltosLib.MISSING; }
59         private int ground_accel_along() { return data16(4); }
60         private int ground_accel_across() { return data16(6); }
61         private int ground_accel_through() { return data16(8); }
62         private int ground_roll() { return data32(12); }
63         private int ground_pitch() { return data32(16); }
64         private int ground_yaw() { return data32(20); }
65
66         /* AO_LOG_STATE elements */
67         private int state() { return data16(0); }
68         private int reason() { return data16(2); }
69
70         /* AO_LOG_SENSOR elements */
71
72         private int accel_along() { return data16(0); }
73         private int accel_across() { return data16(2); }
74         private int accel_through() { return data16(4); }
75         private int gyro_roll() { return data16(6); }
76         private int gyro_pitch() { return data16(8); }
77         private int gyro_yaw() { return data16(10); }
78         private int mag_along() { return data16(12); }
79         private int mag_across() { return data16(14); }
80         private int mag_through() { return data16(16); }
81
82         private int accel() { return -accel_along(); }
83
84         private int v_batt() { return data16(18); }
85         private int v_pbatt() { return data16(20); }
86         private int nsense() { return 2; }
87         private int sense(int i) { return data16(22 + i * 2); }
88         private int pyro() { return data16(26); }
89
90         public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
91                 super.provide_data(listener, cal_data);
92
93                 cal_data.set_imu_type(imu_type());
94                 cal_data.set_imu_model(imu_model());
95                 cal_data.set_mag_model(mag_model());
96
97                 switch (cmd()) {
98                 case AltosLib.AO_LOG_FLIGHT:
99                         cal_data.set_flight(flight());
100                         cal_data.set_ground_accel(ground_accel());
101                         listener.set_accel_ground(cal_data.accel_along(ground_accel_along()),
102                                                   cal_data.accel_across(ground_accel_across()),
103                                                   cal_data.accel_through(ground_accel_through()));
104                         cal_data.set_gyro_zero(ground_roll() / 512.0,
105                                                ground_pitch() / 512.0,
106                                                ground_yaw() / 512.0);
107                         break;
108                 case AltosLib.AO_LOG_STATE:
109                         listener.set_state(state());
110                         break;
111                 case AltosLib.AO_LOG_SENSOR:
112                         AltosConfigData config_data = eeprom.config_data();
113
114                         int     accel_along = accel_along();
115                         int     accel_across = accel_across();
116                         int     accel_through = accel_through();
117                         int     gyro_roll = gyro_roll();
118                         int     gyro_pitch = gyro_pitch();
119                         int     gyro_yaw = gyro_yaw();
120
121                         int     mag_along = mag_along();
122                         int     mag_across = mag_across();
123                         int     mag_through = mag_through();
124
125                         listener.set_accel(cal_data.accel_along(accel_along),
126                                            cal_data.accel_across(accel_across),
127                                            cal_data.accel_through(accel_through));
128                         listener.set_gyro(cal_data.gyro_roll(gyro_roll),
129                                           cal_data.gyro_pitch(gyro_pitch),
130                                           cal_data.gyro_yaw(gyro_yaw));
131
132                         listener.set_mag(cal_data.mag_along(mag_along),
133                                          cal_data.mag_across(mag_across),
134                                          cal_data.mag_through(mag_through));
135
136                         listener.set_acceleration(cal_data.acceleration(accel()));
137
138                         listener.set_battery_voltage(AltosConvert.mega_battery_voltage(v_batt()));
139                         listener.set_pyro_voltage(AltosConvert.mega_pyro_voltage(v_pbatt()));
140
141                         int nsense = nsense();
142
143                         listener.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-2)));
144                         listener.set_main_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-1)));
145
146                         double voltages[] = new double[nsense-2];
147                         for (int i = 0; i < nsense-2; i++)
148                                 voltages[i] = AltosConvert.mega_pyro_voltage(sense(i));
149
150                         listener.set_igniter_voltage(voltages);
151                         listener.set_pyro_fired(pyro());
152                         break;
153                 }
154         }
155
156         public AltosEepromRecord next() {
157                 int     s = next_start();
158                 if (s < 0)
159                         return null;
160                 return new AltosEepromRecordTimer(eeprom, s);
161         }
162
163         public AltosEepromRecordTimer(AltosEeprom eeprom, int start) {
164                 super(eeprom, start, record_length);
165                 log_format = eeprom.config_data().log_format;
166         }
167
168         public AltosEepromRecordTimer(AltosEeprom eeprom) {
169                 this(eeprom, 0);
170         }
171 }