Francisco Guerra
/
SymbitronValves
Amplitude Modulation Code
Fork of SymbitronValves by
Diff: main.cpp
- Revision:
- 4:e69e2983bb6e
- Parent:
- 3:28efa5d4ebe2
- Child:
- 5:4772ef79675f
--- a/main.cpp Tue Jun 30 07:00:35 2015 +0000 +++ b/main.cpp Tue Jun 30 08:23:32 2015 +0000 @@ -12,22 +12,31 @@ #define V4_CLOSE D8 #define V4_OPEN D9 -#define TSI_A PTB16 +#define TSI_A PTB16 #define TSI_B PTB17 #define PRESSURE A0 +DigitalOut v1open(V1_OPEN); +DigitalOut v1close(V1_CLOSE); HIDScope scope(3); AnalogIn pressure(PRESSURE); TSIAnalogSlider slider(TSI_A, TSI_B, 100); - +Ticker valvetick; + +float setpoint1 = 0; + class Valve { - public: +public: Valve(PinName open, PinName close); void write(float pwm, bool direct_mode=true); - void SetDeadtime(float deadtime){if(_period < deadtime) _period = deadtime; _deadtime = deadtime; InitializeValve(_period,_deadtime);}; - private: + void SetDeadtime(float deadtime) { + if(_period < deadtime) _period = deadtime; + _deadtime = deadtime; + InitializeValve(_period,_deadtime); + }; +private: float _pwm; float _period; float _deadtime; @@ -48,7 +57,7 @@ _open.period(_period); _close.period(_period); _open.pulsewidth(0); - _close.pulsewidth(0); + _close.pulsewidth(0); } @@ -59,38 +68,27 @@ pwm = 1; if(pwm < -1) pwm = -1; - - if(direct_mode) - { + + if(direct_mode) { if(pwm > 1) pwm = 1; if(pwm < -1) pwm = -1; - if(pwm > 0) - { + if(pwm > 0) { _close.pulsewidth(0); _open.pulsewidth(_period*pwm); - } - else - { + } else { _open.pulsewidth(0); _close.pulsewidth(_period*pwm*-1); } - } - else - { - if(pwm > 0.001) - { + } else { + if(pwm > 0.001) { _close.pulsewidth(0); _open.pulsewidth(_deadtime+ ((_period-_deadtime)*pwm) ); - } - else if(pwm < 0.001) - { + } else if(pwm < 0.001) { _open.pulsewidth(0); _close.pulsewidth(_deadtime+ ((_period-_deadtime)*pwm*-1)); - } - else - { + } else { _open.pulsewidth(0); _close.pulsewidth(0); } @@ -99,10 +97,10 @@ class PIDcontroller { - public: - PIDcontroller(float p, float i, float d, float dt); +public: + PIDcontroller(float p, float i, float d, float dt); float control(float setpoint, float measurement); - private: +private: float _p; float _i; float _d; @@ -141,14 +139,43 @@ return pressure; } -int main() { +void valvecontrol(void) +{ + static uint16_t usbcounter = 0; + float meas = getPressure_Pa(); + if(setpoint1 - meas >2000) { + v1close = 0; + v1open = 1; + } else if (meas - setpoint1 > 1000) { + v1close = 1; + v1open = 0; + } + else + v1open = v1close = 0; + usbcounter++; + if(usbcounter == 200) { + scope.set(0,setpoint1); + scope.set(1,meas); + scope.set(2,meas); + scope.send(); + usbcounter = 0; + } +} + +int main() +{ + valvetick.attach(valvecontrol,0.0001); + while(1) { + setpoint1 = slider.readPercentage()*30000; + wait(0.1); + } + /* PIDcontroller pid(0.001,0,0.000,0.2); //Valve v1(V1_OPEN, V1_CLOSE); Valve v2(V2_OPEN, V2_CLOSE); Valve v3(V3_OPEN, V3_CLOSE); Valve v4(V4_OPEN, V4_CLOSE); - DigitalOut v1open(V1_OPEN); - DigitalOut v1close(V1_CLOSE); + while(1) { float pid_out; float setpoint; @@ -164,7 +191,7 @@ // v1.write(-0.1); meas = getPressure_Pa(); if(abs(setpoint - meas)>4000) - { + { int32_t count = 0; if(meas < setpoint ) { @@ -185,7 +212,7 @@ } } v1open = v1close = 0; - + } //v1.write(pid_out,false); v2.write(0);//sin(time)); @@ -195,5 +222,5 @@ scope.set(1,slider.readPercentage()); scope.set(2,pid_out); scope.send(); - } + }*/ }