...

Dependencies:   mbed Matrix

Revision:
1:daaa91186ace
Parent:
0:722b391fe228
Child:
2:a9366dce183b
--- a/main.cpp	Tue Aug 05 19:22:19 2014 +0000
+++ b/main.cpp	Thu Aug 07 17:55:45 2014 +0000
@@ -4,14 +4,11 @@
 SPI spi_max1270(p5, p6, p7);
 SPI spi(p5, p6, p7);
 DigitalOut max1270_cs(p8);  //MAX1270 ADC CS
-//DigitalOut max522_cs(p11);  //MAX522 DAC CS
 DigitalOut mot1_ph1(p21);
 DigitalOut mot1_ph2(p22);
 PwmOut mot_en1(p23);
 
-/*DigitalOut mot2_ph1(p24);
-DigitalOut mot2_ph2(p25);
-PwmOut mot_en2(p26);*/
+LocalFileSystem local("local"); // Create the local filesystem under the name "local"
 
 int read_max1270(int chan, int range, int bipol){
     int cword=0x80;     //set the start bit
@@ -66,117 +63,66 @@
    return volts;     
 }
 
-//Motor control routine for PWM on 5 pin motor driver header
-// drv_num is 1 or 2 (defaults to 1, anything but 2)
-// dc is signed duty cycle (+/-1.0)
-
-/*void mot_control(int drv_num, float dc){        
-    if(dc>1.0)
-        dc=1.0;
-    if(dc<-1.0)
-        dc=-1.0;
-    
-    if(drv_num != 2){           
-        if(dc > 0.0){
-            mot1_ph2 = 0;
-            mot1_ph1 = 1;
-            mot_en1 = dc;
-        }
-        else if(dc < -0.0){
-            mot1_ph1 = 0;
-            mot1_ph2 = 1;
-            mot_en1 = abs(dc);
-        }
-        else{
-            mot1_ph1 = 0;
-            mot1_ph2 = 0;
-            mot_en1 = 0.0;
-        }
-    }                
-    else{
-        if(dc > 0.0){
-            mot2_ph2 = 0;
-            mot2_ph1 = 1;
-            mot_en2 = dc;
-        }
-        else if(dc < -0.0){
-            mot2_ph1 = 0;
-            mot2_ph2 = 1;
-            mot_en2 = abs(dc);
-        }
-        else{
-            mot2_ph1 = 0;
-            mot2_ph2 = 0;
-            mot_en2 = 0.0;
-        }
-    }                   
-}*/
-
-//------- MAX522 routines ---------------------------------
-/*void write_max522(int chan, float volts){
-    int cmd=0x20;   //set UB3
-    int data_word = (int)((volts/5.0) * 256.0);
-    if(chan != 2)
-        cmd |= 0x01;    //set DAC A out
-    else
-        cmd |= 0x02;    //set DACB out        
-    
- //   pc.printf("cmd=0x%4X  data_word=0x%4X \r\n", cmd, data_word);
-    
-    spi.format(8, 0);
-    spi.frequency(2000000);
-    max522_cs = 0;
-    spi.write(cmd & 0xFF);
-    spi.write(data_word & 0xFF);
-    max522_cs = 1;    
-    spi.frequency(10000000);
-}*/
-
 float Tank1,Tank2,dt,h1,h2;
-float Ts = 0.5; // 1/Ts Hz
+float Ts = 1.0; // Sampling period 1/Ts Hz
+//float Tl = 0.5; // Logging period
 float c1 = 15.0/(2.77+1.24); // cm/V
 float c2 = 11.5/(3.58-0.79); // cm/V
 float v10 = -0.52; // V
 float v20 = -3.35; // V
+// Arrays for data storage
+float etime[200];
+float t1v[200];
+float t2v[200];
+//float t1h[200];
+//float t2h[200];
+float dcp[200];
 Timer t;
 
+ // Open "results.M" on the local file system for writing
+FILE *fp = fopen("/local/results.M", "w");
+
 float cntr;
 float dc;
