From: Sam Moore Date: Mon, 28 Oct 2013 07:48:35 +0000 (+0800) Subject: Merge branch 'master' of github:szmoore/MCTX3420 into report X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f4f4c762db8c7cb2934bb03ae85063e453d68169;p=matches%2FMCTX3420.git Merge branch 'master' of github:szmoore/MCTX3420 into report Conflicts: server/fastcgi.c server/parameters Also... calibrated sensors --- f4f4c762db8c7cb2934bb03ae85063e453d68169 diff --cc reports/final/references/refs.bib index 60805d2,0000000..852b0a1 mode 100644,000000..100644 --- a/reports/final/references/refs.bib +++ b/reports/final/references/refs.bib @@@ -1,23 -1,0 +1,24 @@@ +@misc{uwaRef, + author = "Example Reference, + year = 2013, + month = Aug, + title = "Harvard Citation Style", + howpublished = "\url{http://guides.is.uwa.edu.au/harvard}", + note = "UWA Reference guide" +} + +@misc{UserCake, + author = "Tyson et. al" + year = 2012 + title = "UserCake: The fully open source user management script", + howpublished = "\url{http://usercake.com}" +} + +@misc{github, + author = "Sam Moore and Jeremy Tan and Justin Kruger and Callum Schofield and James Rosher and Rowan Heinrich", + year = 2013, + title = "MCTX3420 2013 Git Repository on GitHub", + howpublished = "\url{https://github.com/szmoore/MCTX3420}" +} ++ + diff --cc server/actuators/pregulator.c index 0000000,3740729..1cd62d3 mode 000000,100644..100644 --- a/server/actuators/pregulator.c +++ b/server/actuators/pregulator.c @@@ -1,0 -1,33 +1,45 @@@ + #include "pregulator.h" + #include "../bbb_pin.h" + ++ ++#include "../data.h" + #define PREGULATOR_PWM ECAP0 + #define PREGULATOR_PERIOD 500000 + //16666667 + ++/** PWM duty cycles raw **/ ++static double pwm_raw[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6}; ++/** Calibrated pressure values match with pwm_raw **/ ++static double preg_cal[] = {96, 190, 285, 380, 475, 569}; ++ + /** + * Initiliase the pressure regulator + */ + bool Pregulator_Init(const char * name, int id) + { + return PWM_Export(PREGULATOR_PWM) && PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, 0); + } + + bool Pregulator_Cleanup(int id) + { + if (!PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, 0)) + return false; + PWM_Unexport(PREGULATOR_PWM); + return true; + } + + bool Pregulator_Set(int id, double value) + { - return PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, value*(PREGULATOR_PERIOD)); ++ double anti_calibrated = Data_Calibrate(value, preg_cal, pwm_raw, sizeof(pwm_raw)/sizeof(double)); ++ if (anti_calibrated < 0) ++ anti_calibrated = 0; ++ if (anti_calibrated > 1) ++ anti_calibrated = 1; ++ return PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, anti_calibrated*(PREGULATOR_PERIOD)); + } + + bool Pregulator_Sanity(int id, double value) + { - return (value >= 0.0 && value <= 1.0); ++ return (value >= 0 && value < 570); + } + diff --cc server/data.c index edfd4c3,edfd4c3..19828bc --- a/server/data.c +++ b/server/data.c @@@ -356,3 -356,3 +356,51 @@@ DataFormat Data_GetFormat(FCGIValue * f } return JSON; } ++ ++/** ++ * Binary search for index of a double in an array ++ * @param value - The value ++ * @param x - The array ++ * @param size - Sizeof the array ++ */ ++int FindClosest(double value, double x[], int size) ++{ ++ int upper = size-1; ++ int lower = 0; ++ int index = 0; ++ while (upper - lower > 1) ++ { ++ index = lower + ((upper - lower)/2); ++ double look = x[index]; ++ if (look > value) ++ upper = index; ++ else if (look < value) ++ lower = index; ++ else ++ return index; ++ } ++ ++ if (x[index] > value && index > 0) ++ --index; ++ return index; ++ ++} ++ ++/** ++ * Get calibrated value by interpolation in array y ++ * @param value - Raw measured value ++ * @param x - x values (raw values) of the data ++ * @param y - calibrated values ++ * @param size - Number of values in the arrays ++ * @returns interpolated calibrated value ++ */ ++double Data_Calibrate(double value, double x[], double y[], int size) ++{ ++ int i = FindClosest(value, x, size); ++ if (i >= size-1) ++ { ++ i = size-2; ++ } ++ double dist = (value - x[i])/(x[i+1] - x[i]); ++ return y[i] + dist*(y[i+1]-y[i]); ++} diff --cc server/data.h index df16efe,df16efe..d6234fd --- a/server/data.h +++ b/server/data.h @@@ -54,4 -54,4 +54,6 @@@ extern void Data_Handler(DataFile * df extern DataFormat Data_GetFormat(FCGIValue * fmt); // Helper; convert human readable format string to DataFormat ++extern double Data_Callibrate(double value, double map[], int map_size); ++ #endif //_DATAPOINT_H diff --cc server/sensors/common.c index 0000000,0000000..cc17f0d new file mode 100644 --- /dev/null +++ b/server/sensors/common.c @@@ -1,0 -1,0 +1,3 @@@ ++#include "common.h" ++ ++ diff --cc server/sensors/microphone.c index 0000000,0000000..63a9d71 new file mode 100644 --- /dev/null +++ b/server/sensors/microphone.c @@@ -1,0 -1,0 +1,40 @@@ ++/** ++ * @file microphone.c ++ * @purpose Implementation of Pressure reading functions ++ */ ++ ++#include "microphone.h" ++#include "../bbb_pin.h" ++#include "../log.h" // For Fatal() ++ ++#define MIC_ADC ADC2 ++ ++double adc_raw[] = {524,668,733,991,1121,1264,1300,1437,1645,1789,1932,2033,2105,2148,2284,2528,3089}; ++double mic_cal[] = {70,73,75,76.8,77.7,80,81.2,83.3,85.5,87.5,90.7,92.6,94.3,96.2,100,102,125}; ++ ++bool Microphone_Init(const char * name, int id) ++{ ++ assert(sizeof(adc_raw) == sizeof(mic_cal)); ++ return ADC_Export(MIC_ADC); ++} ++ ++bool Microphone_Cleanup(int id) ++{ ++ ADC_Unexport(MIC_ADC); ++ return true; ++} ++ ++bool Microphone_Read(int id, double * value) ++{ ++ int adc = 0; ++ if (!ADC_Read(MIC_ADC, &adc)) ++ return false; ++ ++ *value = Data_Calibrate((double)adc, adc_raw, mic_cal, sizeof(adc_raw)/sizeof(double)); ++ return true; ++} ++ ++bool Microphone_Sanity(int id, double value) ++{ ++ return true; ++} diff --cc server/sensors/microphone.h index 0000000,0000000..371506b new file mode 100644 --- /dev/null +++ b/server/sensors/microphone.h @@@ -1,0 -1,0 +1,11 @@@ ++#ifndef _MICROPHONE_H ++#define _MICROPHONE_H ++ ++extern bool Microphone_Init(const char * name, int id); ++extern bool Microphone_Cleanup(int id); ++extern bool Microphone_Read(int id, double * value); ++extern bool Microphone_Sanity(int id, double value); ++ ++#endif //_MICROPHONE_H ++ ++ diff --cc server/sensors/pressure.c index 61cdbcf,29b8d28..699dda3 --- a/server/sensors/pressure.c +++ b/server/sensors/pressure.c @@@ -6,9 -6,9 +6,15 @@@ #include "pressure.h" #include "../bbb_pin.h" #include "../log.h" // For Fatal() ++#include "../data.h" #define PSI_TO_KPA 6.89475729 ++/** Uncalibrated values in ADC readings **/ ++static double high_raw[] = {642,910,1179,1445,1712,1980}; ++/** Calibrated values in kPa **/ ++static double high_cal[] = {95, 190, 285, 380, 474, 560}; ++ /** * Get the ADC number of a Pressure sensor * @param id - Id of the sensor @@@ -40,18 -40,21 +46,26 @@@ double Pressure_Callibrate(int id, int { //double voltage = ADC_TO_VOLTS(adc); // convert reading to voltage - return (double)adc; ++ + + //TODO: Fix this switch (id) { case PRES_HIGH0: case PRES_HIGH1: { ++ /* static const double Vs = 5e3; // In mVs static const double Pmin = 0.0 * PSI_TO_KPA; 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; ++ */ ++ ++ return Data_Calibrate((double)adc, high_raw, high_cal, sizeof(high_raw)/sizeof(double)); } case PRES_LOW0: ++ // Not calibrated! return (200.0 * (adc / ADC_RAW_MAX)); default: Fatal("Unknown Pressure id %d", id); @@@ -102,3 -105,3 +116,13 @@@ bool Pressure_Read(int id, double * val //pthread_mutex_unlock(&mutex); return result; } ++ ++/** ++ * Sanity check the pressure reading ++ * @param value - The pressure reading (calibrated) ++ * @returns true iff the value is safe, false if it is dangerous ++ */ ++bool Pressure_Sanity(int id, double value) ++{ ++ return (value < 590); ++} diff --cc server/sensors/pressure.h index 22d1adc,22d1adc..a37d918 --- a/server/sensors/pressure.h +++ b/server/sensors/pressure.h @@@ -17,6 -17,6 +17,7 @@@ typedef enu extern bool Pressure_Init(const char * name, int id); extern bool Pressure_Cleanup(int id); extern bool Pressure_Read(int id, double * value); ++extern bool Pressure_Sanity(int id, double value); #endif //_PRESSURE_H diff --cc server/sensors/strain.c index 9492de7,77a11a2..7c78b56 --- a/server/sensors/strain.c +++ b/server/sensors/strain.c @@@ -6,6 -6,8 +6,8 @@@ #include #define STRAIN_ADC ADC0 + // TODO: Choose this -#define STRAIN_GPIO 15 ++#define STRAIN_GPIO 45 /** * Convert Strain gauge id number to a GPIO pin on the Mux @@@ -23,13 -25,13 +25,13 @@@ static int Strain_To_GPIO(StrainID id switch (id) { case STRAIN0: -- return GPIO0_30; ++ return 44; case STRAIN1: -- return GPIO1_28; ++ return 26; case STRAIN2: -- return GPIO0_31; ++ return 46; case STRAIN3: -- return GPIO1_16; ++ return 65; default: Fatal("Unknown StrainID %d", id); return -1; // Should never happen