Mbed code for the lab

Dependencies:   mbed

Committer:
DCamuti
Date:
Thu Sep 24 00:29:53 2015 +0000
Revision:
0:108062801773
Child:
1:abca5455262a
CoupledTankSystem

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DCamuti 0:108062801773 1 #include "mbed.h"
DCamuti 0:108062801773 2 // APIs
DCamuti 0:108062801773 3 Serial pc(USBTX, USBRX); // tx, rx for serial USB interface to pc
DCamuti 0:108062801773 4 SPI spi_max1270(p5, p6, p7);
DCamuti 0:108062801773 5 SPI spi(p5, p6, p7);
DCamuti 0:108062801773 6 DigitalOut max1270_cs(p8); //MAX1270 ADC CS
DCamuti 0:108062801773 7 DigitalOut mot1_ph1(p21);
DCamuti 0:108062801773 8 //DigitalOut mot1_ph2(p22);
DCamuti 0:108062801773 9 PwmOut mot_en1(p23);
DCamuti 0:108062801773 10
DCamuti 0:108062801773 11 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
DCamuti 0:108062801773 12
DCamuti 0:108062801773 13 int read_max1270(int chan, int range, int bipol){
DCamuti 0:108062801773 14 int cword=0x80; //set the start bit
DCamuti 0:108062801773 15
DCamuti 0:108062801773 16 spi_max1270.frequency(10000000);
DCamuti 0:108062801773 17 spi_max1270.format(8, 0); // 8 data bits, CPOL0, and CPHA0 (datasheet Digital Interface)
DCamuti 0:108062801773 18
DCamuti 0:108062801773 19 cword |= (chan << 4); //shift channel
DCamuti 0:108062801773 20 cword |= (range << 3);
DCamuti 0:108062801773 21 cword |= (bipol << 2);
DCamuti 0:108062801773 22
DCamuti 0:108062801773 23 max1270_cs = 0;
DCamuti 0:108062801773 24
DCamuti 0:108062801773 25 spi_max1270.write(cword);
DCamuti 0:108062801773 26 wait_us(15); //15us
DCamuti 0:108062801773 27 spi_max1270.format(12, 3);
DCamuti 0:108062801773 28
DCamuti 0:108062801773 29 int result = spi_max1270.write(0);
DCamuti 0:108062801773 30
DCamuti 0:108062801773 31 max1270_cs = 1;
DCamuti 0:108062801773 32 spi_max1270.format(8, 0);
DCamuti 0:108062801773 33 return result;
DCamuti 0:108062801773 34 }
DCamuti 0:108062801773 35
DCamuti 0:108062801773 36 float read_max1270_volts(int chan, int range, int bipol){
DCamuti 0:108062801773 37 float rangevolts=0.0;
DCamuti 0:108062801773 38 float volts=0.0;
DCamuti 0:108062801773 39 int adc_res;
DCamuti 0:108062801773 40
DCamuti 0:108062801773 41 //read the ADC converter
DCamuti 0:108062801773 42 adc_res = read_max1270(chan, range, bipol) & 0xFFF;
DCamuti 0:108062801773 43
DCamuti 0:108062801773 44 //Determine the voltage range
DCamuti 0:108062801773 45 if(range) //RNG bit
DCamuti 0:108062801773 46 rangevolts=10.0;
DCamuti 0:108062801773 47 else
DCamuti 0:108062801773 48 rangevolts=5.0;
DCamuti 0:108062801773 49
DCamuti 0:108062801773 50 //bi-polar input range
DCamuti 0:108062801773 51 if(bipol){ //BIP is set, input is +/-
DCamuti 0:108062801773 52 if(adc_res < 0x800){ //if result was positive
DCamuti 0:108062801773 53 volts = ((float)adc_res/0x7FF) * rangevolts;
DCamuti 0:108062801773 54 }
DCamuti 0:108062801773 55 else{ //result was negative
DCamuti 0:108062801773 56 volts = -(-((float)adc_res/0x7FF) * rangevolts) - (rangevolts * 2.0);
DCamuti 0:108062801773 57 }
DCamuti 0:108062801773 58 }
DCamuti 0:108062801773 59 else{ //input is positive polarity only
DCamuti 0:108062801773 60 volts = ((float)adc_res/0xFFF) * rangevolts;
DCamuti 0:108062801773 61 }
DCamuti 0:108062801773 62
DCamuti 0:108062801773 63 return volts;
DCamuti 0:108062801773 64 }
DCamuti 0:108062801773 65
DCamuti 0:108062801773 66 float Tank1,Tank2,dt;
DCamuti 0:108062801773 67 float Ts = 1.0; // Sampling period 1/Ts Hz
DCamuti 0:108062801773 68
DCamuti 0:108062801773 69 // Arrays for data storage
DCamuti 0:108062801773 70 float etime[200];
DCamuti 0:108062801773 71 float t1v[200];
DCamuti 0:108062801773 72 float t2v[200];
DCamuti 0:108062801773 73 float dcp[200];
DCamuti 0:108062801773 74 Timer t;
DCamuti 0:108062801773 75
DCamuti 0:108062801773 76
DCamuti 0:108062801773 77 float cntr;
DCamuti 0:108062801773 78 float dc;
DCamuti 0:108062801773 79 int k;
DCamuti 0:108062801773 80
DCamuti 0:108062801773 81 int main ()
DCamuti 0:108062801773 82 {
DCamuti 0:108062801773 83 pc.baud(921600); // Establish baud rate
DCamuti 0:108062801773 84 mot_en1.period_us(50); // Set PWM length to 50 us
DCamuti 0:108062801773 85 max1270_cs = 1; // Activate A/D
DCamuti 0:108062801773 86 cntr = 0.0; // cntr used to keep track of sample period and elpased time
DCamuti 0:108062801773 87
DCamuti 0:108062801773 88 // initialize data vectors
DCamuti 0:108062801773 89 for(k=0;k<100;k++)
DCamuti 0:108062801773 90 { etime[k] = 0.0;
DCamuti 0:108062801773 91 t1v[k] = 0.0;
DCamuti 0:108062801773 92 t2v[k] = 0.0;
DCamuti 0:108062801773 93 dcp[k] = 0.0;
DCamuti 0:108062801773 94 }
DCamuti 0:108062801773 95 k = 0; // Reset index counter
DCamuti 0:108062801773 96 pc.printf("Time Tank1 Tank2 DC \n\r");
DCamuti 0:108062801773 97 while(cntr*Ts <= 60) {
DCamuti 0:108062801773 98 t.start(); // start measuring comp time
DCamuti 0:108062801773 99
DCamuti 0:108062801773 100 // Read pressure sensors
DCamuti 0:108062801773 101 Tank1 = read_max1270_volts(1, 1, 1); // in volt
DCamuti 0:108062801773 102 Tank2 = read_max1270_volts(0, 1, 1);
DCamuti 0:108062801773 103
DCamuti 0:108062801773 104 // Select pump duty cycle, start time, and end time
DCamuti 0:108062801773 105 if (cntr*Ts <= 10) { //start time
DCamuti 0:108062801773 106 dc = 0.0;
DCamuti 0:108062801773 107 } else if (cntr*Ts <= 20) { //end time
DCamuti 0:108062801773 108 //
DCamuti 0:108062801773 109 //dc = -0.80; // duty cycle (must be negative)
DCamuti 0:108062801773 110 //dc=-0.60;
DCamuti 0:108062801773 111 //dc=-0.65;
DCamuti 0:108062801773 112 //dc=-0.70;
DCamuti 0:108062801773 113 //dc=-0.75;
DCamuti 0:108062801773 114 dc = -0.0;
DCamuti 0:108062801773 115 } else {
DCamuti 0:108062801773 116 dc = 0.0;
DCamuti 0:108062801773 117 }
DCamuti 0:108062801773 118
DCamuti 0:108062801773 119 if(dc > 0.0){
DCamuti 0:108062801773 120 //mot1_ph2 = 0;
DCamuti 0:108062801773 121 mot1_ph1 = 1;
DCamuti 0:108062801773 122 mot_en1 = dc;
DCamuti 0:108062801773 123 }
DCamuti 0:108062801773 124 else if(dc <=0.0){
DCamuti 0:108062801773 125 mot1_ph1 = 0;
DCamuti 0:108062801773 126 //mot1_ph2 = 1;
DCamuti 0:108062801773 127 mot_en1 = abs(dc);}
DCamuti 0:108062801773 128
DCamuti 0:108062801773 129 // Log data
DCamuti 0:108062801773 130 etime[k] = cntr*Ts;
DCamuti 0:108062801773 131 t1v[k] = Tank1;
DCamuti 0:108062801773 132 t2v[k] = Tank2;
DCamuti 0:108062801773 133 dcp[k] = -dc;
DCamuti 0:108062801773 134 k++;
DCamuti 0:108062801773 135
DCamuti 0:108062801773 136 t.stop(); // end measuring comp time
DCamuti 0:108062801773 137 dt = Ts-t.read();
DCamuti 0:108062801773 138 //pc.printf("%5.2f %5.2f %5.2f %5.2f \n\r",cntr*Ts,Tank1,Tank2,dc);
DCamuti 0:108062801773 139 pc.printf("%5.2f %5.2f %5.2f %5.2f \n\r",cntr*Ts,Tank1,Tank2,dc);
DCamuti 0:108062801773 140 t.reset();
DCamuti 0:108062801773 141 cntr=cntr+1;
DCamuti 0:108062801773 142 wait(dt); // wait to ensure sampling period set by Ts
DCamuti 0:108062801773 143 }//while
DCamuti 0:108062801773 144 //mot1_ph2 = 0;
DCamuti 0:108062801773 145 mot_en1 = 0.0;
DCamuti 0:108062801773 146
DCamuti 0:108062801773 147
DCamuti 0:108062801773 148 }//main