edited dilatometer
[matches/MCTX3420.git] / server / sensors / pressure.c
1 /**
2  * @file pressure.c
3  * @purpose Implementation of Pressure reading functions
4  */
5
6 #include "pressure.h"
7 #include "../bbb_pin.h"
8 #include "../log.h" // For Fatal()
9 #include "../data.h"
10
11 #define PSI_TO_KPA 6.89475729
12
13 /** Uncalibrated values in ADC readings **/
14 static double high_raw[] = {642,910,1179,1445,1712,1980}; 
15 /** Calibrated values in kPa **/
16 static double high_cal[] = {95, 190, 285, 380, 474, 560};
17
18 /**
19  * Get the ADC number of a Pressure sensor
20  * @param id - Id of the sensor
21  * @returns ADC as defined in bbb_pin_defines.h
22  */
23 static int Pressure_GetADC(int id)
24 {
25         switch (id)
26         {
27                 case PRES_HIGH0:
28                         return ADC1;
29                 case PRES_HIGH1:
30                         return ADC3;
31                 case PRES_LOW0:
32                         return ADC5;
33                 default:
34                         Fatal("Unknown Pressure id %d", id);
35                         return -1; // Should never happen
36         }
37 }
38
39 /**
40  * Convert an ADC voltage into a Pressure reading
41  * @param id - Sensor ID
42  * @param adc - ADC reading
43  * @returns Pressure in kPa
44  */
45 double Pressure_Callibrate(int id, int adc)
46 {
47         //double voltage = ADC_TO_VOLTS(adc); // convert reading to voltage
48
49
50
51         //TODO: Fix this
52         switch (id)
53         {
54                 case PRES_HIGH0:
55                 case PRES_HIGH1:
56                 {
57                         /*
58                         static const double Vs = 5e3; // In mVs
59                         static const double Pmin = 0.0 * PSI_TO_KPA;
60                         static const double Pmax = 150.0 * PSI_TO_KPA;
61                         double Vout = ADC_TO_MVOLTS(adc);
62                         return ((Vout - 0.1*Vs)/(0.8*Vs))*(Pmax - Pmin) + Pmin;
63                         */
64
65                         return Data_Calibrate((double)adc, high_raw, high_cal, sizeof(high_raw)/sizeof(double));
66                 }       
67                 case PRES_LOW0:
68                         // Not calibrated!
69                         return (200.0 * (adc / ADC_RAW_MAX));
70                 default:
71                         Fatal("Unknown Pressure id %d", id);
72                         return -1; // Should never happen
73         }
74         
75 }
76
77 /**
78  * Initialise a Pressure sensor
79  * @param name - Ignored
80  * @param id - The id of the Pressure sensor
81  * @returns true on success, false on error
82  */
83 bool Pressure_Init(const char * name, int id)
84 {
85         return ADC_Export(Pressure_GetADC(id));
86 }
87
88 /**
89  * Cleanup a Pressure Sensor
90  * @param id - The id of the sensor to cleanup
91  * @returns true on success, false on failure
92  */
93 bool Pressure_Cleanup(int id)
94 {
95         ADC_Unexport(Pressure_GetADC(id));
96         return true;
97 }
98
99 /**
100  * Read a Pressure Sensor
101  * @param id - id of the sensor to read
102  * @param value - Where the value will be stored on a successful read
103  * @returns true on success, false on failure
104  */
105 bool Pressure_Read(int id, double * value)
106 {
107         //static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
108         //pthread_mutex_lock(&mutex);
109         bool result = false;
110         int adc = 0;
111         if (ADC_Read(Pressure_GetADC(id), &adc))
112         {
113                 *value = Pressure_Callibrate(id, adc);
114                 result = true;
115         }
116         //pthread_mutex_unlock(&mutex);
117         return result;
118 }
119
120 /**
121  * Sanity check the pressure reading
122  * @param value - The pressure reading (calibrated)
123  * @returns true iff the value is safe, false if it is dangerous
124  */
125 bool Pressure_Sanity(int id, double value)
126 {
127         return (value < 590);
128 }

UCC git Repository :: git.ucc.asn.au