update load cell channel based on repaired board and 1T load cell
[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_14;
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                 /* 1600psi sensor measured 2019.07.10, these values based on that */
51                 double ADC_MIN = 405;
52                 double ADC_SLOPE = 2.020;       /* adc counts per psi */
53                 double ADC_OFFSET = 14.79;      /* psi at ADC_MIN */
54
55 //              /* 2500psi sensor measured 2019.04.30, these values based on that */
56 //              double ADC_MIN = 392;
57 //              double ADC_SLOPE = 0.46;        /* adc counts per psi */
58
59                 /* sensor is asserted to be linear 0 - max psi over ADC_MIN to ADC_MAX */
60                 double raw = adc;
61                 double psi = ((raw - ADC_MIN) / ADC_SLOPE) + ADC_OFFSET;
62
63                 return AltosConvert.psi_to_pa(psi);
64
65         }
66
67         public static double adc_to_n(int adc) {
68
69                 /* load cell sensor looks linear once it "gets going" */
70
71                 /* cal values using 1 metric ton "S" load cell 2020.03.05 */
72                 /* lowest useful cal data point in linear region */
73                 double ADC_MIN_LBS = 71.4;
74                 double ADC_MIN_COUNTS = 153; 
75
76                 /* highest useful cal data point in linear region */
77                 double ADC_MAX_LBS = 211.4;
78                 double ADC_MAX_COUNTS = 313; 
79
80                 /* slope of sensor response in ADC counts per lb */
81                 double ADC_SLOPE = (ADC_MAX_COUNTS - ADC_MIN_COUNTS) / (ADC_MAX_LBS - ADC_MIN_LBS);
82
83                 double raw = adc;
84                 double lb = ((raw - ADC_MIN_COUNTS) / ADC_SLOPE) + ADC_MIN_LBS;
85                 return AltosConvert.lb_to_n(lb);
86         }
87
88         public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
89                 super.provide_data(listener, cal_data);
90
91                 switch (cmd()) {
92                 case AltosLib.AO_LOG_FLIGHT:
93                         cal_data.set_flight(flight());
94                         break;
95                 case AltosLib.AO_LOG_STATE:
96                         listener.set_state(state());
97                         break;
98                 case AltosLib.AO_LOG_SENSOR:
99                         listener.set_pressure(adc_to_pa(pres()));
100                         listener.set_thrust(adc_to_n(thrust()));
101                         break;
102                 }
103         }
104
105         public AltosEepromRecord next() {
106                 int     s = next_start();
107                 if (s < 0)
108                         return null;
109                 return new AltosEepromRecordFireTwo(eeprom, s);
110         }
111
112         public AltosEepromRecordFireTwo(AltosEeprom eeprom, int start) {
113                 super(eeprom, start, record_length);
114         }
115
116         public AltosEepromRecordFireTwo(AltosEeprom eeprom) {
117                 this(eeprom, 0);
118         }
119 }