1 #include "pregulator.h"
2 #include "../bbb_pin.h"
6 #define PREGULATOR_PWM ECAP0
7 #define PREGULATOR_PERIOD 500000
10 /** PWM duty cycles raw **/
11 static double pwm_raw[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
12 /** Calibrated pressure values match with pwm_raw **/
13 static double preg_cal[] = {96, 190, 285, 380, 475, 569};
16 * Initiliase the pressure regulator
18 bool Pregulator_Init(const char * name, int id)
20 return PWM_Export(PREGULATOR_PWM) && PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, 0);
23 bool Pregulator_Cleanup(int id)
25 if (!PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, 0))
27 PWM_Unexport(PREGULATOR_PWM);
31 bool Pregulator_Set(int id, double value)
33 double anti_calibrated = Data_Calibrate(value, preg_cal, pwm_raw, sizeof(pwm_raw)/sizeof(double));
34 Log(LOGDEBUG, "Pregulator value %f -> PWM duty cycle %f", value, anti_calibrated);
35 if (anti_calibrated < 0)
37 if (anti_calibrated > 1)
39 return PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, anti_calibrated*(PREGULATOR_PERIOD));
42 bool Pregulator_Sanity(int id, double value)
44 return (value >= 0 && value < 570);