Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
Packets.hpp
Go to the documentation of this file.
1 #ifndef PACKET_H
2 #define PACKET_H
3 
4 #include <stdint.h>
5 #include <stddef.h> /* offsetof macro */
6 #include <stdbool.h>
7 /* for QUADRATURE_COUNTER_COUNT */
8 #include "quadrature_counter.h"
10 
11 typedef uint8_t PacketId_t;
12 #define PACKET_ID_COMPUTER (0U)
13 #define PACKET_ID_SETTINGS (1U)
14 
15 typedef struct __attribute__((packed)) {
16  uint64_t preamble;
17 
18  /* should be PACKET_ID_SETTINGS */
19  PacketId_t packet_id;
20 
21  /* 24 bytes */
22  FirmwareSetup_t firmware;
23 
24  uint8_t padding[3];
25 
26  /*
27  * CRC value for the packet that is transferred from the
28  * computer to the microcontroller
29  */
30  uint32_t crc;
32 /*
33  * tests to ensure that alignment works as intended
34  * Further info here: https://www.embedded.com/design/prototyping-and-development/4024941/Learn-a-new-trick-with-the-offsetof--macro
35  */
36 /* disable warnings for this one, only fail on error */
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wunused-variable"
39 static union {
40  char wrong_offset_preamble[(offsetof(SettingsPacket, preamble) == 0) ? 1 : -1];
41 
42  char wrong_offset_packet_id[(offsetof(SettingsPacket, packet_id) == 8) ? 1 : -1];
43 
44  char wrong_offset_firmware[(offsetof(SettingsPacket, firmware) == 8 + 1) ? 1 : -1];
45 
46  char wrong_offset_crc[(offsetof(SettingsPacket, crc) == 8 + 1 + 32 + 3) ? 1 : -1];
47 
48  /* to be processable by the CRC calculation unit */
49  char wrong_sizeof_packet[((sizeof(SettingsPacket) > 4) &&
50  (sizeof(SettingsPacket) % sizeof(uint32_t) == 0)) ?
51  1 :
52  -1];
54 /* enable all warnings again */
55 #pragma GCC diagnostic pop
56 
57 typedef struct __attribute__((packed)) {
58  uint64_t preamble;
59 
60  /* should be PACKET_ID_COMPUTER */
61  PacketId_t packet_id;
62 
68  float motor_erpm;
69 
73  float motor_current;
74 
78  int8_t servo_front;
79  int8_t servo_rear;
80 
89  uint16_t lightState;
90 
94  uint8_t brake;
95 
96  uint8_t padding[2];
97 
98  /*
99  * CRC value for the packet that is transferred from the
100  * computer to the microcontroller
101  */
102  uint32_t crc;
104 
105 /*
106  * tests to ensure that alignment works as intended
107  * Further info here: https://www.embedded.com/design/prototyping-and-development/4024941/Learn-a-new-trick-with-the-offsetof--macro
108  */
109 /* disable warnings for this one, only fail on error */
110 #pragma GCC diagnostic push
111 #pragma GCC diagnostic ignored "-Wunused-variable"
112 static union {
113  char wrong_offset_preamble[(offsetof(ComputerPacket, preamble) == 0) ? 1 : -1];
114 
115  char wrong_offset_packet_id[(offsetof(ComputerPacket, packet_id) == 8) ? 1 : -1];
116 
117  char wrong_offset_motor_erpm[(offsetof(ComputerPacket, motor_erpm) == 8 + 1) ? 1 : -1];
118  char wrong_offset_motor_current[(offsetof(ComputerPacket, motor_current) == 8 + 1 + 4) ? 1 : -1];
119 
120  char wrong_offset_servo_front[(offsetof(ComputerPacket, servo_front) == 8 + 1 + 4 + 4) ? 1 : -1];
121  char wrong_offset_servo_rear[(offsetof(ComputerPacket, servo_rear) == 8 + 1 + 4 + 4 + 1) ? 1 : -1];
122 
123  char wrong_offset_lightState[(offsetof(ComputerPacket, lightState) == 8 + 1 + 4 + 4 + 1 + 1) ? 1 : -1];
124 
125  char wrong_offset_brake[(offsetof(ComputerPacket, brake) == 8 + 1 + 4 + 4 + 1 + 1 + 2) ? 1 : -1];
126 
127  char wrong_offset_padding[(offsetof(ComputerPacket, padding) == 8 + 1 + 4 + 4 + 1 + 1 + 2 + 1) ? 1 : -1];
128 
129  char wrong_offset_crc[(offsetof(ComputerPacket, crc) == 8 + 1 + 4 + 4 + 1 + 1 + 2 + 1 + 2) ? 1 : -1];
130 
131  char wrong_sizeof_packet[((sizeof(ComputerPacket) > 4) &&
132  (sizeof(ComputerPacket) % sizeof(uint32_t) == 0)) ?
133  1 :
134  -1]; /* to be processable by the CRC calculation unit */
136 /* enable all warnings again */
137 #pragma GCC diagnostic pop
138 
139 typedef struct __attribute__((packed)) {
140  uint64_t preamble;
141 
142  uint8_t rc_active;
143 
144  /*
145  * The current RPM of the motor
146  * measured by the rotary encoder.
147  */
148  float motor_erpm;
149 
150  /*
151  * The values of the time
152  * of flight sensors.
153  * First value is for the sensor
154  * on the right, second for the
155  * sensor at the back.
156  */
157  uint16_t tof_right;
158  uint16_t tof_rear;
159 
160  /*
161  * The orientation of the car,
162  * filtered already on the ECU
163  */
164  float orientation;
165 
166  float linear_acceleration_x;
167  float linear_acceleration_y;
168  float linear_acceleration_z;
169 
170  float gyro_x;
171 
172  /*
173  * The button board provides
174  * four different buttons whereby
175  * every button state is stored as
176  * a 8 bit integer, packed together
177  * to a 32 bit integer.
178  */
179  uint8_t buttonState;
180 
181  /*
182  * Current voltage of the
183  * battery.
184  */
185  uint16_t batteryLevel;
186 
187  /*
188  * Current voltage of the
189  * battery.
190  */
191  uint16_t batteryCellLevel[3];
192 
196  //int32_t quadratureCounterValues[QUADRATURE_COUNTER_COUNT];
197 
203  int8_t applied_servo_front;
204 
210  int8_t applied_servo_rear;
211 
215  float motor_duty_cycle;
216 
223  float applied_motor_current;
224 
228  float applied_motor_erpm;
229 
233  uint8_t applied_motor_brake;
234 
235  uint8_t padding[3];
236 
237  /*
238  * CRC value for the packet that is transferred from the
239  * microcontroller to the computer
240  */
241  uint32_t crc;
243 
244 /*
245  * tests to ensure that alignment works as intended
246  * Further info here: https://www.embedded.com/design/prototyping-and-development/4024941/Learn-a-new-trick-with-the-offsetof--macro
247  */
248 /* disable warnings for this one, only fail on error */
249 #pragma GCC diagnostic push
250 #pragma GCC diagnostic ignored "-Wunused-variable"
251 static union {
252  char wrong_offset_preamble[(offsetof(EcuPacket, preamble) == 0) ? 1 : -1];
253  char wrong_offset_rc_active[(offsetof(EcuPacket, rc_active) == 8) ? 1 : -1];
254  char wrong_offset_motor_erpm[(offsetof(EcuPacket, motor_erpm) == 8 + 1) ? 1 : -1];
255 
256  char wrong_offset_tof_right[(offsetof(EcuPacket, tof_right) == 8 + 1 + 4) ? 1 : -1];
257  char wrong_offset_tof_rear[(offsetof(EcuPacket, tof_rear) == 8 + 1 + 4 + 2) ? 1 : -1];
258 
259  char wrong_offset_orientation[(offsetof(EcuPacket, orientation) == 8 + 1 + 4 + 4) ? 1 : -1];
260 
261  char wrong_offset_linear_acceleration_x[(offsetof(EcuPacket, linear_acceleration_x) == 8 + 1 + 4 + 4 + 4) ? 1 : -1];
262  char wrong_offset_linear_acceleration_y[(offsetof(EcuPacket, linear_acceleration_y) == 8 + 1 + 4 + 4 + 4 + 4) ? 1 : -1];
263  char wrong_offset_linear_acceleration_z[(offsetof(EcuPacket, linear_acceleration_z) == 8 + 1 + 4 + 4 + 4 + 4 + 4) ? 1 : -1];
264 
265  char wrong_offset_gyro_x[(offsetof(EcuPacket, gyro_x) == 8 + 1 + 4 + 4 + 4 + 12) ? 1 : -1];
266 
267  char wrong_offset_buttonState[(offsetof(EcuPacket, buttonState) == 8 + 1 + 4 + 4 + 4 + 12 + 4) ? 1 : -1];
268 
269  char wrong_offset_batteryLevel[(offsetof(EcuPacket, batteryLevel) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 1) ? 1 : -1];
270  char wrong_offset_batteryCellLevel[(offsetof(EcuPacket, batteryCellLevel) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 1 + 2) ? 1 : -1];
271 
272  char wrong_offset_applied_servo_front[(offsetof(EcuPacket, applied_servo_front) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 3 + 6) ? 1 : -1];
273  char wrong_offset_applied_servo_rear[(offsetof(EcuPacket, applied_servo_rear) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 3 + 6 + 1) ? 1 : -1];
274 
275  char wrong_offset_motor_duty_cycle[(offsetof(EcuPacket, motor_duty_cycle) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 1 + 2 + 6 + 2) ? 1 : -1];
276 
277  char wrong_offset_applied_motor_current[(offsetof(EcuPacket, applied_motor_current) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 1 + 2 + 6 + 2 + 4) ? 1 : -1];
278 
279  char wrong_offset_applied_motor_erpm[(offsetof(EcuPacket, applied_motor_erpm) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 1 + 2 + 6 + 2 + 4 + 4) ? 1 : -1];
280 
281  char wrong_offset_applied_motor_brake[(offsetof(EcuPacket, applied_motor_brake) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 1 + 2 + 6 + 2 + 4 + 4 + 4) ? 1 : -1];
282 
283  char wrong_offset_padding[(offsetof(EcuPacket, padding) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 1 + 2 + 6 + 2 + 4 + 4 + 4 + 1) ? 1 : -1];
284 
285  char wrong_offset_crc[(offsetof(EcuPacket, crc) == 8 + 1 + 4 + 4 + 4 + 12 + 4 + 1 + 2 + 6 + 2 + 4 + 4 + 4 + 4) ? 1 : -1];
286  /* to be processable by the CRC calculation unit */
287  char wrong_sizeof_packet[(
288  (sizeof(EcuPacket) > 4) && (sizeof(EcuPacket) % sizeof(uint32_t) == 0)) ?
289  1 :
290  -1];
291  char wrong_sizeof_float[(sizeof(float) == 4) ? 1 : -1];
292 } EcuPacketTest;
293 /* enable all warnings again */
294 #pragma GCC diagnostic pop
295 
296 #endif /* PACKET_H */
wrong_offset_applied_motor_current
char wrong_offset_applied_motor_current[(offsetof(EcuPacket, applied_motor_current)==8+1+4+4+4+12+4+1+2+6+2+4) ? 1 :-1]
Definition: Packets.hpp:277
wrong_sizeof_packet
char wrong_sizeof_packet[((sizeof(SettingsPacket) > 4) &&(sizeof(SettingsPacket) % sizeof(uint32_t)==0)) ? 1 :-1]
Definition: Packets.hpp:52
wrong_offset_lightState
char wrong_offset_lightState[(offsetof(ComputerPacket, lightState)==8+1+4+4+1+1) ? 1 :-1]
Definition: Packets.hpp:123
wrong_offset_servo_rear
char wrong_offset_servo_rear[(offsetof(ComputerPacket, servo_rear)==8+1+4+4+1) ? 1 :-1]
Definition: Packets.hpp:121
firmware_settings.h
FirmwareSetup_t
FirmwareSetup_t
Definition: firmware_settings.h:33
wrong_offset_firmware
char wrong_offset_firmware[(offsetof(SettingsPacket, firmware)==8+1) ? 1 :-1]
Definition: Packets.hpp:44
wrong_offset_orientation
char wrong_offset_orientation[(offsetof(EcuPacket, orientation)==8+1+4+4) ? 1 :-1]
Definition: Packets.hpp:259
ComputerPacketTest
static union @2 ComputerPacketTest
wrong_offset_applied_servo_rear
char wrong_offset_applied_servo_rear[(offsetof(EcuPacket, applied_servo_rear)==8+1+4+4+4+12+4+3+6+1) ? 1 :-1]
Definition: Packets.hpp:273
EcuPacket
EcuPacket
Definition: Packets.hpp:242
EcuPacketTest
static union @3 EcuPacketTest
wrong_sizeof_float
char wrong_sizeof_float[(sizeof(float)==4) ? 1 :-1]
Definition: Packets.hpp:291
wrong_offset_applied_motor_brake
char wrong_offset_applied_motor_brake[(offsetof(EcuPacket, applied_motor_brake)==8+1+4+4+4+12+4+1+2+6+2+4+4+4) ? 1 :-1]
Definition: Packets.hpp:281
wrong_offset_batteryCellLevel
char wrong_offset_batteryCellLevel[(offsetof(EcuPacket, batteryCellLevel)==8+1+4+4+4+12+4+1+2) ? 1 :-1]
Definition: Packets.hpp:270
wrong_offset_packet_id
char wrong_offset_packet_id[(offsetof(SettingsPacket, packet_id)==8) ? 1 :-1]
Definition: Packets.hpp:42
wrong_offset_linear_acceleration_y
char wrong_offset_linear_acceleration_y[(offsetof(EcuPacket, linear_acceleration_y)==8+1+4+4+4+4) ? 1 :-1]
Definition: Packets.hpp:262
wrong_offset_padding
char wrong_offset_padding[(offsetof(ComputerPacket, padding)==8+1+4+4+1+1+2+1) ? 1 :-1]
Definition: Packets.hpp:127
__attribute__
struct __attribute__((packed))
Definition: Packets.hpp:15
wrong_offset_motor_current
char wrong_offset_motor_current[(offsetof(ComputerPacket, motor_current)==8+1+4) ? 1 :-1]
Definition: Packets.hpp:118
wrong_offset_applied_servo_front
char wrong_offset_applied_servo_front[(offsetof(EcuPacket, applied_servo_front)==8+1+4+4+4+12+4+3+6) ? 1 :-1]
Definition: Packets.hpp:272
wrong_offset_linear_acceleration_x
char wrong_offset_linear_acceleration_x[(offsetof(EcuPacket, linear_acceleration_x)==8+1+4+4+4) ? 1 :-1]
Definition: Packets.hpp:261
wrong_offset_servo_front
char wrong_offset_servo_front[(offsetof(ComputerPacket, servo_front)==8+1+4+4) ? 1 :-1]
Definition: Packets.hpp:120
wrong_offset_crc
char wrong_offset_crc[(offsetof(SettingsPacket, crc)==8+1+32+3) ? 1 :-1]
Definition: Packets.hpp:46
wrong_offset_rc_active
char wrong_offset_rc_active[(offsetof(EcuPacket, rc_active)==8) ? 1 :-1]
Definition: Packets.hpp:253
quadrature_counter.h
wrong_offset_gyro_x
char wrong_offset_gyro_x[(offsetof(EcuPacket, gyro_x)==8+1+4+4+4+12) ? 1 :-1]
Definition: Packets.hpp:265
wrong_offset_motor_duty_cycle
char wrong_offset_motor_duty_cycle[(offsetof(EcuPacket, motor_duty_cycle)==8+1+4+4+4+12+4+1+2+6+2) ? 1 :-1]
Definition: Packets.hpp:275
PacketId_t
uint8_t PacketId_t
Definition: Packets.hpp:11
wrong_offset_motor_erpm
char wrong_offset_motor_erpm[(offsetof(ComputerPacket, motor_erpm)==8+1) ? 1 :-1]
Definition: Packets.hpp:117
wrong_offset_preamble
char wrong_offset_preamble[(offsetof(SettingsPacket, preamble)==0) ? 1 :-1]
Definition: Packets.hpp:40
ComputerPacket
ComputerPacket
Definition: Packets.hpp:103
wrong_offset_tof_right
char wrong_offset_tof_right[(offsetof(EcuPacket, tof_right)==8+1+4) ? 1 :-1]
Definition: Packets.hpp:256
wrong_offset_buttonState
char wrong_offset_buttonState[(offsetof(EcuPacket, buttonState)==8+1+4+4+4+12+4) ? 1 :-1]
Definition: Packets.hpp:267
wrong_offset_batteryLevel
char wrong_offset_batteryLevel[(offsetof(EcuPacket, batteryLevel)==8+1+4+4+4+12+4+1) ? 1 :-1]
Definition: Packets.hpp:269
wrong_offset_brake
char wrong_offset_brake[(offsetof(ComputerPacket, brake)==8+1+4+4+1+1+2) ? 1 :-1]
Definition: Packets.hpp:125
wrong_offset_tof_rear
char wrong_offset_tof_rear[(offsetof(EcuPacket, tof_rear)==8+1+4+2) ? 1 :-1]
Definition: Packets.hpp:257
SettingsPacket
SettingsPacket
Definition: Packets.hpp:31
wrong_offset_linear_acceleration_z
char wrong_offset_linear_acceleration_z[(offsetof(EcuPacket, linear_acceleration_z)==8+1+4+4+4+4+4) ? 1 :-1]
Definition: Packets.hpp:263
wrong_offset_applied_motor_erpm
char wrong_offset_applied_motor_erpm[(offsetof(EcuPacket, applied_motor_erpm)==8+1+4+4+4+12+4+1+2+6+2+4+4) ? 1 :-1]
Definition: Packets.hpp:279
SettingsPacketTest
static union @1 SettingsPacketTest