Amplitude Modulation Code

Dependencies:   HIDScope mbed

Fork of SymbitronValves by First Last

Committer:
vsluiter
Date:
Fri Jul 03 10:20:08 2015 +0000
Revision:
6:d73f051400ee
Parent:
5:4772ef79675f
Child:
7:9f6dfd88cd18
removed unnecessary libraries / objects

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 0:80993ec26dfd 18 #define PRESSURE A0
vsluiter 5:4772ef79675f 19 #define EXTERNAL_SETPOINT A4
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 1:926025d703f4 24 AnalogIn pressure(PRESSURE);
vsluiter 5:4772ef79675f 25 AnalogIn ain_setpoint(EXTERNAL_SETPOINT);
vsluiter 6:d73f051400ee 26 //TSIAnalogSlider slider(TSI_A, TSI_B, 100);
vsluiter 4:e69e2983bb6e 27 Ticker valvetick;
vsluiter 4:e69e2983bb6e 28
vsluiter 4:e69e2983bb6e 29 float setpoint1 = 0;
vsluiter 4:e69e2983bb6e 30
vsluiter 1:926025d703f4 31 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 5:4772ef79675f 35 float pressure = sensor_output*10000; //gain (might be off a bit. Is the full scale range without the offset? then it would be ~9000).
vsluiter 1:926025d703f4 36 return pressure;
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();
vsluiter 4:e69e2983bb6e 43 if(setpoint1 - meas >2000) {
vsluiter 4:e69e2983bb6e 44 v1close = 0;
vsluiter 4:e69e2983bb6e 45 v1open = 1;
vsluiter 5:4772ef79675f 46 } else if (meas - setpoint1 > 2000) {
vsluiter 4:e69e2983bb6e 47 v1close = 1;
vsluiter 4:e69e2983bb6e 48 v1open = 0;
vsluiter 5:4772ef79675f 49 } else
vsluiter 4:e69e2983bb6e 50 v1open = v1close = 0;
vsluiter 4:e69e2983bb6e 51 usbcounter++;
vsluiter 4:e69e2983bb6e 52 if(usbcounter == 200) {
vsluiter 4:e69e2983bb6e 53 scope.set(0,setpoint1);
vsluiter 4:e69e2983bb6e 54 scope.set(1,meas);
vsluiter 4:e69e2983bb6e 55 scope.set(2,meas);
vsluiter 4:e69e2983bb6e 56 scope.send();
vsluiter 4:e69e2983bb6e 57 usbcounter = 0;
vsluiter 4:e69e2983bb6e 58 }
vsluiter 4:e69e2983bb6e 59 }
vsluiter 4:e69e2983bb6e 60
vsluiter 4:e69e2983bb6e 61 int main()
vsluiter 4:e69e2983bb6e 62 {
vsluiter 4:e69e2983bb6e 63 valvetick.attach(valvecontrol,0.0001);
vsluiter 4:e69e2983bb6e 64 while(1) {
vsluiter 5:4772ef79675f 65 setpoint1 = ain_setpoint.read()*30000;
vsluiter 5:4772ef79675f 66 wait(0.05);
vsluiter 4:e69e2983bb6e 67 }
vsluiter 0:80993ec26dfd 68 }