From ec5915a6f9843d0a98cf6ab176fe3df52fd39d36 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Sun, 21 Jan 2024 22:32:28 -0500 Subject: [PATCH] Start calculating based on pulses, not on incrementing outside the interrupt --- ino/motor_encoder_poc/motor_encoder_poc.ino | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ino/motor_encoder_poc/motor_encoder_poc.ino b/ino/motor_encoder_poc/motor_encoder_poc.ino index 9a23c3b..5bedc04 100644 --- a/ino/motor_encoder_poc/motor_encoder_poc.ino +++ b/ino/motor_encoder_poc/motor_encoder_poc.ino @@ -31,21 +31,23 @@ const int maxRotations = 10; const int ppr = 11; const float ratio = 62.0 / 1.0; const int maxPulses = (int) round((float) ppr * ratio); -const int speed = 230; +const int speed = 220; const int framesPerRotation = 18; const float pulsesPerFrame = (float) maxPulses / (float) framesPerRotation; volatile long start = 0; +volatile long total; volatile bool done = false; volatile bool stop = false; volatile int incoming; volatile float rpm; +volatile float fps; volatile int rotations = 0; volatile int lastRotationPosition = 0; volatile int lastFramePosition = 0; volatile int frames = 0; -float calculateFPS () { - +float calculateFPS (long timeLength, int frames) { + return (float) frames / (float) timeLength; } float calculateRPM (long rotationLength) { return 60000.0 / (float) (rotationLength); @@ -114,10 +116,14 @@ void loop() { //Serial.print("Final: "); //Serial.println(pos); Serial.print("Time: "); - Serial.println(millis() - start); - rpm = calculateRPM(millis() - start) * (float) (rotations + 1); + total = millis() - start; + Serial.println(total); + rpm = calculateRPM(total) * (float) (rotations + 1); + fps = calculateFPS(total, 18); Serial.print("RPM: "); Serial.println(rpm); + Serial.print("FPS: "); + Serial.println(fps); Serial.print("Rotations: "); Serial.println(rotations + 1); Serial.print("Frames: ");