|
|
|
|
@@ -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);
|
|
|
|
|
|