ValveController implementation
This commit is contained in:
55
.vscode/settings.json
vendored
55
.vscode/settings.json
vendored
@@ -15,8 +15,63 @@
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"clang-format.executable": "C:/qt/qt-creator-8_win_x64/bin/clang-format.exe",
|
"clang-format.executable": "C:/qt/qt-creator-8_win_x64/bin/clang-format.exe",
|
||||||
|
"clang-format.style": "file",
|
||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
"[cpp]": {
|
"[cpp]": {
|
||||||
"editor.defaultFormatter": "demiaochen.clang-format-indent-4",
|
"editor.defaultFormatter": "demiaochen.clang-format-indent-4",
|
||||||
},
|
},
|
||||||
|
"files.associations": {
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"compare": "cpp",
|
||||||
|
"complex": "cpp",
|
||||||
|
"concepts": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"ranges": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"cinttypes": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"chrono": "cpp",
|
||||||
|
"ctime": "cpp",
|
||||||
|
"ratio": "cpp"
|
||||||
|
},
|
||||||
}
|
}
|
||||||
@@ -5,10 +5,6 @@ cmake_minimum_required(VERSION 3.13)
|
|||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
# Initialise pico_sdk from installed location
|
|
||||||
# (note this can come from environment, CMake cache etc)
|
|
||||||
#set(PICO_SDK_PATH "C:/workRaspberry_Pi/Pico_SDK_v1.5.1/pico-sdk")
|
|
||||||
|
|
||||||
set(PICO_BOARD pico CACHE STRING "Board type")
|
set(PICO_BOARD pico CACHE STRING "Board type")
|
||||||
|
|
||||||
# Pull in Raspberry Pi Pico SDK (must be before project)
|
# Pull in Raspberry Pi Pico SDK (must be before project)
|
||||||
@@ -23,8 +19,6 @@ project(ValveController C CXX ASM)
|
|||||||
# Initialise the Raspberry Pi Pico SDK
|
# Initialise the Raspberry Pi Pico SDK
|
||||||
pico_sdk_init()
|
pico_sdk_init()
|
||||||
|
|
||||||
# Add executable. Default name is the project name, version 0.1
|
|
||||||
|
|
||||||
add_executable(ValveController ValveController.cpp)
|
add_executable(ValveController ValveController.cpp)
|
||||||
|
|
||||||
pico_set_program_name(ValveController "ValveController")
|
pico_set_program_name(ValveController "ValveController")
|
||||||
@@ -34,8 +28,7 @@ pico_enable_stdio_uart(ValveController 1)
|
|||||||
pico_enable_stdio_usb(ValveController 0)
|
pico_enable_stdio_usb(ValveController 0)
|
||||||
|
|
||||||
# Add the standard library to the build
|
# Add the standard library to the build
|
||||||
target_link_libraries(ValveController
|
target_link_libraries(ValveController pico_stdlib)
|
||||||
pico_stdlib)
|
|
||||||
|
|
||||||
# Add the standard include files to the build
|
# Add the standard include files to the build
|
||||||
target_include_directories(ValveController PRIVATE
|
target_include_directories(ValveController PRIVATE
|
||||||
@@ -44,8 +37,7 @@ target_include_directories(ValveController PRIVATE
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Add any user requested libraries
|
# Add any user requested libraries
|
||||||
target_link_libraries(ValveController
|
target_link_libraries(ValveController)
|
||||||
)
|
|
||||||
|
|
||||||
pico_add_extra_outputs(ValveController)
|
pico_add_extra_outputs(ValveController)
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <chrono>
|
||||||
#include <pico/stdlib.h>
|
#include <pico/stdlib.h>
|
||||||
|
|
||||||
void gpio_init_out(uint gpio) {
|
void gpio_init_out(uint gpio) {
|
||||||
@@ -6,45 +7,110 @@ void gpio_init_out(uint gpio) {
|
|||||||
gpio_set_function(gpio, GPIO_FUNC_SIO);
|
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 {
|
class Valve {
|
||||||
public:
|
public:
|
||||||
|
enum class State {
|
||||||
|
Unknown,
|
||||||
|
Opening,
|
||||||
|
Opened,
|
||||||
|
Closing,
|
||||||
|
Closed
|
||||||
|
};
|
||||||
|
|
||||||
Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end)
|
Valve(uint gpio_motor1, uint gpio_motor2, uint gpio_start, uint gpio_end)
|
||||||
: motor1(gpio_motor1), motor2(gpio_motor2), start_pin(gpio_start),
|
: motor1(gpio_motor1)
|
||||||
end_pin(gpio_end) {
|
, motor2(gpio_motor2)
|
||||||
|
, start_pin(gpio_start)
|
||||||
|
, end_pin(gpio_end) {
|
||||||
gpio_init_out(motor1);
|
gpio_init_out(motor1);
|
||||||
gpio_init_out(motor2);
|
gpio_init_out(motor2);
|
||||||
gpio_set_irq_enabled_with_callback(start_pin, GPIO_IRQ_EDGE_RISE, true,
|
gpio_init(start_pin);
|
||||||
&gpio_callback);
|
gpio_init(end_pin);
|
||||||
gpio_set_irq_enabled_with_callback(end_pin, GPIO_IRQ_EDGE_RISE, true,
|
start_time = std::chrono::steady_clock::now();
|
||||||
&gpio_callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void open() {
|
void open() {
|
||||||
|
if (state == State::Opening) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
gpio_put(motor2, 0);
|
gpio_put(motor2, 0);
|
||||||
gpio_put(motor1, 1);
|
gpio_put(motor1, 1);
|
||||||
|
state = State::Opening;
|
||||||
|
start_time = std::chrono::steady_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
|
if (state == State::Closing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
gpio_put(motor1, 0);
|
gpio_put(motor1, 0);
|
||||||
gpio_put(motor2, 1);
|
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:
|
private:
|
||||||
uint motor1, motor2;
|
uint motor1, motor2;
|
||||||
uint start_pin, end_pin;
|
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() {
|
int main() {
|
||||||
Valve *v = new Valve(0, 1, 4, 5);
|
Valve cold_valve{10, 11, 12, 13};
|
||||||
|
Valve hot_valve{21, 20, 19, 18};
|
||||||
|
|
||||||
|
const int button_open = 14;
|
||||||
|
const int button_close = 15;
|
||||||
gpio_init_out(PICO_DEFAULT_LED_PIN);
|
gpio_init_out(PICO_DEFAULT_LED_PIN);
|
||||||
|
gpio_init_input(button_open);
|
||||||
|
gpio_init_input(button_close);
|
||||||
|
|
||||||
|
const int tick_ms = 500;
|
||||||
while (true) {
|
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);
|
gpio_put(PICO_DEFAULT_LED_PIN, 1);
|
||||||
sleep_ms(200);
|
sleep_ms(tick_ms / 2);
|
||||||
|
cold_valve.process();
|
||||||
|
hot_valve.process();
|
||||||
gpio_put(PICO_DEFAULT_LED_PIN, 0);
|
gpio_put(PICO_DEFAULT_LED_PIN, 0);
|
||||||
sleep_ms(200);
|
sleep_ms(tick_ms / 2);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user