From 76d0d0647c61e5c7b3f3e53340f0e5a5ea431297 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 11 Jul 2025 01:21:41 +0200 Subject: [PATCH] feat: Implement obstacle detection for valve movement Implement obstacle detection for valve movement that stops the motor if the current exceeds a predefined threshold during opening or closing. - : - Added new defines and with a default value of 500 mA. - : - Modified function to compare the measured current with the new obstacle thresholds. - If the threshold is exceeded, the valve movement is stopped and the status is set to . Signed-off-by: Your Name --- software/include/lib/valve.h | 3 +++ software/lib/valve/valve.c | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/software/include/lib/valve.h b/software/include/lib/valve.h index 5c8aba3..2cfbde7 100644 --- a/software/include/lib/valve.h +++ b/software/include/lib/valve.h @@ -17,6 +17,9 @@ #define VALVE_CHANNEL_CLOSE 1 #define VALVE_ENDPOSITION_CHECK_INTERVAL K_MSEC(100) +#define VALVE_OBSTACLE_THRESHOLD_OPEN_MA 500 +#define VALVE_OBSTACLE_THRESHOLD_CLOSE_MA 500 + /** * @brief Represents the static state of the valve (open or closed). */ diff --git a/software/lib/valve/valve.c b/software/lib/valve/valve.c index ad8abc6..8a1af91 100644 --- a/software/lib/valve/valve.c +++ b/software/lib/valve/valve.c @@ -47,14 +47,28 @@ 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); - if (current_ma > end_current_threshold_open_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; + } else if (current_ma > end_current_threshold_open_ma) { k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL); return; } LOG_INF("Valve finished opening"); } else if (current_movement == VALVE_MOVEMENT_CLOSING) { vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, ¤t_ma); - if (current_ma > end_current_threshold_close_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; + } else if (current_ma > end_current_threshold_close_ma) { k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL); return; }