Motor speed is now calculated (flawed). Tests can be run on various PWM speeds, resolutions and with and without loads to determine the ability for the printer run at maximum speeds.

This commit is contained in:
Matt McWilliams 2024-02-21 22:48:08 +01:00
parent d0e0ca2f7a
commit 7c8be86fb8
7 changed files with 117 additions and 39 deletions

View File

@ -1 +1 @@
0.2.1 0.2.2

View File

@ -27,8 +27,8 @@ void ContactPrinter::Setup () {
digitalWrite(takeup_pin_dir_b, LOW); digitalWrite(takeup_pin_dir_b, LOW);
SetDirectionTakeup(true); SetDirectionTakeup(true);
SetSpeedTakeup(0.9); SetSpeedTakeup(1.0);
SetSpeedDrive(0.8); SetSpeedDrive(1.0);
start_time = millis(); start_time = millis();
} }
@ -52,7 +52,9 @@ void ContactPrinter::SetSpeedTakeup(float speed) {
takeup_speed = speed; takeup_speed = speed;
takeup_pwm_duty_cycle = floor(speed * pwm_maximum); takeup_pwm_duty_cycle = floor(speed * pwm_maximum);
Serial.print("Set takeup motors PWM = "); Serial.print("Set takeup motors PWM = ");
Serial.println(takeup_pwm_duty_cycle); Serial.print(takeup_pwm_duty_cycle);
Serial.print(" / ");
Serial.println(pwm_maximum);
} }
void ContactPrinter::StartTakeup () { void ContactPrinter::StartTakeup () {

View File

@ -27,7 +27,7 @@ class ContactPrinter {
const uint32_t pwm_frequency = 5000; const uint32_t pwm_frequency = 5000;
const uint8_t takeup_pwm_channel = 1; const uint8_t takeup_pwm_channel = 1;
const uint8_t pwm_resolution = 8; const uint8_t pwm_resolution = 8;
const uint16_t pwm_maximum = 255; //8 = 255, 16 = 65535 const uint16_t pwm_maximum = 255; //8 = 255, 10 = 1024, 16 = 65535
/* BUTTONS */ /* BUTTONS */
const uint16_t button_delay = 500; const uint16_t button_delay = 500;
@ -82,8 +82,6 @@ class ContactPrinter {
bool IsRunning (); bool IsRunning ();
float CalculateFPS (long timeLength, uint32_t frames);
float CalculateRPM (long rotationLength);
}; };
#endif #endif

View File

@ -24,10 +24,22 @@ void DriveMotor::Setup () {
digitalWrite(forward_pin, LOW); digitalWrite(forward_pin, LOW);
digitalWrite(backward_pin, LOW); digitalWrite(backward_pin, LOW);
//attachInterrupt(digitalPinToInterrupt(encoder_b_pin), ReadEncoder, RISING); attachInterrupt(digitalPinToInterrupt(encoder_b_pin), ReadEncoder, RISING);
} }
void DriveMotor::Start() { void DriveMotor::Start() {
pulses = 0;
fps = 0.0;
fps_max = -1.0;
fps_min = 100000.0;
fps_avg = -1.0;
rpm = 0.0;
rpm_max = -1.0;
rpm_min = 100000.0;
rpm_avg = -1.0;
start_frame = frames;
start_rotation = rotations;
start_time = millis();
ledcWrite(pwm_channel, pwm_duty_cycle); ledcWrite(pwm_channel, pwm_duty_cycle);
digitalWrite(forward_pin, HIGH); digitalWrite(forward_pin, HIGH);
digitalWrite(backward_pin, LOW); digitalWrite(backward_pin, LOW);
@ -37,38 +49,86 @@ void DriveMotor::Stop() {;
digitalWrite(forward_pin, LOW); digitalWrite(forward_pin, LOW);
digitalWrite(backward_pin, LOW); digitalWrite(backward_pin, LOW);
ledcWrite(pwm_channel, 0); ledcWrite(pwm_channel, 0);
fps = CalculateFPS(timer - start_time, frames - start_frame);
rpm = CalculateRPM(timer - start_time, rotations - start_rotation);
Serial.print("RPM ");
Serial.println(rpm);
Serial.print("RPM avg: ");
Serial.println(rpm_avg);
Serial.print("RPM min: ");
Serial.println(rpm_min);
Serial.print("RPM max: ");
Serial.println(rpm_max);
Serial.print("FPS ");
Serial.println(fps);
Serial.print("FPS avg: ");
Serial.println(fps_avg);
Serial.print("FPS min: ");
Serial.println(fps_min);
Serial.print("FPS max: ");
Serial.println(fps_max);
} }
void DriveMotor::SetSpeed(float speed) { void DriveMotor::SetSpeed(float speed) {
pwm_duty_cycle = floor(pwm_maximum * speed); pwm_duty_cycle = floor(pwm_maximum * speed);
Serial.print("Set drive motor PWM = "); Serial.print("Set drive motor PWM = ");
Serial.println(pwm_duty_cycle); Serial.print(pwm_duty_cycle);
Serial.print(" / ");
Serial.println(pwm_maximum);
} }
int64_t DriveMotor::posi = 0; int32_t DriveMotor::pulses = 0;
/*
void DriveMotor::ReadEncoder () { void DriveMotor::ReadEncoder () {
int b = digitalRead(DriveMotor::encoder_b_pin); int b = digitalRead(encoder_b_pin);
if (b > 0) { if (b > 0) {
posi++; pulses++;
} else { } else {
posi--; pulses--;
} }
}*/ }
void DriveMotor::Loop () { void DriveMotor::Loop () {
int64_t pos; timer = millis();
//monitor speed //monitor speed
/*ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { frames = (int32_t) floor((float) pulses / pulses_per_frame);
pos = posi; if (frames != last_frame) {
}*/ last_frame = frames;
fps = CalculateFPS(timer - start_time, frames - start_frame);
if (fps < fps_min) { fps_min = fps; }
if (fps > fps_max) { fps_max = fps; }
if (fps_avg < 0.0) {
fps_avg = fps;
} else {
fps_avg = (fps_avg + fps) / 2.0;
}
Serial.print("Frame: ");
Serial.println(frames);
}
rotations = (int32_t) floor((float) pulses / (float) pulses_per_rotation);
if (rotations != last_rotation) {
last_rotation = rotations;
//correction
frames = rotations * frames_per_rotation;
rpm = CalculateRPM(timer - start_time, rotations - start_rotation);
if (rpm < rpm_min) { rpm_min = rpm; }
if (rpm > rpm_max) { rpm_max = rpm; }
if (rpm_avg < 0.0) {
rpm_avg = rpm;
} else {
rpm_avg = (rpm_avg + rpm) / 2.0;
}
Serial.print("Rotation: ");
Serial.println(rotations);
}
} }
float DriveMotor::CalculateFPS (long timeLength, uint32_t frames) { float DriveMotor::CalculateFPS (long time_length, uint32_t frames) {
return 1000.0 / ((float) timeLength / (float) frames); return 1000.0 / ((float) time_length / (float) frames);
} }
float DriveMotor::CalculateRPM (long rotationLength) { float DriveMotor::CalculateRPM (long time_length, uint32_t rotations) {
return 60000.0 / (float) (rotationLength); return 60000.0 / ((float) time_length / (float) rotations);
} }

