altoslib: Validate TeleMega GPS_SAT log packet nsat
[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_3;
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         public int record_length() { return record_length; }
30
31         /* AO_LOG_FLIGHT elements */
32         public int flight() { return data16(0); }
33         public int ground_accel() { return data16(2); }
34         public int ground_pres() { return data32(4); }
35         public int ground_temp() { return data32(8); }
36
37         /* AO_LOG_STATE elements */
38         public int state() { return data16(0); }
39         public int reason() { return data16(2); }
40
41         /* AO_LOG_SENSOR elements */
42         public int pres() { return data32(0); }
43         public int temp() { return data32(4); }
44         public int accel_x() { return data16(8); }
45         public int accel_y() { return data16(10); }
46         public int accel_z() { return data16(12); }
47         public int gyro_x() { return data16(14); }
48         public int gyro_y() { return data16(16); }
49         public int gyro_z() { return data16(18); }
50         public int mag_x() { return data16(20); }
51         public int mag_y() { return data16(22); }
52         public int mag_z() { return data16(24); }
53         public int accel() { return data16(26); }
54
55         /* AO_LOG_TEMP_VOLT elements */
56         public int v_batt() { return data16(0); }
57         public int v_pbatt() { return data16(2); }
58         public int nsense() { return data16(4); }
59         public int sense(int i) { return data16(6 + i * 2); }
60         public int pyro() { return data16(26); }
61
62         /* AO_LOG_GPS_TIME elements */
63         public int latitude() { return data32(0); }
64         public int longitude() { return data32(4); }
65         public int altitude() { return data16(8); }
66         public int hour() { return data8(10); }
67         public int minute() { return data8(11); }
68         public int second() { return data8(12); }
69         public int flags() { return data8(13); }
70         public int year() { return data8(14); }
71         public int month() { return data8(15); }
72         public int day() { return data8(16); }
73         
74         /* AO_LOG_GPS_SAT elements */
75         public int nsat() { return data16(0); }
76         public int svid(int n) { return data8(2 + n * 2); }
77         public int c_n(int n) { return data8(2 + n * 2 + 1); }
78
79         public AltosEepromMega (AltosEepromChunk chunk, int start) throws ParseException {
80                 parse_chunk(chunk, start);
81         }
82
83         public void update_state(AltosState state) {
84                 super.update_state(state);
85
86                 AltosGPS        gps;
87
88                 /* Flush any pending GPS changes */
89                 if (state.gps_pending) {
90                         switch (cmd) {
91                         case AltosLib.AO_LOG_GPS_LAT:
92                         case AltosLib.AO_LOG_GPS_LON:
93                         case AltosLib.AO_LOG_GPS_ALT:
94                         case AltosLib.AO_LOG_GPS_SAT:
95                         case AltosLib.AO_LOG_GPS_DATE:
96                                 break;
97                         default:
98                                 state.set_temp_gps();
99                                 break;
100                         }
101                 }
102
103                 switch (cmd) {
104                 case AltosLib.AO_LOG_FLIGHT:
105                         state.set_boost_tick(tick);
106                         state.set_flight(flight());
107                         state.set_ground_accel(ground_accel());
108                         state.set_ground_pressure(ground_pres());
109                         state.set_temperature(ground_temp() / 100.0);
110                         break;
111                 case AltosLib.AO_LOG_STATE:
112                         state.set_tick(tick);
113                         state.set_state(state());
114                         break;
115                 case AltosLib.AO_LOG_SENSOR:
116                         state.set_tick(tick);
117                         state.set_ms5607(pres(), temp());
118
119                         AltosIMU imu = new AltosIMU();
120                         imu.accel_x = AltosIMU.convert_accel(accel_x());
121                         imu.accel_y = AltosIMU.convert_accel(accel_y());
122                         imu.accel_z = AltosIMU.convert_accel(accel_z());
123
124                         imu.gyro_x = AltosIMU.convert_gyro(gyro_x());
125                         imu.gyro_y = AltosIMU.convert_gyro(gyro_y());
126                         imu.gyro_z = AltosIMU.convert_gyro(gyro_z());
127                         state.imu = imu;
128
129                         AltosMag mag = new AltosMag();
130                         mag.x = AltosMag.convert_gauss(mag_x());
131                         mag.y = AltosMag.convert_gauss(mag_y());
132                         mag.z = AltosMag.convert_gauss(mag_z());
133
134                         state.mag = mag;
135
136                         state.set_accel(accel());
137
138                         break;
139                 case AltosLib.AO_LOG_TEMP_VOLT:
140                         state.set_battery_voltage(AltosConvert.mega_battery_voltage(v_batt()));
141                         state.set_pyro_voltage(AltosConvert.mega_pyro_voltage(v_pbatt()));
142
143                         int nsense = nsense();
144
145                         state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-2)));
146                         state.set_main_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-1)));
147
148                         double voltages[] = new double[nsense-2];
149                         for (int i = 0; i < nsense-2; i++)
150                                 voltages[i] = AltosConvert.mega_pyro_voltage(sense(i));
151
152                         state.set_ignitor_voltage(voltages);
153                         break;
154                 case AltosLib.AO_LOG_GPS_TIME:
155                         state.set_tick(tick);
156                         gps = state.make_temp_gps(false);
157                         gps.lat = latitude() / 1e7;
158                         gps.lon = longitude() / 1e7;
159                         gps.alt = altitude();
160
161                         gps.hour = hour();
162                         gps.minute = minute();
163                         gps.second = second();
164
165                         int flags = flags();
166
167                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
168                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
169                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
170                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
171
172                         gps.year = 2000 + year();
173                         gps.month = month();
174                         gps.day = day();
175                         break;
176                 case AltosLib.AO_LOG_GPS_SAT:
177                         state.set_tick(tick);
178                         gps = state.make_temp_gps(true);
179
180                         int n = nsat();
181                         if (n > max_sat)
182                                 n = max_sat;
183                         for (int i = 0; i < n; i++)
184                                 gps.add_sat(svid(i), c_n(i));
185                         break;
186                 }
187         }
188
189         public AltosEepromMega (String line) {
190                 parse_string(line);
191         }
192
193         static public LinkedList<AltosEeprom> read(FileInputStream input) {
194                 LinkedList<AltosEeprom> megas = new LinkedList<AltosEeprom>();
195
196                 for (;;) {
197                         try {
198                                 String line = AltosLib.gets(input);
199                                 if (line == null)
200                                         break;
201                                 try {
202                                         AltosEepromMega mega = new AltosEepromMega(line);
203                                         if (mega.cmd != AltosLib.AO_LOG_INVALID)
204                                                 megas.add(mega);
205                                 } catch (Exception e) {
206                                         System.out.printf ("exception\n");
207                                 }
208                         } catch (IOException ie) {
209                                 break;
210                         }
211                 }
212
213                 return megas;
214         }
215 }