diff --git a/software/apps/slave_node/boards/weact_stm32g431_core.overlay b/software/apps/slave_node/boards/weact_stm32g431_core.overlay index e9c6f4f..4e41f6b 100644 --- a/software/apps/slave_node/boards/weact_stm32g431_core.overlay +++ b/software/apps/slave_node/boards/weact_stm32g431_core.overlay @@ -15,7 +15,7 @@ fault-reset-gpios = <&gpiob 3 GPIO_ACTIVE_LOW>; io-channels = <&adc1 1>; r-sense-ohms = <1500>; - k-vcc = <4139>; + k-vcc = <3816>; }; }; diff --git a/software/apps/slave_node/prj.conf b/software/apps/slave_node/prj.conf index f6575e3..6e3225e 100644 --- a/software/apps/slave_node/prj.conf +++ b/software/apps/slave_node/prj.conf @@ -26,4 +26,4 @@ CONFIG_MODBUS_BUFFER_SIZE=256 # Enable VND7050AJ CONFIG_VND7050AJ=y - +CONFIG_LOG_VALVE_LEVEL=4 \ No newline at end of file diff --git a/software/lib/valve/Kconfig b/software/lib/valve/Kconfig index ba26cb1..3798d64 100644 --- a/software/lib/valve/Kconfig +++ b/software/lib/valve/Kconfig @@ -3,3 +3,12 @@ config LIB_VALVE default y help Enable the Valve Library. + +if LIB_VALVE +config LOG_VALVE_LEVEL + int "Valve Log Level" + default 3 + help + Set the log level for the Valve Library. + 0 = None, 1 = Error, 2 = Warning, 3 = Info, 4 = Debug +endif # LIB_VALVE diff --git a/software/lib/valve/valve.c b/software/lib/valve/valve.c index 8a1af91..2ba6dca 100644 --- a/software/lib/valve/valve.c +++ b/software/lib/valve/valve.c @@ -21,15 +21,15 @@ #endif const struct device *vnd7050aj_dev = DEVICE_DT_GET(VND_NODE); -LOG_MODULE_REGISTER(valve, LOG_LEVEL_INF); +LOG_MODULE_REGISTER(valve, CONFIG_LOG_VALVE_LEVEL); static enum valve_state current_state = VALVE_STATE_OPEN; static enum valve_movement current_movement = VALVE_MOVEMENT_IDLE; static uint16_t max_opening_time_s = 10; static uint16_t max_closing_time_s = 10; -static uint16_t end_current_threshold_open_ma = 10; // Default value for open -static uint16_t end_current_threshold_close_ma = 10; // Default value for close -static struct k_work_delayable valve_work; // Work item for scheduling valve movement timeouts +static uint16_t end_current_threshold_open_ma = 10; +static uint16_t end_current_threshold_close_ma = 10; +static struct k_work_delayable valve_work; static struct k_timer movement_timer; /** @@ -47,36 +47,39 @@ static void valve_work_handler(struct k_work *work) if (current_movement == VALVE_MOVEMENT_OPENING) { vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_OPEN, ¤t_ma); + LOG_DBG("Current load during opening: %d mA", current_ma); if (current_ma > VALVE_OBSTACLE_THRESHOLD_OPEN_MA) { LOG_ERR( "Obstacle detected during opening (current: %d mA), stopping motor.", current_ma); current_movement = VALVE_MOVEMENT_ERROR; valve_stop(); - return; + goto work_handler_cleanup; } else if (current_ma > end_current_threshold_open_ma) { k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL); return; } - LOG_INF("Valve finished opening"); + LOG_DBG("Valve finished opening"); } else if (current_movement == VALVE_MOVEMENT_CLOSING) { vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, ¤t_ma); + LOG_DBG("Current load during closing: %d mA", current_ma); if (current_ma > VALVE_OBSTACLE_THRESHOLD_CLOSE_MA) { LOG_ERR( "Obstacle detected during closing (current: %d mA), stopping motor.", current_ma); current_movement = VALVE_MOVEMENT_ERROR; valve_stop(); - return; + goto work_handler_cleanup; } else if (current_ma > end_current_threshold_close_ma) { k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL); return; } current_state = VALVE_STATE_CLOSED; - LOG_INF("Valve finished closing"); + LOG_DBG("Valve finished closing"); } current_movement = VALVE_MOVEMENT_IDLE; +work_handler_cleanup: // Reset the movement timer k_timer_stop(&movement_timer); @@ -84,6 +87,15 @@ static void valve_work_handler(struct k_work *work) vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false); } +/** + * @brief Timer handler for valve movement timeouts. + * + * This function is called when the maximum allowed time for valve movement + * (opening or closing) has been reached. It stops the valve motor, cancels + * any pending end-position checks, and sets the movement status to error. + * + * @param timer Pointer to the k_timer instance that expired. + */ void movement_timeout_handler(struct k_timer *timer) { // Stop the end position check if the timer expires @@ -131,6 +143,7 @@ int valve_init(void) void valve_open(void) { + LOG_DBG("Opening valve"); vnd7050aj_reset_fault(vnd7050aj_dev); vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false); vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, true); @@ -145,6 +158,7 @@ void valve_open(void) void valve_close(void) { + LOG_DBG("Closing valve"); vnd7050aj_reset_fault(vnd7050aj_dev); vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, false); vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, true);