3 * @purpose Implementation of Pressure reading functions
7 #include "../bbb_pin.h"
8 #include "../log.h" // For Fatal()
11 #define PSI_TO_KPA 6.89475729
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};
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
23 static int Pressure_GetADC(int id)
34 Fatal("Unknown Pressure id %d", id);
35 return -1; // Should never happen
40 * Convert an ADC voltage into a Pressure reading
41 * @param id - Sensor ID
42 * @param adc - ADC reading
43 * @returns Pressure in kPa
45 double Pressure_Callibrate(int id, int adc)
47 //double voltage = ADC_TO_VOLTS(adc); // convert reading to voltage
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;
65 return Data_Calibrate((double)adc, high_raw, high_cal, sizeof(high_raw)/sizeof(double));
69 return (200.0 * (adc / ADC_RAW_MAX));
71 Fatal("Unknown Pressure id %d", id);
72 return -1; // Should never happen
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
83 bool Pressure_Init(const char * name, int id)
85 return ADC_Export(Pressure_GetADC(id));
89 * Cleanup a Pressure Sensor
90 * @param id - The id of the sensor to cleanup
91 * @returns true on success, false on failure
93 bool Pressure_Cleanup(int id)
95 ADC_Unexport(Pressure_GetADC(id));
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
105 bool Pressure_Read(int id, double * value)
107 //static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
108 //pthread_mutex_lock(&mutex);
111 if (ADC_Read(Pressure_GetADC(id), &adc))
113 *value = Pressure_Callibrate(id, adc);
116 //pthread_mutex_unlock(&mutex);
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
125 bool Pressure_Sanity(int id, double value)
127 return (value < 590);