Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hepa-uv): Add CAN messages to control the Hepa/UV filter. #753

Merged
merged 7 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cpp-utils/include/ot_utils/freertos/freertos_timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
#include "task.h"
#include "timers.h"

constexpr auto ROUNDED_DIV(uint32_t A, uint32_t B) noexcept -> uint32_t {
return (((A) + ((B) / 2)) / (B));
}

constexpr auto TICKS_TO_MS(uint32_t ticks) noexcept -> uint32_t {
return (uint32_t)ROUNDED_DIV((ticks) * 1000, (uint32_t)configTICK_RATE_HZ);
}

namespace ot_utils {
namespace freertos_timer {

Expand Down Expand Up @@ -54,6 +62,14 @@ class FreeRTOSTimer {
}
}

auto get_remaining_time() -> uint32_t {
/*
The time in ms remaining before this timer expires and the callback is executed.
*/
if (!is_running()) { return uint32_t(0); }
return TICKS_TO_MS(xTimerGetExpiryTime( timer ) - xTaskGetTickCount());
}

void start() { xTimerStart(timer, 1); }

void stop() { xTimerStop(timer, 1); }
Expand Down
19 changes: 14 additions & 5 deletions hepa-uv/core/can_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include "eeprom/core/message_handler.hpp"
#include "hepa-uv/core/can_task.hpp"
#include "hepa-uv/core/hepa_task.hpp"
#include "hepa-uv/core/hepauv_info.hpp"
#include "hepa-uv/core/message_handler.hpp"

using namespace can::dispatch;

Expand All @@ -15,6 +17,9 @@ auto can_sender_queue = freertos_message_queue::FreeRTOSMessageQueue<
static auto hepauv_info_handler =
hepauv_info::HepaUVInfoMessageHandler{main_queues, main_queues};

static auto hepa_fan_handler = hepa::message_handler::HepaHandler{main_queues};
static auto uv_light_handler = uv::message_handler::UVHandler{main_queues};

/** Handler of system messages. */
static auto system_message_handler =
can::message_handlers::system::SystemMessageHandler{
Expand All @@ -25,6 +30,8 @@ static auto system_message_handler =
std::cend(version_get()->sha)),
revision_get()->primary,
revision_get()->secondary};
static auto system_dispatch_target =
can_task::SystemDispatchTarget{system_message_handler};

/** Handler for eeprom messages.*/
static auto eeprom_message_handler =
Expand All @@ -35,12 +42,14 @@ static auto eeprom_dispatch_target =
can::messages::ReadFromEEPromRequest>{
eeprom_message_handler};

static auto system_dispatch_target =
can_task::SystemDispatchTarget{system_message_handler};

static auto hepauv_info_dispatch_target =
can_task::HepaUVInfoDispatchTarget{hepauv_info_handler};

static auto hepa_dispatch_target =
can_task::HepaDispatchTarget{hepa_fan_handler};

static auto uv_dispatch_target = can_task::UVDispatchTarget{uv_light_handler};

struct CheckForNodeId {
can::ids::NodeId node_id;
auto operator()(uint32_t arbitration_id) const {
Expand All @@ -60,8 +69,8 @@ static auto main_dispatcher = can::dispatch::Dispatcher(
return ((node_id == can::ids::NodeId::broadcast) ||
(node_id == can::ids::NodeId::hepa_uv));
},
system_dispatch_target, hepauv_info_dispatch_target,
eeprom_dispatch_target);
system_dispatch_target, hepauv_info_dispatch_target, eeprom_dispatch_target,
hepa_dispatch_target, uv_dispatch_target);

auto static reader_message_buffer =
freertos_message_buffer::FreeRTOSMessageBuffer<1024>{};
Expand Down
4 changes: 2 additions & 2 deletions hepa-uv/core/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ void hepauv_tasks::start_tasks(
eeprom_hw_iface);

auto& hepa_task = hepa_task_builder.start(5, "hepa_fan", gpio_drive_pins,
hepa_hardware, queues);
hepa_hardware, queues, queues);
auto& uv_task =
uv_task_builder.start(5, "uv_ballast", gpio_drive_pins, queues);
uv_task_builder.start(5, "uv_ballast", gpio_drive_pins, queues, queues);
auto& led_control_task =
led_control_task_builder.start(5, "push_button_leds", led_hardware);

