Andreas Steffen / Mbed 2 deprecated PIservo

Dependencies:   mbed

Revision:
1:dc799997391e
Parent:
0:3e6ae21a6bb1
Child:
2:6d472c554758
diff -r 3e6ae21a6bb1 -r dc799997391e main.cpp
--- a/main.cpp	Mon Aug 06 12:53:57 2012 +0000
+++ b/main.cpp	Mon Aug 06 17:24:16 2012 +0000
@@ -8,10 +8,9 @@
 
 #include "mbed.h"
 
-AnalogIn ctl(p20);
-AnalogIn pd(p16);
+DigitalOut adcCS(p8);
 AnalogOut vca(p18);
-DigitalOut led1(LED1);
+SPI adcSPI(p5,p6,p7); //talk to ad7921
 Serial pc(USBTX, USBRX);
 
 
@@ -19,14 +18,22 @@
 Ticker commTick;
 Ticker intTick;
 
-float pGain = 1.;
-float iGain = 1000.;
-float integrator = 0.; //the building up integrator value
+float pGain = 0.5;
+float iGain = 5000.;
+float integrator = 0; //the building up integrator value
+
+//for keeping track of the cycle rate
+float cycT=0;  
+float cycN=0;
 
 void serialComm(){
     char c;
     float f;
     
+    pc.printf("Average cycle time [us]: %f\n",cycT/cycN);
+    cycT = 0;
+    cycN = 0;
+    
     if (pc.readable()) { 
         c = pc.getc();
         pc.scanf("%f", &f);
@@ -41,7 +48,7 @@
                 break;
             default:
                 pc.printf("Command not understood.\n",iGain);
-                pc.printf("Read %c and %lf.\n",c,f);
+                pc.printf("Read %c and %f.\n",c,f);
                 break;
         }
     }
@@ -57,18 +64,42 @@
 }
 
 int main() {
-    float err;
+    float err=0.;
+    int ctl=0;
+    int pd=0; 
     pc.printf("mbed restarted!\n");
     
-    commTick.attach(&serialComm,0.5); //check serial every half second  
-    intTick.attach(&limitIntegrator,0.001); //check integrator overrun every ms  
+    adcSPI.format(16,3);
+    adcSPI.frequency(2000000); //2MHz
+    
+    //commTick.attach(&serialComm,0.5); //check serial every half second  
+    //intTick.attach(&limitIntegrator,0.001); //check integrator overrun every ms  
     
     t.start();
+    
 
     while(1) {
-        err = ctl - pd;  
-        integrator += err * iGain*1e-6 * t.read_us(); //iGain is in Hz!
-        t.reset();  //reset timer to get next integration time      
-        vca = err * pGain + integrator;        
+        adcCS = 0;
+        ctl = adcSPI.write(3<<13); //select ch 1
+        adcCS = 1;
+        wait_us(20);
+        adcCS = 0;
+        pd = adcSPI.write(1);       // select ch 0
+        //take the last 12 bits that carry data
+        // for some reason, it seems shifted up by one...
+        pd = (pd>>1) & 0xFFF;
+        ctl = (ctl>>1) & 0xFFF;
+        pc.printf("Read %i from V0 and %i from V1.\n",ctl,pd);
+        adcCS = 1;
+        wait_ms(100);
+       /* err = ctl.read() - pd.read();  
+        cycT += t.read_us();
+        cycN += 1.;
+        integrator += err * iGain * t.read_us(); 
+        t.reset(); //reset timer to get next integration time 
+             
+        vca = err * pGain + integrator;        //analog output
+        
+        */
     }
 }