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
20inline std::array<Code, 7> fifoCodes = {{{0, DASH_ID},
21 {1, BAMO_RESPONSE_ID},
22 {2, AS_CU_EMERGENCY_SIGNAL},
23 {3, MISSION_FINISHED},
24 {4, AS_CU_ID},
25 {5, RES_STATE},
26 {6, RES_READY}}};
27
32inline std::array<Code, 1> fifoExtendedCodes = {{
33 {7, STEERING_ID},
34}};
35
42private:
43 // Static FlexCAN_T4 object for CAN2 interface with RX and TX buffer sizes specified
44 inline static FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> can2;
45
46public:
47 // Pointer to SystemData instance for storing system-related data
48 inline static SystemData *_systemData = nullptr;
49
55 explicit Communicator(SystemData *systemdata);
56
60 void init();
61
65 static void parse_message(const CAN_message_t &msg);
66
74 template <std::size_t N>
75 static int send_message(unsigned len, const std::array<uint8_t, N> &buffer, unsigned id);
76
80 static void pc_callback(const uint8_t *buf);
81
85 static void res_state_callback(const uint8_t *buf);
86
90 static void res_ready_callback();
91
95 static void bamocar_callback(const uint8_t *buf);
96
100 static void steering_callback();
101
105 static int publish_state(int state_id);
106
110 static int publish_mission(int mission_id);
114 static int publish_soc(uint8_t soc);
118 static int publish_asms_on(bool asms_on);
122 static int publish_debug_morning_log(const SystemData &system_data, uint8_t sate,
123 uint8_t state_checkup);
124
128 static int publish_rpm();
129};
130
132
134 can2.begin();
135 can2.setBaudRate(500000);
136
137 can2.enableFIFO();
138 can2.enableFIFOInterrupt();
139
140 can2.setFIFOFilter(REJECT_ALL);
141 for (auto &fifoCode : fifoCodes) can2.setFIFOFilter(fifoCode.key, fifoCode.code, STD);
142
143 for (auto &fifoExtendedCode : fifoExtendedCodes)
144 can2.setFIFOFilter(fifoExtendedCode.key, fifoExtendedCode.code, EXT);
145
146 can2.onReceive(FIFO, parse_message);
147
148 can2.mailboxStatus();
149}
150
151inline void Communicator::res_state_callback(const uint8_t *buf) {
152 bool emg_stop1 = buf[0] & 0x01;
153 bool emg_stop2 = buf[3] >> 7 & 0x01;
154 bool go_switch = (buf[0] >> 1) & 0x01;
155 bool go_button = (buf[0] >> 2) & 0x01;
156
157 if (go_button || go_switch)
159 else if (!(emg_stop1 || emg_stop2)) { // If both are false
161 }
162
164 bool signal_loss = (buf[7] >> 6) & 0x01;
165 if (!signal_loss) {
167 .reset(); // making sure we dont receive only signal loss for the defined time interval
168 // DEBUG_PRINT("SIGNAL OKAY");
169 } else {
170 // Too many will violate the disconnection time limit
171 // DEBUG_PRINT("SIGNAL LOSS");
172 }
173}
174
176 // If res sends boot message, activate it
177 std::array<uint8_t, 2> msg = {0x01, NODE_ID}; // 0x00 in byte 2 for all nodes
178
179 send_message(2, msg, RES_ACTIVATE);
180}
181
182inline void Communicator::bamocar_callback(const uint8_t *buf) {
184
185 if (buf[0] == BTB_READY) {
186 if (buf[1] == false) {
188 }
189 } else if (buf[0] == BAMOCAR_BATTERY_VOLTAGE_CODE) {
190 unsigned dc_voltage = (buf[2] << 8) | buf[1];
192
193 // Voltage hysteresis:
194 if (dc_voltage < DC_THRESHOLD) {
195 // When voltage drops/is below threshold:
196 // Reset hold timer and check if voltage has been below threshold long enough
200 }
201 } else {
202 // When voltage is above threshold:
203 // Reset drop timer and check if voltage has been above threshold long enough
207 }
208 }
209 }
210}
211
212inline void Communicator::pc_callback(const uint8_t *buf) {
213 if (buf[0] == PC_ALIVE) {
215 } else if (buf[0] == MISSION_FINISHED) {
217 } else if (buf[0] == AS_CU_EMERGENCY_SIGNAL) {
219 }
220}
221
225
226inline void Communicator::parse_message(const CAN_message_t &msg) {
227 switch (msg.id) {
228 case AS_CU_ID:
229 pc_callback(msg.buf);
230 case RES_STATE:
232 break;
233 case RES_READY:
235 break;
236 case BAMO_RESPONSE_ID:
238 break;
239 case STEERING_ID:
241 break;
242 default:
243 break;
244 }
245}
246
247inline int Communicator::publish_state(const int state_id) {
248 const std::array<uint8_t, 2> msg = {STATE_MSG, static_cast<uint8_t>(state_id)};
249 send_message(2, msg, MASTER_ID);
250 return 0;
251}
252
253inline int Communicator::publish_mission(int mission_id) {
254 const std::array<uint8_t, 2> msg = {MISSION_MSG, static_cast<uint8_t>(mission_id)};
255
256 send_message(2, msg, MASTER_ID);
257 return 0;
258}
260 uint8_t state_checkup) {
261 send_message(8, create_debug_message_1(system_data, state, state_checkup), MASTER_ID);
263 return 0;
264}
265
266inline int Communicator::publish_soc(uint8_t soc) {
267 const std::array<uint8_t, 2> msg = {SOC_MSG, soc};
268 send_message(2, msg, MASTER_ID);
269 return 0;
270}
271
272inline int Communicator::publish_asms_on(bool asms_on) {
273 const std::array<uint8_t, 2> msg = {ASMS_ON, asms_on};
274 send_message(2, msg, MASTER_ID);
275 return 0;
276}
277
279 std::array<uint8_t, 5> rl_rpm_msg = {0};
280 std::array<uint8_t, 5> rr_rpm_msg = {0};
281
282 char rr_rpm_byte[4];
283 char rl_rpm_byte[4];
286
287 rl_rpm_msg[0] = LEFT_WHEEL_CODE;
288 for (int i = 0; i < 4; i++) {
289 rl_rpm_msg[i + 1] = rl_rpm_byte[i];
290 }
291
292 rr_rpm_msg[0] = RIGHT_WHEEL_CODE;
293 for (int i = 0; i < 4; i++) {
294 rr_rpm_msg[i + 1] = rr_rpm_byte[i];
295 }
296 send_message(5, rl_rpm_msg, MASTER_ID);
297 send_message(5, rr_rpm_msg, MASTER_ID);
298
299 return 0;
300}
301
302template <std::size_t N>
303inline int Communicator::send_message(const unsigned len, const std::array<uint8_t, N> &buffer,
304 const unsigned id) {
305 CAN_message_t can_message;
306 can_message.id = id;
307 can_message.len = len;
308 for (unsigned i = 0; i < len; i++) {
309 can_message.buf[i] = buffer[i];
310 }
311 can2.write(can_message);
312
313 return 0;
314}
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 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, 8 > create_debug_message_1(const SystemData &system_data, uint8_t state, uint8_t state_checkup)
Definition utils.hpp:21
std::array< uint8_t, 7 > create_debug_message_2(const SystemData &system_data)
Definition utils.hpp:44
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...
SystemData system_data
Definition main.cpp:11
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