From 4329b7ae8d97c96ddbf8a41f536c1b5fe0f6d699 Mon Sep 17 00:00:00 2001 From: Marek Gergel Date: Fri, 15 Dec 2023 05:46:50 +0100 Subject: [PATCH] Hotfix pwm, updated braking lights, added client connected blinking --- GlBoard.ino | 28 +++++++++++++++++++++++++--- config.h | 2 +- src/io.c | 1 + 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/GlBoard.ino b/GlBoard.ino index 190e364..6e53f6c 100644 --- a/GlBoard.ino +++ b/GlBoard.ino @@ -22,6 +22,7 @@ VescUart VESC; BluetoothSerial BT_PORT; Preferences preferences; +bool braking = false; int maxLightPower = pow(2, PWM_RESOLUTION) - 1; void debug_message(const char *fmt, ...); @@ -78,8 +79,8 @@ void setup() { // pin setup pinSetupGPIO(LED_ONBOARD_PIN, OUTPUT); - pinSetupPWM(FRONT_LIGHT_PIN, 1, 0, PWM_FREQUENCY, PWM_RESOLUTION, false); - pinSetupPWM(REAR_LIGHT_PIN, 2, 1, PWM_FREQUENCY, PWM_RESOLUTION, false); + pinSetupPWM(FRONT_LIGHT_PIN, 0, 0, PWM_FREQUENCY, PWM_RESOLUTION, false); + pinSetupPWM(REAR_LIGHT_PIN, 1, 1, PWM_FREQUENCY, PWM_RESOLUTION, false); debug_message("GPIO ready"); } @@ -91,6 +92,19 @@ void loop() { hasClient = BT_PORT.hasClient(); debug_message("Client %s", hasClient ? "connected" : "disconnected"); digitalWrite(LED_ONBOARD_PIN, BT_PORT.hasClient() ? true : false); + + // blink front light when client connects + if (hasClient) { + pinWritePWM(FRONT_LIGHT_PIN, 0); + delay(50); + pinWritePWM(FRONT_LIGHT_PIN, maxLightPower * preferences.getFloat(KEY_FRONT_LIGHT_POWER)); + delay(50); + pinWritePWM(FRONT_LIGHT_PIN, 0); + delay(50); + pinWritePWM(FRONT_LIGHT_PIN, maxLightPower * preferences.getFloat(KEY_FRONT_LIGHT_POWER)); + delay(50); + pinWritePWM(FRONT_LIGHT_PIN, 0); + } } // Check if client is connected @@ -261,14 +275,17 @@ void parse_received_data() { // calculate current and send to vesc if (current > 0) { + braking = false; if (reverse) { // reverse VESC.setCurrent(-current * MAX_THROTTLE_POWER / 100); } else { // forward VESC.setCurrent(current * MAX_THROTTLE_POWER / 100); } } else if (current < 0) { + braking = true; VESC.setBrakeCurrent(abs(current) * MAX_BRAKE_POWER / 100); } else { + braking = false; VESC.setCurrent(0); } } @@ -298,6 +315,7 @@ void parse_received_data() { // reset current if no control message received for a while if (resetCurrent) { if (millis() - lastTimeValidControlReceived > CONTROL_TIMEOUT) { + braking = false; VESC.setCurrent(0); } } @@ -369,7 +387,11 @@ void update_lights() { pinWritePWM(FRONT_LIGHT_PIN, 0); } if (preferences.getBool(KEY_REAR_LIGHT_ENABLED)) { - pinWritePWM(REAR_LIGHT_PIN, maxLightPower * preferences.getFloat(KEY_REAR_LIGHT_POWER)); + if (braking) { + pinWritePWM(REAR_LIGHT_PIN, maxLightPower); + } else { + pinWritePWM(REAR_LIGHT_PIN, maxLightPower * preferences.getFloat(KEY_REAR_LIGHT_POWER)); + } } else { pinWritePWM(REAR_LIGHT_PIN, 0); } diff --git a/config.h b/config.h index c86d635..278f927 100644 --- a/config.h +++ b/config.h @@ -9,7 +9,7 @@ #define CLIENT_PROJECT_NAME "GlControl" // Project client name #define PROJECT_VERSION "2.0" // Project version -// debug options , comment for no debug output +// debug options, comment for no debug output #define DEBUG 1 // Default debug info //#define VESC_DEBUG 1 // Vesc debug info diff --git a/src/io.c b/src/io.c index a8f8bab..98ba63b 100644 --- a/src/io.c +++ b/src/io.c @@ -258,6 +258,7 @@ void pinSetupPWM(int pin, int channel, int timer, int frequency, int resolution, ledc_timer.freq_hz = frequency; ledc_timer.speed_mode = highSpeed ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE; ledc_timer.timer_num = timer; + ledc_timer.clk_cfg = LEDC_AUTO_CLK; ledc_timer_config(&ledc_timer); ledc_channel_config_t ledc_channel;