stuff
[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         return (double)adc;
44
45         //TODO: Fix this
46         switch (id)
47         {
48                 case PRES_HIGH0:
49                 case PRES_HIGH1:
50                 {
51                         static const double Vs = 5e3; // In mVs
52                         static const double Pmin = 0.0 * PSI_TO_KPA;
53                         static const double Pmax = 150.0 * PSI_TO_KPA;
54                         double Vout = ADC_TO_MVOLTS(adc);
55                         return ((Vout - 0.1*Vs)/(0.8*Vs))*(Pmax - Pmin) + Pmin;
56                 }       
57                 case PRES_LOW0:
58                         return (200.0 * (adc / ADC_RAW_MAX));
59                 default:
60                         Fatal("Unknown Pressure id %d", id);
61                         return -1; // Should never happen
62         }
63         
64 }
65
66 /**
67  * Initialise a Pressure sensor
68  * @param name - Ignored
69  * @param id - The id of the Pressure sensor
70  * @returns true on success, false on error
71  */
72 bool Pressure_Init(const char * name, int id)
73 {
74         return ADC_Export(Pressure_GetADC(id));
75 }
76
77 /**
78  * Cleanup a Pressure Sensor
79  * @param id - The id of the sensor to cleanup
80  * @returns true on success, false on failure
81  */
82 bool Pressure_Cleanup(int id)
83 {
84         ADC_Unexport(Pressure_GetADC(id));
85         return true;
86 }
87
88 /**
89  * Read a Pressure Sensor
90  * @param id - id of the sensor to read
91  * @param value - Where the value will be stored on a successful read
92  * @returns true on success, false on failure
93  */
94 bool Pressure_Read(int id, double * value)
95 {
96         //static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
97         //pthread_mutex_lock(&mutex);
98         bool result = false;
99         int adc = 0;
100         if (ADC_Read(Pressure_GetADC(id), &adc))
101         {
102                 *value = Pressure_Callibrate(id, adc);
103                 result = true;
104         }
105         //pthread_mutex_unlock(&mutex);
106         return result;
107 }

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