[chronojump/michrolab: 33/57] Unified the function to capture with loadCell and encoder




commit 48f2a0f7aac3eea3271657023e8ba75e07a6947a
Author: Xavier Padullés <testing chronojump org>
Date:   Sat May 14 21:56:48 2022 +0200

    Unified the function to capture with loadCell and encoder

 arduino/ForceSensorTFT/ForceSensorTFT.ino | 260 +++++++-----------------------
 1 file changed, 61 insertions(+), 199 deletions(-)
---
diff --git a/arduino/ForceSensorTFT/ForceSensorTFT.ino b/arduino/ForceSensorTFT/ForceSensorTFT.ino
index b521cce3b..2bc64838e 100644
--- a/arduino/ForceSensorTFT/ForceSensorTFT.ino
+++ b/arduino/ForceSensorTFT/ForceSensorTFT.ino
@@ -64,6 +64,16 @@ float scaled_data = 0;
 //Wether the sensor has to capture or not
 boolean capturing = false;
 
+//Variables to capture
+enum sensorType {
+  loadCell,
+  incEncoder,
+  loadCellIncEncoder,
+  none
+};
+
+sensorType sensor = none;
+
 ////Wether the sync time must be sent or not
 //bool sendSyncTime = false;
 
@@ -220,6 +230,8 @@ ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MIS
 #define BLACK     0x0000
 #define CJCOLOR   0X1109
 
+//Don't plot all the samples. values plotted every plotPeriod samples
+int plotPeriod = 10;
 boolean startOver = true;
 double ox , oy ;
 double x, y;
