From e65ba3c91fd5b6f17ff6e577ef38297932afe260 Mon Sep 17 00:00:00 2001 From: bdring Date: Fri, 31 Aug 2018 13:52:26 -0500 Subject: [PATCH] Changed PWM to change only when needed - Checks to see if new PWM is different before changing it. --- Grbl_Esp32/Grbl_Esp32.ino | 7 ++++--- Grbl_Esp32/spindle_control.cpp | 21 +++++++++++++-------- Grbl_Esp32/spindle_control.h | 4 +++- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Grbl_Esp32/Grbl_Esp32.ino b/Grbl_Esp32/Grbl_Esp32.ino index f02c92d2..7545f036 100644 --- a/Grbl_Esp32/Grbl_Esp32.ino +++ b/Grbl_Esp32/Grbl_Esp32.ino @@ -3,7 +3,7 @@ Part of Grbl Copyright (c) 2014-2016 Sungeun K. Jeon for Gnea Research LLC - 2018 - Bart Dring This file was modifed for use on the ESP32 + 2018 - Bart Dring This file was modified for use on the ESP32 CPU. Do not use this with Grbl for atMega328P Grbl is free software: you can redistribute it and/or modify @@ -52,8 +52,9 @@ void setup() { char line[LINE_BUFFER_SIZE]; settings_read_build_info(line); if (line[0] != '\0') { - Serial.printf("Starting Bluetooth:%s", line); - bluetooth_init(line); + // just send to serial because it is the only interface available + Serial.printf("Starting Bluetooth:%s", line); + bluetooth_init(line); } #endif diff --git a/Grbl_Esp32/spindle_control.cpp b/Grbl_Esp32/spindle_control.cpp index d1597ec2..b8e3afe0 100644 --- a/Grbl_Esp32/spindle_control.cpp +++ b/Grbl_Esp32/spindle_control.cpp @@ -33,7 +33,7 @@ void spindle_init() void spindle_stop() { - ledcWrite(SPINDLE_PWM_CHANNEL, 0); + grbl_analogWrite(SPINDLE_PWM_CHANNEL, 0); } uint8_t spindle_get_state() @@ -47,14 +47,12 @@ uint8_t spindle_get_state() void spindle_set_speed(uint8_t pwm_value) { - ledcWrite(SPINDLE_PWM_CHANNEL, pwm_value); + grbl_analogWrite(SPINDLE_PWM_CHANNEL, pwm_value); } // Called by spindle_set_state() and step segment generator. Keep routine small and efficient. uint8_t spindle_compute_pwm_value(float rpm) { - // - uint8_t pwm_value; pwm_value = map(rpm, settings.rpm_min, settings.rpm_max, SPINDLE_PWM_OFF_VALUE, SPINDLE_PWM_MAX_VALUE); // TODO_ESP32 .. make it 16 bit @@ -78,10 +76,8 @@ void spindle_set_state(uint8_t state, float rpm) if (settings.flags & BITFLAG_LASER_MODE) { if (state == SPINDLE_ENABLE_CCW) { rpm = 0.0; } // TODO: May need to be rpm_min*(100/MAX_SPINDLE_SPEED_OVERRIDE); } - spindle_set_speed(spindle_compute_pwm_value(rpm)); - - } - + spindle_set_speed(spindle_compute_pwm_value(rpm)); + } sys.report_ovr_counter = 0; // Set to report change immediately } @@ -94,3 +90,12 @@ void spindle_sync(uint8_t state, float rpm) } +void grbl_analogWrite(uint8_t chan, uint32_t duty) +{ + if (ledcRead(chan) != duty) // reduce unnecessary calls to ledcWrite() + { + ledcWrite(chan, duty); + } +} + + diff --git a/Grbl_Esp32/spindle_control.h b/Grbl_Esp32/spindle_control.h index ce7a5b5c..1201a650 100644 --- a/Grbl_Esp32/spindle_control.h +++ b/Grbl_Esp32/spindle_control.h @@ -3,7 +3,7 @@ Part of Grbl Copyright (c) 2014-2016 Sungeun K. Jeon for Gnea Research LLC - 2018 - Bart Dring This file was modifed for use on the ESP32 + 2018 - Bart Dring This file was modified for use on the ESP32 CPU. Do not use this with Grbl for atMega328P Grbl is free software: you can redistribute it and/or modify @@ -38,5 +38,7 @@ uint8_t spindle_compute_pwm_value(float rpm); void spindle_set_state(uint8_t state, float rpm); void spindle_sync(uint8_t state, float rpm); + + void grbl_analogWrite(uint8_t chan, uint32_t duty); #endif