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 <FlexCAN_T4.h>
4#include <Arduino.h>
5#include <string>
6
9#include "comm/utils.hpp"
10#include "debugUtils.hpp"
11
16inline Code fifoCodes[] = {
17 {0, C1_ID},
21 {4, AS_CU_ID},
22 {5, RES_STATE},
23 {6, RES_READY}
24};
25
31 {7, STEERING_ID},
32};
33
34
41private:
42 // Static FlexCAN_T4 object for CAN2 interface with RX and TX buffer sizes specified
43 inline static FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can2;
44
45public:
46 // Pointer to SystemData instance for storing system-related data
47 inline static SystemData *_systemData = nullptr;
48
54 Communicator(SystemData* systemdata);
55
59 void init();
60
64 static void parse_message(const CAN_message_t& msg);
65
73 static int send_message(unsigned len, const unsigned char* buffer, unsigned id);
74
75
79 static void pcCallback(const uint8_t *buf);
80
84 static void c1Callback(const uint8_t *buf);
85
89 static void resStateCallback(const uint8_t *buf);
90
94 static void resReadyCallback();
95
99 static void bamocarCallback(const uint8_t *buf);
100
104 static void steeringCallback();
105
106
110 static int publish_state(int state_id);
111
115 static int publish_mission(int mission_id);
116
120 static int publish_left_wheel_rpm(double value);
121};
122
126
128
129 // Library initialization
130 can2.begin();
131 can2.setBaudRate(500000);
132
133 // Enable FIFO
134 can2.enableFIFO();
135 can2.enableFIFOInterrupt();
136
137 // Set filters
138 can2.setFIFOFilter(REJECT_ALL);
139 for (auto &fifoCode: fifoCodes)
140 can2.setFIFOFilter(fifoCode.key, fifoCode.code, STD);
141
142 // Set filters for extended
143 for (auto &fifoExtendedCode: fifoExtendedCodes)
144 can2.setFIFOFilter(fifoExtendedCode.key, fifoExtendedCode.code, EXT);
145
146 // Set callback
147 can2.onReceive(FIFO, parse_message);
148
149 DEBUG_PRINT("CAN2 started");
150 can2.mailboxStatus(); // Prints CAN mailbox info
151}
152
153inline void Communicator::c1Callback(const uint8_t *buf) {
154 if (buf[0] == HYDRAULIC_LINE) {
155 _systemData->sensors._hydraulic_line_pressure = (buf[2] << 8) | buf[1];
156 // DEBUG_PRINT_VAR(_systemData->sensors._hydraulic_line_pressure);
157 } else if (buf[0] == RIGHT_WHEEL_CODE) {
158 double right_wheel_rpm = (buf[4] << 24) | (buf[3] << 16) | (buf[2] << 8) | buf[1];
159 right_wheel_rpm *= WHEEL_PRECISION; // convert back adding decimal part
160 _systemData->sensors._right_wheel_rpm = right_wheel_rpm;
161 // DEBUG_PRINT_VAR(_systemData->sensors._right_wheel_rpm);
162 } else if (buf[0] == LEFT_WHEEL_CODE) {
163 double left_wheel_rpm = (buf[4] << 24) | (buf[3] << 16) | (buf[2] << 8) | buf[1];
164 left_wheel_rpm *= WHEEL_PRECISION; // convert back adding decimal part
165 _systemData->sensors._left_wheel_rpm = left_wheel_rpm;
166 // DEBUG_PRINT_VAR(_systemData->sensors._left_wheel_rpm);
167 }
168}
169
170inline void Communicator::resStateCallback(const uint8_t *buf) {
171 bool emg_stop1 = buf[0] & 0x01;
172 bool emg_stop2 = buf[3] >> 7 & 0x01;
173 bool go_switch = (buf[0] >> 1) & 0x01;
174 bool go_button = (buf[0] >> 2) & 0x01;
175
176 // DEBUG_PRINT("Received message from RES");
177
178 // DEBUG_PRINT_VAR(emg_stop1);
179 // DEBUG_PRINT_VAR(emg_stop2);
180 // DEBUG_PRINT_VAR(go_switch);
181 // DEBUG_PRINT_VAR(go_button);
182
183 if (go_button || go_switch)
185 else if (!emg_stop1 && !emg_stop2) {
186 DEBUG_PRINT("RES Emergency Signal");
188 }
189
191 // DEBUG_PRINT_VAR(_systemData->failureDetection.radio_quality);
192 bool signal_loss = (buf[7] >> 6) & 0x01;
193 if (!signal_loss) {
195 } else {
196 // Too many will violate the disconnection time limit
197 DEBUG_PRINT("SIGNAL LOSS");
198 }
199
200 // DEBUG_PRINT_VAR(_systemData->failureDetection.emergencySignal);
201 // DEBUG_PRINT_VAR(_systemData->r2dLogics.r2d);
202 // DEBUG_PRINT_VAR(_systemData->failureDetection.radio_quality);
203}
204
206 // If res sends boot message, activate it
207 // DEBUG_PRINT("Received RES Ready");
208 unsigned id = RES_ACTIVATE;
209 uint8_t msg[] = {0x01, NODE_ID}; // 0x00 in byte 2 for all nodes
210
211 send_message(2, msg, id);
212}
213
214inline void Communicator::bamocarCallback(const uint8_t *buf) {
216 // DEBUG_PRINT("Received Bamocar Alive");
217
218 if (buf[0] == BTB_READY) {
219 if (buf[1] == false) {
221 }
222
223 } else if (buf[0] == VDC_BUS) {
224 int dc_voltage = (buf[2] << 8) | buf[1];
225
226 // DEBUG_PRINT_VAR(dc_voltage);
227
228 if (dc_voltage < DC_THRESHOLD) {
230 } else {
232 }
233 }
234 // DEBUG_PRINT_VAR(_systemData->failureDetection.ts_on);
235
236}
237
238inline void Communicator::pcCallback(const uint8_t *buf) {
239 if (buf[0] == PC_ALIVE) {
241 // DEBUG_PRINT("Received AS CU Alive");
242 } else if (buf[0] == MISSION_FINISHED) {
244 // DEBUG_PRINT_VAR(_systemData->missionFinished);
245 } else if (buf[0] == AS_CU_EMERGENCY_SIGNAL) {
248 }
249
250}
251
254 // DEBUG_PRINT("Received Steering Alive");
255}
256
257inline void Communicator::parse_message(const CAN_message_t& msg) {
258 switch(msg.id) {
259 case AS_CU_ID:
260 pcCallback(msg.buf);
261 case RES_STATE:
263 break;
264 case RES_READY:
266 break;
267 case C1_ID:
268 c1Callback(msg.buf); // rwheel, lwheel and hydraulic line
269 break;
270 case BAMO_RESPONSE_ID:
271 bamocarCallback(msg.buf);
272 break;
273 case STEERING_ID:
275 break;
276 default:
277 break;
278 }
279}
280
281inline int Communicator::publish_state(const int state_id) {
282 const uint8_t msg[] = {STATE_MSG, static_cast<uint8_t>(state_id)};
283
284 // DEBUG_PRINT_VAR(state_id);
286 return 0;
287}
288
289inline int Communicator::publish_mission(int mission_id) {
290 const uint8_t msg[] = {MISSION_MSG, static_cast<uint8_t>(mission_id)};
291
293 return 0;
294}
295
296inline int Communicator::publish_left_wheel_rpm(double value) {
297 uint8_t msg[5];
299
301 return 0;
302}
303
304inline int Communicator::send_message(const unsigned len, const unsigned char* buffer, 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 c1Callback(const uint8_t *buf)
Callback for data from C1 Teensy.
static int publish_mission(int mission_id)
Publish AS Mission to CAN.
void init()
Initializes the CAN bus.
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 resStateCallback(const uint8_t *buf)
Callback RES default callback.
static int send_message(unsigned len, const unsigned char *buffer, unsigned id)
Sends a message to the CAN bus.
static void resReadyCallback()
Callback for RES activation.
static void pcCallback(const uint8_t *buf)
Callback for message from AS CU.
static void bamocarCallback(const uint8_t *buf)
Callback from inversor, for alive signal and data.
static void steeringCallback()
Callback for steering actuator information.
static SystemData * _systemData
static int publish_left_wheel_rpm(double value)
Publish rl wheel rpm to CAN.
static int publish_state(int state_id)
Publish AS State to CAN.
void reset()
Resets the timer to the current time.
Definition metro.h:125
Code fifoExtendedCodes[]
Array of extended CAN message codes to be used for FIFO filtering Contains the key and corresponding ...
Code fifoCodes[]
Array of standard CAN message codes to be used for FIFO filtering Each Code struct contains a key and...
constexpr auto RIGHT_WHEEL_CODE
constexpr auto AS_CU_EMERGENCY_SIGNAL
constexpr auto RES_ACTIVATE
constexpr auto STEERING_ID
constexpr auto RES_READY
constexpr auto RES_STATE
constexpr auto MISSION_FINISHED
constexpr auto AS_CU_ID
constexpr auto MISSION_MSG
constexpr auto NODE_ID
constexpr auto HYDRAULIC_LINE
constexpr auto LEFT_WHEEL_CODE
constexpr auto STATE_MSG
constexpr auto VDC_BUS
constexpr auto MASTER_ID
constexpr auto BTB_READY
constexpr auto DC_THRESHOLD
constexpr auto BAMO_RESPONSE_ID
constexpr auto PC_ALIVE
constexpr auto C1_ID
constexpr auto WHEEL_PRECISION
#define DEBUG_PRINT(str)
#define DEBUG_PRINT_VAR(var)
SystemData systemData
Definition main.cpp:9
bool processGoSignal()
Processes the go signal.
int _hydraulic_line_pressure
Definition sensors.hpp:9
double _right_wheel_rpm
Definition sensors.hpp:7
double _left_wheel_rpm
Definition sensors.hpp:8
The whole model of the system: holds all the data necessary.
Sensors sensors
bool missionFinished
FailureDetection failureDetection
R2DLogics r2dLogics
CAN_message_t msg
void create_left_wheel_msg(uint8_t *msg, double value)
Function to create left wheel msg.
Definition utils.hpp:8