@@ -31,7 +31,7 @@ bool KPIRoverECU::Start() {
3131 processingThread_ = std::thread ([this ] { ProcessingThreadFunction (); });
3232 imuThread_ = std::thread ([this ] { IMUThreadFucntion (this ->imu_controller_ ); });
3333
34- if (!timerThread_.joinable () || !processingThread_.joinable ()) {
34+ if (!timerThread_.joinable () || !processingThread_.joinable () || !imuThread_. joinable () ) {
3535 std::cout << " Error creating thread" << ' \n ' ;
3636 return false ;
3737 }
@@ -45,45 +45,45 @@ void KPIRoverECU::IMUThreadFucntion(IMUController *workClass) {
4545 std::string destination_address;
4646 int destination_port = 0 ;
4747
48- while (destination_address.empty ()) {
49- destination_address = tcp_transport_->GetClientIp ();
50- destination_port = tcp_transport_->GetClientPort ();
51- }
52-
53- udp_client_->Init (destination_address, destination_port);
54-
5548 while (runningProcess_) {
56- const std::vector<float > kImuData = workClass->GetData ();
57- if (!kImuData .empty ()) {
58- if (packet_number == k16MaxCount) {
59- packet_number = 0 ;
60- }
61- packet_number += 1 ;
49+ if (destination_address.empty ()) {
50+ destination_address = tcp_transport_->GetClientIp ();
51+ destination_port = tcp_transport_->GetClientPort ();
52+ if (!destination_address.empty ()) { udp_client_->Init (destination_address, destination_port); }
53+
54+ } else {
55+ const std::vector<float > kImuData = workClass->GetData ();
56+ if (!kImuData .empty ()) {
57+ if (packet_number == k16MaxCount) {
58+ packet_number = 0 ;
59+ }
60+ packet_number += 1 ;
6261
63- std::vector<uint8_t > send_val;
64- send_val.push_back (workClass->GetId ());
62+ std::vector<uint8_t > send_val;
63+ send_val.push_back (workClass->GetId ());
6564
66- uint16_t send_packet_number = htons (packet_number);
67- auto *bytes = reinterpret_cast <uint8_t *>(&send_packet_number);
68- for (size_t i = 0 ; i < 2 ; ++i) {
69- send_val.push_back (bytes[i]);
70- }
65+ uint16_t send_packet_number = htons (packet_number);
66+ auto *bytes = reinterpret_cast <uint8_t *>(&send_packet_number);
67+ for (size_t i = 0 ; i < 2 ; ++i) {
68+ send_val.push_back (bytes[i]);
69+ }
7170
72- float insert_value = 0 ;
73- uint32_t value = 0 ;
71+ float insert_value = 0 ;
72+ uint32_t value = 0 ;
7473
75- for (const float kImuValue : kImuData ) {
76- insert_value = kImuValue ;
77- std::memcpy (&value, &insert_value, sizeof (float ));
78- value = ntohl (value);
79- bytes = reinterpret_cast <uint8_t *>(&value);
74+ for (const float kImuValue : kImuData ) {
75+ insert_value = kImuValue ;
76+ std::memcpy (&value, &insert_value, sizeof (float ));
77+ value = ntohl (value);
78+ bytes = reinterpret_cast <uint8_t *>(&value);
8079
81- for (size_t j = 0 ; j < sizeof (uint32_t ); ++j) {
82- send_val.push_back (bytes[j]);
80+ for (size_t j = 0 ; j < sizeof (uint32_t ); ++j) {
81+ send_val.push_back (bytes[j]);
82+ }
8383 }
84+ udp_client_->Send (send_val);
85+
8486 }
85-
86- udp_client_->Send (send_val);
8787 }
8888 rc_usleep (kTimerPrecision );
8989 }
@@ -134,6 +134,10 @@ void KPIRoverECU::Stop() {
134134 if (processingThread_.joinable ()) {
135135 processingThread_.join ();
136136 }
137+ if (imuThread_.joinable ()) {
138+ imuThread_.join ();
139+ }
140+
137141 std::cout << " destroying drivers" << ' \n ' ;
138142 // tcp_transport_->Destroy();
139143}
0 commit comments