Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3
4#include "../../CAN_IDs.h"
6#include "enum_utils.hpp"
7
11void create_left_wheel_msg(std::array<uint8_t, 5> &msg, double value) {
12 value /= WHEEL_PRECISION; // take precision off to send integer value
13 if (value < 0) value = 0;
14
15 msg[0] = LEFT_WHEEL_MSG;
16 // Copy the bytes of the double value to msg[1] to msg[4]
17 for (int i = 0; i < 4; i++)
18 msg[i + 1] = static_cast<int>(value) >> (8 * i); // shift 8(byte) to msb each time
19}
20
21inline std::array<uint8_t, 8> create_debug_message_1(const SystemData& system_data, const uint8_t state, const uint8_t state_checkup) {
22 // Ensure boolean values are normalized to 0 or 1
23 auto normalize_bool = [](bool value) -> uint8_t { return value ? 1 : 0; };
24
25 // Pack byte 5 flags with explicit normalization
26 uint8_t byte5 = (normalize_bool(system_data.failure_detection_.emergency_signal_) << 7) |
30 (normalize_bool(system_data.failure_detection_.steer_dead_) << 3) |
31 (normalize_bool(system_data.failure_detection_.pc_dead_) << 2) |
32 (normalize_bool(system_data.failure_detection_.inversor_dead_) << 1) |
34
35 // Pack byte 6 flags with explicit normalization
36 uint8_t byte6 = (normalize_bool(system_data.hardware_data_.asms_on_) << 7) |
37 (normalize_bool(system_data.failure_detection_.ts_on_) << 6) |
38 (normalize_bool(system_data.hardware_data_.bspd_sdc_open_) << 5) |
39 (state_checkup & 0x0F);
40
41 return {
42 DBG_LOG_MSG,
43 static_cast<uint8_t>((system_data.hardware_data_._hydraulic_line_pressure >> 24) & 0xFF),
44 static_cast<uint8_t>((system_data.hardware_data_._hydraulic_line_pressure >> 16) & 0xFF),
45 static_cast<uint8_t>((system_data.hardware_data_._hydraulic_line_pressure >> 8) & 0xFF),
46 static_cast<uint8_t>(system_data.hardware_data_._hydraulic_line_pressure & 0xFF),
47 byte5,
48 byte6,
49 static_cast<uint8_t>((to_underlying(system_data.mission_) & 0x0F) | ((state & 0x0F) << 4))
50 };
51}
52
53inline std::array<uint8_t, 7> create_debug_message_2(const SystemData& system_data) {
54 return {
55 DBG_LOG_MSG_2,
56 static_cast<uint8_t>((system_data.failure_detection_.dc_voltage_ >> 24) & 0xFF),
57 static_cast<uint8_t>((system_data.failure_detection_.dc_voltage_ >> 16) & 0xFF),
58 static_cast<uint8_t>((system_data.failure_detection_.dc_voltage_ >> 8) & 0xFF),
59 static_cast<uint8_t>(system_data.failure_detection_.dc_voltage_ & 0xFF),
60 static_cast<uint8_t>(system_data.hardware_data_.pneumatic_line_pressure_1_ & 0x01),
61 static_cast<uint8_t>(system_data.hardware_data_.pneumatic_line_pressure_2_ & 0x01)
62 };
63}
bool checkWithoutReset() const
Checks if the interval has passed without resetting the timer.
Definition metro.h:115
void create_left_wheel_msg(std::array< uint8_t, 5 > &msg, double value)
Function to create left wheel msg.
Definition utils.hpp:11
std::array< uint8_t, 7 > create_debug_message_2(const SystemData &system_data)
Definition utils.hpp:53
std::array< uint8_t, 8 > create_debug_message_1(const SystemData &system_data, const uint8_t state, const uint8_t state_checkup)
Definition utils.hpp:21
constexpr auto to_underlying(Enum e) noexcept
Definition enum_utils.hpp:4
SystemData system_data
Definition main.cpp:11
int _hydraulic_line_pressure
bool pneumatic_line_pressure_
bool pneumatic_line_pressure_2_
bool pneumatic_line_pressure_1_
Metro releaseEbsTimestamp
The whole model of the system: holds all the data necessary.
Mission mission_
R2DLogics r2d_logics_
FailureDetection failure_detection_
HardwareData hardware_data_
CAN_message_t msg