Sketch has been updated to do monitoring of multiple rotations, active monitoring of number of estimated frames. Currently not registering the first ~910 pulses (why??)

This commit is contained in:
Matt McWilliams 2024-01-13 23:29:25 -05:00
parent 82fbd4af33
commit d1302e79fd
1 changed files with 55 additions and 18 deletions

View File

@ -25,17 +25,31 @@
#define MOTORA 10
#define MOTORB 11
volatile int posi = 0; // specify posi as volatile
const long maxTime = 10000;
volatile int64_t posi = 0; // specify posi as volatile
const long maxTime = 100000;
const int maxRotations = 10;
const int ppr = 11;
const float ratio = 62.0;
const float ratio = 62.0 / 1.0;
const int maxPulses = (int) round((float) ppr * ratio);
const int speed = 40;
const int speed = 230;
const int framesPerRotation = 18;
const int pulsesPerFrame = (int) round((float) maxPulses / (float) framesPerRotation);
volatile long start = 0;
volatile bool done = false;
volatile bool stop = false;
volatile int incoming;
volatile float rpm;
volatile int rotations = 0;
volatile int lastRotationPosition = 0;
volatile int lastFramePosition = 0;
volatile int frames = 0;
float calculateFPS () {
}
float calculateRPM (long rotationLength) {
return 60000.0 / (float) (rotationLength);
}
void setup() {
Serial.begin(57600);
@ -54,18 +68,14 @@ void setup() {
Serial.println(ratio);
Serial.print("Pulses: ");
Serial.println(maxPulses);
Serial.print("Frames per Rotation: ");
Serial.println(framesPerRotation);
Serial.print("Pulses per Frame: ");
Serial.println(pulsesPerFrame);
}
void loop() {
int pos;
if (Serial.available() > 0) {
incoming = Serial.read();
start = -1;
posi = 0;
done = false;
stop = false;
}
int64_t pos;
// Read the position in an atomic block to avoid a potential
// misread if the interrupt coincides with this code running
// see: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/
@ -74,7 +84,22 @@ void loop() {
pos = posi;
}
if (abs(pos) == maxPulses) {
if (pos != lastRotationPosition && abs(pos) % maxPulses == 0) {
lastRotationPosition = pos;
rotations++;
frames = rotations * framesPerRotation;
}
if (pos != lastFramePosition && abs(pos) % pulsesPerFrame == 0) {
lastFramePosition = pos;
frames++;
Serial.print("Frames: ");
Serial.print(frames);
Serial.print("@");
Serial.println(String(pos));
}
if (abs(pos) >= maxPulses * maxRotations) {
stop = true;
}
if (start == -1) {
@ -82,6 +107,7 @@ void loop() {
analogWrite(MOTORA, speed);
digitalWrite(MOTORB, LOW);
start = millis();
rotations = 0;
} else if ((stop || millis() - start >= maxTime) && !done) {
digitalWrite(MOTORA, LOW);
digitalWrite(MOTORB, LOW);
@ -89,11 +115,22 @@ void loop() {
Serial.println(pos);
Serial.print("Time: ");
Serial.println(millis() - start);
rpm = (float) 60000 / (float) (millis() - start);
rpm = calculateRPM(millis() - start) * (float) (rotations + 1);
Serial.print("RPM: ");
Serial.println(rpm);
Serial.print("Rotations: ");
Serial.println(rotations + 1);
Serial.print("Frames: ");
Serial.println(frames);
done = true;
}
if (Serial.available() > 0) {
incoming = Serial.read();
start = -1;
posi = 0;
done = false;
stop = false;
}
}
void readEncoder(){