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:
11:fc82dd22a527
Parent:
10:77fcbad99a31
Child:
13:219c16e7d32c
--- a/main.cpp	Mon Feb 17 15:15:24 2020 +0000
+++ b/main.cpp	Wed Aug 12 12:05:58 2020 +0000
@@ -8,10 +8,12 @@
 //Both the general mbed header and the test rig bench header are needed 
 #include "mbed.h"
 #include "bench.h"
-
  
 //Example experiment method 
-void runDemoExperiment0(int cycles, float targetkPa);
+void runFatigueExperiment0(int cycles, float targetkPa, float inflateTimeOut,float deflateTimeOut);
+void runFailureExp0(float targetkPa);
+void runBenchmarkExperiment0();
+void runBenchmarkExperiment1(int pwm, int cycles, int logHz);
 
 //Methods for testing DAC chip - leave for now
 void selectDACB();
@@ -21,7 +23,7 @@
 void testToggleChannel();
 
 DigitalOut myled(LED1);
-DigitalOut CSA(DAC_CSA);
+DigitalOut CSA(DAC_CSA);    
 DigitalOut CSB(DAC_CSB);
 SPI spi(DAC_MOSI,DAC_MISO,DAC_SCLK);
 
@@ -33,16 +35,16 @@
  */
 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 
     stand cycles currently completed in the experiment */ 
-    string colNames[] = {"Target pressure (kPa)","Cycle"}; //add data headings
+    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;                                                                                                                                                                                             0; //Target pressure in kPa
+    int expCycles = 100; //Number of sit to stand to sit cycles 
     float vals[] = {targetP,0}; //set initial values of data that will be logged
     leg.setExtraData(vals);
     
@@ -57,8 +59,13 @@
     (or when experiment stops - then datalogging stops by itself) */
     while (true) {
         if (leg.isLogging()) {
-            runDemoExperiment0(expCycles,targetP);            
+            leg.pc.printf("Logging started");
+            //runFatigueExperiment0(expCycles,targetP,10,10);
+            //runBenchmarkExperiment0();
+            runBenchmarkExperiment1(75, 1, 10);    
+            //runFailureExp0(targetP);        
         }
+        wait(0.5);
     }
 }
 
@@ -69,16 +76,21 @@
  * @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
  */