+int k;
+
 int main ()
 {
-    pc.baud(921600);
-    mot_en1.period_us(50);    
-    max1270_cs = 1;
-    //Ts = 0.1; 
-    //dc = 0.2;
-    cntr = 0.0;
-    while(cntr*Ts <= 120) {
+    pc.baud(921600); // Establish baud rate 
+    mot_en1.period_us(50); // Set PWM length to 50 us   
+    max1270_cs = 1; // Activate A/D
+    cntr = 0.0; // cntr used to keep track of sample period and elpased time
+    
+    // initialize data vectors
+    for(k=0;k<200;k++)
+  { etime[k] = 0.0;
+    t1v[k] = 0.0;
+    t2v[k]    = 0.0;
+    //t1h[k]    = 0.0;
+    //t2h[k]    = 0.0;
+    dcp[k]    = 0.0;    
+  }
+  k = 0; // Reset index counter
+    
+    while(cntr*Ts <= 180) {
         t.start(); // start measuring comp time
-        // Read pressure sensors
+        
+        // Read pressure sensors        
+        Tank1 = read_max1270_volts(1, 1, 1);
+        Tank2 = read_max1270_volts(0, 1, 1);
+        
+        // Convert pressure voltage to tank height
+        //h1 = c1*(Tank1 - v10);
+        //h2 = c2*(Tank2 - v20);
                 
-        Tank1 = read_max1270_volts(0, 1, 1);
-        h1 = c1*(Tank1 - v10);
-        Tank2 = read_max1270_volts(1, 1, 1);
-        h2 = c2*(Tank2 - v20);
-        // drive motor
-        //if (cntr*Ts <= 0.05) {
-        //    dc = 0.0;
-        //} else if (cntr*Ts <= 0.2) {
-        //    dc = 0.3;
-        //} else {
-        //    dc = 0.7;
-        //}
-        
+        // Drive pump for ID
         if (cntr*Ts <= 5) {
             dc = 0.0;
-        } else if (cntr*Ts <= 110) {
-            dc = -0.75;
+        } else if (cntr*Ts <= 175) {
+            dc = -0.7;
         } else {
             dc = 0.0;
-        }
-        
-        //dc = 0.75;
-        //mot_control(1,-dc);
+        }                
         
         if(dc > 0.0){
             mot1_ph2 = 0;
@@ -187,18 +133,41 @@
             mot1_ph1 = 0;
             mot1_ph2 = 1;
             mot_en1 = abs(dc);}
-        
-       
+        // Log data
+        etime[k] = cntr*Ts;
+        t1v[k] = Tank1;
+        t2v[k] = Tank2;
+        //t1h[k] = h1;
+        //t2h[k] = h2;
+        dcp[k] = -dc;
+        k++;
+              
         t.stop(); // end measuring comp time
-        //printf("%f\n\r",t.read());
         dt = Ts-t.read();
-        //printf("%f\n\r",dt);
-        pc.printf("%5.2f %5.2f %5.2f %5.2f %5.2f %5.2f \n\r",cntr*Ts,Tank1,Tank2,h1,h2,dc);
-        //printf("%5.2f \n\r",angleNew);
+        pc.printf("%5.2f %5.2f %5.2f %5.2f \n\r",cntr*Ts,Tank1,Tank2,dc);
+        //pc.printf("%5.2f %5.2f %5.2f %5.2f %5.2f %5.2f \n\r",cntr*Ts,Tank1,Tank2,h1,h2,dc);        
         t.reset();
         cntr=cntr+1;
         wait(dt); // wait to ensure sampling period set by Ts    
     }//while
     mot1_ph2 = 0;
     mot_en1 = 0.0;
+    
+    // Print out log variables in MATLAB structured variable format.
+    pc.printf("Printing log variables to file on mBed           ... ");
+    if(1) {
+        for(k=0; k<200; k++) {
+            fprintf(fp,"t(%d,1) = %.5f;\n",k+1,etime[k]);
+            fprintf(fp,"t1v(%d,1) = %.5f;\n",k+1,t1v[k]);
+            fprintf(fp,"t2v(%d,1)    = %.5f;\n",k+1,t2v[k]);
+            //fprintf(fp,"t1h(%d,1)    = %.5f;\n",k+1,t1h[k]);
+            //fprintf(fp,"t2h(%d,1)    = %.5f;\n",k+1,t2h[k]);
+            fprintf(fp,"dcp(%d,1)    = %.5f;\n",k+1,dcp[k]);            
+        }        
+    }
+    printf("done.\r\n");
+
+    // Close file
+    fclose(fp);
+    
 }//main
\ No newline at end of file