Skip to content

Commit f9f7864

Browse files
committed
add pressure_task and pump_task skeletons
1 parent e224dde commit f9f7864

File tree

15 files changed

+205
-16
lines changed

15 files changed

+205
-16
lines changed

stm32-modules/include/vacuum-module/firmware/firmware_tasks.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ constexpr uint8_t UI_TASK_PRIORITY = 1;
2323

2424
constexpr size_t PRESSURE_STACK_SIZE = 256;
2525
constexpr uint8_t PRESSURE_TASK_PRIORITY = 1;
26+
27+
constexpr size_t PUMP_STACK_SIZE = 256;
28+
constexpr uint8_t PUMP_TASK_PRIORITY = 1;
2629
} // namespace tasks

stm32-modules/include/vacuum-module/firmware/freertos_tasks.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ namespace pressure_control_task {
2828
auto run(tasks::FirmwareTasks::QueueAggregator* aggregator,
2929
i2c::hardware::I2C* i2c_comms) -> void;
3030
} // namespace pressure_control_task
31+
32+
namespace pump_control_task {
33+
// Actual function that runs in the task
34+
auto run(tasks::FirmwareTasks::QueueAggregator* aggregator,
35+
i2c::hardware::I2C* i2c_comms) -> void;
36+
} // namespace pump_control_task

stm32-modules/include/vacuum-module/firmware/pump_policy.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class PumpPolicy {
1313
auto stop_pump_motor() -> bool;
1414
auto set_pump_duty_cycle(uint16_t duty) -> void;
1515
auto get_pump_duty_cycle() -> uint16_t;
16-
auto enable_pump_tach() -> bool;
16+
auto enable_pump_tach(bool enable) -> bool;
1717
auto get_pump_rpm() -> float;
18+
auto sleep_ms(uint32_t ms) -> void;
1819
};
1920
} // namespace pump_policy

stm32-modules/include/vacuum-module/vacuum-module/lps22df.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace lps22df {
88

9+
910
template <typename P>
1011
concept LPS22DFPolicy = requires(P p, uint16_t dev_addr, uint16_t reg,
1112
uint16_t size, uint8_t* data) {
@@ -20,6 +21,7 @@ class LPS222DF {
2021
_policy = policy;
2122
}
2223
}
24+
2325
};
2426

25-
} // namespace lps22df
27+
} // namespace lps22df

stm32-modules/include/vacuum-module/vacuum-module/messages.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ struct SetStatusBarStateMessage {
110110
std::optional<float> power = std::nullopt;
111111
};
112112

113+
struct SetTargetPressureMessage {
114+
uint32_t id = 0;
115+
bool from_host = false;
116+
uint32_t pressure_setpoint = 0;
117+
};
118+
119+
struct SetTargetRPMMessage {
120+
uint32_t id = 0;
121+
bool from_host = false;
122+
uint32_t rpm_setpoint = 0;
123+
};
124+
113125
using HostCommsMessage =
114126
::std::variant<std::monostate, IncomingMessageFromHost, ForceUSBDisconnect,
115127
ErrorMessage, AcknowledgePrevious, GetSystemInfoResponse,
@@ -122,6 +134,8 @@ using SystemMessage =
122134

123135
using UIMessage = ::std::variant<std::monostate, SetStatusBarStateMessage>;
124136

125-
using PressureMessage = ::std::variant<std::monostate>;
137+
using PressureMessage = ::std::variant<std::monostate, SetTargetPressureMessage>;
138+
139+
using PumpMessage = ::std::variant<std::monostate, SetTargetRPMMessage>;
126140

127141
}; // namespace messages

stm32-modules/include/vacuum-module/vacuum-module/pressure_task.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ class PressureTask {
8888
static_cast<void>(policy);
8989
}
9090

