...

Dependencies:   mbed Matrix

Files at this revision

API Documentation at this revision

Comitter:
riobrien
Date:
Wed Feb 11 21:03:39 2015 +0000
Parent:
1:daaa91186ace
Commit message:
none

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r daaa91186ace -r a9366dce183b main.cpp
--- a/main.cpp	Thu Aug 07 17:55:45 2014 +0000
+++ b/main.cpp	Wed Feb 11 21:03:39 2015 +0000
@@ -5,7 +5,7 @@
 SPI spi(p5, p6, p7);
 DigitalOut max1270_cs(p8);  //MAX1270 ADC CS
 DigitalOut mot1_ph1(p21);
-DigitalOut mot1_ph2(p22);
+//DigitalOut mot1_ph2(p22);
 PwmOut mot_en1(p23);
 
 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
@@ -65,22 +65,25 @@
 
 float Tank1,Tank2,dt,h1,h2;
 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
+
+// Calibration constant: change for your system
+float v10 = 0;// V
+float v20 = 0; //V
+float c1 = 1; // cm/V
+float c2 = 1;// cm/V
+
+int leng = 200;
 // Arrays for data storage
 float etime[200];
 float t1v[200];
 float t2v[200];
-//float t1h[200];
-//float t2h[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");
+FILE *fp = fopen("/local/cal.M","w");
 
 float cntr;
 float dc;
@@ -94,17 +97,17 @@
     cntr = 0.0; // cntr used to keep track of sample period and elpased time
     
     // initialize data vectors
-    for(k=0;k<200;k++)
+    for(k=0;k<leng;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;    
+    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) {
+    pc.printf(" Time Tank1  Tank2 H1    H2     DC \n\r");        
+    while(cntr*Ts <= leng) {
         t.start(); // start measuring comp time
         
         // Read pressure sensors        
@@ -112,45 +115,46 @@
         Tank2 = read_max1270_volts(0, 1, 1);
         
         // Convert pressure voltage to tank height
-        //h1 = c1*(Tank1 - v10);
-        //h2 = c2*(Tank2 - v20);
+        h1 = c1*(Tank1 - v10);
+        h2 = c2*(Tank2 - v20);
                 
         // Drive pump for ID
-        if (cntr*Ts <= 5) {
+        if (cntr*Ts <= 10) {
             dc = 0.0;
-        } else if (cntr*Ts <= 175) {
+        } else if (cntr*Ts <= 200) {
             dc = -0.7;
         } else {
             dc = 0.0;
-        }                
+        } 
+        //dc = -0.7;               
         
         if(dc > 0.0){
-            mot1_ph2 = 0;
+            //mot1_ph2 = 0;
             mot1_ph1 = 1;
             mot_en1 = dc;
         }
         else if(dc < -0.0){
             mot1_ph1 = 0;
-            mot1_ph2 = 1;
+            //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;
+        t1h[k] = h1;
+        t2h[k] = h2;
         dcp[k] = -dc;
         k++;
               
         t.stop(); // end measuring comp time
         dt = Ts-t.read();
-        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);        
+        //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;
+    //mot1_ph2 = 0;
     mot_en1 = 0.0;
     
     // Print out log variables in MATLAB structured variable format.
@@ -160,8 +164,8 @@
             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,"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]);            
         }        
     }