2 #include "rclcpp/rclcpp.hpp"
11 static const uint8_t
gcu8_header[8] = { 0xDE, 0xAD, 0xF0, 0x0D, 0xDE, 0xAD, 0xBE, 0xEF };
17 crc.process_bytes(&packet,
sizeof(
ComputerPacket) -
sizeof(packet.crc));
19 return crc.checksum();
26 crc.process_bytes(&packet,
sizeof(
SettingsPacket) -
sizeof(packet.crc));
28 return crc.checksum();
34 xlog(
"ECUHandler",
"Successfully Initialised...");
38 memcpy(&(packet.preamble),
gcu8_header,
sizeof(packet.preamble));
47 xlog(
"ECUHandler",
"Could not write the whole computer packet");
52 memcpy(&(packet.preamble),
gcu8_header,
sizeof(packet.preamble));
59 xlog(
"ECUHandler",
"Could not write the whole settings packet");
66 if(this->
cb_ ==
nullptr || packet ==
nullptr) {
67 xlog(
"ECUHandler Error",
"callback is a nullptr");
82 xlog(
"ECUHandler",
"INIT UART");
88 xlog(
"ECUHandler",
"Failed to open serial port to breakout");
97 std::this_thread::sleep_for(std::chrono::milliseconds(1));
113 } state = RECEIVING_PREAMBLE;
119 for(
size_t i = 0; i < bytesRead; i++) {
124 case RECEIVING_PREAMBLE:
126 packet[packetIndex] = byte;
131 state = RECEIVING_DATA;
137 packet[packetIndex++] = byte;
139 if(packetIndex ==
sizeof(packet))
149 if(state == CRC_UPDATE) {
150 const size_t begin_of_crc_sum =
sizeof(packet) - 4;
152 crc.process_bytes(packet, begin_of_crc_sum);
153 const uint32_t computed_crc = crc.checksum();
155 uint32_t packetCrc = packet[begin_of_crc_sum];
156 packetCrc |= packet[begin_of_crc_sum + 1] << 8;
157 packetCrc |= packet[begin_of_crc_sum + 2] << 16;
158 packetCrc |= packet[begin_of_crc_sum + 3] << 24;
160 if(packetCrc == computed_crc) {
161 std::stringstream ss;
166 std::chrono::duration<double, std::milli> diff_ms =
168 if(diff_ms.count() < 9.5 || diff_ms.count() > 10.5 ) {
187 xlog(
"ECUHandler",
"Wrong CRC checksum");
191 state = RECEIVING_PREAMBLE;