feat(valve): Implement obstacle detection with configurable thresholds

Introduces separate configurable current thresholds for obstacle detection
during valve opening and closing movements.

- Added  state to .
- Added  and
   to .
- Modified  to implement obstacle detection in ,
  setting  on high current, and to load/save
  these new thresholds via settings.
- Added new setter/getter functions for obstacle thresholds to  and .
- Updated  with new shell commands (, )
  and updated  to display these settings.
- Updated  to document the new registers and error states.
- Updated  to include new register definitions, menu options,
  and display of obstacle current thresholds.
This commit is contained in:
2025-07-11 00:27:31 +02:00
parent a3e8d5c168
commit 3c2235733b
6 changed files with 152 additions and 8 deletions

View File

@@ -55,6 +55,32 @@ static int cmd_valve_set_end_curr_close(const struct shell *sh, size_t argc, cha
return 0;
}
static int cmd_valve_set_obs_curr_open(const struct shell *sh, size_t argc, char **argv)
{
if (argc != 2) {
shell_print(sh, "Usage: valve set_obs_curr_open <milliamps>");
return -EINVAL;
}
uint16_t current_ma = (uint16_t)atoi(argv[1]);
valve_set_obstacle_current_threshold_open(current_ma);
shell_print(sh, "Obstacle current threshold (open) set to %u mA.", current_ma);
return 0;
}
static int cmd_valve_set_obs_curr_close(const struct shell *sh, size_t argc, char **argv)
{
if (argc != 2) {
shell_print(sh, "Usage: valve set_obs_curr_close <milliamps>");
return -EINVAL;
}
uint16_t current_ma = (uint16_t)atoi(argv[1]);
valve_set_obstacle_current_threshold_close(current_ma);
shell_print(sh, "Obstacle current threshold (close) set to %u mA.", current_ma);
return 0;
}
static int cmd_valve_show(const struct shell *sh, size_t argc, char **argv)
{
const int label_width = 30;
@@ -72,6 +98,16 @@ static int cmd_valve_show(const struct shell *sh, size_t argc, char **argv)
label_width,
"End Current Threshold (Close):",
valve_get_end_current_threshold_close());
shell_print(sh,
"%*s %u mA",
label_width,
"Obstacle Current Threshold (Open):",
valve_get_obstacle_current_threshold_open());
shell_print(sh,
"%*s %u mA",
label_width,
"Obstacle Current Threshold (Close):",
valve_get_obstacle_current_threshold_close());
return 0;
}
@@ -86,6 +122,14 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_settings,
NULL,
"Set end current threshold for closing (mA)",
cmd_valve_set_end_curr_close),
SHELL_CMD(set_obs_curr_open,
NULL,
"Set obstacle current threshold for opening (mA)",
cmd_valve_set_obs_curr_open),
SHELL_CMD(set_obs_curr_close,
NULL,
"Set obstacle current threshold for closing (mA)",
cmd_valve_set_obs_curr_close),
SHELL_CMD(show, NULL, "Show valve configuration", cmd_valve_show),
SHELL_SUBCMD_SET_END);

View File

@@ -29,6 +29,8 @@ 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 uint16_t obstacle_current_threshold_open_ma = 600; // Default value for open
static uint16_t obstacle_current_threshold_close_ma = 600; // Default value for close
static struct k_work_delayable valve_work; // Work item for scheduling valve movement timeouts
static struct k_timer movement_timer;
@@ -47,6 +49,11 @@ static void valve_work_handler(struct k_work *work)
if (current_movement == VALVE_MOVEMENT_OPENING) {
vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_OPEN, &current_ma);
if (current_ma > obstacle_current_threshold_open_ma) {
LOG_ERR("Obstacle detected during opening! Current: %d mA", current_ma);
current_movement = VALVE_MOVEMENT_OBSTACLE;
goto work_handler_finish;
}
if (current_ma > end_current_threshold_open_ma) {
k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL);
return;
@@ -54,6 +61,11 @@ static void valve_work_handler(struct k_work *work)
LOG_INF("Valve finished opening");
} else if (current_movement == VALVE_MOVEMENT_CLOSING) {
vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, &current_ma);
if (current_ma > obstacle_current_threshold_close_ma) {
LOG_ERR("Obstacle detected during closing! Current: %d mA", current_ma);
current_movement = VALVE_MOVEMENT_OBSTACLE;
goto work_handler_finish;
}
if (current_ma > end_current_threshold_close_ma) {
k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL);
return;
@@ -63,6 +75,7 @@ static void valve_work_handler(struct k_work *work)
}
current_movement = VALVE_MOVEMENT_IDLE;
work_handler_finish:
// Reset the movement timer
k_timer_stop(&movement_timer);
@@ -104,13 +117,21 @@ int valve_init(void)
settings_load_one("valve/end_current_threshold_close",
&end_current_threshold_close_ma,
sizeof(end_current_threshold_close_ma));
settings_load_one("valve/obstacle_current_threshold_open",
&obstacle_current_threshold_open_ma,
sizeof(obstacle_current_threshold_open_ma));
settings_load_one("valve/obstacle_current_threshold_close",
&obstacle_current_threshold_close_ma,
sizeof(obstacle_current_threshold_close_ma));
LOG_INF("Valve initialized: max_open=%us, max_close=%us, end_curr_open=%umA, "
"end_curr_close=%umA",
"end_curr_close=%umA, obs_curr_open=%umA, obs_curr_close=%umA",
max_opening_time_s,
max_closing_time_s,
end_current_threshold_open_ma,
end_current_threshold_close_ma);
end_current_threshold_close_ma,
obstacle_current_threshold_open_ma,
obstacle_current_threshold_close_ma);
valve_close();
return 0;
}
@@ -190,6 +211,22 @@ void valve_set_end_current_threshold_close(uint16_t current_ma)
sizeof(end_current_threshold_close_ma));
}
void valve_set_obstacle_current_threshold_open(uint16_t current_ma)
{
obstacle_current_threshold_open_ma = current_ma;
settings_save_one("valve/obstacle_current_threshold_open",
&obstacle_current_threshold_open_ma,
sizeof(obstacle_current_threshold_open_ma));
}
void valve_set_obstacle_current_threshold_close(uint16_t current_ma)
{
obstacle_current_threshold_close_ma = current_ma;
settings_save_one("valve/obstacle_current_threshold_close",
&obstacle_current_threshold_close_ma,
sizeof(obstacle_current_threshold_close_ma));
}
uint16_t valve_get_max_open_time(void)
{
return max_opening_time_s;
@@ -209,6 +246,16 @@ uint16_t valve_get_end_current_threshold_close(void)
return end_current_threshold_close_ma;
}
uint16_t valve_get_obstacle_current_threshold_open(void)
{
return obstacle_current_threshold_open_ma;
}
uint16_t valve_get_obstacle_current_threshold_close(void)
{
return obstacle_current_threshold_close_ma;
}
int32_t valve_get_opening_current(void)
{
int32_t current;