ValveController implementation
This commit is contained in:
@@ -1,50 +1,116 @@
|
||||
#include <chrono>
|
||||
#include <pico/stdlib.h>
|
||||
|
||||
void gpio_init_out(uint gpio) {
|
||||
gpio_set_dir(gpio, GPIO_OUT);
|
||||
gpio_put(gpio, 0);
|
||||
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
||||
gpio_set_dir(gpio, GPIO_OUT);
|
||||
gpio_put(gpio, 0);
|
||||
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
||||
}
|
||||
|
||||
void gpio_callback(uint gpio, uint32_t events) {}
|
||||
void gpio_init_input(uint gpio) {
|
||||
gpio_set_dir(gpio, GPIO_IN);
|
||||
gpio_pull_up(gpio);
|
||||
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
||||
}
|
||||
|
||||
class Valve {
|
||||
public:
|
||||
Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end)
|
||||
: motor1(gpio_motor1), motor2(gpio_motor2), start_pin(gpio_start),
|
||||
end_pin(gpio_end) {
|
||||
gpio_init_out(motor1);
|
||||
gpio_init_out(motor2);
|
||||
gpio_set_irq_enabled_with_callback(start_pin, GPIO_IRQ_EDGE_RISE, true,
|
||||
&gpio_callback);
|
||||
gpio_set_irq_enabled_with_callback(end_pin, GPIO_IRQ_EDGE_RISE, true,
|
||||
&gpio_callback);
|
||||
}
|
||||
public:
|
||||
enum class State {
|
||||
Unknown,
|
||||
Opening,
|
||||
Opened,
|
||||
Closing,
|
||||
Closed
|
||||
};
|
||||
|
||||
void open() {
|
||||
gpio_put(motor2, 0);
|
||||
gpio_put(motor1, 1);
|
||||
}
|
||||
Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end)
|
||||
: motor1(gpio_motor1)
|
||||
, motor2(gpio_motor2)
|
||||
, start_pin(gpio_start)
|
||||
, end_pin(gpio_end) {
|
||||
gpio_init_out(motor1);
|
||||
gpio_init_out(motor2);
|
||||
gpio_init(start_pin);
|
||||
gpio_init(end_pin);
|
||||
start_time = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void close() {
|
||||
gpio_put(motor1, 0);
|
||||
gpio_put(motor2, 1);
|
||||
}
|
||||
void open() {
|
||||
if (state == State::Opening) {
|
||||
return;
|
||||
}
|
||||
gpio_put(motor2, 0);
|
||||
gpio_put(motor1, 1);
|
||||
state = State::Opening;
|
||||
start_time = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
private:
|
||||
uint motor1, motor2;
|
||||
uint start_pin, end_pin;
|
||||
void close() {
|
||||
if (state == State::Closing) {
|
||||
return;
|
||||
}
|
||||
gpio_put(motor1, 0);
|
||||
gpio_put(motor2, 1);
|
||||
state = State::Closing;
|
||||
start_time = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
State process() {
|
||||
if (state == State::Opening && gpio_get(start_pin) == 0) {
|
||||
gpio_put(motor1, 0);
|
||||
state = State::Opened;
|
||||
}
|
||||
if (state == State::Closing && gpio_get(end_pin) == 0) {
|
||||
gpio_put(motor2, 0);
|
||||
state = State::Closed;
|
||||
}
|
||||
if (state == State::Opening || state == State::Closing) {
|
||||
using namespace std::chrono;
|
||||
if (duration_cast<seconds>(steady_clock::now() - start_time) > timeout) {
|
||||
gpio_put(motor1, 0);
|
||||
gpio_put(motor2, 0);
|
||||
state = State::Unknown;
|
||||
}
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
State getState() const { return state; }
|
||||
|
||||
private:
|
||||
uint motor1, motor2;
|
||||
uint start_pin, end_pin;
|
||||
State state = State::Unknown;
|
||||
std::chrono::seconds timeout = std::chrono::seconds(10);
|
||||
decltype(std::chrono::steady_clock::now()) start_time;
|
||||
};
|
||||
|
||||
int main() {
|
||||
Valve *v = new Valve(0, 1, 4, 5);
|
||||
Valve cold_valve{10, 11, 12, 13};
|
||||
Valve hot_valve{21, 20, 19, 18};
|
||||
|
||||
gpio_init_out(PICO_DEFAULT_LED_PIN);
|
||||
while (true) {
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 1);
|
||||
sleep_ms(200);
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 0);
|
||||
sleep_ms(200);
|
||||
}
|
||||
return 0;
|
||||
const int button_open = 14;
|
||||
const int button_close = 15;
|
||||
gpio_init_out(PICO_DEFAULT_LED_PIN);
|
||||
gpio_init_input(button_open);
|
||||
gpio_init_input(button_close);
|
||||
|
||||
const int tick_ms = 500;
|
||||
while (true) {
|
||||
if (gpio_get(button_open) == 0) {
|
||||
cold_valve.open();
|
||||
hot_valve.open();
|
||||
}
|
||||
if (gpio_get(button_close) == 0) {
|
||||
cold_valve.close();
|
||||
hot_valve.close();
|
||||
}
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 1);
|
||||
sleep_ms(tick_ms / 2);
|
||||
cold_valve.process();
|
||||
hot_valve.process();
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 0);
|
||||
sleep_ms(tick_ms / 2);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user