Version 3 is with update to the test rig with a linear actuator

Dependencies:   SPTE_10Bar_5V mbed AS5048 SDFileSystem MODSERIAL PinDetect LCM101 LinearActuator

Revision:
12:b0a6faaa5e9f
Parent:
10:77fcbad99a31
--- a/main.cpp	Mon Feb 17 15:15:24 2020 +0000
+++ b/main.cpp	Tue Feb 25 15:04:21 2020 +0000
@@ -10,20 +10,8 @@
 #include "bench.h"
 
  
-//Example experiment method 
-void runDemoExperiment0(int cycles, float targetkPa);
-
-//Methods for testing DAC chip - leave for now
-void selectDACB();
-void deselectDACB();
-
-void testDAC();
-void testToggleChannel();
-
-DigitalOut myled(LED1);
-DigitalOut CSA(DAC_CSA);
-DigitalOut CSB(DAC_CSB);
-SPI spi(DAC_MOSI,DAC_MISO,DAC_SCLK);
+//Experiment methods
+void runFatigueExperiment0(int cycles, float targetkPa,float inflateTimeOut,float deflateTimeOut);
 
 // Create bench object - this is used to control the test rig
 Bench leg;
@@ -33,7 +21,7 @@
  */
 int main()
 {   
-    leg.setLoggingFrequency(100); //Set datalogging frequency
+    leg.setLoggingFrequency(10); //Set datalogging frequency
     
     /* Two extra columns of data will be recorded in this experiment.
     One for the target pressure, and the other for the number of sit and 
@@ -41,266 +29,121 @@
     string colNames[] = {"Target pressure (kPa)","Cycle"}; //add data headings
     leg.setExtraColumns(colNames,2);
     
-    float targetP = 200; //Target pressure in kPa
-    int expCycles = 2; //Number of sit to stand to sit cycles 
+    float targetP = 350; //Target pressure in kPa
+    int expCycles = 50; //Number of sit to stand to sit cycles 
     float vals[] = {targetP,0}; //set initial values of data that will be logged
     leg.setExtraData(vals);
     
     /* Setup all peripherals on rig, display info about SD card and the 
     user interface menu */ 
-    leg.initialise(); //this gives the 10MHz, normal clock polarity, mode 1 spi we need, pins are same as for encoder
-    
-
+    leg.initialise();
       
     /*Run an experiment when the button is pressed to start datalogging and 
     stop it if the button is pressed again to stop datalogging 
     (or when experiment stops - then datalogging stops by itself) */
     while (true) {
         if (leg.isLogging()) {
-            runDemoExperiment0(expCycles,targetP);            
+            runFatigueExperiment0(expCycles,targetP,10,10);            
         }
+        wait(0.5);
     }
 }
 
 /**
- * Shows how a demo experiment works. This experiment pressurises the leg to 
+ * Shows how a fatigue experiment works. This experiment pressurises the leg to 
  * pressure targetkPa, depressurises it, and then repeats the process cycles 
  * number of times
  * @param cycles: the number of cycles the leg goes up and down
  * @param targetkPa: the pressure at which the valve is opened to let the leg go down
+ * @param inflateTimeOut: time in seconds after which experiment will stop if target pressure is not reached
+  * @param inflateTimeOut: time in seconds after which experiment will stop if deflation pressure is not reached
  */