@@ -227,7 +239,6 @@ double x, y;
 void setup() {
   pinMode(redButtonPin, INPUT_PULLUP);
   pinMode(blueButtonPin, INPUT_PULLUP);
-  //  lcd.begin(16, 2);
 
   Serial.begin(256000);
 
@@ -282,7 +293,7 @@ void loop()
       if (menu == 0)
       {
         PCControlled = false;
-        start_capture();
+        startLoadCellCapture();
       } else if (menu == 1)
       {
         Serial.println("Velocity");
@@ -290,11 +301,11 @@ void loop()
       } else if (menu == 2)
       {
         tareTemp();
-        start_capture();
+        startLoadCellCapture();
       } else if (menu == 3)
       {
         start_steadiness();
-        start_capture();
+        startLoadCellCapture();
 
       } else if (menu == 4)
       {
@@ -331,152 +342,7 @@ void showMenu(void)
   tft.print(menuDescription[menu]);
 }
 
-void capture(void)
-{
-  //Position graph's lower left corner.
-  double graphX = 30;
-  double graphY = 200;
-
-  //Size of the graph
-  double graphW = 290;
-  double graphH = 200;
-
-  //Minimum and maximum values to show
-  double xMin = 0;
-  double xMax = 290;
-
-  //Size an num of divisions
-  double yDivSize = 100;
-  double yDivN = 10;
-  double xDivSize = 100;
-  double yBuffer[320];
-
-  int plotPeriod = 1;
-  double plotBuffer[plotPeriod];
-
-  bool resized = true;
-
-  long lastUpdateTime = 0;
-
-  tft.fillScreen(BLACK);
-
-  double xGraph = 1;
-
-  //Print summary results
-  tft.setTextSize(2);
-  tft.setCursor(10, 215);
-  tft.print("Fmax: ");
-  printTftFormat(measuredMax, 100, 215, 2, 2);
-  tft.setCursor(148, 215);
-  tft.print(" N");
-  tft.setCursor(308, 215);
-  tft.print("s");
-
-  while (capturing)
-  {
-    //Deleting the previous plotted points
-    for (int i = xMin; i < xGraph; i++)
-    {
-      Graph(tft, i, yBuffer[i], graphX, graphY, graphW, graphH, xMin, xMax, xDivSize, graphMin, graphMax, 
yDivSize, "", "", "", WHITE, WHITE, BLACK, WHITE, BLACK, startOver);
-    }
-    startOver = true;
-    redrawAxes(tft, graphX, graphY, graphW, graphH, xMin, xMax, graphMin, graphMax, yDivSize, "", "", "", 
WHITE, BLACK, BLACK, BLACK, BLACK, resized);
-    graphMax = newGraphMax;
-    graphMin = newGraphMin;
-    yDivSize = (graphMax - graphMin) / yDivN;
-    if (resized) {
-      Graph(tft, xMin, yBuffer[(int)xMin], graphX, graphY, graphW, graphH, xMin, xMax, xDivSize, graphMin, 
graphMax, yDivSize, "", "", "", WHITE, WHITE, BLACK, WHITE, BLACK, startOver);
-      for (int i = xMin; i < xGraph; i++)
-      {
-        Graph(tft, i, yBuffer[i], graphX, graphY, graphW, graphH, xMin, xMax, xDivSize, graphMin, graphMax, 
yDivSize, "", "", "", WHITE, WHITE, BLUE, WHITE, BLACK, startOver);
-      }
-    }
-    redrawAxes(tft, graphX, graphY, graphW, graphH, xMin, xMax, graphMin, graphMax, yDivSize, "", "", "", 
WHITE, GREY, WHITE, WHITE, BLACK, resized);
-    resized = false;
-
-    if (xGraph >= xMax) xGraph = 0;
-    while ((xGraph < xMax && !resized) && capturing) {
-      for (int n = 0; n < plotPeriod; n++)
-      {
-        //Checking the RCA state
-        if (rcaState != lastRcaState) {       //Event generated by the RCA
-          Serial.print(rcaTime);
-          Serial.print(";");
-
-          if (rcaState) {
-            Serial.println("R");
-            forceTrigger = measured;
-          } else {
-            Serial.println("r");
-          }
-          lastRcaState = rcaState;
-
-          //If no RCA event, read the force as usual
-        } else {
-          //Calculation of the variables shown in the results
-          getForceResults();
-
-          //Force exceeds the plotting area
-          if (measured > newGraphMax) {
-            newGraphMax = measured + (graphMax - graphMin) * 0.5;
-            resized = true;
-          }
-          if (measured < newGraphMin) {
-            newGraphMin = measured - (graphMax - graphMin) * 0.5;
-            resized = true;
-          }
-        }
-        Serial.print(totalTime); Serial.print(";");
-        Serial.println(measured, 2); //scale.get_units() returns a float
-        plotBuffer[n] = measured;
-      }
-
-      //Check the buttons state
-      redButton.update();
-      blueButton.update();
-      //Pressing blue or red button ends the capture
-      if (redButton.fallingEdge() || blueButton.fallingEdge()) {
-        //Not in any steadiness phase
-        if (! (capturingPreSteadiness || capturingSteadiness))
-        {
-          end_capture();
-          xGraph = xMax;
-        } else if (capturingPreSteadiness)  //In Pre steadiness. Showing force until button pressed
-        {
-          capturingPreSteadiness = false;
-          capturingSteadiness = true;
-          start_capture();
-        }
-      }
-
-      if (capturing)
-      {
-        yBuffer[(int)xGraph] = 0;
-
-        for (int i = 0; i < plotPeriod; i++)
-        {
-          yBuffer[(int)xGraph] = yBuffer[(int)xGraph] + plotBuffer[i];
-        }
-
-        yBuffer[(int)xGraph] = yBuffer[(int)xGraph] / plotPeriod;
-        Graph(tft, xGraph, yBuffer[(int)xGraph], graphX, graphY, graphW, graphH, xMin, xMax, xDivSize, 
graphMin, graphMax, yDivSize, "", "", "", WHITE, WHITE, BLUE, WHITE, BLACK, startOver);
-        xGraph++;
-        if (measured > measuredMax)
-        {
-          measuredMax = measured;
-          printTftFormat(measuredMax, 100, 215, 2, 2);
-        }
-
-        if ((lastUpdateTime - totalTime) > 1000000) {
-          lastUpdateTime = totalTime;
-          updateTime();
-        }
-      }
-    }
-  }
-}
-
-void getForceResults(void)
+void getLoadCellDynamics(void)
 {
   measured = scale.get_units();
 
@@ -515,7 +381,7 @@ void getForceResults(void)
     if (samplesSSD >= 5 * (freq - 1))
     {
       end_steadiness();
-      end_capture();
+      endLoadCellCapture();
     }
   }
 
@@ -539,9 +405,6 @@ void getForceResults(void)
              ((totalTime - totalTimes1s[(currentTSlot + samples200ms - samples100ms) % samples200ms]) / 
1e6); //Increment of time
     if (RFD100 > maxRFD100) maxRFD100 = RFD100;
   }
