Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
communicator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Arduino.h>
4#include <FlexCAN_T4.h>
5
6#include <string>
7
8#include "../../CAN_IDs.h"
9#include "comm/utils.hpp"
10#include "debugUtils.hpp"
11#include "enum_utils.hpp"
12#include "model/systemData.hpp"
13#include "utils.hpp"
14#include "../utils.hpp"
15
16
21inline std::array<Code, 7> fifoCodes = {{{0, DASH_ID},
22 {1, BAMO_RESPONSE_ID},
23 {2, AS_CU_EMERGENCY_SIGNAL},
24 {3, MISSION_FINISHED},
25 {4, AS_CU_ID},
26 {5, RES_STATE},
27 {6, RES_READY}}};
28
33inline std::array<Code, 1> fifoExtendedCodes = {{
34 {7, STEERING_ID},
35}};
36
43private:
44 // Static FlexCAN_T4 object for CAN2 interface with RX and TX buffer sizes specified
45 inline static FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> can2;
46
47public:
48 // Pointer to SystemData instance for storing system-related data
49 inline static SystemData *_systemData = nullptr;
50
56 explicit Communicator(SystemData *systemdata);
57
61 void init();
62
66 static void parse_message(const CAN_message_t &msg);
67
75 template <std::size_t N>
76 static int send_message(unsigned len, const std::array<uint8_t, N> &buffer, unsigned id);
77
81 static void pc_callback(const uint8_t *buf);
82
86 static void res_state_callback(const uint8_t *buf);
87
91 static void res_ready_callback();
92
96 static void bamocar_callback(const uint8_t *buf);
97
101 static void steering_callback();
102
106 static void dash_callback(const uint8_t* buf);
107
111 static int publish_state(int state_id);
112
116 static int publish_mission(int mission_id);
120 static int publish_soc(uint8_t soc);
124 static int publish_asms_on(bool asms_on);
128 static int publish_debug_morning_log(const SystemData &system_data, uint8_t sate,
129 uint8_t state_checkup);
130
134 static int publish_rpm();
135};
136
138
140 can2.begin();
141 can2.setBaudRate(1000000);
142
143 can2.enableFIFO();
144 can2.enableFIFOInterrupt();
145
146 can2.setFIFOFilter(REJECT_ALL);
147 for (auto &fifoCode : fifoCodes) can2.setFIFOFilter(fifoCode.key, fifoCode.code, STD);
148
149 for (auto &fifoExtendedCode : fifoExtendedCodes)
150 can2.setFIFOFilter(fifoExtendedCode.key, fifoExtendedCode.code, EXT);
151
152 can2.onReceive(FIFO, parse_message);
153
154 can2.mailboxStatus();
155}
156
157inline void Communicator::res_state_callback(const uint8_t *buf) {
158 bool emg_stop1 = buf[0] & 0x01;
159 bool emg_stop2 = buf[3] >> 7 & 0x01;
160 bool go_switch = (buf[0] >> 1) & 0x01;
161 bool go_button = (buf[0] >> 2) & 0x01;
162 // DEBUG_PRINT("RES GO: " + String(go_switch) + " EMG 1: " + String(emg_stop1) + " EMG 2: " + String(emg_stop2));
163
164 if (go_button || go_switch)
166 else if (!(emg_stop1 || emg_stop2)) { // If both are false
167 DEBUG_PRINT("Received Emergency from RES");
169 }
170
172 bool signal_loss = (buf[7] >> 6) & 0x01;
173 if (!signal_loss) {
175 .reset(); // making sure we dont receive only signal loss for the defined time interval
176 // DEBUG_PRINT("SIGNAL OKAY");
177 } else {
178 // Too many will violate the disconnection time limit
179 // DEBUG_PRINT("SIGNAL LOSS");
180 }
181}
182
184 // If res sends boot message, activate it
185 std::array<uint8_t, 2> msg = {0x01, NODE_ID}; // 0x00 in byte 2 for all nodes
186
187 send_message(2, msg, RES_ACTIVATE);
188}
189
190inline void Communicator::bamocar_callback(const uint8_t *buf) {
192
193 if (buf[0] == BTB_READY) {
194 if (buf[1] == false) {
196 }
197 } else if (buf[0] == BAMOCAR_BATTERY_VOLTAGE_CODE) {
198 unsigned dc_voltage = (buf[2] << 8) | buf[1];
200
201 // Voltage hysteresis:
202 if (dc_voltage < DC_THRESHOLD) {
203 // When voltage drops/is below threshold:
204 // Reset hold timer and check if voltage has been below threshold long enough
208 }
209 } else {
210 // When voltage is above threshold:
211 // Reset drop timer and check if voltage has been above threshold long enough
215 }
216 }
217 }
218}
219
220inline void Communicator::pc_callback(const uint8_t *buf) {
221 // DEBUG_PRINT("PC alive signal received");
222 if (buf[0] == PC_ALIVE) {
224 } else if (buf[0] == MISSION_FINISHED) {
226 } else if (buf[0] == AS_CU_EMERGENCY_SIGNAL) {
227 DEBUG_PRINT("Received Emergency from AS CU");
229 }
230}
231
235
236inline void Communicator::dash_callback(const uint8_t *buf) {
237 if (buf[0] == HYDRAULIC_LINE) {
239 }
240}
241
242
243inline void Communicator::parse_message(const CAN_message_t &msg) {
244 switch (msg.id) {
245 case AS_CU_ID:
246 pc_callback(msg.buf);
247 case RES_STATE:
249 break;
250 case RES_READY:
252 break;
253 case BAMO_RESPONSE_ID:
255 break;
256 case STEERING_ID:
258 break;
259 case DASH_ID:
260 dash_callback(msg.buf);
261 break;
262 default:
263 break;
264 }
265}
266
267
268inline int Communicator::publish_state(const int state_id) {
269 const std::array<uint8_t, 2> msg = {STATE_MSG, static_cast<uint8_t>(state_id)};
270 send_message(2, msg, MASTER_ID);
271 return 0;
272}
273
274inline int Communicator::publish_mission(int mission_id) {
275 const std::array<uint8_t, 2> msg = {MISSION_MSG, static_cast<uint8_t>(mission_id)};
276
277 send_message(2, msg, MASTER_ID);
278 return 0;
279}
281 uint8_t state_checkup) {
282 send_message(8, create_debug_message_1(system_data, state, state_checkup), MASTER_ID);
284 return 0;
285}
286
287inline int Communicator::publish_soc(uint8_t soc) {
288 const std::array<uint8_t, 2> msg = {SOC_MSG, soc};
289 send_message(2, msg, MASTER_ID);
290 return 0;
291}
292
293inline int Communicator::publish_asms_on(bool asms_on) {
294 const std::array<uint8_t, 2> msg = {ASMS, asms_on};
295 send_message(2, msg, MASTER_ID);
296 return 0;
297}
298
300 std::array<uint8_t, 5> rl_rpm_msg = {0};
301 std::array<uint8_t, 5> rr_rpm_msg = {0};
302
303 char rr_rpm_byte[4];
304 char rl_rpm_byte[4];
307
308 rl_rpm_msg[0] = LEFT_WHEEL_CODE;
309 for (int i = 0; i < 4; i++) {
310 rl_rpm_msg[i + 1] = rl_rpm_byte[i];
311 }
312
313 rr_rpm_msg[0] = RIGHT_WHEEL_CODE;
314 for (int i = 0; i < 4; i++) {
315 rr_rpm_msg[i + 1] = rr_rpm_byte[i];
316 }
317 send_message(5, rl_rpm_msg, MASTER_ID);
318 send_message(5, rr_rpm_msg, MASTER_ID);
319
320 return 0;
321}
322
323template <std::size_t N>
324inline int Communicator::send_message(const unsigned len, const std::array<uint8_t, N> &buffer,
325 const unsigned id) {
326 CAN_message_t can_message;
327 can_message.id = id;
328 can_message.len = len;
329 for (unsigned i = 0; i < len; i++) {
330 can_message.buf[i] = buffer[i];
331 }
332 can2.write(can_message);
333
334 return 0;
335}
Class that contains definitions of typical messages to send via CAN It serves only as an example of t...
static void steering_callback()
Callback for steering actuator information.
static int publish_mission(int mission_id)
Publish AS Mission to CAN.
static int publish_rpm()
Publish rl wheel rpm to CAN.
void init()
Initializes the CAN bus.
static int publish_debug_morning_log(const SystemData &system_data, uint8_t sate, uint8_t state_checkup)
Publish AS Mission to CAN.
Communicator(SystemData *systemdata)
Constructor for the Communicator class Initializes the Communicator with the given system data instan...
static void parse_message(const CAN_message_t &msg)
Parses the message received from the CAN bus.
static void res_state_callback(const uint8_t *buf)
Callback RES default callback.
static void dash_callback(const uint8_t *buf)
Callback for dashboard.
static int send_message(unsigned len, const std::array< uint8_t, N > &buffer, unsigned id)
Sends a message to the CAN bus.
static void pc_callback(const uint8_t *buf)
Callback for message from AS CU.
static int publish_soc(uint8_t soc)
Publish SOC to CAN.
static int publish_asms_on(bool asms_on)
Publish ASMS state to CAN.
static void bamocar_callback(const uint8_t *buf)
Callback from inversor, for alive signal and data.
static SystemData * _systemData
static void res_ready_callback()
Callback for RES activation.
static int publish_state(int state_id)
Publish AS State to CAN.
void reset()
Resets the timer to the current time.
Definition metro.h:124
bool checkWithoutReset() const
Checks if the interval has passed without resetting the timer.
Definition metro.h:115
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
std::array< Code, 1 > fifoExtendedCodes
Array of extended CAN message codes to be used for FIFO filtering Contains the key and corresponding ...
std::array< Code, 7 > fifoCodes
Array of standard CAN message codes to be used for FIFO filtering Each Code struct contains a key and...
#define DEBUG_PRINT(str)
SystemData system_data
Definition main.cpp:11
int hydraulic_line_front_pressure
double _right_wheel_rpm
double _left_wheel_rpm
void process_go_signal()
Processes the go signal.
The whole model of the system: holds all the data necessary.
R2DLogics r2d_logics_
FailureDetection failure_detection_
HardwareData hardware_data_
bool mission_finished_
CAN_message_t msg
void rpm_2_byte(const float rr_rpm, char *rr_rpm_byte)
Definition utils.hpp:32