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"
5#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_signals_msg_1(const SystemData& system_data,
22 const uint8_t state,
23 const uint8_t state_checkup) {
24 // Ensure boolean values are normalized to 0 or 1
25 auto normalize_bool = [](bool value) -> uint8_t { return value ? 1 : 0; };
26 // Pack byte 0 with explicit normalization
27 uint8_t byte0 = (normalize_bool(true) << 7) | // placeholder
28 (normalize_bool(system_data.hardware_data_.asms_on_) << 6) |
29 (normalize_bool(system_data.hardware_data_.asats_pressed_) << 5) |
30 (normalize_bool(system_data.hardware_data_.ats_pressed_) << 4) |
31 (normalize_bool(system_data.hardware_data_.tsms_sdc_closed_) << 3) |
32 (normalize_bool(system_data.hardware_data_.master_sdc_closed_) << 2) |
33 (normalize_bool(system_data.failure_detection_.ts_on_) << 1) |
34 normalize_bool(system_data.hardware_data_.wd_ready_);
35
36 // Pack byte 5 flags with explicit normalization
37 uint8_t byte1 =
38 (normalize_bool(system_data.failure_detection_.emergency_signal_) << 7) |
39 (normalize_bool(system_data.failure_detection_.bms_dead_) << 6) |
42 (normalize_bool(system_data.failure_detection_.steer_dead_) << 3) |
43 (normalize_bool(system_data.failure_detection_.pc_dead_) << 2) |
44 (normalize_bool(system_data.failure_detection_.inversor_dead_) << 1) |
46
47 uint8_t byte2 = (state_checkup & 0x0F) |
51
52 return {
53 byte0,
54 byte1,
55 byte2,
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>((to_underlying(system_data.mission_) & 0x0F) | ((state & 0x0F) << 4))};
61}
62
63inline std::array<uint8_t, 8> create_hydraulic_presures_msg(const SystemData& system_data) {
65 float hydraulic_front_bar =
66 (adc_front - HYDRAULIC_PRESSURE_ADC_MIN) *
68
70 float hydraulic_rear_bar =
71 (adc_rear - HYDRAULIC_PRESSURE_ADC_MIN) *
73
74 union FloatToBytes {
75 float f;
76 uint8_t bytes[4];
77 };
78
79 FloatToBytes front_converter{hydraulic_front_bar};
80 FloatToBytes rear_converter{hydraulic_rear_bar};
81
82 std::array<uint8_t, 8> data;
83 data[0] = front_converter.bytes[0];
84 data[1] = front_converter.bytes[1];
85 data[2] = front_converter.bytes[2];
86 data[3] = front_converter.bytes[3];
87
88 data[4] = rear_converter.bytes[0];
89 data[5] = rear_converter.bytes[1];
90 data[6] = rear_converter.bytes[2];
91 data[7] = rear_converter.bytes[3];
92
93 return data;
94}
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, 8 > create_signals_msg_1(const SystemData &system_data, const uint8_t state, const uint8_t state_checkup)
Definition utils.hpp:21
std::array< uint8_t, 8 > create_hydraulic_presures_msg(const SystemData &system_data)
Definition utils.hpp:63
constexpr auto to_underlying(Enum e) noexcept
Definition enum_utils.hpp:4
constexpr int HYDRAULIC_PRESSURE_MAX_BAR
constexpr int HYDRAULIC_PRESSURE_SPAN_ADC
constexpr int HYDRAULIC_PRESSURE_ADC_MIN
SystemData system_data
Definition main.cpp:11
volatile bool emergency_signal_
volatile unsigned dc_voltage_
int hydraulic_line_front_pressure
int _hydraulic_line_pressure
bool master_sdc_closed_
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