-void runDemoExperiment0(int cycles, float targetkPa) 
+void runFatigueExperiment0(int cycles, float targetkPa, float inflateTimeOut,float deflateTimeOut) 
 {
-    //The experiment starts when logging does
-    bool experimentRunning = leg.isLogging(); 
+    Timer flowT;//used to time flow into and out of actuator
+    float loopTime = 0.1; //(s) time between checking pressure
+    
+    leg.StartLogging();
     
     //Stop the Bench class from printing, so this method can print
     leg.pausePrint();
-    
-    if (experimentRunning) {
-        // Pressurise and depressurise the leg cycles number of times
-        for (int i=0; i<cycles; i++) {
-            leg.pc.printf("\r\nCycle: \t%i out of \t%i",i+1,cycles);
-            
-            //Update cycles logged
-            float data[] = {targetkPa,i+1};
-            leg.setExtraData(data);
-
-            //Pressurise
-            leg.setValve(true);
+        
+    // Pressurise and depressurise the leg cycles number of times
+    for (int i=0; i<cycles; i++) {
+        //Print the progress
+        leg.pc.printf("\r\nCycle: \t%i out of \t%i",i+1,cycles);
 
-            //Wait until measured pressure reaches target pressure
-            float measureP = 0;
-            while(measureP < targetkPa) {
-                //Keep checking logging is going
-                experimentRunning = leg.isLogging(); 
-                
-                if (experimentRunning) {
-                    measureP = leg.getPressure0()*100;//Conversion of bar to kPa
-                    wait(0.05);//Wait a bit
-                } else { //Logging stopped
-                    leg.stopLogging(); //Stop logging data
-                    leg.setValve(false); //Depressurise
-                    leg.resumePrint(); //Let the Bench class print
-                    return;
-                }
-            }
-
-            //Depressurise
-            leg.setValve(false);
+        //Update cycles logged
+        float data[] = {targetkPa,i+1};
+        leg.setExtraData(data);        
 
-            /*Wait until depressurised (completely depressurised is 
-            around 10-12 kPa due to current sensor calibration)*/
-            while(measureP > 15) {
-                //Keep checking logging is going
-                experimentRunning = leg.isLogging();
-                
-                if (experimentRunning) {
-                    measureP = leg.getPressure0()*100;//Conversion of bar to kpa
-                    wait(0.05);//Wait a bit
-                } else { //Logging stopped
-                    leg.stopLogging(); //Stop logging data
-                    leg.resumePrint(); //Let the Bench class print
-                    return;
-                }
-            }
-        }
-        // Logging stopped as experiment is fully completed
-        leg.stopLogging(); //Stop logging data
-        leg.resumePrint(); //Let the Bench class print
-    }
-}
+        //Pressurise
+        leg.setValve(true);
+        flowT.reset();
+        flowT.start();// start inflation timer
+
+        //Wait until measured pressure reaches target pressure
+        while(leg.getPressure0()*100 < targetkPa && flowT.read() < inflateTimeOut) {
 
-void testDAC() {
-    //setup SPI to write 8 bit words, mode 1 and turn select lines high
-    spi.format(8,1);
-    spi.frequency(200000);
-    deselectDACB();
-    wait_ms(20);
-    
-    //Power up DAC
-    selectDACB();
-    spi.write(0xE0);
-    spi.write(0x00);
-    deselectDACB();
-    
-    //Write a value to a channel
-    selectDACB();
-    spi.write(0x47);
-    spi.write(0xFF);
-    deselectDACB();
-    
+            //Keep checking logging is going
+            if (!leg.isLogging()) {
+                leg.pc.printf("\r\nExit A");
+                //Logging stopped
+                leg.setValve(false); //Depressurise
+                leg.StopLogging(); //Stop logging data
+                leg.resumePrint(); //Let the Bench class print
+                return;
+            }
+            
+            leg.LogData();
+            wait(loopTime);//Wait a bit
+        }
+        leg.pc.printf("\r\nTimer inflate: \t%7.2f",flowT.read());
+        if(flowT.read() >= inflateTimeOut) {
+            leg.pc.printf("\r\nExit B");
+            //Logging stopped
+            leg.setValve(false); //Depressurise
+            leg.StopLogging(); //Stop logging data
+            leg.resumePrint(); //Let the Bench class print
+            return;
+        }
+        
+        //Depressurise
+        leg.setValve(false);
+        flowT.reset();
 
-    
-    
-  /*  selectDACB();
-    spi.write(0x2F);
-    spi.write(0xFF);
-    deselectDACB();
-    
-    selectDACB();
-    spi.write(0x60);
-    spi.write(0x00);
-    deselectDACB();*/
-    
-    //wait(3);
-    
-    //Write a value to a channel
-  /*  selectDACB();
-    spi.write(0x40);
-    spi.write(0x00);
-    deselectDACB();*/
-    
-    while (true) {
-        myled = !myled;
-
-
-        wait_ms(500);
+        /*Wait until depressurised (completely depressurised is
+        around 10-12 kPa due to current sensor calibration)*/
+        while(leg.getPressure0()*100 > 15 && flowT.read() < deflateTimeOut) {
+            
+            //Keep checking logging is going
+            if (!leg.isLogging()) {
+                leg.pc.printf("\r\nExit C");
+                //Logging stopped
+                leg.setValve(false); //Depressurise
+                leg.StopLogging(); //Stop logging data
+                leg.resumePrint(); //Let the Bench class print
+                return;
+            }
+                        
+            leg.LogData();
+            wait(loopTime);//Wait a bit
+        }
+        
+        leg.pc.printf("\r\nTimer deflate: \t%7.2f",flowT.read());
+        if(flowT.read() >= deflateTimeOut) {
+            leg.pc.printf("\r\nExit D");
+            //Logging stopped
+            leg.setValve(false); //Depressurise
+            leg.StopLogging(); //Stop logging data
+            leg.resumePrint(); //Let the Bench class print
+            return;
+        }
     }
-}    
-
-/** Selects DAC B (enable line goes low)
- */
-void selectDACB()
-{
-    CSB.write(0);
-    wait_us(1);
+    leg.pc.printf("\r\nExit E");
+    // Logging stopped as experiment is fully completed
+    leg.setValve(false); //Depressurise
+    leg.StopLogging(); //Stop logging data
+    leg.resumePrint(); //Let the Bench class print    
 }
 
-/** Deselects DAC B (enable line goes high)
- */
-void deselectDACB()
-{
-    CSB.write(1);
-    wait_us(1);
-}
-    
-void testToggleChannel()
-{
-        // POWER up dac
-    //select chip
-    CSB.write(0);
-    wait_us(1);
-
-    spi.write(0b11100000);
-    spi.write(0b00000000);
-
-    //deselect chip
-    CSB.write(1);
-    wait_us(1);
-
-
-    //write output on a
-    //select chip
-    CSB.write(0);
-    wait_us(1);
-
-    spi.write(0b01000011);
-    spi.write(0b11111111);
-
-    //deselect chip
-    CSB.write(1);
-    wait_us(1);
-    
-   
-    //write output on b
-    //select chip
-    CSB.write(0);
-    wait_us(1);
-
-    spi.write(0b01011011);
-    spi.write(0b11111111);
-
-    //deselect chip
-    CSB.write(1);
-    wait_us(1);
-    
-    while (true) {
-        //leg.pc.printf("Hi");
-        // POWER up dac
-        //select chip
-        CSB.write(0);
-        wait_us(1);
-
-        spi.write(0b11100000);
-        spi.write(0b00000000);
-
-        //deselect chip
-        CSB.write(1);
-        wait_us(1);
-        //write output on a
-        //select chip
-        CSB.write(0);
-        wait_us(1);
-
-        spi.write(0b01010011);
-        spi.write(0b11111111);
-
-        spi.write(0b01001011);
-        spi.write(0b11111111);
-
-        //deselect chip
-        CSB.write(1);
-        wait_us(1);
-
-        wait_ms(100);
-
-
-    }
-    bool ch = true;
-    while (true) {
-        //data value
-        unsigned int data = 0xFFF;
-
-        //if more than 12 bits (0xfff) then set all bits true)
-        if (data > 0xFFF) {
-            data = 0xFFF;
-        }
-
-        //select chip
-        //bring cs low
-        CSB.write(0);
-        //wait a bit (more than 40ns)
-        wait_us(1);
-
-        //transfer a command (for channel a 0x4<<12 + data masked to 12 bits, for channel b 0x5<<12 + data masked to 12 bits)
-
-        int command = (0x01<<12);//default to channel a
-        /*if (!ch) {
-            command = (0x05<<12);
-            } */
-        data = command + (data&0xFFF);
-        //spi.write(data);
-        spi.write(data>>8);
-        spi.write(data & 0x00FF);
-        //bring cs high
-        CSB.write(1);
-        //wait a bit (more than 10-15ns)
-        wait_us(1);
-        wait_ms(10);
-        //leg.pc.printf("\r\nCommand: \t%i",command);
-
-        //leg.pc.printf("\r\nData: \t%i",data);
-        ch = !ch;
-    }
-}