Merge branch 'master' of github:szmoore/MCTX3420 into report
[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         if (anti_calibrated < 0)
35                 anti_calibrated = 0;
36         if (anti_calibrated > 1)
37                 anti_calibrated = 1;
38         return PWM_Set(PREGULATOR_PWM, false, PREGULATOR_PERIOD, anti_calibrated*(PREGULATOR_PERIOD));
39 }
40
41 bool Pregulator_Sanity(int id, double value)
42 {
43         return (value >= 0 && value < 570);
44 }
45

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