Skip to content

Commit 2a72541

Browse files
taketook34AksonovSergei
authored andcommitted
Issue #29. Fixed abortion bug when stop
1 parent c9a8e8a commit 2a72541

File tree

8 files changed

+61
-49
lines changed

8 files changed

+61
-49
lines changed

src/kpi_rover_ecu/include/IClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class IClient {
1414
virtual bool Receive(std::vector<std::uint8_t>& data) = 0;
1515
virtual int Init(std::string ip_address, int port) = 0;
1616
virtual void Destroy() = 0;
17-
virtual ~IClient() = default;
17+
// virtual ~IClient() = default;
1818

1919
IClient(const IClient&) = delete;
2020
IClient& operator=(const IClient&) = delete;

src/kpi_rover_ecu/include/ITransport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ITransport {
1414
virtual int Init() = 0;
1515
virtual void Start() = 0;
1616
virtual void Destroy() = 0;
17-
virtual ~ITransport() = default;
17+
// virtual ~ITransport() = default;
1818

1919
ITransport(const ITransport&) = delete;
2020
ITransport& operator=(const ITransport&) = delete;

src/kpi_rover_ecu/include/TCPTransport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TCPTransport : public ITransport {
2222
TCPTransport& operator=(const TCPTransport&) = delete;
2323
TCPTransport(TCPTransport&&) = delete;
2424
TCPTransport& operator=(TCPTransport&&) = delete;
25-
~TCPTransport() override;
25+
// ~TCPTransport() override;
2626

2727
bool Send(const std::vector<std::uint8_t>& data) override;
2828
bool Receive(std::vector<std::uint8_t>& data) override;

src/kpi_rover_ecu/include/UDPClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class UDPClient : public IClient {
2323
bool Send(const std::vector<std::uint8_t>& data) override;
2424
bool Receive(std::vector<std::uint8_t>& data) override;
2525
void Destroy() override;
26-
~UDPClient() override;
26+
// ~UDPClient() override;
2727

2828
private:
2929
int sockfd_;

src/kpi_rover_ecu/src/KPIRoverECU.cpp

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/kpi_rover_ecu/src/TCPTransport.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ TCPTransport::TCPTransport(const char *ip_address, int port)
2626
strncpy(server_address_, ip_address, strlen(ip_address) + 1);
2727
}
2828

29-
TCPTransport::~TCPTransport() {
30-
running_ = false;
31-
shutdown(sockfd_, SHUT_RDWR);
32-
std::cout << "joining thread ... " << '\n';
33-
acceptThread_.join();
34-
std::cout << "closing socket" << '\n';
35-
close(client_sockfd_);
36-
close(sockfd_);
37-
delete[] server_address_;
38-
}
29+
// TCPTransport::~TCPTransport() {
30+
// running_ = false;
31+
// shutdown(sockfd_, SHUT_RDWR);
32+
// std::cout << "joining thread ... " << '\n';
33+
// acceptThread_.join();
34+
// std::cout << "closing socket" << '\n';
35+
// close(client_sockfd_);
36+
// close(sockfd_);
37+
// delete[] server_address_;
38+
// }
3939

4040
int TCPTransport::Init() {
4141
struct sockaddr_in serv_addr = {};
@@ -124,4 +124,5 @@ void TCPTransport::Destroy() {
124124
std::cout << "closing socket" << '\n';
125125
close(client_sockfd_);
126126
close(sockfd_);
127+
delete[] server_address_;
127128
}

src/kpi_rover_ecu/src/UDPClient.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ bool UDPClient::Send(const std::vector<std::uint8_t> &data) {
4747

4848
bool UDPClient::Receive(std::vector<std::uint8_t> &data) { return false; }
4949

50-
void UDPClient::Destroy() {}
51-
52-
UDPClient::~UDPClient() {
50+
void UDPClient::Destroy() {
5351
shutdown(sockfd_, SHUT_WR);
5452

5553
close(sockfd_);
54+
std::cout << "joining udp client socket ..." << '\n';
5655
}
56+
57+
// UDPClient::~UDPClient() {
58+
// shutdown(sockfd_, SHUT_WR);
59+
60+
// close(sockfd_);
61+
// }

src/kpi_rover_ecu/src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ int main(int argc, char* argv[]) {
109109

110110
kpi_rover_ecu.Stop();
111111
motors_processor.Destroy();
112+
tcp_transport.Destroy();
113+
udp_client.Destroy();
112114

113115
return 0;
114116
}

0 commit comments

Comments
 (0)