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 * ((double)adc / ADC_RAW_MAX));
72 Fatal("Unknown Pressure id %d", id);
73 return -1; // Should never happen
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
84 bool Pressure_Init(const char * name, int id)
86 return ADC_Export(Pressure_GetADC(id));
90 * Cleanup a Pressure Sensor
91 * @param id - The id of the sensor to cleanup
92 * @returns true on success, false on failure
94 bool Pressure_Cleanup(int id)
96 ADC_Unexport(Pressure_GetADC(id));
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
106 bool Pressure_Read(int id, double * value)
108 //static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
109 //pthread_mutex_lock(&mutex);
112 if (ADC_Read(Pressure_GetADC(id), &adc))
114 *value = Pressure_Callibrate(id, adc);
117 //pthread_mutex_unlock(&mutex);
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
126 bool Pressure_Sanity(int id, double value)
128 return (value < 590);