2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.altoslib_10;
24 public class AltosEepromMega extends AltosEeprom {
25 public static final int record_length = 32;
27 public static final int max_sat = 12;
29 private int log_format;
31 public int record_length() { return record_length; }
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() {
42 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
44 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
47 return AltosLib.MISSING;
50 public int ground_pitch() {
52 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
54 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
57 return AltosLib.MISSING;
60 public int ground_yaw() {
62 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
64 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
67 return AltosLib.MISSING;
71 /* AO_LOG_STATE elements */
72 public int state() { return data16(0); }
73 public int reason() { return data16(2); }
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); }
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); }
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); }
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); }
121 public AltosEepromMega (AltosEepromChunk chunk, int start, int log_format) throws ParseException {
122 this.log_format = log_format;
123 parse_chunk(chunk, start);
126 public void update_state(AltosState state) {
127 super.update_state(state);
131 /* Flush any pending GPS changes */
132 if (state.gps_pending) {
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:
141 state.set_temp_gps();
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);
159 case AltosLib.AO_LOG_STATE:
160 state.set_tick(tick);
161 state.set_state(state());
163 case AltosLib.AO_LOG_SENSOR:
164 state.set_tick(tick);
165 state.set_ms5607(pres(), temp());
167 AltosIMU imu = new AltosIMU(accel_y(), /* along */
168 accel_x(), /* across */
169 accel_z(), /* through */
171 gyro_x(), /* pitch */
174 if (log_format == AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD)
175 state.check_imu_wrap(imu);
179 state.set_mag(new AltosMag(mag_x(),
183 state.set_accel(accel());
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()));
190 int nsense = nsense();
192 state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-2)));
193 state.set_main_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-1)));
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));
199 state.set_ignitor_voltage(voltages);
200 state.set_pyro_fired(pyro());
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;
208 if (state.altitude_32())
209 gps.alt = (altitude_low() & 0xffff) | (altitude_high() << 16);
211 gps.alt = altitude_low();
214 gps.minute = minute();
215 gps.second = second();
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;
224 gps.year = 2000 + year();
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;
235 gps.pdop = pdop() / 100.0;
238 gps.hdop = hdop() / 100.0;
241 gps.vdop = vdop() / 100.0;
246 case AltosLib.AO_LOG_GPS_SAT:
247 state.set_tick(tick);
248 gps = state.make_temp_gps(true);
253 for (int i = 0; i < n; i++)
254 gps.add_sat(svid(i), c_n(i));
259 public AltosEepromMega (String line, int log_format) {
260 this.log_format = log_format;
264 static public LinkedList<AltosEeprom> read(FileInputStream input, int log_format) {
265 LinkedList<AltosEeprom> megas = new LinkedList<AltosEeprom>();
269 String line = AltosLib.gets(input);
273 AltosEepromMega mega = new AltosEepromMega(line, log_format);
274 if (mega.cmd != AltosLib.AO_LOG_INVALID)
276 } catch (Exception e) {
277 System.out.printf ("exception\n");
279 } catch (IOException ie) {