From cb1b7f91ead91a4ae354a05d6b363c472b512609 Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 8 Jun 2020 03:24:07 -0500 Subject: [PATCH 1/6] Auto detect PWM_MAX_INPUT_US so it's not hardcoded to a single frequency --- Router_PID/Router_PID.ino | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Router_PID/Router_PID.ino b/Router_PID/Router_PID.ino index 94af2df..0f68ce8 100644 --- a/Router_PID/Router_PID.ino +++ b/Router_PID/Router_PID.ino @@ -44,7 +44,7 @@ double Kd=0.025; PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); // PID library const int MAX_TOOL_RPM = 30000; // SETTINGS per "spindle" ?1k headroom needed? -const int MAX_PWM_INPUT_US = 2024; // Settings the microseconds of the max PWM from Marlin. +int MAX_PWM_INPUT_US = 0; // Settings the microseconds of the max PWM from Marlin. void setup() { @@ -131,6 +131,9 @@ void spindleRPM() { // PWM duty cycle. ISR. void rising() { // Capture when this is rising. + if (MAX_PWM_INPUT_US == 0 && prev_time != 0) { + MAX_PWM_INPUT_US = micros()-prev_time; + } prev_time = micros(); // Set the next interrupt. From cc91c95d2aeffe4082674ff8feaec0cee8e1c9e0 Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 8 Jun 2020 03:33:24 -0500 Subject: [PATCH 2/6] Apply a fudge factor to the MAX_INPUT_PWM_US so we can go for "full" duty cycle --- Router_PID/Router_PID.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Router_PID/Router_PID.ino b/Router_PID/Router_PID.ino index 0f68ce8..9e6f573 100644 --- a/Router_PID/Router_PID.ino +++ b/Router_PID/Router_PID.ino @@ -132,7 +132,7 @@ void spindleRPM() { void rising() { // Capture when this is rising. if (MAX_PWM_INPUT_US == 0 && prev_time != 0) { - MAX_PWM_INPUT_US = micros()-prev_time; + MAX_PWM_INPUT_US = (micros()-prev_time)*.99; } prev_time = micros(); From 68f1c05123c4fc7f113e65b72836d36d34428a4b Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 8 Jun 2020 15:17:23 -0500 Subject: [PATCH 3/6] Store micros() in a local variable in rising() --- Router_PID/Router_PID.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Router_PID/Router_PID.ino b/Router_PID/Router_PID.ino index 9e6f573..66d0bb2 100644 --- a/Router_PID/Router_PID.ino +++ b/Router_PID/Router_PID.ino @@ -130,11 +130,12 @@ void spindleRPM() { // rising() is called on the rising edge of the PHOTO_PIN. Basically starts the timer for measuring the // PWM duty cycle. ISR. void rising() { + int new_pulse = micros(); // Capture when this is rising. if (MAX_PWM_INPUT_US == 0 && prev_time != 0) { - MAX_PWM_INPUT_US = (micros()-prev_time)*.99; + MAX_PWM_INPUT_US = (new_pulse-prev_time)*.99; } - prev_time = micros(); + prev_time = new_pulse; // Set the next interrupt. attachInterrupt(digitalPinToInterrupt(PWM_PIN), falling, FALLING); From 03f22a49e2876e94662a46c3077f0cd7803c4b9a Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 8 Jun 2020 15:51:21 -0500 Subject: [PATCH 4/6] Storing micros() locally broke things for some reason. --- Router_PID/Router_PID.ino | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Router_PID/Router_PID.ino b/Router_PID/Router_PID.ino index 66d0bb2..e3c79d9 100644 --- a/Router_PID/Router_PID.ino +++ b/Router_PID/Router_PID.ino @@ -130,12 +130,13 @@ void spindleRPM() { // rising() is called on the rising edge of the PHOTO_PIN. Basically starts the timer for measuring the // PWM duty cycle. ISR. void rising() { - int new_pulse = micros(); + // Ideally we store micros() in a local variable and use it instead of calling again but this is breaking things. + // int new_pulse = micros(); // Capture when this is rising. if (MAX_PWM_INPUT_US == 0 && prev_time != 0) { - MAX_PWM_INPUT_US = (new_pulse-prev_time)*.99; + MAX_PWM_INPUT_US = (micros()-prev_time)*.99; } - prev_time = new_pulse; + prev_time = micros(); // Set the next interrupt. attachInterrupt(digitalPinToInterrupt(PWM_PIN), falling, FALLING); From 6e9d21a7a963f61680f86d4f5c16aa9ff84d6022 Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 8 Jun 2020 16:04:02 -0500 Subject: [PATCH 5/6] Use volatile for MAX_PWM_INPUT_US Use -1 as start value for MAX_PWM_INPUT_US this solves a potential divide by zero isse Clear MAX_PWM_INPUT_US and prev_value when spindle disabled --- Router_PID/Router_PID.ino | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Router_PID/Router_PID.ino b/Router_PID/Router_PID.ino index e3c79d9..074f94b 100644 --- a/Router_PID/Router_PID.ino +++ b/Router_PID/Router_PID.ino @@ -44,7 +44,7 @@ double Kd=0.025; PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); // PID library const int MAX_TOOL_RPM = 30000; // SETTINGS per "spindle" ?1k headroom needed? -int MAX_PWM_INPUT_US = 0; // Settings the microseconds of the max PWM from Marlin. +volatile int MAX_PWM_INPUT_US = -1; // Settings the microseconds of the max PWM from Marlin. void setup() { @@ -84,6 +84,8 @@ void loop() { pwm_value = 0; // Reset PID analogWrite(ROUTER_PWM_OUTPIN, 0); // Turn off Spindle AC rpm_value = US_PERIOD_TO_RPM; // Disregards the spindown but clears the PID + MAX_PWM_INPUT_US = -1; // Reset detected maximum input pulse to autodetect again. + prev_time = 0; } else { // If spindle is enabled write PID value to triac triac_scaled = map(Output, 0, 255, 0, 255); // Traic scaling 20-240 not needed? test/tune @@ -133,7 +135,7 @@ void rising() { // Ideally we store micros() in a local variable and use it instead of calling again but this is breaking things. // int new_pulse = micros(); // Capture when this is rising. - if (MAX_PWM_INPUT_US == 0 && prev_time != 0) { + if (MAX_PWM_INPUT_US == -1 && prev_time != 0) { MAX_PWM_INPUT_US = (micros()-prev_time)*.99; } prev_time = micros(); From fe567d9b0bef1b11b22b5eb4b36380bfaf92f327 Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 8 Jun 2020 16:15:31 -0500 Subject: [PATCH 6/6] store micros() in an unsigned long local variable instead of calling it multiple times --- Router_PID/Router_PID.ino | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Router_PID/Router_PID.ino b/Router_PID/Router_PID.ino index 074f94b..d0de71b 100644 --- a/Router_PID/Router_PID.ino +++ b/Router_PID/Router_PID.ino @@ -132,13 +132,12 @@ void spindleRPM() { // rising() is called on the rising edge of the PHOTO_PIN. Basically starts the timer for measuring the // PWM duty cycle. ISR. void rising() { - // Ideally we store micros() in a local variable and use it instead of calling again but this is breaking things. - // int new_pulse = micros(); + unsigned long new_pulse = micros(); // Capture when this is rising. if (MAX_PWM_INPUT_US == -1 && prev_time != 0) { - MAX_PWM_INPUT_US = (micros()-prev_time)*.99; + MAX_PWM_INPUT_US = (new_pulse-prev_time)*.99; } - prev_time = micros(); + prev_time = new_pulse; // Set the next interrupt. attachInterrupt(digitalPinToInterrupt(PWM_PIN), falling, FALLING);