16019c8c280320c10cd86c84b7dada7fd71efa9a
[fw/altos] / altoslib / AltosEepromFireTwo.java
1 /*
2  * Copyright © 2017 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24
25 public class AltosEepromFireTwo extends AltosEeprom {
26         public static final int record_length = 32;
27
28         public int record_length() { return record_length; }
29
30         /* AO_LOG_FLIGHT elements */
31         public int flight() { return data16(0); }
32         public int idle_pres() { return data16(2); }
33         public int idle_thrust() { return data16(4); }
34
35         /* AO_LOG_STATE elements */
36         public int state() { return data16(0); }
37         public int reason() { return data16(2); }
38
39         /* AO_LOG_SENSOR elements */
40         public int pres() { return data16(0); }
41         public int thrust() { return data16(2); }
42         public int temp(int i) { return data16(4+i*2); }
43
44         public AltosEepromFireTwo (AltosEepromChunk chunk, int start) throws ParseException {
45                 parse_chunk(chunk, start);
46         }
47
48         private static final double r_above = 5600.0;
49         private static final double r_below = 10000.0;
50         private static final double v_adc = 3.3;
51
52         private static double firetwo_adc(int raw) {
53                 return raw / 4095.0;
54         }
55
56         public static double adc_to_pa(int adc) {
57
58                 /* raw adc to processor voltage, then back through the
59                  * voltage divider to the sensor voltage
60                  */
61
62                 double  v = firetwo_adc(adc) * v_adc * (r_above + r_below) / r_below;
63
64                 /* Bound to ranges provided in sensor */
65                 if (v < 0.5) v = 0.5;
66                 if (v > 4.5) v = 4.5;
67
68                 double  psi = (v - 0.5) / 4.0 * 1600.0;
69                 return AltosConvert.psi_to_pa(psi);
70         }
71
72         public static double adc_to_n(int adc) {
73                 double v = firetwo_adc(adc);
74
75                 /* this is a total guess */
76                 return AltosConvert.lb_to_n(v * 298 * 9.807);
77         }
78
79         public void update_state(AltosState state) {
80                 super.update_state(state);
81
82                 switch (cmd) {
83                 case AltosLib.AO_LOG_FLIGHT:
84                         state.set_flight(flight());
85                         state.set_ground_pressure(adc_to_pa(idle_pres()));
86                         state.set_accel_g(0, -1);
87                         break;
88                 case AltosLib.AO_LOG_STATE:
89                         state.set_state(state());
90                         break;
91                 case AltosLib.AO_LOG_SENSOR:
92                         state.set_pressure(adc_to_pa(pres()));
93                         state.set_accel(firetwo_adc(thrust()) * 100);
94                         break;
95                 }
96         }
97
98         public AltosEepromFireTwo (String line) {
99                 parse_string(line);
100         }
101
102         static public LinkedList<AltosEeprom> read(FileInputStream input) {
103                 LinkedList<AltosEeprom> firetwos = new LinkedList<AltosEeprom>();
104
105                 for (;;) {
106                         try {
107                                 String line = AltosLib.gets(input);
108                                 if (line == null)
109                                         break;
110                                 try {
111                                         AltosEepromFireTwo firetwo = new AltosEepromFireTwo(line);
112
113                                         if (firetwo.cmd != AltosLib.AO_LOG_INVALID)
114                                                 firetwos.add(firetwo);
115                                 } catch (Exception e) {
116                                         System.out.printf ("exception\n");
117                                 }
118                         } catch (IOException ie) {
119                                 break;
120                         }
121                 }
122
123                 return firetwos;
124         }
125 }