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_3;
24 public class AltosEepromMega extends AltosEeprom {
25 public static final int record_length = 32;
27 public static final int max_sat = 12;
29 public int record_length() { return record_length; }
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); }
37 /* AO_LOG_STATE elements */
38 public int state() { return data16(0); }
39 public int reason() { return data16(2); }
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); }
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); }
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); }
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); }
79 public AltosEepromMega (AltosEepromChunk chunk, int start) throws ParseException {
80 parse_chunk(chunk, start);
83 public void update_state(AltosState state) {
84 super.update_state(state);
88 /* Flush any pending GPS changes */
89 if (state.gps_pending) {
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:
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);
111 case AltosLib.AO_LOG_STATE:
112 state.set_tick(tick);
113 state.set_state(state());
115 case AltosLib.AO_LOG_SENSOR:
116 state.set_tick(tick);
117 state.set_ms5607(pres(), temp());
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());
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());
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());
136 state.set_accel(accel());
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()));
143 int nsense = nsense();
145 state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-2)));
146 state.set_main_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-1)));
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));
152 state.set_ignitor_voltage(voltages);
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();
162 gps.minute = minute();
163 gps.second = second();
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;
172 gps.year = 2000 + year();
176 case AltosLib.AO_LOG_GPS_SAT:
177 state.set_tick(tick);
178 gps = state.make_temp_gps(true);
183 for (int i = 0; i < n; i++)
184 gps.add_sat(svid(i), c_n(i));
189 public AltosEepromMega (String line) {
193 static public LinkedList<AltosEeprom> read(FileInputStream input) {
194 LinkedList<AltosEeprom> megas = new LinkedList<AltosEeprom>();
198 String line = AltosLib.gets(input);
202 AltosEepromMega mega = new AltosEepromMega(line);
203 if (mega.cmd != AltosLib.AO_LOG_INVALID)
205 } catch (Exception e) {
206 System.out.printf ("exception\n");
208 } catch (IOException ie) {