Expand Down
6 changes: 2 additions & 4 deletions hepa-uv/firmware/main_rev1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
case UV_NO_MCU_PIN:
if (hepauv_queues.hepa_queue != nullptr) {
static_cast<void>(hepauv_queues.hepa_queue->try_write_isr(
interrupt_task_messages::GPIOInterruptChanged{
.pin = GPIO_Pin}));
GPIOInterruptChanged{.pin = GPIO_Pin}));
}
if (hepauv_queues.uv_queue != nullptr) {
static_cast<void>(hepauv_queues.uv_queue->try_write_isr(
interrupt_task_messages::GPIOInterruptChanged{
.pin = GPIO_Pin}));
GPIOInterruptChanged{.pin = GPIO_Pin}));
}
break;
default:
Expand Down
6 changes: 6 additions & 0 deletions include/bootloader/core/ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ typedef enum {
can_messageid_peripheral_status_request = 0x8c,
can_messageid_peripheral_status_response = 0x8d,
can_messageid_baseline_sensor_response = 0x8e,
can_messageid_set_hepa_fan_state_request = 0x90,
can_messageid_get_hepa_fan_state_request = 0x91,
can_messageid_get_hepa_fan_state_response = 0x92,
can_messageid_set_hepa_uv_state_request = 0x93,
can_messageid_get_hepa_uv_state_request = 0x94,
can_messageid_get_hepa_uv_state_response = 0x95,
} CANMessageId;

/** Can bus arbitration id node id. */
Expand Down
6 changes: 6 additions & 0 deletions include/can/core/ids.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ enum class MessageId {
peripheral_status_request = 0x8c,
peripheral_status_response = 0x8d,
baseline_sensor_response = 0x8e,
set_hepa_fan_state_request = 0x90,
get_hepa_fan_state_request = 0x91,
get_hepa_fan_state_response = 0x92,
set_hepa_uv_state_request = 0x93,
get_hepa_uv_state_request = 0x94,
get_hepa_uv_state_response = 0x95,
};

/** Can bus arbitration id node id. */
Expand Down
94 changes: 93 additions & 1 deletion include/can/core/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,97 @@ struct HepaUVInfoResponse : BaseMessage<MessageId::hepauv_info_response> {
auto operator==(const HepaUVInfoResponse& other) const -> bool = default;
};

struct SetHepaFanStateRequest
: BaseMessage<MessageId::set_hepa_fan_state_request> {
uint32_t message_index;
uint32_t duty_cycle;
uint8_t fan_on;

template <bit_utils::ByteIterator Input, typename Limit>
static auto parse(Input body, Limit limit) -> SetHepaFanStateRequest {
uint8_t fan_on = 0;
uint32_t duty_cycle = 0;
uint32_t msg_ind = 0;

body = bit_utils::bytes_to_int(body, limit, msg_ind);
body = bit_utils::bytes_to_int(body, limit, duty_cycle);
body = bit_utils::bytes_to_int(body, limit, fan_on);
return SetHepaFanStateRequest{.message_index = msg_ind,
.duty_cycle = duty_cycle,
.fan_on = fan_on};
}

auto operator==(const SetHepaFanStateRequest& other) const
-> bool = default;
};

using GetHepaFanStateRequest = Empty<MessageId::get_hepa_fan_state_request>;

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
struct GetHepaFanStateResponse
: BaseMessage<MessageId::get_hepa_fan_state_response> {
uint32_t message_index;
uint32_t duty_cycle;
uint8_t fan_on;

template <bit_utils::ByteIterator Output, typename Limit>
auto serialize(Output body, Limit limit) const -> uint8_t {
auto iter = bit_utils::int_to_bytes(message_index, body, limit);
iter = bit_utils::int_to_bytes(duty_cycle, iter, limit);
iter = bit_utils::int_to_bytes(fan_on, iter, limit);
return iter - body;
}

auto operator==(const GetHepaFanStateResponse& other) const
-> bool = default;
};