91+
template <PressureControlPolicy Policy>
92+
auto visit_message(const messages::SetTargetPressureMessage& m, Policy& policy) -> void {
93+
static_cast<void>(m);
94+
static_cast<void>(policy);
95+
}
96+
9197
Queue& _message_queue;
9298
Aggregator* _task_registry;
9399
bool _initialized{false};
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#pragma once
2+
#include <cmath>
3+
#include <cstdint>
4+
5+
#include "core/ack_cache.hpp"
6+
#include "core/queue_aggregator.hpp"
7+
#include "core/version.hpp"
8+
#include "firmware/pump_policy.hpp"
9+
#include "vacuum-module/errors.hpp"
10+
#include "vacuum-module/messages.hpp"
11+
#include "vacuum-module/tasks.hpp"
12+
#include "hal/message_queue.hpp"
13+
#include "messages.hpp"
14+
15+
namespace pump_task {
16+
template <typename P>
17+
concept PumpControlPolicy = requires(P p) {
18+
{ p.sleep_ms(1) };
19+
};
20+
21+
using PumpPolicy = pump_policy::PumpPolicy;
22+
using Message = messages::PumpMessage;
23+
using Error = errors::ErrorCode;
24+
25+
template <template <class> class QueueImpl>
26+
requires MessageQueue<QueueImpl<Message>, Message>
27+
class PumpTask {
28+
private:
29+
using Queue = QueueImpl<Message>;
30+
using Aggregator = typename tasks::Tasks<QueueImpl>::QueueAggregator;
31+
using Queues = typename tasks::Tasks<QueueImpl>;
32+
33+
public:
34+
explicit PumpTask(Queue& q, Aggregator* aggregator, PumpPolicy* policy)
35+
: _message_queue(q),
36+
_task_registry(aggregator) {}
37+
PumpTask(const PumpTask& other) = delete;
38+
auto operator=(const PumpTask& other) -> PumpTask& = delete;
39+
PumpTask(PumpTask&& other) noexcept = delete;
40+
auto operator=(PumpTask&& other) noexcept -> PumpTask& = delete;
41+
~PumpTask() = default;
42+
43+
auto provide_aggregator(Aggregator* aggregator) {
44+
_task_registry = aggregator;
45+
}
46+
47+
template <PumpControlPolicy Policy>
48+
auto run_once(Policy& policy) -> void {
49+
if (!_task_registry) {
50+
return;
51+
}
52+
53+
if (!_initialized) {
54+
_message_queue.set_ready();
55+
_initialized = true;
56+
}
57+
58+
auto message = Message(std::monostate());
59+
_message_queue.recv(&message);
60+
auto visit_helper = [this, &policy](auto& message) -> void {
61+
this->visit_message(message, policy);
62+
};
63+
std::visit(visit_helper, message);
64+
}
65+
66+
private:
67+
auto send_error_message(Error error) -> void {
68+
if (_task_registry) {
69+
auto msg = messages::ErrorMessage{.code = error};
70+
static_cast<void>(
71+
_task_registry->send_to_address(msg, Queues::HostCommsAddress));
72+
}
73+
}
74+
75+
auto send_ack_message(uint32_t response_id, Error error = Error::NO_ERROR)
76+
-> void {
77+
if (_task_registry) {
78+
auto msg = messages::AcknowledgePrevious{
79+
.responding_to_id = response_id, .with_error = error};
80+
static_cast<void>(
81+
_task_registry->send_to_address(msg, Queues::HostCommsAddress));
82+
}
83+
}
84+
85+
template <PumpControlPolicy Policy>
86+
auto visit_message(const std::monostate& m, Policy& policy) -> void {
87+
static_cast<void>(m);
88+
static_cast<void>(policy);
89+
}
90+
91+
template <PumpControlPolicy Policy>
92+
auto visit_message(const messages::SetTargetRPMMessage& m, Policy& policy) -> void {
93+
static_cast<void>(m);
94+
static_cast<void>(policy);
95+
}
96+
97+
Queue& _message_queue;
98+
Aggregator* _task_registry;
99+
bool _initialized{false};
100+
};
101+
102+
} // namespace pump_task

stm32-modules/include/vacuum-module/vacuum-module/tasks.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ struct Tasks {
1919
using UIQueue = QueueImpl<messages::UIMessage>;
2020
// Message queue for Pressure task
2121
using PressureQueue = QueueImpl<messages::PressureMessage>;
22+
// Message queue for Pump task
23+
using PumpQueue = QueueImpl<messages::PumpMessage>;
2224

2325
// Central aggregator
2426
using QueueAggregator =
25-
queue_aggregator::QueueAggregator<HostCommsQueue, SystemQueue, UIQueue, PressureQueue>;
27+
queue_aggregator::QueueAggregator<HostCommsQueue, SystemQueue, UIQueue, PressureQueue, PumpQueue>;
2628

2729
// Addresses
2830
static constexpr size_t HostCommsAddress =
@@ -33,6 +35,8 @@ struct Tasks {
3335
QueueAggregator::template get_queue_idx<UIQueue>();
3436
static constexpr size_t PressureAddress =
3537
QueueAggregator::template get_queue_idx<PressureQueue>();
38+
static constexpr size_t PumpAddress =
39+
QueueAggregator::template get_queue_idx<PumpQueue>();
3640
};
3741

3842
}; // namespace tasks

