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:
9:60760807834f
--- a/bench.cpp	Mon Feb 17 15:15:24 2020 +0000
+++ b/bench.cpp	Wed Aug 12 12:05:58 2020 +0000
@@ -11,23 +11,20 @@
  * @param p_spte0: analog input pin for pressure sensor 0
  * @param p_spte1: analog input pin for pressure sensor 1
  * @param p_valve: digital output for valve (on/off) that inflates leg actuator
+ * @param p_laDir: digital output for linear actuator direction (retract/extend)
+ * @param p_laPwm: pwm output for linear actuator speed
  * @param mosi: mosi pin for sd card
  * @param miso: miso pin for sd card
  * @param sck: clock pin for sd card
  * @param cs: chip select pin for sd card
  * @param tx: serial transmission line
  * @param rx: serial receive line
- * @param but0: first input button 
- * @param but1: second input button
  */
 Bench::Bench(PinName mosi, PinName miso, PinName sck, PinName cs,
-             bool use5kNLoadCell,PinName p_lcm101, PinName p_spte0, PinName p_spte1, PinName p_valve,
+             bool use5kNLoadCell,PinName p_lcm101, PinName p_spte0, PinName p_spte1, PinName p_valve,PinName p_laDir, PinName p_laPwm,
              PinName sd_mosi, PinName sd_miso, PinName sd_sck, PinName sd_cs,
-             PinName tx, PinName rx,
-             PinName but0, PinName but1) :
+             PinName tx, PinName rx) :
     pc(tx,rx),
-    lowerBut(but0,PullUp),
-    upperBut(but1,PullUp),
     as5048_(mosi, miso, sck, cs, sensors::kNumJoints),
     loadCell5kN(p_lcm101, sensors::kGen5kNOffset, sensors::kGen5kNFactor),
     loadCell1kN(p_lcm101, sensors::kLcm101Offset, sensors::kLcm101Factor),
@@ -35,6 +32,7 @@
     spte0(p_spte0, sensors::kSPTE0Offset, sensors::kSPTE0Factor),
     spte1(p_spte1, sensors::kSPTE1Offset, sensors::kSPTE1Factor),
     valveFesto(p_valve),
+    LinAct(p_laDir,p_laPwm),
     sd(sd_mosi, sd_miso, sd_sck, sd_cs, "sd")
 {    
     //init serial things
@@ -51,6 +49,9 @@
     firstReadMS = 0; //first timer value read
     startedLogging = false; //in the middle of a logging cycle
     
+    keyIntOffset = timing::minKeyInt;//any right keypress is ok at the start
+    k = '\0'; //keyboard value starts off as a null value
+    
     //set data logging frequency
     loggingUS = timing::kTimeLogDataUs;    
     loggingHz = (int)(1000000/timing::kTimeLogDataUs); 
@@ -74,12 +75,8 @@
     // set rate at which data is printed
     tick_serial.attach_us(this,&Bench::PrintStatus,timing::kTimeSerialPrintUs);
      
-    //setup the buttons with debouncing    
-    lowerBut.attach_asserted(this,&Bench::TogglePrinting);
-    upperBut.attach_asserted(this,&Bench::ToggleLogging);
-    
-    lowerBut.setSampleFrequency();
-    upperBut.setSampleFrequency();
+    //setup the timer to read key presses
+    Bench::pc.attach(this,&Bench::ToggleState,MODSERIAL::RxIrq);
     
     //display welcome message
     pc.printf("\r\n**Hello!**\r\n");
@@ -300,6 +297,32 @@
     return valveFesto.getValve();   
 }
 
+/**
+ * The linear actuator state (true is retracting and false is extending)
+ * @return: linear actuator state
+ */
+bool Bench::getDir() {
+    return LinAct.getDirection();   
+}
+
+/**
+ * Control the linear actuator direction (true retracts and false extends)
+ * @param set: direction
+ */
+void Bench::setDir(bool dir)
+{
+    LinAct.setDirection(dir);
+}
+
+/**
+ * Control the linear actuator speed (0-100% integer of pwm. generally need 
+ * above 50% to get it moving)
+ * @param pwm: pwm duty cycle percentage
+ */
+void Bench::setPWM(int pwm)
+{
+    LinAct.setPWM(pwm);
+}
 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 // IMPLEMENTATION DATA LOGGING
 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