View File

@ -8,29 +8,47 @@ class DriveMotor {
private: private:
//defaults are for EPS32 dev board //defaults are for EPS32 dev board
const uint8_t enable_pin = 26; static const uint8_t enable_pin = 26;
const uint8_t forward_pin = 27; //Clockwise static const uint8_t forward_pin = 27; //Clockwise
const uint8_t backward_pin = 14; //Counter-clockwise static const uint8_t backward_pin = 14; //Counter-clockwise
const uint8_t encoder_a_pin = 33; static const uint8_t encoder_a_pin = 33;
const uint8_t encoder_b_pin = 25; static const uint8_t encoder_b_pin = 25;
const uint32_t pwm_frequency = 5000; const uint32_t pwm_frequency = 5000;
const uint8_t pwm_channel = 0; const uint8_t pwm_channel = 0;
const uint8_t pwm_resolution = 8; const uint8_t pwm_resolution = 10;
const uint16_t pwm_maximum = 255; //8 = 255, 16 = 65535 const uint16_t pwm_maximum = 1024; //8 = 255, 10 = 1024, 16 = 65535
const uint8_t ppr = 11; const uint8_t ppr = 11;
const float ratio = 187.0 / 3.0; const float ratio = 187.0 / 3.0;
const uint32_t maxPulses = (int) round((float) ppr * ratio); const uint32_t pulses_per_rotation = (int) round((float) ppr * ratio);
const uint8_t framesPerRotation = 18; const uint8_t frames_per_rotation = 18;
const float pulses_per_frame = (float) pulses_per_rotation / (float) frames_per_rotation;
volatile uint8_t pwm_duty_cycle = 0; volatile uint32_t pwm_duty_cycle = 0;
static int64_t posi; static int32_t pulses;
//measured //state
volatile long timer = 0;
volatile long start_time = 0;
volatile int32_t start_rotation = 0;
volatile int32_t start_frame = 0;
//measure
volatile float rpm = 0.0; volatile float rpm = 0.0;
volatile float rpm_max = -1.0;
volatile float rpm_min = 100000.0;
volatile float rpm_avg = -1.0;
volatile int32_t rotations = 0;
volatile int32_t last_rotation = 0;
volatile float fps = 0.0; volatile float fps = 0.0;
volatile float fps_max = -1.0;
volatile float fps_min = 100000.0;
volatile float fps_avg = -1.0;
volatile int32_t frames = 0;
volatile int32_t last_frame = 0;
//target //target
volatile float target_fps = 0.0; volatile float target_fps = 0.0;
@ -45,8 +63,8 @@ class DriveMotor {
void Stop(); void Stop();
void SetSpeed(float speed); void SetSpeed(float speed);
float CalculateFPS (long timeLength, uint32_t frames); float CalculateFPS (long time_length, uint32_t frames);
float CalculateRPM (long rotationLength); float CalculateRPM (long time_length, uint32_t rotations);
protected: protected:

View File

@ -1,6 +1,6 @@
#include "ContactPrinter.h"; #include "ContactPrinter.h";
#define VERSION "0" #define VERSION "0.2.2"
/** /**
* *

View File

@ -22,7 +22,7 @@ fi
V="${VERSION[0]}.${VERSION[1]}.${VERSION[2]}" V="${VERSION[0]}.${VERSION[1]}.${VERSION[2]}"
#echo "{ \"version\" : \"$V\", \"bin\" : \"/bin/contact_printer.bin\", \"date\" : $DATESTR }" > ./ota.json #echo "{ \"version\" : \"$V\", \"bin\" : \"/bin/contact_printer.bin\", \"date\" : $DATESTR }" > ./ota.json
VERSION_UPDATE=`sed "s/.*define VERSION.*/ #define VERSION \"$VERSION\"/" "${SOURCE_FILE}"` VERSION_UPDATE=`sed "s/.*define VERSION.*/ #define VERSION \"${V}\"/" "${SOURCE_FILE}"`
echo "${VERSION_UPDATE}" > "${SOURCE_FILE}" echo "${VERSION_UPDATE}" > "${SOURCE_FILE}"
echo $V > "${VERSION_FILE}" echo $V > "${VERSION_FILE}"