-void runDemoExperiment0(int cycles, float targetkPa) 
+void runFatigueExperiment0(int cycles, float targetkPa, float inflateTimeOut, float deflateTimeOut) 
 {
+    //leg.pc.printf("\r\nEntered experiment");
     //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
+    int num_file = 10; //number of data files to save 
     
+    for (int i=0; i<num_file; i++) {
+        
+    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
+    // 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);
             
@@ -88,49 +100,195 @@
 
             //Pressurise
             leg.setValve(true);
-
+            flowT.reset();
+            flowT.start();// start inflation timer
+            
             //Wait until measured pressure reaches target pressure
-            float measureP = 0;
-            while(measureP < targetkPa) {
+            
+            while(leg.getPressure0()*100 < targetkPa && flowT.read() < inflateTimeOut) {
+    
                 //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
+                //experimentRunning = leg.isLogging(); 
+                //leg.pc.printf("\r\nPressure (kPa): \t%7.2f",leg.getPressure0()*100);
+                if (!leg.isLogging()) {
+                    leg.pc.printf("\r\nExit A");
+                    //Logging stopped
+                    //leg.pc.printf("\r\nPressurising exit"); 
                     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.pausePrint();
+        leg.setValve(false);
+        flowT.reset();
+
+        /*Wait until depressurised (completely depressurised is
+        around 10-12 kPa due to current sensor calibration)*/
+        while(leg.getPressure0()*100 > 15 && flowT.read() < deflateTimeOut) {
+            //leg.pc.printf("\r\nDepressurising");
+            //Keep checking logging is going
+            //experimentRunning = leg.isLogging();
+            //leg.pc.printf("\r\nPressure (kPa): \t%7.2f",leg.getPressure0()*100);
+            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;
             }
 
-            //Depressurise
-            leg.setValve(false);
+            leg.LogData();
+            wait(loopTime);//Wait a bit
+        }
 
-            /*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;
-                }
-            }
+        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;
         }
-        // Logging stopped as experiment is fully completed
-        leg.stopLogging(); //Stop logging data
-        leg.resumePrint(); //Let the Bench class print
+    }
+
+    // Logging stopped as experiment is fully completed
+    leg.pc.printf("\r\nExit D");
+    leg.setValve(false); //Depressurise
+    leg.StopLogging(); //Stop logging data
+    leg.resumePrint(); //Let the Bench class print
     }
 }
 
+
+void runBenchmarkExperiment0()
+{
+    //Pressurise
+    leg.StartLogging();
+    while(leg.isLogging()) {
+        leg.setValve(true);
+        leg.pc.printf("\r\nPressure (kPa): \t%7.2f",leg.getPressure0()*100);
+        leg.LogData();
+        wait(0.1);
+    }
+    //Depressurise
+    leg.pc.printf("\r\nExit Benchmarking Test");
+    leg.setValve(false);
+    leg.StopLogging(); //Stop logging data
+    leg.resumePrint(); //Let the Bench class print
+}
+
+/**
+ * A performance test with the linear actuator
+ * @param pwm: actuator speed
+ * @param cycles: the number of up and down cycles of the leg
+ * @param logHz: datalogging frequency
+ */
+void runBenchmarkExperiment1(int pwm, int cycles, int logHz)
+{
+    int minForce = 5; //N force at which cycle goes from extension to flexion
+    int maxAngle = 70; //deg angle at which cycle goes from flexion to extension
+    double logTime = 1/logHz;//s interval between logging data
+
+    //Stop the Bench class from printing, so this method can print
+    leg.pausePrint();
+
+    //Pressurise
+    leg.setValve(true);
+    Timer t;
+    t.reset();
+    t.start();
+    while(leg.isLogging() && t.read() < 3) {
+        leg.pc.printf("\r\nPressure (kPa): \t%7.2f",leg.getPressure0()*100);
+        wait(0.2);
+    }
+
+    leg.StartLogging();
+    // in loop
+    for (int i=0; i<cycles; i++) {
+        //allow leg to go up until force is below threshold
+        leg.setPWM(pwm);
+        leg.setDir(0);
+        leg.pc.printf("\r\nExtending: \t%i",cycles);
+        while((leg.getForce() >= minForce) && leg.isLogging()) {
+            leg.pc.printf("\r\nForce (N): \t%7.2f",leg.getForce());
+            leg.LogData();
+            wait(logTime);
+        }
+        if (!leg.isLogging()) {
+            leg.pc.printf("\r\nExit A");
+            //Logging stopped
+            leg.setPWM(0); 
+            leg.setValve(false);//Depressurise
+            leg.StopLogging(); //Stop logging data
+            leg.resumePrint(); //Let the Bench class print
+            return;
+        }
+
+        //allow leg to go down until angle is greater than threshold
+        leg.setPWM(pwm);
+        leg.setDir(1);
+        leg.pc.printf("\r\nFlexing: \t%i",cycles);
+        while((leg.getDegrees(0) <= maxAngle) && leg.isLogging()) {
+            leg.pc.printf("\r\nAngle (deg): \t%7.2f",leg.getDegrees(0));
+            leg.LogData();
+            wait(logTime);
+        }
+        if (!leg.isLogging()) {
+            leg.pc.printf("\r\nExit B");
+            //Logging stopped
+            leg.setPWM(0); 
+            leg.setValve(false);//Depressurise
+            leg.StopLogging(); //Stop logging data
+            leg.resumePrint(); //Let the Bench class print
+            return;
+        }
+    }
+    //Depressurise
+    leg.pc.printf("\r\nExit Benchmarking Test");
+    leg.setPWM(0);
+    leg.setValve(false);
+    leg.StopLogging(); //Stop logging data
+    leg.resumePrint(); //Let the Bench class print
+}
+
+void runFailureExp0(float targetkPa) 
+{
+    
+     //Pressurise
+    leg.StartLogging();
+    while(leg.isLogging()) {
+        leg.setValve(true);
+        leg.pc.printf("\r\nPressure (kPa): \t%7.2f",leg.getPressure0()*100);
+        leg.LogData();
+        wait(0.1);
+    }
+    //Depressurise
+    leg.pc.printf("\r\nExit Benchmarking Test");
+    leg.setValve(false);
+    leg.StopLogging(); //Stop logging data
+    leg.resumePrint(); //Let the Bench class print
+}
+
+    
 void testDAC() {
     //setup SPI to write 8 bit words, mode 1 and turn select lines high
     spi.format(8,1);