Francisco Guerra
/
SymbitronValves
Amplitude Modulation Code
Fork of SymbitronValves by
main.cpp@0:80993ec26dfd, 2015-06-26 (annotated)
- Committer:
- vsluiter
- Date:
- Fri Jun 26 15:12:05 2015 +0000
- Revision:
- 0:80993ec26dfd
- Child:
- 1:926025d703f4
Demonstrator
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:80993ec26dfd | 1 | #include "mbed.h" |
vsluiter | 0:80993ec26dfd | 2 | #include "FastPWM.h" |
vsluiter | 0:80993ec26dfd | 3 | |
vsluiter | 0:80993ec26dfd | 4 | |
vsluiter | 0:80993ec26dfd | 5 | #define V1_OPEN D2 |
vsluiter | 0:80993ec26dfd | 6 | #define V1_CLOSE D3 |
vsluiter | 0:80993ec26dfd | 7 | #define V2_OPEN D4 |
vsluiter | 0:80993ec26dfd | 8 | #define V2_CLOSE D5 |
vsluiter | 0:80993ec26dfd | 9 | #define V3_OPEN D6 |
vsluiter | 0:80993ec26dfd | 10 | #define V3_CLOSE D7 |
vsluiter | 0:80993ec26dfd | 11 | #define V4_OPEN D8 |
vsluiter | 0:80993ec26dfd | 12 | #define V4_CLOSE D9 |
vsluiter | 0:80993ec26dfd | 13 | |
vsluiter | 0:80993ec26dfd | 14 | #define PRESSURE A0 |
vsluiter | 0:80993ec26dfd | 15 | |
vsluiter | 0:80993ec26dfd | 16 | class Valve |
vsluiter | 0:80993ec26dfd | 17 | { |
vsluiter | 0:80993ec26dfd | 18 | public: |
vsluiter | 0:80993ec26dfd | 19 | Valve(PinName open, PinName close); |
vsluiter | 0:80993ec26dfd | 20 | void write(float pwm); |
vsluiter | 0:80993ec26dfd | 21 | private: |
vsluiter | 0:80993ec26dfd | 22 | float _pwm; |
vsluiter | 0:80993ec26dfd | 23 | float _period; |
vsluiter | 0:80993ec26dfd | 24 | FastPWM _open; |
vsluiter | 0:80993ec26dfd | 25 | FastPWM _close; |
vsluiter | 0:80993ec26dfd | 26 | }; |
vsluiter | 0:80993ec26dfd | 27 | |
vsluiter | 0:80993ec26dfd | 28 | Valve::Valve(PinName open, PinName close): _open(open), _close(close) |
vsluiter | 0:80993ec26dfd | 29 | { |
vsluiter | 0:80993ec26dfd | 30 | _period = 0.05; |
vsluiter | 0:80993ec26dfd | 31 | _open.period(_period); |
vsluiter | 0:80993ec26dfd | 32 | _close.period(_period); |
vsluiter | 0:80993ec26dfd | 33 | _open.pulsewidth(0); |
vsluiter | 0:80993ec26dfd | 34 | _close.pulsewidth(0); |
vsluiter | 0:80993ec26dfd | 35 | } |
vsluiter | 0:80993ec26dfd | 36 | |
vsluiter | 0:80993ec26dfd | 37 | void Valve::write(float pwm) |
vsluiter | 0:80993ec26dfd | 38 | { |
vsluiter | 0:80993ec26dfd | 39 | if(pwm > 1) |
vsluiter | 0:80993ec26dfd | 40 | pwm = 1; |
vsluiter | 0:80993ec26dfd | 41 | if(pwm < -1) |
vsluiter | 0:80993ec26dfd | 42 | pwm = -1; |
vsluiter | 0:80993ec26dfd | 43 | if(pwm > 0) |
vsluiter | 0:80993ec26dfd | 44 | { |
vsluiter | 0:80993ec26dfd | 45 | _close.pulsewidth(0); |
vsluiter | 0:80993ec26dfd | 46 | _open.pulsewidth(_period*pwm); |
vsluiter | 0:80993ec26dfd | 47 | } |
vsluiter | 0:80993ec26dfd | 48 | else |
vsluiter | 0:80993ec26dfd | 49 | { |
vsluiter | 0:80993ec26dfd | 50 | _open.pulsewidth(0); |
vsluiter | 0:80993ec26dfd | 51 | _close.pulsewidth(_period*pwm*-1); |
vsluiter | 0:80993ec26dfd | 52 | } |
vsluiter | 0:80993ec26dfd | 53 | } |
vsluiter | 0:80993ec26dfd | 54 | |
vsluiter | 0:80993ec26dfd | 55 | int main() { |
vsluiter | 0:80993ec26dfd | 56 | Valve v1(V1_OPEN, V1_CLOSE); |
vsluiter | 0:80993ec26dfd | 57 | Valve v2(V2_OPEN, V2_CLOSE); |
vsluiter | 0:80993ec26dfd | 58 | Valve v3(V3_OPEN, V3_CLOSE); |
vsluiter | 0:80993ec26dfd | 59 | Valve v4(V4_OPEN, V4_CLOSE); |
vsluiter | 0:80993ec26dfd | 60 | //PwmOut v1(V1_OPEN); |
vsluiter | 0:80993ec26dfd | 61 | //v1.period(0.2); |
vsluiter | 0:80993ec26dfd | 62 | while(1) { |
vsluiter | 0:80993ec26dfd | 63 | static float time = 0; |
vsluiter | 0:80993ec26dfd | 64 | wait(0.02); |
vsluiter | 0:80993ec26dfd | 65 | time +=0.05; |
vsluiter | 0:80993ec26dfd | 66 | v1.write(sin(time)); |
vsluiter | 0:80993ec26dfd | 67 | v2.write(sin(time)); |
vsluiter | 0:80993ec26dfd | 68 | v3.write(sin(time)); |
vsluiter | 0:80993ec26dfd | 69 | v4.write(sin(time)); |
vsluiter | 0:80993ec26dfd | 70 | } |
vsluiter | 0:80993ec26dfd | 71 | } |