From 87f6a0bf3ba03f0ec67641422738da3fdca1500b Mon Sep 17 00:00:00 2001 From: Anton Mukhin Date: Fri, 14 Jun 2024 18:27:46 +0300 Subject: [PATCH] E-STOP, Light button and pump status functionality fixed/implemented; FW is ready for testing! --- Core/Inc/board_logic.h | 5 ++- Core/Src/board_logic.c | 33 +++++++++++---- Relay_RS485_V2 Debug.launch | 82 +++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 Relay_RS485_V2 Debug.launch diff --git a/Core/Inc/board_logic.h b/Core/Inc/board_logic.h index 8f6e2d4..866d8f4 100644 --- a/Core/Inc/board_logic.h +++ b/Core/Inc/board_logic.h @@ -14,14 +14,15 @@ #define PWM_DUTY_MIN 0 #define PWM_DUTY_MAX 255 -#define PWM_LIGHTS_STEP 20 // Time step in ms between LED light PWM changes -#define LIGHTS_TIME 1000 // Time in ms to spend for smooth lights level change +#define PWM_LIGHTS_STEP 20 // Time step in ms between LED light PWM updates +#define LIGHTS_TIME 600 // Time in ms to spend for smooth lights level change void estop_reset(void); void board_init(void); uint16_t clamp_duty(uint16_t duty); void set_pwm(uint8_t unit, uint16_t duty); +void set_light(uint16_t duty); void loop_iterate(); void update_service_indication(void); diff --git a/Core/Src/board_logic.c b/Core/Src/board_logic.c index 3219457..4551776 100644 --- a/Core/Src/board_logic.c +++ b/Core/Src/board_logic.c @@ -5,6 +5,7 @@ #include #include +#include #include "main.h" #include "modbus_logic.h" @@ -49,6 +50,10 @@ void board_init(void) { // Activate (normalize) E-STOP trigger estop_reset(); + // Check if E-STOP is not shorted + if (HAL_GPIO_ReadPin(ESTOP_GPIO_Port, ESTOP_Pin) == GPIO_PIN_RESET) + status = status | 0b010; + // 1ms timer start HAL_TIM_Base_Start_IT(&htim1); } @@ -91,6 +96,17 @@ void set_pwm(uint8_t unit, uint16_t duty) { } } +void set_light(uint16_t duty) { + lights_pwm_target = clamp_duty(duty); + if (lights_pwm_target != lights_pwm) { + lights_pwm_delta = (lights_pwm_target - lights_pwm) / (LIGHTS_TIME / PWM_LIGHTS_STEP); + if (lights_pwm_delta == 0) { + if (lights_pwm_target - lights_pwm > 0) lights_pwm_delta = 1; + else lights_pwm_delta = -1; + } + } +} + void loop_iterate() { @@ -169,7 +185,7 @@ uint8_t read_register(uint16_t address, uint16_t* value) { *value = lights_pwm; } - else if (address == 0x2004) //Read status + else if (address == 0x2010) //Read status { *value = status; status &= 0b011; // Reset light button press event status @@ -188,6 +204,7 @@ uint8_t write_register(uint16_t address, uint16_t value) #endif switch (address) { case 0x2001: + if (status & 0b010) break; // Check if we are in emergency stop mode relays = value; break; case 0x2002: @@ -199,14 +216,7 @@ uint8_t write_register(uint16_t address, uint16_t value) set_pwm(2, motor2_pwm); break; case 0x2004: - lights_pwm_target = clamp_duty(value); - if (lights_pwm_target != lights_pwm) { - lights_pwm_delta = (lights_pwm_target - lights_pwm) / LIGHTS_TIME / PWM_LIGHTS_STEP; - if (lights_pwm_delta == 0) { - if (lights_pwm_target - lights_pwm > 0) lights_pwm_delta = 1; - else lights_pwm_delta = -1; - } - } + set_light(value); break; case 0x2020: if (value == 1) { @@ -225,14 +235,19 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == ESTOP_Pin) { status = status | 0b010; + relays = 0; } if(GPIO_Pin == LIGHTS_SW_Pin) { // Set "lights switch pressed" status bit status |= 0b100; + if (lights_pwm_target > 0) set_light(0); + else set_light(255); } if(GPIO_Pin == WATER_Pin) { // Set or reset "water" status bit status |= HAL_GPIO_ReadPin(WATER_GPIO_Port, WATER_Pin); + if (HAL_GPIO_ReadPin(WATER_GPIO_Port, WATER_Pin)) status |= 1; + else status &= ~1; } } diff --git a/Relay_RS485_V2 Debug.launch b/Relay_RS485_V2 Debug.launch new file mode 100644 index 0000000..88b614d --- /dev/null +++ b/Relay_RS485_V2 Debug.launch @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +