[chronojump/michrolab: 30/57] Added encoder measure and plotting




commit 77860410c0a03ab47b938cd25baad0a2f5ca1cae
Author: Xavier Padullés <testing chronojump org>
Date:   Fri May 13 20:48:48 2022 +0200

    Added encoder measure and plotting

 arduino/ForceSensorTFT/ForceSensorTFT.ino | 182 ++++++++++++++++++++++++++++--
 1 file changed, 172 insertions(+), 10 deletions(-)
---
diff --git a/arduino/ForceSensorTFT/ForceSensorTFT.ino b/arduino/ForceSensorTFT/ForceSensorTFT.ino
index faafb54eb..37dbb4c8a 100644
--- a/arduino/ForceSensorTFT/ForceSensorTFT.ino
+++ b/arduino/ForceSensorTFT/ForceSensorTFT.ino
@@ -42,6 +42,13 @@
 String version = "0.7";
 
 
+//Encoder variables
+Encoder encoder(8,9);  
+int position = 0;
+int lastPosition = 0;
+
+long lastEncoderTime = 0;
+
 int tareAddress = 0;
 int calibrationAddress = 4;
 
@@ -253,8 +260,7 @@ void setup() {
   //Start TFT
   tft.begin();
   tft.setRotation(1);
-  //tft.fillRect(0, 50, 320, 240, BLACK);
-  tft.fillScreen(BLACK);
+  tft.fillRect(0, 50, 320, 240, BLACK);
   drawMenuBackground();
   showMenu();
 }
@@ -281,11 +287,8 @@ void loop()
         start_capture();
       } else if (menu == 1)
       {
-        //captureVelocity();
-      } else if (menu == 2)
-      {
-        tareTemp();
-        start_capture();
+        Serial.println("Velocity");
+        startEncoderCapture();
       } else if (menu == 2)
       {
         tareTemp();
@@ -297,7 +300,7 @@ void loop()
 
       } else if (menu == 4)
       {
-        //capture(forceVelocity();
+        //captureForceVelocity();
       } else if (menu == 5)
       {
         systemMenu();
@@ -413,7 +416,7 @@ void capture(void)
           //If no RCA event, read the force as usual
         } else {
           //Calculation of the variables shown in the results
-          getResults();
+          getForceResults();
 
           //Force exceeds the plotting area
           if (measured > newGraphMax) {
@@ -475,7 +478,7 @@ void capture(void)
   }
 }
 
-void getResults(void)
+void getForceResults(void)
 {
   measured = scale.get_units();
 
@@ -1276,3 +1279,162 @@ void drawMenuBackground() {
   tft.setTextSize(3);
   tft.setCursor(30, 20);
 }
+
+void encoderCapture()
+{  //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
+          getEncoderResults();
+
+          //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;
+      }
+//      Serial.println("Ended plotPeriod");
+
+      //Check the buttons state
+      redButton.update();
+      blueButton.update();
+        Serial.println("Button 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;
+
+        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 getEncoderResults()
+{
+    position = encoder.read();
+    measured = (float)(position - lastPosition) * 1000000 / (totalTime - lastEncoderTime);
+    lastEncoderTime = totalTime;
+    lastPosition = position;
+    delay(10);
+    redButton.update();
+}
+
+void startEncoderCapture()
+{
+  capturing = true;
+  encoderCapture();
+}
+
+void endEncoderCapture()
+ {
+  capturing = false;
+ }


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