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