initial tests made using Terry's stand with a 2500psi sensor
[fw/altos] / altoslib / AltosEepromRecordFireTwo.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 AltosEepromRecordFireTwo extends AltosEepromRecord {
26         public static final int record_length = 32;
27
28         /* AO_LOG_FLIGHT elements */
29         public int flight() { return data16(0); }
30
31         /* AO_LOG_STATE elements */
32         public int state() { return data16(0); }
33         public int reason() { return data16(2); }
34
35         /* AO_LOG_SENSOR elements */
36         public int pres() { return data16(0); }
37         public int thrust() { return data16(2); }
38         public int temp(int i) { return data16(4+i*2); }
39
40         private static final double r_above = 5600.0;
41         private static final double r_below = 10000.0;
42         private static final double v_adc = 3.3;
43
44         private static double firetwo_adc(int raw) {
45                 return raw / 4095.0;
46         }
47
48         public static double adc_to_pa(int adc) {
49
50                 /* raw adc to processor voltage, then back through the
51                  * voltage divider to the sensor voltage
52                  */
53
54                 double  v = firetwo_adc(adc) * v_adc * (r_above + r_below) / r_below;
55
56                 /* Bound to ranges provided in sensor */
57                 if (v < 0.5) v = 0.5;
58                 if (v > 4.5) v = 4.5;
59
60                 double  psi = (v - 0.5) / 4.0 * 2500.0;
61                 return AltosConvert.psi_to_pa(psi);
62         }
63
64         public static double adc_to_n(int adc) {
65                 double v = firetwo_adc(adc);
66
67                 /* this is a total guess */
68                 return AltosConvert.lb_to_n(v * 298 * 9.807);
69         }
70
71         public void update_state(AltosState state) {
72                 super.update_state(state);
73
74                 switch (cmd()) {
75                 case AltosLib.AO_LOG_FLIGHT:
76                         state.set_flight(flight());
77                         state.set_ground_pressure(0.0);
78                         state.set_accel_g(0, -1);
79                         break;
80                 case AltosLib.AO_LOG_STATE:
81                         state.set_state(state());
82                         break;
83                 case AltosLib.AO_LOG_SENSOR:
84                         state.set_pressure(adc_to_pa(pres()));
85                         state.set_accel(adc_to_n(thrust()));
86                         break;
87                 }
88         }
89
90         public AltosEepromRecord next() {
91                 int     s = next_start();
92                 if (s < 0)
93                         return null;
94                 return new AltosEepromRecordFireTwo(eeprom, s);
95         }
96
97         public AltosEepromRecordFireTwo(AltosEepromNew eeprom, int start) {
98                 super(eeprom, start, record_length);
99         }
100
101         public AltosEepromRecordFireTwo(AltosEepromNew eeprom) {
102                 this(eeprom, 0);
103         }
104 }