From a87a6176816d9dc67e0cee99e707a5e87c3a7c4d Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 14 Oct 2013 14:28:21 +0800 Subject: [PATCH] Add pressure sensors Untested on BBB --- server/bbb_pin_defines.h | 3 ++ server/sensor.c | 5 +- server/sensors/Makefile | 2 +- server/sensors/pressure.c | 103 ++++++++++++++++++++++++++++++++++++++ server/sensors/pressure.h | 23 +++++++++ 5 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 server/sensors/pressure.c create mode 100644 server/sensors/pressure.h diff --git a/server/bbb_pin_defines.h b/server/bbb_pin_defines.h index 2eb2d08..cb77dac 100644 --- a/server/bbb_pin_defines.h +++ b/server/bbb_pin_defines.h @@ -136,6 +136,9 @@ extern const unsigned char g_pin_index_to_gpio[GPIO_NUM_PINS]; #define ADC5 5 #define ADC6 6 #define ADC7 7 +#define ADC_VOLTAGE_MAX 1800 +#define ADC_RAW_MAX (2 << ADC_BITS) +#define ADC_TO_MVOLTS(x) ((double)((x)/2 << ADC_BITS) * (double)ADC_VOLTAGE_MAX) /** Number of ADC pins **/ #define ADC_NUM_PINS 8 diff --git a/server/sensor.c b/server/sensor.c index 792bbbd..fd477c9 100644 --- a/server/sensor.c +++ b/server/sensor.c @@ -68,11 +68,14 @@ int Sensor_Add(const char * name, int user_id, ReadFn read, InitFn init, CleanFn */ #include "sensors/resource.h" #include "sensors/strain.h" -#include "sensors/piped.h" +#include "sensors/pressure.h" void Sensor_Init() { Sensor_Add("cpu_stime", RESOURCE_CPU_SYS, Resource_Read, NULL, NULL, 1e50,-1e50,1e50,-1e50); Sensor_Add("cpu_utime", RESOURCE_CPU_USER, Resource_Read, NULL, NULL, 1e50,-1e50,1e50,-1e50); + Sensor_Add("pressure_high0", PRES_HIGH0, Pressure_Read, Pressure_Init, Pressure_Cleanup, 800, 110, 800, 110); + Sensor_Add("pressure_high1", PRES_HIGH1, Pressure_Read, Pressure_Init, Pressure_Cleanup, 800, 110, 800, 110); + Sensor_Add("pressure_low0", PRES_LOW0, Pressure_Read, Pressure_Init, Pressure_Cleanup, 250, 110, 250, 110); //Sensor_Add("../testing/count.py", 0, Piped_Read, Piped_Init, Piped_Cleanup, 1e50,-1e50,1e50,-1e50); //Sensor_Add("strain0", STRAIN0, Strain_Read, Strain_Init, 5000,0,5000,0); //Sensor_Add("strain1", STRAIN1, Strain_Read, Strain_Init, 5000,0,5000,0); diff --git a/server/sensors/Makefile b/server/sensors/Makefile index b72201d..a412f7c 100644 --- a/server/sensors/Makefile +++ b/server/sensors/Makefile @@ -3,7 +3,7 @@ CXX = gcc FLAGS = -std=c99 -Wall -pedantic -g -I../ #-I/usr/include/opencv -I/usr/include/opencv2/highgui For OpenCV LIB = -lpthread -OBJ = strain.o resource.o piped.o +OBJ = strain.o resource.o pressure.o HEADERS = $(wildcard *.h) RM = rm -f diff --git a/server/sensors/pressure.c b/server/sensors/pressure.c new file mode 100644 index 0000000..ddb46df --- /dev/null +++ b/server/sensors/pressure.c @@ -0,0 +1,103 @@ +/** + * @file pressure.c + * @purpose Implementation of Pressure reading functions + */ + +#include "pressure.h" +#include "../bbb_pin.h" +#include "../log.h" // For Fatal() + +#define PSI_TO_KPA 6.89475729 + +/** + * Get the ADC number of a Pressure sensor + * @param id - Id of the sensor + * @returns ADC as defined in bbb_pin_defines.h + */ +static int Pressure_GetADC(int id) +{ + switch (id) + { + case PRES_HIGH0: + return ADC1; + case PRES_HIGH1: + return ADC3; + case PRES_LOW0: + return ADC5; + default: + Fatal("Unknown Pressure id %d", id); + return -1; // Should never happen + } +} + +/** + * Convert an ADC voltage into a Pressure reading + * @param id - Sensor ID + * @param adc - ADC reading + * @returns Pressure in kPa + */ +double Pressure_Callibrate(int id, int adc) +{ + //double voltage = ADC_TO_VOLTS(adc); // convert reading to voltage + + switch (id) + { + case PRES_HIGH0: + case PRES_HIGH1: + { + static const double Vs = 5e3; // In mVs + static const double Pmin = 0.0; + static const double Pmax = 150.0 * PSI_TO_KPA; + double Vout = ADC_TO_MVOLTS(adc); + return ((Vout - 0.1*Vs)/(0.8*Vs))*(Pmax - Pmin) + Pmin; + } + case PRES_LOW0: + return (200.0 * (adc / ADC_RAW_MAX)); + default: + Fatal("Unknown Pressure id %d", id); + return -1; // Should never happen + } + +} + +/** + * Initialise a Pressure sensor + * @param name - Ignored + * @param id - The id of the Pressure sensor + * @returns true on success, false on error + */ +bool Pressure_Init(const char * name, int id) +{ + return ADC_Export(Pressure_GetADC(id)); +} + +/** + * Cleanup a Pressure Sensor + * @param id - The id of the sensor to cleanup + * @returns true on success, false on failure + */ +bool Pressure_Cleanup(int id) +{ + ADC_Unexport(Pressure_GetADC(id)); + return true; +} + +/** + * Read a Pressure Sensor + * @param id - id of the sensor to read + * @param value - Where the value will be stored on a successful read + * @returns true on success, false on failure + */ +bool Pressure_Read(int id, double * value) +{ + //static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + //pthread_mutex_lock(&mutex); + bool result = false; + if (ADC_Read(Pressure_GetADC(id), value)) + { + *value = Pressure_Callibrate(id, *value); + result = true; + } + //pthread_mutex_unlock(&mutex); + return result; +} diff --git a/server/sensors/pressure.h b/server/sensors/pressure.h new file mode 100644 index 0000000..22d1adc --- /dev/null +++ b/server/sensors/pressure.h @@ -0,0 +1,23 @@ +/** + * @file pressure.h + * @purpose Declarations for Pressure Sensor functions + */ + +#ifndef _PRESSURE_H + +#include "../common.h" + +typedef enum +{ + PRES_HIGH0, + PRES_HIGH1, + PRES_LOW0 +} PressureId; + +extern bool Pressure_Init(const char * name, int id); +extern bool Pressure_Cleanup(int id); +extern bool Pressure_Read(int id, double * value); + +#endif //_PRESSURE_H + + -- 2.20.1