[chronojump] Races encoder: add possibility to config precission



commit 5803d55a6bc168a2a8c4e9107df04c178b8244d1
Author: Xavier Padullés <x padulles gmail com>
Date:   Thu Apr 12 13:55:34 2018 +0200

    Races encoder: add possibility to config precission

 arduino/raceEncoder/raceEncoder.ino |  152 +++++++++++++++++++++--------------
 1 files changed, 93 insertions(+), 59 deletions(-)
---
diff --git a/arduino/raceEncoder/raceEncoder.ino b/arduino/raceEncoder/raceEncoder.ino
index b0228b9..dba139d 100644
--- a/arduino/raceEncoder/raceEncoder.ino
+++ b/arduino/raceEncoder/raceEncoder.ino
@@ -1,17 +1,29 @@
+#include <EEPROM.h>
 
-int val;
 int encoderPinA = 2;
 int encoderPinB = 3;
 volatile int encoderDisplacement = 0;
-volatile unsigned long changingTime;
-unsigned long elapsedTime;
-unsigned long lastTime;
+volatile unsigned long changingTime = 0;
+unsigned long elapsedTime = 0;
+unsigned long totalTime = 0;
+unsigned long lastTime = 0;
+
+//Version of the firmware
+String version = "Race_Encoder-0.1";
+
+int pps; //Pulses Per Sample. How many pulses are needed to get a sample
+int ppsAddress = 0; //Where is stored the pps value in the EEPROM
+
+//Wether the sensor has to capture or not
+boolean capturing = false;
 
 void setup() {
   pinMode (encoderPinA, INPUT);
   pinMode (encoderPinB, INPUT);
   Serial.begin (115200);
 
+  EEPROM.get(ppsAddress, pps);
+
   //Using the rising flank of the A photocell we have a 200 PPR.
   attachInterrupt(digitalPinToInterrupt(encoderPinA), changingA, RISING);
 
@@ -20,28 +32,31 @@ void setup() {
 }
 
 void loop() {
-  //With a diameter is of 160mm, each pulse is 2.513274mm. 4 pulses equals 1.00531cm
-  if ( abs(encoderDisplacement) >= 4 ) {
-
-    int lastEncoderDisplacement = encoderDisplacement; //Assigned to another variable for in the case that 
encoder displacement changes before printing it
-    unsigned long Time = changingTime;
-    encoderDisplacement = 0;
-
-    //Managing the timer overflow
-    if (Time > lastTime)      //No overflow
-    {
-      elapsedTime = Time - lastTime;
-    } else  if (Time <= lastTime)  //Overflow
-    {
-      elapsedTime = (4294967295 - lastTime) + Time; //Time from the last measure to the overflow event plus 
the changingTime
-    }
+  if (capturing)
+  {
+    //With a diameter is of 160mm, each pulse is 2.513274mm. 4 pulses equals 1.00531cm
+    if ( abs(encoderDisplacement) >= pps ) {
+
+      int lastEncoderDisplacement = encoderDisplacement; //Assigned to another variable for in the case that 
encoder displacement changes before printing it
+      unsigned long Time = changingTime;
+      encoderDisplacement = 0;
 
-    Serial.print(lastEncoderDisplacement);
-    Serial.print(";");
-    Serial.println(elapsedTime);
-    lastTime = changingTime;
+      //Managing the timer overflow
+      if (Time > lastTime)      //No overflow
+      {
+        elapsedTime = Time - lastTime;
+      } else  if (Time <= lastTime)  //Overflow
+      {
+        elapsedTime = (4294967295 - lastTime) + Time; //Time from the last measure to the overflow event 
plus the changingTime
+      }
+      totalTime += elapsedTime;
+      Serial.print(lastEncoderDisplacement);
+      Serial.print(";");
+      Serial.println(totalTime);
+      lastTime = Time;
+    }
+    delayMicroseconds(100);
   }
-  delayMicroseconds(100);
 }
 
 void changingA() {
@@ -53,43 +68,62 @@ void changingA() {
   }
 }
 
-/***********************************************************************
-   If we need more precission we can use both flanks of both photocells
- * *********************************************************************
-
-  void changingA() {
-  changingTime = micros();
-  if (digitalRead(encoderPinA) == HIGH) {
-    if (digitalRead(encoderPinB) == HIGH) {
-      encoderDisplacement--;
-    } else {
-      encoderDisplacement++;
-    }
+void serialEvent()
+{
+  String inputString = Serial.readString();
+  String commandString = inputString.substring(0, inputString.lastIndexOf(":"));
+  if (commandString == "start_capture") {
+    start_capture();
+  } else if (commandString == "end_capture") {
+    end_capture();
+  } else if (commandString == "get_version") {
+    get_version();
+  } else if (commandString == "set_pps") {
+    set_pps(inputString);
+  } else if (commandString == "get_pps") {
+    get_pps();
   } else {
-    if (digitalRead(encoderPinB) == HIGH) {
-      encoderDisplacement++;
-    } else {
-      encoderDisplacement--;
-    }
-  }
+    Serial.println("Not a valid command");
   }
+  inputString = "";
+}
 
-  void changingB() {
-  changingTime = micros();
-  if (digitalRead(encoderPinB) == HIGH) {
-    if (digitalRead(encoderPinA) == HIGH) {
-      encoderDisplacement++;
-    } else {
-      encoderDisplacement--;
-    }
-  } else {
-    if (digitalRead(encoderPinA) == HIGH) {
-      encoderDisplacement--;
-    } else {
-      encoderDisplacement++;
-    }
-  }
-  }
+void start_capture()
+{
+  Serial.println("Starting capture...");
+  totalTime = 0;
+  lastTime = micros();
+  capturing = true;
+}
+
+void end_capture()
+{
+  capturing = false;
+  Serial.println("Capture ended");
+}
 
-*/
+void get_version()
+{
+  Serial.println(version);
+}
+
+//Setting how many pulses are needed to get a sample
+void set_pps(String inputString)
+{
+  String argument = get_command_argument(inputString);
+  pps = argument.toInt();
+  EEPROM.put(ppsAddress, pps);
+  Serial.print("pps set to: ");
+  Serial.println(pps);
+}
+
+void get_pps()
+{
+  Serial.println(pps);
+}
+
+String get_command_argument(String inputString)
+{
+  return(inputString.substring(inputString.lastIndexOf(":") + 1, inputString.lastIndexOf(";")));
+}
 


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