feat: Make valve obstacle detection parameters configurable via settings and shell
This commit introduces configurable obstacle detection thresholds for the valve, allowing them to be set and persisted via the Zephyr settings subsystem and controlled through the shell and Modbus tool.
- `software/lib/valve/Kconfig`: Added new Kconfig options `VALVE_OBSTACLE_THRESHOLD_OPEN_MA` and `VALVE_OBSTACLE_THRESHOLD_CLOSE_MA` for compile-time configuration and default values.
- `software/include/lib/valve.h`: Removed hardcoded defines and added API functions for setting and getting obstacle thresholds.
- `software/lib/valve/valve.c`:
- Updated `valve_work_handler` to use the new configurable obstacle thresholds.
- Integrated loading and saving of obstacle thresholds via the settings subsystem in `valve_init`.
- Implemented the new setter and getter functions for obstacle thresholds.
- Updated the `LOG_INF` message in `valve_init` to display the new obstacle threshold values.
- `software/apps/slave_node/prj.conf`: Added default values for the new Kconfig options.
- `software/lib/shell_valve/shell_valve.c`: Added new shell commands `valve set_obstacle_open` and `valve set_obstacle_close` to modify the obstacle thresholds, and updated `valve show` to display them.
- `software/tools/modbus_tool/modbus_tool.py`:
- Defined new Modbus holding registers (`REG_HOLDING_OBSTACLE_THRESHOLD_OPEN_MA`, `REG_HOLDING_OBSTACLE_THRESHOLD_CLOSE_MA`).
- Updated `poll_status` to read these new registers.
- Modified the `main_menu` to include "Set Obstacle Open" and "Set Obstacle Close" options in the settings menu, allowing users to view and modify these parameters.
- `software/lib/modbus_server/modbus_server.c`:
- Updated `holding_reg_rd` to read the new obstacle threshold registers.
- Updated `holding_reg_wr` to write to the new obstacle threshold registers.
- Removed incorrect `REG_HOLDING_END_CURRENT_THRESHOLD_OPEN_MA` and `REG_HOLDING_END_CURRENT_THRESHOLD_CLOSE_MA` cases from `input_reg_rd`.
- `software/include/lib/modbus_registers.h`: Created a new header file to centralize Modbus register definitions, which were previously hardcoded in `modbus_tool.py`.
Signed-off-by: Eduard Iten <eduard@iten.pro>
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <zephyr/usb/usb_device.h>
|
||||
#include <app_version.h>
|
||||
#include <lib/fwu.h>
|
||||
#include <lib/modbus_registers.h>
|
||||
#include <lib/modbus_server.h>
|
||||
#include <lib/valve.h>
|
||||
|
||||
@@ -87,6 +88,12 @@ static int holding_reg_rd(uint16_t addr, uint16_t *reg)
|
||||
case REG_HOLDING_END_CURRENT_THRESHOLD_CLOSE_MA:
|
||||
*reg = valve_get_end_current_threshold_close();
|
||||
break;
|
||||
case REG_HOLDING_OBSTACLE_THRESHOLD_OPEN_MA:
|
||||
*reg = valve_get_obstacle_threshold_open();
|
||||
break;
|
||||
case REG_HOLDING_OBSTACLE_THRESHOLD_CLOSE_MA:
|
||||
*reg = valve_get_obstacle_threshold_close();
|
||||
break;
|
||||
default:
|
||||
*reg = 0;
|
||||
break;
|
||||
@@ -126,6 +133,12 @@ static int holding_reg_wr(uint16_t addr, uint16_t reg)
|
||||
case REG_HOLDING_END_CURRENT_THRESHOLD_CLOSE_MA:
|
||||
valve_set_end_current_threshold_close(reg);
|
||||
break;
|
||||
case REG_HOLDING_OBSTACLE_THRESHOLD_OPEN_MA:
|
||||
valve_set_obstacle_threshold_open(reg);
|
||||
break;
|
||||
case REG_HOLDING_OBSTACLE_THRESHOLD_CLOSE_MA:
|
||||
valve_set_obstacle_threshold_close(reg);
|
||||
break;
|
||||
case REG_HOLDING_WATCHDOG_TIMEOUT_S:
|
||||
watchdog_timeout_s = reg;
|
||||
if (watchdog_timeout_s > 0) {
|
||||
@@ -188,12 +201,6 @@ static int input_reg_rd(uint16_t addr, uint16_t *reg)
|
||||
case REG_INPUT_FIRMWARE_VERSION_PATCH:
|
||||
*reg = APP_PATCHLEVEL;
|
||||
break;
|
||||
case REG_HOLDING_END_CURRENT_THRESHOLD_OPEN_MA:
|
||||
*reg = valve_get_end_current_threshold_open();
|
||||
break;
|
||||
case REG_HOLDING_END_CURRENT_THRESHOLD_CLOSE_MA:
|
||||
*reg = valve_get_end_current_threshold_close();
|
||||
break;
|
||||
default:
|
||||
*reg = 0;
|
||||
break;
|
||||
|
||||
@@ -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_obstacle_open(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
shell_print(sh, "Usage: valve set_obstacle_open <milliamps>");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
uint16_t current_ma = (uint16_t)atoi(argv[1]);
|
||||
valve_set_obstacle_threshold_open(current_ma);
|
||||
shell_print(sh, "Obstacle threshold (open) set to %u mA.", current_ma);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_valve_set_obstacle_close(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
shell_print(sh, "Usage: valve set_obstacle_close <milliamps>");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
uint16_t current_ma = (uint16_t)atoi(argv[1]);
|
||||
valve_set_obstacle_threshold_close(current_ma);
|
||||
shell_print(sh, "Obstacle 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 Threshold (Open):",
|
||||
valve_get_obstacle_threshold_open());
|
||||
shell_print(sh,
|
||||
"%*s %u mA",
|
||||
label_width,
|
||||
"Obstacle Threshold (Close):",
|
||||
valve_get_obstacle_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_obstacle_open,
|
||||
NULL,
|
||||
"Set obstacle threshold for opening (mA)",
|
||||
cmd_valve_set_obstacle_open),
|
||||
SHELL_CMD(set_obstacle_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);
|
||||
|
||||
|
||||
@@ -11,4 +11,21 @@ config LOG_VALVE_LEVEL
|
||||
help
|
||||
Set the log level for the Valve Library.
|
||||
0 = None, 1 = Error, 2 = Warning, 3 = Info, 4 = Debug
|
||||
|
||||
config VALVE_OBSTACLE_THRESHOLD_OPEN_MA
|
||||
int "Obstacle Threshold Open (mA)"
|
||||
default 200
|
||||
help
|
||||
Set the current threshold in milliamps for obstacle detection
|
||||
during valve opening. If the motor current exceeds this value,
|
||||
an obstacle is detected and the valve stops.
|
||||
|
||||
config VALVE_OBSTACLE_THRESHOLD_CLOSE_MA
|
||||
int "Obstacle Threshold Close (mA)"
|
||||
default 200
|
||||
help
|
||||
Set the current threshold in milliamps for obstacle detection
|
||||
during valve closing. If the motor current exceeds this value,
|
||||
an obstacle is detected and the valve stops.
|
||||
|
||||
endif # LIB_VALVE
|
||||
|
||||
@@ -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;
|
||||
static uint16_t end_current_threshold_close_ma = 10;
|
||||
static uint16_t obstacle_threshold_open_ma = CONFIG_VALVE_OBSTACLE_THRESHOLD_OPEN_MA;
|
||||
static uint16_t obstacle_threshold_close_ma = CONFIG_VALVE_OBSTACLE_THRESHOLD_CLOSE_MA;
|
||||
static struct k_work_delayable valve_work;
|
||||
static struct k_timer movement_timer;
|
||||
|
||||
@@ -48,7 +50,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);
|
||||
if (current_ma > VALVE_OBSTACLE_THRESHOLD_OPEN_MA) {
|
||||
if (current_ma > obstacle_threshold_open_ma) {
|
||||
LOG_ERR(
|
||||
"Obstacle detected during opening (current: %d mA), stopping motor.",
|
||||
current_ma);
|
||||
@@ -63,7 +65,7 @@ static void valve_work_handler(struct k_work *work)
|
||||
} 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) {
|
||||
if (current_ma > obstacle_threshold_close_ma) {
|
||||
LOG_ERR(
|
||||
"Obstacle detected during closing (current: %d mA), stopping motor.",
|
||||
current_ma);
|
||||
@@ -125,13 +127,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_threshold_open",
|
||||
&obstacle_threshold_open_ma,
|
||||
sizeof(obstacle_threshold_open_ma));
|
||||
settings_load_one("valve/obstacle_threshold_close",
|
||||
&obstacle_threshold_close_ma,
|
||||
sizeof(obstacle_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_open=%umA, obs_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_threshold_open_ma,
|
||||
obstacle_threshold_close_ma);
|
||||
valve_close();
|
||||
return 0;
|
||||
}
|
||||
@@ -255,3 +265,29 @@ int32_t valve_get_vnd_voltage(void)
|
||||
vnd7050aj_read_supply_voltage(vnd7050aj_dev, &voltage_mv);
|
||||
return voltage_mv;
|
||||
}
|
||||
|
||||
void valve_set_obstacle_threshold_open(uint16_t current_ma)
|
||||
{
|
||||
obstacle_threshold_open_ma = current_ma;
|
||||
settings_save_one("valve/obstacle_threshold_open",
|
||||
&obstacle_threshold_open_ma,
|
||||
sizeof(obstacle_threshold_open_ma));
|
||||
}
|
||||
|
||||
void valve_set_obstacle_threshold_close(uint16_t current_ma)
|
||||
{
|
||||
obstacle_threshold_close_ma = current_ma;
|
||||
settings_save_one("valve/obstacle_threshold_close",
|
||||
&obstacle_threshold_close_ma,
|
||||
sizeof(obstacle_threshold_close_ma));
|
||||
}
|
||||
|
||||
uint16_t valve_get_obstacle_threshold_open(void)
|
||||
{
|
||||
return obstacle_threshold_open_ma;
|
||||
}
|
||||
|
||||
uint16_t valve_get_obstacle_threshold_close(void)
|
||||
{
|
||||
return obstacle_threshold_close_ma;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user