@@ -375,11 +398,11 @@
             }
             //pc.printf("%s", fHeader.c_str());
             fprintf(fp_data, "%s", fHeader.c_str());
-            tick_logging.attach_us(this,&Bench::LogData,loggingUS);
+            //tick_logging.attach_us(this,&Bench::LogData,loggingUS);
             timer.start();
             startedLogging = true;
             
-            pc.printf("\t> Logging started.\r\n");
+            //pc.printf("\t> Logging started.\r\n");
             
             is_logging = true;
         }
@@ -389,6 +412,39 @@
     }
 }
 
+/**
+ * Restart logging data (opens data logging file)
+ * @param fname_append: a string describing the name appended to each logged file
+ * (along with its unique loggging number).
+ */
+void Bench::RestartLogging(const char * fname_append)
+{    
+    if (sd_card_present) {
+ 
+        // file name carries on from what we last had
+        char fname[50];
+        sprintf(fname, "/sd/%d_%s.csv",fname_prepend,fname_append);
+ 
+        pc.printf("\t> Opening data log file '%s'...\r\n",fname);
+ 
+        // open file for writing and start logging after success
+        fp_data = fopen(fname,"w");
+        if (fp_data==NULL) {
+            pc.printf("\t> ERROR: failed to open log file (t=%d ms)\r\n",
+                      timer.read_ms());
+        } else {            
+            timer.start();
+            startedLogging = true;
+            
+            pc.printf("\t> Logging file opened.\r\n");
+            
+            is_logging = true;
+        }
+    } else {
+        pc.printf("\t> No SD Card; no data will be logged.\r\n");
+        startedLogging = false;
+    }
+}
 
 /**
  * Stop logging data.
@@ -481,8 +537,8 @@
 void Bench::PrintMenu()
 {
     Bench::pc.printf("\r\nMENU\r\n");
-    Bench::pc.printf("\t> Press SW2 to toggle printing leg status\r\n");
-    Bench::pc.printf("\t> Press SW3 to toggle data logging\r\n");
+    Bench::pc.printf("\t> Press s to toggle printing leg status\r\n");
+    Bench::pc.printf("\t> Press l to toggle data logging\r\n");
     
     Bench::pc.printf("\tSD card detected? %9s\r\n",sd_card_present?"Yes":"No");    
     if(sd_card_present)
@@ -514,7 +570,37 @@
 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 
 /**
- * SW2 toggles printing of data
+ * d toggles printing state of sensors and l toggles logging of data
+ * use the keyboard on the pc
+ * do not press longer than a second or it will toggle back
+ */
+void Bench::ToggleState(MODSERIAL_IRQ_INFO *q)
+{
+    //if we have data and it was not too soon since the last data
+    if(Bench::pc.readable() && (((Bench::keyIntT.read() + keyIntOffset) >= timing::minKeyInt))) {
+    k = Bench::pc.getc();//read the keystroke
+        Bench::pc.rxBufferFlush();
+        //toggle status of logging
+        if(k == 's') {//toggle printing of data
+            Bench::pc.printf("\tInput: %c\r\n",k);
+            TogglePrinting();
+            keyIntT.reset();
+            keyIntT.start();
+            keyIntOffset = 0;
+
+        } else if (k == 'l') {
+            Bench::pc.printf("\tInput: %c\r\n",k);
+            ToggleLogging();
+            keyIntT.reset();
+            keyIntT.start();
+            keyIntOffset = 0;
+        }
+        k = '\0';
+    }
+}
+
+/**
+ * toggles printing of data
  */
 void Bench::TogglePrinting()
 {
@@ -528,17 +614,16 @@
 }
 
 /*
- * SW3 toggles logging of data
+ * toggles logging of data
  */
 void Bench::ToggleLogging()
 {
     if (not is_logging) {
-        StartLogging();
+        is_logging = true;
     } else {
         is_logging = false;
-        StopLogging();
     }        
-    PrintMenu();
+    //PrintMenu();
 }
 
 /**