-  //  if (abs(measured) > abs(measuredLcdDelayMax)) {
-  //    measuredLcdDelayMax = measured;
-  //  }
 }
 
 void printTftFormat (float val, int xStart, int y, int fontSize, int decimal) {
@@ -579,10 +442,10 @@ void serialEvent() {
 
   if (commandString == "start_capture") {
     PCControlled = true;
-    start_capture();
+    startLoadCellCapture();
     //capture();
   } else if (commandString == "end_capture") {
-    end_capture();
+    endLoadCellCapture();
   } else if (commandString == "get_version") {
     get_version();
   } else if (commandString == "get_calibration_factor") {
@@ -612,7 +475,7 @@ void serialEvent() {
 
 }
 
-void start_capture()
+void startLoadCellCapture()
 {
   Serial.println("Starting capture...");
   totalTime = 0;
@@ -638,13 +501,15 @@ void start_capture()
   sumSSD = 0.0;
   sumMeasures = lastMeasure;
   samplesSSD = 0;
-  //lcd.clear();
   capturing = true;
+  sensor = loadCell;
+  plotPeriod = 5;
 }
 
-void end_capture()
+void endLoadCellCapture()
 {
   capturing = false;
+  sensor = none;
   Serial.println("Capture ended:");
 
   //If the device is controlled by the PC the results menu is not showed
@@ -943,26 +808,10 @@ void calibrateTFT(void) {
   showMenu();
 }
 
-/*
-   ¡¡¡¡ Atention !!!!
-   Starting MsTimer2 causes instability in teensy or TFT.
-   Don't update battery state using the MsTimer2
-*/
+//function to read battery level. Not implemented in SportAnalyzer hardware version 1.0
 void showBatteryLevel() {
   float sensorValue = analogRead(A0);
-  if (sensorValue >= 788) {
-    //    lcd.createChar(0, battery5);
-  } else if (sensorValue < 788 && sensorValue >= 759) {
-    //    lcd.createChar(0, battery4);
-  } else if (sensorValue < 759 && sensorValue >= 730) {
-    //    lcd.createChar(0, battery3);
-  } else if (sensorValue < 730 && sensorValue >= 701) {
-    //    lcd.createChar(0, battery2);
-  } else if (sensorValue < 701 && sensorValue >= 672) {
-    //    lcd.createChar(0, battery1);
-  } else if (sensorValue <= 701) {
-    //    lcd.createChar(0, battery0);
-  }
+
 }
 
 void updateTime() {
@@ -1142,7 +991,6 @@ void start_steadiness()
   totalTime = 0;
   lastTime = micros();
 
-  //  lcd.clear();
   capturing = true;
   capturingPreSteadiness = true;
   capturingSteadiness = false;
@@ -1278,8 +1126,9 @@ void drawMenuBackground() {
   tft.setCursor(30, 20);
 }
 
-void encoderCapture()
-{ //Position graph's lower left corner.
+void capture()
+{
+  //Position graph's lower left corner.
   double graphX = 30;
   double graphY = 200;
 
@@ -1297,7 +1146,6 @@ void encoderCapture()
   double xDivSize = 100;
   double yBuffer[320];
 
-  int plotPeriod = 1000;
   double plotBuffer[plotPeriod];
 
   bool resized = true;
@@ -1357,12 +1205,18 @@ void encoderCapture()
           }
           lastRcaState = rcaState;
 
-          //If no RCA event, read the force as usual
+          //If no RCA event, read the sensor as usual
         } else {
           //Calculation of the variables shown in the results
-          getEncoderResults();
+          if (sensor == incEncoder)
+          {
+            geEncoderDynamics();
+          } else if (sensor == loadCell)
+          {
+            getLoadCellDynamics();
+          }
 
-          //Force exceeds the plotting area
+          //Value exceeds the plotting area
           if (measured > newGraphMax) {
             newGraphMax = measured + (graphMax - graphMin) * 0.5;
             resized = true;
@@ -1372,23 +1226,28 @@ void encoderCapture()
             resized = true;
           }
         }
-        //                Serial.print(totalTime); Serial.print(";");
-        //                Serial.println(measured, 2); //scale.get_units() returns a float
+        Serial.print(totalTime); Serial.print(";");
+        Serial.println(measured, 2); //scale.get_units() returns a float
         plotBuffer[n] = measured;
+        
+        //Pressing blue or red button ends the capture
+        //Check the buttons state
+        redButton.update();
+        blueButton.update();
+        if (redButton.fallingEdge() || blueButton.fallingEdge()) {
+          Serial.println("Button pressed");
+            if (sensor == incEncoder)
+            {
+              endEncoderCapture();
+            } else if (sensor == loadCell)
+            {
+              endLoadCellCapture();
+            }
+          xGraph = xMax;
+        }
       }
       //      Serial.println("Ended plotPeriod");
 
-      //Check the buttons state
-      //delay(10);
-      redButton.update();
-      blueButton.update();
-      //Pressing blue or red button ends the capture
-      if (redButton.fallingEdge() || blueButton.fallingEdge()) {
-        Serial.println("Button pressed");
-        endEncoderCapture();
-        xGraph = xMax;
-      }
-
       if (capturing)
       {
         yBuffer[(int)xGraph] = 0;
@@ -1416,7 +1275,7 @@ void encoderCapture()
   }
 }
 
-void getEncoderResults()
+void geEncoderDynamics()
 {
   int sampleDuration = totalTime - lastEncoderTime;
   if (sampleDuration >= 1000)
@@ -1431,12 +1290,15 @@ void getEncoderResults()
 void startEncoderCapture()
 {
   capturing = true;
-  encoderCapture();
+  sensor = incEncoder;
+  plotPeriod = 500;
+  capture();
 }
 
 void endEncoderCapture()
 {
   capturing = false;
+  sensor = none;
   //If the device is controlled by the PC the results menu is not showed
   //because during the menu navigation the Serial is not listened.
   if (!PCControlled) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]