Amplitude Modulation Code

Dependencies:   HIDScope mbed

Fork of SymbitronValves by First Last

Committer:
franciscoguerra
Date:
Thu Jan 14 16:07:39 2016 +0000
Revision:
8:89be5a6807f0
Parent:
7:9f6dfd88cd18
Amplitude Modulation Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:80993ec26dfd 1 #include "mbed.h"
vsluiter 6:d73f051400ee 2 //#include "FastPWM.h"
vsluiter 1:926025d703f4 3 #include "HIDScope.h"
vsluiter 6:d73f051400ee 4 //#include "tsi_sensor.h"
vsluiter 0:80993ec26dfd 5
vsluiter 1:926025d703f4 6 #define V1_CLOSE D2
vsluiter 1:926025d703f4 7 #define V1_OPEN D3
vsluiter 1:926025d703f4 8 #define V2_CLOSE D4
vsluiter 1:926025d703f4 9 #define V2_OPEN D5
vsluiter 1:926025d703f4 10 #define V3_CLOSE A2
vsluiter 1:926025d703f4 11 #define V3_OPEN A3
vsluiter 1:926025d703f4 12 #define V4_CLOSE D8
vsluiter 1:926025d703f4 13 #define V4_OPEN D9
vsluiter 1:926025d703f4 14
vsluiter 4:e69e2983bb6e 15 #define TSI_A PTB16
vsluiter 1:926025d703f4 16 #define TSI_B PTB17
vsluiter 0:80993ec26dfd 17
vsluiter 7:9f6dfd88cd18 18 #define PRESSURE PTB0
vsluiter 7:9f6dfd88cd18 19 #define EXTERNAL_SETPOINT PTC2
vsluiter 0:80993ec26dfd 20
vsluiter 4:e69e2983bb6e 21 DigitalOut v1open(V1_OPEN);
vsluiter 4:e69e2983bb6e 22 DigitalOut v1close(V1_CLOSE);
vsluiter 1:926025d703f4 23 HIDScope scope(3);
vsluiter 7:9f6dfd88cd18 24 AnalogIn ain_setpoint(EXTERNAL_SETPOINT);
vsluiter 1:926025d703f4 25 AnalogIn pressure(PRESSURE);
vsluiter 6:d73f051400ee 26 //TSIAnalogSlider slider(TSI_A, TSI_B, 100);
vsluiter 4:e69e2983bb6e 27 Ticker valvetick;
vsluiter 4:e69e2983bb6e 28
vsluiter 7:9f6dfd88cd18 29 volatile float setpoint1;
vsluiter 4:e69e2983bb6e 30
vsluiter 7:9f6dfd88cd18 31 static inline float getPressure_Pa(void)
vsluiter 1:926025d703f4 32 {
vsluiter 1:926025d703f4 33 float voltage = pressure.read()*3.3;
vsluiter 1:926025d703f4 34 float sensor_output = (voltage*2)-0.2;//offset compensation
vsluiter 7:9f6dfd88cd18 35 float press = sensor_output*10000; //gain (might be off a bit. Is the full scale range without the offset? then it would be ~9000).
vsluiter 7:9f6dfd88cd18 36 return press;
vsluiter 1:926025d703f4 37 }
vsluiter 1:926025d703f4 38
vsluiter 4:e69e2983bb6e 39 void valvecontrol(void)
vsluiter 4:e69e2983bb6e 40 {
vsluiter 4:e69e2983bb6e 41 static uint16_t usbcounter = 0;
vsluiter 4:e69e2983bb6e 42 float meas = getPressure_Pa();
franciscoguerra 8:89be5a6807f0 43 setpoint1= ain_setpoint.read()*60000;
franciscoguerra 8:89be5a6807f0 44 if(setpoint1 - meas >2500) {
vsluiter 4:e69e2983bb6e 45 v1close = 0;
vsluiter 4:e69e2983bb6e 46 v1open = 1;
franciscoguerra 8:89be5a6807f0 47 } else if (meas - setpoint1 > 2500) {
vsluiter 4:e69e2983bb6e 48 v1close = 1;
vsluiter 4:e69e2983bb6e 49 v1open = 0;
vsluiter 5:4772ef79675f 50 } else
vsluiter 4:e69e2983bb6e 51 v1open = v1close = 0;
vsluiter 4:e69e2983bb6e 52 usbcounter++;
vsluiter 7:9f6dfd88cd18 53 if(usbcounter == 20) {
vsluiter 4:e69e2983bb6e 54 scope.set(0,setpoint1);
vsluiter 4:e69e2983bb6e 55 scope.set(1,meas);
vsluiter 4:e69e2983bb6e 56 scope.set(2,meas);
vsluiter 4:e69e2983bb6e 57 scope.send();
vsluiter 4:e69e2983bb6e 58 usbcounter = 0;
vsluiter 4:e69e2983bb6e 59 }
vsluiter 4:e69e2983bb6e 60 }
vsluiter 4:e69e2983bb6e 61
vsluiter 4:e69e2983bb6e 62 int main()
vsluiter 4:e69e2983bb6e 63 {
vsluiter 7:9f6dfd88cd18 64 valvetick.attach(valvecontrol,0.001);
vsluiter 4:e69e2983bb6e 65 while(1) {
vsluiter 7:9f6dfd88cd18 66 //setpoint1 = ain_setpoint.read()*30000;
vsluiter 7:9f6dfd88cd18 67 //scope.set(2,ain_setpoint);
vsluiter 7:9f6dfd88cd18 68 wait(0.1);
vsluiter 4:e69e2983bb6e 69 }
vsluiter 0:80993ec26dfd 70 }