Some fixes
[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                         return adc;
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 * ((double)adc / ADC_RAW_MAX));
70                         return adc;
71                 default:
72                         Fatal("Unknown Pressure id %d", id);
73                         return -1; // Should never happen
74         }
75         
76 }
77
78 /**
79  * Initialise a Pressure sensor
80  * @param name - Ignored
81  * @param id - The id of the Pressure sensor
82  * @returns true on success, false on error
83  */
84 bool Pressure_Init(const char * name, int id)
85 {
86         return ADC_Export(Pressure_GetADC(id));
87 }
88
89 /**
90  * Cleanup a Pressure Sensor
91  * @param id - The id of the sensor to cleanup
92  * @returns true on success, false on failure
93  */
94 bool Pressure_Cleanup(int id)
95 {
96         ADC_Unexport(Pressure_GetADC(id));
97         return true;
98 }
99
100 /**
101  * Read a Pressure Sensor
102  * @param id - id of the sensor to read
103  * @param value - Where the value will be stored on a successful read
104  * @returns true on success, false on failure
105  */
106 bool Pressure_Read(int id, double * value)
107 {
108         //static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
109         //pthread_mutex_lock(&mutex);
110         bool result = false;
111         int adc = 0;
112         if (ADC_Read(Pressure_GetADC(id), &adc))
113         {
114                 *value = Pressure_Callibrate(id, adc);
115                 result = true;
116         }
117         //pthread_mutex_unlock(&mutex);
118         return result;
119 }
120
121 /**
122  * Sanity check the pressure reading
123  * @param value - The pressure reading (calibrated)
124  * @returns true iff the value is safe, false if it is dangerous
125  */
126 bool Pressure_Sanity(int id, double value)
127 {
128         return (value < 590);
129 }

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