stm32-modules/vacuum-module/firmware/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ set(COMMS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/host_comms_task")
1313
set(SYSTEM_DIR "${CMAKE_CURRENT_SOURCE_DIR}/system")
1414
set(UI_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ui")
1515
set(VENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/vent")
16-
set(PUMP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/pump")
1716
set(ATMOSPHERE_PS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/atmosphere-pressure-sensor")
1817
set(VACUUM_PS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/vacuum-pressure-sensor")
1918
set(PRESSURE_CONTROL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/pressure_task")
19+
set(PUMP_CONTROL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/pump_task")
2020

2121
# Add source files that should be checked by clang-tidy here
2222
set(${TARGET_MODULE_NAME}_FW_LINTABLE_SRCS
@@ -29,11 +29,12 @@ set(${TARGET_MODULE_NAME}_FW_LINTABLE_SRCS
2929
${UI_DIR}/freertos_ui_task.cpp
3030
${UI_DIR}/ui_policy.cpp
3131
${VENT_DIR}/vent_policy.cpp
32-
${PUMP_DIR}/pump_policy.cpp
3332
${ATMOSPHERE_PS_DIR}/atmosphere_pressure_sensor_policy.cpp
3433
${VACUUM_PS_DIR}/vacuum_pressure_sensor_policy.cpp
3534
${PRESSURE_CONTROL_DIR}/freertos_pressure_task.cpp
3635
${PRESSURE_CONTROL_DIR}/pressure_policy.cpp
36+
${PUMP_CONTROL_DIR}/freertos_pump_task.cpp
37+
${PUMP_CONTROL_DIR}/pump_policy.cpp
3738
)
3839

3940
# Add source files that should NOT be checked by clang-tidy here
@@ -50,7 +51,7 @@ set(${TARGET_MODULE_NAME}_FW_NONLINTABLE_SRCS
5051
${COMMS_DIR}/usbd_desc.c
5152
${COMMS_DIR}/usb_hardware.c
5253
${VENT_DIR}/vent_hardware.c
53-
${PUMP_DIR}/pump_hardware.c
54+
${PUMP_CONTROL_DIR}/pump_hardware.c
5455
)
5556

5657
add_executable(${TARGET_MODULE_NAME}

stm32-modules/vacuum-module/firmware/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ using EntryPointUI = std::function<void(tasks::FirmwareTasks::QueueAggregator *,
2323
i2c::hardware::I2C *)>;
2424
using EntryPointPressure = std::function<void(tasks::FirmwareTasks::QueueAggregator *,
2525
i2c::hardware::I2C *)>;
26+
using EntryPointPump = std::function<void(tasks::FirmwareTasks::QueueAggregator *,
27+
i2c::hardware::I2C *)>;
2628

2729
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
2830
static auto ui_task_entry = EntryPointUI(ui_control_task::run);
@@ -32,6 +34,8 @@ static auto host_comms_entry = EntryPoint(host_comms_control_task::run);
3234
static auto system_task_entry = EntryPoint(system_control_task::run);
3335
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
3436
static auto pressure_task_entry = EntryPointPressure(pressure_control_task::run);
37+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
38+
static auto pump_task_entry = EntryPointPump(pump_control_task::run);
3539

3640
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
3741
static auto host_comms_task =
@@ -49,6 +53,10 @@ static auto system_task =
4953
static auto pressure_task =
5054
ot_utils::freertos_task::FreeRTOSTask<tasks::PRESSURE_STACK_SIZE, EntryPointPressure>(
5155
pressure_task_entry);
56+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
57+
static auto pump_task =
58+
ot_utils::freertos_task::FreeRTOSTask<tasks::PUMP_STACK_SIZE, EntryPointPump>(
59+
pump_task_entry);
5260

5361
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
5462
static auto aggregator = tasks::FirmwareTasks::QueueAggregator();
@@ -74,6 +82,7 @@ auto main() -> int {
7482
host_comms_task.start(tasks::COMMS_TASK_PRIORITY, "Comms", &aggregator);
7583
ui_task.start(tasks::UI_TASK_PRIORITY, "UI", &aggregator, &i2c2_comms);
7684
pressure_task.start(tasks::PRESSURE_TASK_PRIORITY, "Pressure", &aggregator, &i2c2_comms);
85+
pump_task.start(tasks::PUMP_TASK_PRIORITY, "Pump", &aggregator, &i2c2_comms);
7786

7887
vTaskStartScheduler();
7988
return 0;

0 commit comments

Comments
 (0)