8b9d06e626fd6e254dee39f8f9fb8735b9983037
[matches/MCTX3420.git] / server / actuators / pregulator.c
1 #include "pregulator.h"
2 #include "../bbb_pin.h"
3
4
5 #include "../data.h"
6 #define PREGULATOR_PWM ECAP0
7 #define PREGULATOR_PERIOD 500000
8 //16666667
9
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};
14
15 /**
16  * Initiliase the pressure regulator
17  */
18 bool Pregulator_Init(const char * name, int id)
19 {
20         return PWM_Export(PREGULATOR_PWM) && PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, 0);
21 }
22
23 bool Pregulator_Cleanup(int id)
24 {
25         if (!PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, 0))
26                 return false;
27         PWM_Unexport(PREGULATOR_PWM);
28         return true;
29 }
30
31 bool Pregulator_Set(int id, double value)
32 {
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)
36                 anti_calibrated = 0;
37         if (anti_calibrated > 1)
38                 anti_calibrated = 1;
39         return PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, anti_calibrated*(PREGULATOR_PERIOD));
40 }
41
42 bool Pregulator_Sanity(int id, double value)
43 {
44         return (value >= 0 && value < 570);
45 }
46

UCC git Repository :: git.ucc.asn.au