61cdbcfce2a1e614dccb3f0bab9a83da75240f95
[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
10 #define PSI_TO_KPA 6.89475729
11
12 /**
13  * Get the ADC number of a Pressure sensor
14  * @param id - Id of the sensor
15  * @returns ADC as defined in bbb_pin_defines.h
16  */
17 static int Pressure_GetADC(int id)
18 {
19         switch (id)
20         {
21                 case PRES_HIGH0:
22                         return ADC1;
23                 case PRES_HIGH1:
24                         return ADC3;
25                 case PRES_LOW0:
26                         return ADC5;
27                 default:
28                         Fatal("Unknown Pressure id %d", id);
29                         return -1; // Should never happen
30         }
31 }
32
33 /**
34  * Convert an ADC voltage into a Pressure reading
35  * @param id - Sensor ID
36  * @param adc - ADC reading
37  * @returns Pressure in kPa
38  */
39 double Pressure_Callibrate(int id, int adc)
40 {
41         //double voltage = ADC_TO_VOLTS(adc); // convert reading to voltage
42
43         switch (id)
44         {
45                 case PRES_HIGH0:
46                 case PRES_HIGH1:
47                 {
48                         static const double Vs = 5e3; // In mVs
49                         static const double Pmin = 0.0 * PSI_TO_KPA;
50                         static const double Pmax = 150.0 * PSI_TO_KPA;
51                         double Vout = ADC_TO_MVOLTS(adc);
52                         return ((Vout - 0.1*Vs)/(0.8*Vs))*(Pmax - Pmin) + Pmin;
53                 }       
54                 case PRES_LOW0:
55                         return (200.0 * (adc / ADC_RAW_MAX));
56                 default:
57                         Fatal("Unknown Pressure id %d", id);
58                         return -1; // Should never happen
59         }
60         
61 }
62
63 /**
64  * Initialise a Pressure sensor
65  * @param name - Ignored
66  * @param id - The id of the Pressure sensor
67  * @returns true on success, false on error
68  */
69 bool Pressure_Init(const char * name, int id)
70 {
71         return ADC_Export(Pressure_GetADC(id));
72 }
73
74 /**
75  * Cleanup a Pressure Sensor
76  * @param id - The id of the sensor to cleanup
77  * @returns true on success, false on failure
78  */
79 bool Pressure_Cleanup(int id)
80 {
81         ADC_Unexport(Pressure_GetADC(id));
82         return true;
83 }
84
85 /**
86  * Read a Pressure Sensor
87  * @param id - id of the sensor to read
88  * @param value - Where the value will be stored on a successful read
89  * @returns true on success, false on failure
90  */
91 bool Pressure_Read(int id, double * value)
92 {
93         //static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
94         //pthread_mutex_lock(&mutex);
95         bool result = false;
96         int adc = 0;
97         if (ADC_Read(Pressure_GetADC(id), &adc))
98         {
99                 *value = Pressure_Callibrate(id, adc);
100                 result = true;
101         }
102         //pthread_mutex_unlock(&mutex);
103         return result;
104 }

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