]> git.gag.com Git - fw/altos/blob - altoslib/AltosEepromMega.java
altosdroid: Rate limit map loading pacifier
[fw/altos] / altoslib / AltosEepromMega.java
1 /*
2  * Copyright © 2011 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 package org.altusmetrum.altoslib_10;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosEepromMega extends AltosEeprom {
25         public static final int record_length = 32;
26
27         public static final int max_sat = 12;
28
29         private int log_format;
30
31         public int record_length() { return record_length; }
32
33         /* AO_LOG_FLIGHT elements */
34         public int flight() { return data16(0); }
35         public int ground_accel() { return data16(2); }
36         public int ground_pres() { return data32(4); }
37         public int ground_accel_along() { return data16(8); }
38         public int ground_accel_across() { return data16(10); }
39         public int ground_accel_through() { return data16(12); }
40         public int ground_roll() {
41                 switch (log_format) {
42                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
43                         return data32(16);
44                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
45                         return data16(14);
46                 default:
47                         return AltosLib.MISSING;
48                 }
49         }
50         public int ground_pitch() {
51                 switch (log_format) {
52                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
53                         return data32(20);
54                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
55                         return data16(16);
56                 default:
57                         return AltosLib.MISSING;
58                 }
59         }
60         public int ground_yaw() {
61                 switch (log_format) {
62                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
63                         return data32(24);
64                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
65                         return data16(18);
66                 default:
67                         return AltosLib.MISSING;
68                 }
69         }
70
71         /* AO_LOG_STATE elements */
72         public int state() { return data16(0); }
73         public int reason() { return data16(2); }
74
75         /* AO_LOG_SENSOR elements */
76         public int pres() { return data32(0); }
77         public int temp() { return data32(4); }
78         public int accel_x() { return data16(8); }
79         public int accel_y() { return data16(10); }
80         public int accel_z() { return data16(12); }
81         public int gyro_x() { return data16(14); }
82         public int gyro_y() { return data16(16); }
83         public int gyro_z() { return data16(18); }
84         public int mag_x() { return data16(20); }
85         public int mag_y() { return data16(22); }
86         public int mag_z() { return data16(24); }
87         public int accel() { return data16(26); }
88
89         /* AO_LOG_TEMP_VOLT elements */
90         public int v_batt() { return data16(0); }
91         public int v_pbatt() { return data16(2); }
92         public int nsense() { return data16(4); }
93         public int sense(int i) { return data16(6 + i * 2); }
94         public int pyro() { return data16(26); }
95
96         /* AO_LOG_GPS_TIME elements */
97         public int latitude() { return data32(0); }
98         public int longitude() { return data32(4); }
99         public int altitude_low() { return data16(8); }
100         public int hour() { return data8(10); }
101         public int minute() { return data8(11); }
102         public int second() { return data8(12); }
103         public int flags() { return data8(13); }
104         public int year() { return data8(14); }
105         public int month() { return data8(15); }
106         public int day() { return data8(16); }
107         public int course() { return data8(17); }
108         public int ground_speed() { return data16(18); }
109         public int climb_rate() { return data16(20); }
110         public int pdop() { return data8(22); }
111         public int hdop() { return data8(23); }
112         public int vdop() { return data8(24); }
113         public int mode() { return data8(25); }
114         public int altitude_high() { return data16(26); }
115
116         /* AO_LOG_GPS_SAT elements */
117         public int nsat() { return data16(0); }
118         public int svid(int n) { return data8(2 + n * 2); }
119         public int c_n(int n) { return data8(2 + n * 2 + 1); }
120
121         public AltosEepromMega (AltosEepromChunk chunk, int start, int log_format) throws ParseException {
122                 this.log_format = log_format;
123                 parse_chunk(chunk, start);
124         }
125
126         public void update_state(AltosState state) {
127                 super.update_state(state);
128
129                 AltosGPS        gps;
130
131                 /* Flush any pending GPS changes */
132                 if (state.gps_pending) {
133                         switch (cmd) {
134                         case AltosLib.AO_LOG_GPS_LAT:
135                         case AltosLib.AO_LOG_GPS_LON:
136                         case AltosLib.AO_LOG_GPS_ALT:
137                         case AltosLib.AO_LOG_GPS_SAT:
138                         case AltosLib.AO_LOG_GPS_DATE:
139                                 break;
140                         default:
141                                 state.set_temp_gps();
142                                 break;
143                         }
144                 }
145
146                 switch (cmd) {
147                 case AltosLib.AO_LOG_FLIGHT:
148                         state.set_boost_tick(tick);
149                         state.set_flight(flight());
150                         state.set_ground_accel(ground_accel());
151                         state.set_ground_pressure(ground_pres());
152                         state.set_accel_ground(ground_accel_along(),
153                                                ground_accel_across(),
154                                                ground_accel_through());
155                         state.set_gyro_zero(ground_roll() / 512.0,
156                                             ground_pitch() / 512.0,
157                                             ground_yaw() / 512.0);
158                         break;
159                 case AltosLib.AO_LOG_STATE:
160                         state.set_tick(tick);
161                         state.set_state(state());
162                         break;
163                 case AltosLib.AO_LOG_SENSOR:
164                         state.set_tick(tick);
165                         state.set_ms5607(pres(), temp());
166
167                         AltosIMU        imu = new AltosIMU(accel_y(),   /* along */
168                                                            accel_x(),   /* across */
169                                                            accel_z(),   /* through */
170                                                            gyro_y(),    /* roll */
171                                                            gyro_x(),    /* pitch */
172                                                            gyro_z());   /* yaw */
173
174                         if (log_format == AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD)
175                                 state.check_imu_wrap(imu);
176
177                         state.set_imu(imu);
178
179                         state.set_mag(new AltosMag(mag_x(),
180                                                    mag_y(),
181                                                    mag_z()));
182
183                         state.set_accel(accel());
184
185                         break;
186                 case AltosLib.AO_LOG_TEMP_VOLT:
187                         state.set_battery_voltage(AltosConvert.mega_battery_voltage(v_batt()));
188                         state.set_pyro_voltage(AltosConvert.mega_pyro_voltage(v_pbatt()));
189
190                         int nsense = nsense();
191
192                         state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-2)));
193                         state.set_main_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-1)));
194
195                         double voltages[] = new double[nsense-2];
196                         for (int i = 0; i < nsense-2; i++)
197                                 voltages[i] = AltosConvert.mega_pyro_voltage(sense(i));
198
199                         state.set_ignitor_voltage(voltages);
200                         state.set_pyro_fired(pyro());
201                         break;
202                 case AltosLib.AO_LOG_GPS_TIME:
203                         state.set_tick(tick);
204                         gps = state.make_temp_gps(false);
205                         gps.lat = latitude() / 1e7;
206                         gps.lon = longitude() / 1e7;
207
208                         if (state.altitude_32())
209                                 gps.alt = (altitude_low() & 0xffff) | (altitude_high() << 16);
210                         else
211                                 gps.alt = altitude_low();
212
213                         gps.hour = hour();
214                         gps.minute = minute();
215                         gps.second = second();
216
217                         int flags = flags();
218
219                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
220                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
221                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
222                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
223
224                         gps.year = 2000 + year();
225                         gps.month = month();
226                         gps.day = day();
227                         gps.ground_speed = ground_speed() * 1.0e-2;
228                         gps.course = course() * 2;
229                         gps.climb_rate = climb_rate() * 1.0e-2;
230                         if (state.compare_version("1.4.9") >= 0) {
231                                 gps.pdop = pdop() / 10.0;
232                                 gps.hdop = hdop() / 10.0;
233                                 gps.vdop = vdop() / 10.0;
234                         } else {
235                                 gps.pdop = pdop() / 100.0;
236                                 if (gps.pdop < 0.8)
237                                         gps.pdop += 2.56;
238                                 gps.hdop = hdop() / 100.0;
239                                 if (gps.hdop < 0.8)
240                                         gps.hdop += 2.56;
241                                 gps.vdop = vdop() / 100.0;
242                                 if (gps.vdop < 0.8)
243                                         gps.vdop += 2.56;
244                         }
245                         break;
246                 case AltosLib.AO_LOG_GPS_SAT:
247                         state.set_tick(tick);
248                         gps = state.make_temp_gps(true);
249
250                         int n = nsat();
251                         if (n > max_sat)
252                                 n = max_sat;
253                         for (int i = 0; i < n; i++)
254                                 gps.add_sat(svid(i), c_n(i));
255                         break;
256                 }
257         }
258
259         public AltosEepromMega (String line, int log_format) {
260                 this.log_format = log_format;
261                 parse_string(line);
262         }
263
264         static public LinkedList<AltosEeprom> read(FileInputStream input, int log_format) {
265                 LinkedList<AltosEeprom> megas = new LinkedList<AltosEeprom>();
266
267                 for (;;) {
268                         try {
269                                 String line = AltosLib.gets(input);
270                                 if (line == null)
271                                         break;
272                                 try {
273                                         AltosEepromMega mega = new AltosEepromMega(line, log_format);
274                                         if (mega.cmd != AltosLib.AO_LOG_INVALID)
275                                                 megas.add(mega);
276                                 } catch (Exception e) {
277                                         System.out.printf ("exception\n");
278                                 }
279                         } catch (IOException ie) {
280                                 break;
281                         }
282                 }
283
284                 return megas;
285         }
286 }