struct SetHepaUVStateRequest
: BaseMessage<MessageId::set_hepa_uv_state_request> {
uint32_t message_index;
uint32_t timeout_s;
uint8_t uv_light_on;

template <bit_utils::ByteIterator Input, typename Limit>
static auto parse(Input body, Limit limit) -> SetHepaUVStateRequest {
uint8_t uv_light_on = 0;
uint32_t timeout_s = 0;
uint32_t msg_ind = 0;

body = bit_utils::bytes_to_int(body, limit, msg_ind);
body = bit_utils::bytes_to_int(body, limit, timeout_s);
body = bit_utils::bytes_to_int(body, limit, uv_light_on);
return SetHepaUVStateRequest{.message_index = msg_ind,
.timeout_s = timeout_s,
.uv_light_on = uv_light_on};
}

auto operator==(const SetHepaUVStateRequest& other) const -> bool = default;
};

using GetHepaUVStateRequest = Empty<MessageId::get_hepa_uv_state_request>;

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
struct GetHepaUVStateResponse
: BaseMessage<MessageId::get_hepa_uv_state_response> {
uint32_t message_index;
uint32_t timeout_s;
uint8_t uv_light_on;
uint32_t remaining_time_s;

template <bit_utils::ByteIterator Output, typename Limit>
auto serialize(Output body, Limit limit) const -> uint8_t {
auto iter = bit_utils::int_to_bytes(message_index, body, limit);
iter = bit_utils::int_to_bytes(timeout_s, iter, limit);
iter = bit_utils::int_to_bytes(uv_light_on, iter, limit);
iter = bit_utils::int_to_bytes(remaining_time_s, iter, limit);
return iter - body;
}

auto operator==(const GetHepaUVStateResponse& other) const
-> bool = default;
};

/**
* A variant of all message types we might send..
*/
Expand All @@ -1597,6 +1688,7 @@ using ResponseMessageType = std::variant<
PeripheralStatusResponse, BrushedMotorConfResponse,
UpdateMotorPositionEstimationResponse, BaselineSensorResponse,
PushTipPresenceNotification, GetMotorUsageResponse, GripperJawStateResponse,
GripperJawHoldoffResponse, HepaUVInfoResponse>;
GripperJawHoldoffResponse, HepaUVInfoResponse, GetHepaFanStateResponse,
GetHepaUVStateResponse>;

} // namespace can::messages
12 changes: 12 additions & 0 deletions include/hepa-uv/core/can_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "can/core/freertos_can_dispatch.hpp"
#include "can/core/message_handlers/system.hpp"
#include "common/core/freertos_message_queue.hpp"
#include "hepa-uv/core/hepa_task.hpp"
#include "hepa-uv/core/hepauv_info.hpp"
#include "hepa-uv/core/message_handler.hpp"
#include "hepa-uv/core/tasks.hpp"

namespace can_task {
Expand All @@ -15,11 +17,21 @@ using SystemDispatchTarget = can::dispatch::DispatchParseTarget<
hepauv_tasks::QueueClient>,
can::messages::DeviceInfoRequest, can::messages::InitiateFirmwareUpdate,
can::messages::FirmwareUpdateStatusRequest, can::messages::TaskInfoRequest>;

using HepaUVInfoDispatchTarget = can::dispatch::DispatchParseTarget<
hepauv_info::HepaUVInfoMessageHandler<hepauv_tasks::QueueClient,
hepauv_tasks::QueueClient>,
can::messages::InstrumentInfoRequest, can::messages::SetSerialNumber>;

using HepaDispatchTarget = can::dispatch::DispatchParseTarget<
hepa::message_handler::HepaHandler<hepauv_tasks::QueueClient>,
can::messages::SetHepaFanStateRequest,
can::messages::GetHepaFanStateRequest>;

using UVDispatchTarget = can::dispatch::DispatchParseTarget<
uv::message_handler::UVHandler<hepauv_tasks::QueueClient>,
can::messages::SetHepaUVStateRequest, can::messages::GetHepaUVStateRequest>;

auto constexpr reader_message_buffer_size = 1024;

struct CanMessageReaderTask {
Expand Down
Loading
Loading