From 9325fa20c8692eab7fa2a9e22810d344ebcdf771 Mon Sep 17 00:00:00 2001 From: Eduard Iten Date: Tue, 22 Jul 2025 10:53:24 +0200 Subject: [PATCH] feat(valve): Add current measurement callbacks and shell commands This commit introduces several enhancements to the valve library. - New weak callback functions `valve_current_open_callback` and `valve_current_close_callback` are added to allow the application to monitor the current during valve opening and closing operations. - The `movement_timeout_handler` now correctly sets the valve state to `VALVE_STATE_OPEN` and movement to `VALVE_MOVEMENT_IDLE` upon timeout. - New shell commands `valve open`, `valve close`, and `valve stop` are added for direct control of the valve. - The existing setting commands are reorganized under a `valve set` subcommand, and their names are shortened (e.g., `set_open_t` to `open_t`). - The default configuration for `LIB_MODBUS_SERVER` and `LIB_VALVE` is changed to `n`. Signed-off-by: Eduard Iten --- software/include/lib/valve.h | 20 ++++++++ software/lib/modbus_server/Kconfig | 2 +- software/lib/shell_valve/shell_valve.c | 65 ++++++++++++++++++++++---- software/lib/valve/Kconfig | 2 +- software/lib/valve/valve.c | 17 +++++-- 5 files changed, 93 insertions(+), 13 deletions(-) diff --git a/software/include/lib/valve.h b/software/include/lib/valve.h index 4738e3b..ab5b8b8 100644 --- a/software/include/lib/valve.h +++ b/software/include/lib/valve.h @@ -191,4 +191,24 @@ uint16_t valve_get_obstacle_threshold_open(void); */ uint16_t valve_get_obstacle_threshold_close(void); +/** + * @brief Callback function called during valve opening with current readings. + * + * This is a weak function that can be overridden to provide custom handling + * of current readings during valve opening operations. + * + * @param current_ma The current reading in milliamps. + */ +void valve_current_open_callback(int current_ma); + +/** + * @brief Callback function called during valve closing with current readings. + * + * This is a weak function that can be overridden to provide custom handling + * of current readings during valve closing operations. + * + * @param current_ma The current reading in milliamps. + */ +void valve_current_close_callback(int current_ma); + #endif // VALVE_H \ No newline at end of file diff --git a/software/lib/modbus_server/Kconfig b/software/lib/modbus_server/Kconfig index 0a5339d..655f04c 100644 --- a/software/lib/modbus_server/Kconfig +++ b/software/lib/modbus_server/Kconfig @@ -1,5 +1,5 @@ config LIB_MODBUS_SERVER bool "Enable Modbus Server Library" - default y + default n help Enable the Modbus Server Library. diff --git a/software/lib/shell_valve/shell_valve.c b/software/lib/shell_valve/shell_valve.c index 57291e8..5f77b83 100644 --- a/software/lib/shell_valve/shell_valve.c +++ b/software/lib/shell_valve/shell_valve.c @@ -111,26 +111,75 @@ static int cmd_valve_show(const struct shell *sh, size_t argc, char **argv) return 0; } +static int cmd_valve_open(const struct shell *sh, size_t argc, char **argv) +{ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + if (valve_get_movement() != VALVE_MOVEMENT_IDLE) { + shell_print(sh, "Valve is already moving."); + return -EBUSY; + } + + valve_open(); + shell_print(sh, "Valve is opening."); + return 0; +} + +static int cmd_valve_close(const struct shell *sh, size_t argc, char **argv) +{ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + if (valve_get_movement() != VALVE_MOVEMENT_IDLE) { + shell_print(sh, "Valve is already moving."); + return -EBUSY; + } + + valve_close(); + shell_print(sh, "Valve is closing."); + return 0; +} + +static int cmd_valve_stop(const struct shell *sh, size_t argc, char **argv) +{ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + if (valve_get_movement() == VALVE_MOVEMENT_IDLE) { + shell_print(sh, "Valve is already stopped."); + return -EINVAL; + } + + valve_stop(); + shell_print(sh, "Valve movement stopped."); + return 0; +} SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_settings, - SHELL_CMD(set_open_t, NULL, "Set max open time (seconds)", cmd_valve_set_open_t), - SHELL_CMD(set_close_t, NULL, "Set max close time (seconds)", cmd_valve_set_close_t), - SHELL_CMD(set_end_curr_open, + SHELL_CMD(open_t, NULL, "Set max open time (seconds)", cmd_valve_set_open_t), + SHELL_CMD(close_t, NULL, "Set max close time (seconds)", cmd_valve_set_close_t), + SHELL_CMD(end_curr_open, NULL, "Set end current threshold for opening (mA)", cmd_valve_set_end_curr_open), - SHELL_CMD(set_end_curr_close, + SHELL_CMD(end_curr_close, NULL, "Set end current threshold for closing (mA)", cmd_valve_set_end_curr_close), - SHELL_CMD(set_obstacle_open, + SHELL_CMD(obstacle_curr_open, NULL, "Set obstacle threshold for opening (mA)", cmd_valve_set_obstacle_open), - SHELL_CMD(set_obstacle_close, + SHELL_CMD(obstacle_curr_close, NULL, "Set obstacle threshold for closing (mA)", cmd_valve_set_obstacle_close), - SHELL_CMD(show, NULL, "Show valve configuration", cmd_valve_show), + SHELL_SUBCMD_SET_END); -SHELL_CMD_REGISTER(valve, &sub_valve_settings, "Valve commands", NULL); +SHELL_STATIC_SUBCMD_SET_CREATE(valve_cmds, + SHELL_CMD(show, NULL, "Show valve configuration", cmd_valve_show), + SHELL_CMD(set, &sub_valve_settings, "Valve settings commands", NULL), + SHELL_CMD(open, NULL, "Open the valve", cmd_valve_open), + SHELL_CMD(close, NULL, "Close the valve", cmd_valve_close), + SHELL_CMD(stop, NULL, "Stop the valve movement", cmd_valve_stop), + SHELL_SUBCMD_SET_END); + +SHELL_CMD_REGISTER(valve, &valve_cmds, "Valve commands", NULL); diff --git a/software/lib/valve/Kconfig b/software/lib/valve/Kconfig index 0bc1d8b..7e0d9b8 100644 --- a/software/lib/valve/Kconfig +++ b/software/lib/valve/Kconfig @@ -1,6 +1,6 @@ config LIB_VALVE bool "Enable Valve Library" - default y + default n help Enable the Valve Library. diff --git a/software/lib/valve/valve.c b/software/lib/valve/valve.c index 9a57c51..8b86de6 100644 --- a/software/lib/valve/valve.c +++ b/software/lib/valve/valve.c @@ -49,7 +49,7 @@ 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); + valve_current_open_callback(current_ma); if (current_ma > obstacle_threshold_open_ma) { LOG_ERR( "Obstacle detected during opening (current: %d mA), stopping motor.", @@ -64,7 +64,7 @@ static void valve_work_handler(struct k_work *work) 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); + valve_current_close_callback(current_ma); if (current_ma > obstacle_threshold_close_ma) { LOG_ERR( "Obstacle detected during closing (current: %d mA), stopping motor.", @@ -106,7 +106,8 @@ void movement_timeout_handler(struct k_timer *timer) } vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, false); vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false); - current_state = VALVE_STATE_CLOSED; + current_state = VALVE_STATE_OPEN; + current_movement = VALVE_MOVEMENT_IDLE; } int valve_init(void) @@ -291,3 +292,13 @@ uint16_t valve_get_obstacle_threshold_close(void) { return obstacle_threshold_close_ma; } + +__weak void valve_current_open_callback(int current_ma) +{ + LOG_DBG("Open current callback: %d mA", current_ma); +} + +__weak void valve_current_close_callback(int current_ma) +{ + LOG_DBG("Close current callback: %d mA", current_ma); +}