/** * @file valve.c * @brief Implementation of the motorized valve control library. * * This file contains the logic for controlling a motorized valve using a * VND7050AJ high-side driver. It uses a delayed work item to handle the * safety timeouts for opening and closing operations. */ #include #include #include #include #include #include #include #define VND_NODE DT_ALIAS(vnd7050aj) #if !DT_NODE_HAS_STATUS(VND_NODE, okay) #error VND7050AJ node is not defined or enabled #endif const struct device *vnd7050aj_dev = DEVICE_DT_GET(VND_NODE); LOG_MODULE_REGISTER(valve, LOG_LEVEL_INF); 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 uint32_t movement_start_time = 0; static struct k_work_delayable valve_work; // Work item for scheduling valve movement timeouts /** * @brief Work handler for valve movement timeouts. * * This function is executed when the valve's movement timer expires. * It stops the motor to prevent damage and updates the valve's state. * * @param work Pointer to the k_work item. */ static void valve_work_handler(struct k_work *work) { int current_ma = 0; uint32_t now; now = k_uptime_get_32(); if (current_movement == VALVE_MOVEMENT_OPENING) { if (now - movement_start_time > max_opening_time_s * 1000) { LOG_WRN("Valve opening timeout reached, stopping motor."); current_movement = VALVE_MOVEMENT_ERROR; goto work_handler_finish; } vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_OPEN, ¤t_ma); if (current_ma > 10) { k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL); return; } LOG_INF("Valve finished opening"); } else if (current_movement == VALVE_MOVEMENT_CLOSING) { if (now - movement_start_time > max_closing_time_s * 1000) { LOG_WRN("Valve closing timeout reached, stopping motor."); current_movement = VALVE_MOVEMENT_ERROR; goto work_handler_finish; } vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, ¤t_ma); if (current_ma > 10) { k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL); return; } current_state = VALVE_STATE_CLOSED; LOG_INF("Valve finished closing"); } current_movement = VALVE_MOVEMENT_IDLE; work_handler_finish: vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, false); vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false); LOG_INF("Valve work handler finished. Current state: %d, Movement: %d", current_state, current_movement); } int valve_init(void) { if (!device_is_ready(vnd7050aj_dev)) { LOG_ERR("VND7050AJ device is not ready"); return -ENODEV; } k_work_init_delayable(&valve_work, valve_work_handler); settings_load_one("valve/max_open_time", &max_opening_time_s, sizeof(max_opening_time_s)); settings_load_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s)); LOG_INF("Valve initialized: max_open=%us, max_close=%us", max_opening_time_s, max_closing_time_s); valve_close(); return 0; } void valve_open(void) { 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); current_state = VALVE_STATE_OPEN; current_movement = VALVE_MOVEMENT_OPENING; /* Security: assume valve open as soons as it starts opening */ movement_start_time = k_uptime_get_32(); k_work_schedule(&valve_work, K_MSEC(100)); } void valve_close(void) { 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); movement_start_time = k_uptime_get_32(); current_movement = VALVE_MOVEMENT_CLOSING; k_work_schedule(&valve_work, VALVE_ENDPOSITION_CHECK_INTERVAL); } void valve_stop(void) { k_work_cancel_delayable(&valve_work); vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_OPEN, false); vnd7050aj_set_output_state(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, false); current_movement = VALVE_MOVEMENT_IDLE; } enum valve_state valve_get_state(void) { return current_state; } enum valve_movement valve_get_movement(void) { return current_movement; } uint16_t valve_get_motor_current(void) { return (current_movement != VALVE_MOVEMENT_IDLE) ? 150 : 10; } void valve_set_max_open_time(uint16_t seconds) { max_opening_time_s = seconds; settings_save_one("valve/max_open_time", &max_opening_time_s, sizeof(max_opening_time_s)); } void valve_set_max_close_time(uint16_t seconds) { max_closing_time_s = seconds; settings_save_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s)); } uint16_t valve_get_max_open_time(void) { return max_opening_time_s; } uint16_t valve_get_max_close_time(void) { return max_closing_time_s; } int32_t valve_get_opening_current(void) { int32_t current; vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_OPEN, ¤t); return current; } int32_t valve_get_closing_current(void) { int32_t current; vnd7050aj_read_load_current(vnd7050aj_dev, VALVE_CHANNEL_CLOSE, ¤t); return current; } int32_t valve_get_vnd_temp(void) { int32_t temp_c; vnd7050aj_read_chip_temp(vnd7050aj_dev, &temp_c); return temp_c; } int32_t valve_get_vnd_voltage(void) { int32_t voltage_mv; vnd7050aj_read_supply_voltage(vnd7050aj_dev, &voltage_mv); return voltage_mv; }