Skip to content

Commit

Permalink
Hotfix pwm, updated braking lights, added client connected blinking
Browse files Browse the repository at this point in the history
  • Loading branch information
GargiMan committed Dec 15, 2023
1 parent 1c1f3f6 commit 4329b7a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
28 changes: 25 additions & 3 deletions GlBoard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...);
Expand Down Expand Up @@ -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");
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4329b7a

Please sign in to comment.