Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
ECUHandler.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #define xlog(X, Y) \
4  do { \
5  std::cout << "[" << (X) << "]: " << (Y) << std::endl; \
6  } while(0)
7 
8 /* c includes */
9 #include <cstdlib>
10 #include <cstddef>
11 #include <cstring>
12 
13 /* cpp includes */
14 #include <chrono>
15 #include <thread>
16 #include <sstream>
17 #include <functional>
18 #include <mutex>
19 #include <boost/crc.hpp>
20 #include <atomic>
21 #include <array>
22 
23 /* project includes */
24 #include "util/Conversions.hpp"
25 #include "util/Packets.hpp"
26 #include "com/ICommunication.h"
27 #include "com/UART.hpp"
28 
29 using CRCType = boost::crc_32_type;
30 
34 typedef std::function<void(const EcuPacket* const, const std::array<std::chrono::duration<double, std::milli>, 10>&)> PublishMeasurementsCallback;
35 
36 class ECUHandler {
37 public:
40 
42  Stop();
43 
44  if(m_pCommunication != nullptr) {
45  m_pCommunication->VDisconnect();
46  }
47  }
48 
49  uint32_t calculateChecksum(ComputerPacket const& packet);
50  uint32_t calculateChecksum(SettingsPacket const& packet);
51 
59 
63  void TransmitPacket(SettingsPacket& packet);
64 
68  void TransmitPacket(ComputerPacket& packet);
69 
70 private:
71  std::chrono::steady_clock::time_point current_timestamp;
72  std::chrono::steady_clock::time_point last_timestamp;
74  std::array<std::chrono::duration<double, std::milli>, 10> packet_differences;
75 
76  typedef enum {
77  BTN_SC1 = 1,
78  BTN_SC2 = 1 << 1,
79  BTN_SC3 = 1 << 2,
80  } ButtonType;
81 
82  typedef enum {
87 
89 
90  /*
91  * Stores the Callback function to the ecu
92  * node to publish data from the ecu.
93  */
95 
96  /*
97  * The thread that is used for the handling
98  * of the communication with the ECU
99  */
100  std::thread* receiverThread = nullptr;
102  std::mutex mutex_on_packet;
103 
104  /*
105  * The packets that will be used for
106  * the communication between the Jetson and
107  * the microcontroller
108  */
112 
113  /*
114  * A pointer to the implementation of the selected
115  * communication protocol
116  */
117  std::shared_ptr<ICommunication> m_pCommunication = nullptr;
118 
119  /*
120  * Initializes the communication channel that has
121  * been selected in the launch file and passed
122  * as an enum value to this method
123  */
125 
126  /*
127  * Runner function for the thread
128  * that handles the communication.
129  */
130  void ReceiveHandler();
131 
132  /*
133  * Callback Function when a new
134  * message is received from the ECU and
135  * needs to be send to the ECUNode to
136  * be send out.
137  */
138  void onNewPacketReceived(const EcuPacket* const);
139 
140  /*
141  * Stops the ECU handler by disconnecting the communicatino
142  * and joining the thread back into the main thread
143  */
144  void Stop();
145 };
146 /* vim:set et sw=2 ts=2 tw=80: */
ECUHandler::firstPacketReceived
bool firstPacketReceived
Definition: ECUHandler.hpp:111
ECUHandler::receiverThreadRunning
bool receiverThreadRunning
Definition: ECUHandler.hpp:101
ECUHandler::m_lastEcuPacket
ComputerPacket m_lastEcuPacket
Definition: ECUHandler.hpp:88
ECUHandler::current_timestamp
std::chrono::steady_clock::time_point current_timestamp
Definition: ECUHandler.hpp:71
ECUHandler::difference_index
size_t difference_index
Definition: ECUHandler.hpp:73
Packets.hpp
ICommunication.h
ECUHandler::InitializeCommunication
void InitializeCommunication()
Definition: ECUHandler.cpp:81
ECUHandler::ReceiveHandler
void ReceiveHandler()
Definition: ECUHandler.cpp:102
EcuPacket
EcuPacket
Definition: Packets.hpp:242
ECUHandler::ButtonType
ButtonType
Definition: ECUHandler.hpp:76
ECUHandler::~ECUHandler
~ECUHandler()
Definition: ECUHandler.hpp:41
ECUHandler::TransmitPacket
void TransmitPacket(SettingsPacket &packet)
This function transmits a packet to the ECU.
Definition: ECUHandler.cpp:51
ECUHandler::onNewPacketReceived
void onNewPacketReceived(const EcuPacket *const)
Definition: ECUHandler.cpp:65
ECUHandler::WITHOUT_OBSTACLES
@ WITHOUT_OBSTACLES
Definition: ECUHandler.hpp:85
ECUHandler::currentPacket
EcuPacket currentPacket
Definition: ECUHandler.hpp:109
ECUHandler::cb_
PublishMeasurementsCallback cb_
Definition: ECUHandler.hpp:94
PublishMeasurementsCallback
std::function< void(const EcuPacket *const, const std::array< std::chrono::duration< double, std::milli >, 10 > &)> PublishMeasurementsCallback
Callback function that processes an ECU packet.
Definition: ECUHandler.hpp:34
Conversions.hpp
CRCType
boost::crc_32_type CRCType
Definition: ECUHandler.hpp:29
ECUHandler::m_pCommunication
std::shared_ptr< ICommunication > m_pCommunication
Definition: ECUHandler.hpp:117
ECUHandler::mutex_on_packet
std::mutex mutex_on_packet
Definition: ECUHandler.hpp:102
ECUHandler::init
void init(PublishMeasurementsCallback cb)
Provides the handler with a connection to the ECUNode to communicate via ROS.
Definition: ECUHandler.cpp:31
ECUHandler::BTN_SC2
@ BTN_SC2
Definition: ECUHandler.hpp:78
ECUHandler::UNDEFINED
@ UNDEFINED
Definition: ECUHandler.hpp:83
ECUHandler::DrivingModeType
DrivingModeType
Definition: ECUHandler.hpp:82
ECUHandler::receiverThread
std::thread * receiverThread
Definition: ECUHandler.hpp:100
ECUHandler::last_timestamp
std::chrono::steady_clock::time_point last_timestamp
Definition: ECUHandler.hpp:72
ECUHandler::BTN_SC3
@ BTN_SC3
Definition: ECUHandler.hpp:79
ECUHandler::BTN_SC1
@ BTN_SC1
Definition: ECUHandler.hpp:77
ComputerPacket
ComputerPacket
Definition: Packets.hpp:103
UART.hpp
ECUHandler::packet_differences
std::array< std::chrono::duration< double, std::milli >, 10 > packet_differences
Definition: ECUHandler.hpp:74
ECUHandler::m_computerPacket
ComputerPacket m_computerPacket
Definition: ECUHandler.hpp:110
ECUHandler::WITH_OBSTACLES
@ WITH_OBSTACLES
Definition: ECUHandler.hpp:84
ECUHandler::calculateChecksum
uint32_t calculateChecksum(ComputerPacket const &packet)
Definition: ECUHandler.cpp:13
SettingsPacket
SettingsPacket
Definition: Packets.hpp:31
ECUHandler::Stop
void Stop()
Definition: ECUHandler.cpp:75
ECUHandler::ECUHandler
ECUHandler()
Definition: ECUHandler.hpp:38
ECUHandler
Definition: ECUHandler.hpp:36