11#include < gtest/gtest.h>
22#include " ControlCommunicator.h"
3+ #include < list>
34
45class ControlCommunicatorTest : public ::testing::Test
56{
@@ -9,31 +10,15 @@ class ControlCommunicatorTest : public ::testing::Test
910
1011 void SetUp () override
1112 {
12- // Find which port to use
13- const char *port1 = " /dev/ttyTHS0" ;
14- const char *port2 = " /dev/ttyTHS1" ;
15- const char *port3 = " /dev/ttyTHS2" ;
16- bool port1_exists = access (port1, F_OK) != -1 && open (port1, O_RDWR | O_NOCTTY | O_NONBLOCK) != -1 ;
17- bool port2_exists = access (port2, F_OK) != -1 && open (port2, O_RDWR | O_NOCTTY | O_NONBLOCK) != -1 ;
18- bool port3_exists = access (port3, F_OK) != -1 && open (port3, O_RDWR | O_NOCTTY | O_NONBLOCK) != -1 ;
19-
20- // If either of the ports exist, use it
21- if (port1_exists)
22- {
23- port = port1;
24- }
25- else if (port2_exists)
26- {
27- port = port2;
28- }
29- else if (port3_exists)
13+ // Loop through possible UART ports to fnd one we can access and open
14+ const std::list<const char *> ports = {" /dev/ttyTHS0" , " /dev/ttyTHS1" , " /dev/ttyTHS2" };
15+ for (const char *p : ports)
3016 {
31- port = port3;
32- }
33- else
34- {
35- // Neither port exists on dev server or local machine, since no serial device
36- port = nullptr ;
17+ if (access (p, F_OK) != -1 && open (p, O_RDWR | O_NOCTTY | O_NONBLOCK) != -1 )
18+ {
19+ port = p;
20+ break ;
21+ }
3722 }
3823 }
3924
@@ -83,7 +68,7 @@ TEST_F(ControlCommunicatorTest, test_read_uart)
8368 if (port != nullptr )
8469 {
8570 EXPECT_TRUE (control_communicator.start_uart_connection (port));
86- EXPECT_NE (control_communicator.port_fd , -1 ); // Port should be opened
71+ EXPECT_NE (control_communicator.port_fd , -1 ); // Port should be opened
8772 EXPECT_TRUE (control_communicator.is_connected ); // Connection should be established
8873
8974 PackageIn package;
@@ -99,21 +84,21 @@ TEST_F(ControlCommunicatorTest, test_read_uart)
9984 EXPECT_TRUE (result); // Ensure at least one successful read
10085
10186 // Validate the package data
102- EXPECT_EQ (package.head , 0xAA ); // Check head byte
87+ EXPECT_EQ (package.head , 0xAA ); // Check head byte
10388 EXPECT_EQ (package.ref_flags & 0x01 , 0 ); // Check ref_flags (LSB should be 0 since match not started)
104- EXPECT_LT (package.pitch , M_PI); // Check pitch value
105- EXPECT_GT (package.pitch , -M_PI); // Check pitch value
106- EXPECT_LT (package.pitch_vel , M_PI); // Check pitch velocity
107- EXPECT_GT (package.pitch_vel , -M_PI); // Check pitch velocity
108- EXPECT_LT (package.yaw_vel , M_PI); // Check yaw velocity
109- EXPECT_GT (package.yaw_vel , -M_PI); // Check yaw velocity
110-
89+ EXPECT_LT (package.pitch , M_PI); // Check pitch value
90+ EXPECT_GT (package.pitch , -M_PI); // Check pitch value
91+ EXPECT_LT (package.pitch_vel , M_PI); // Check pitch velocity
92+ EXPECT_GT (package.pitch_vel , -M_PI); // Check pitch velocity
93+ EXPECT_LT (package.yaw_vel , M_PI); // Check yaw velocity
94+ EXPECT_GT (package.yaw_vel , -M_PI); // Check yaw velocity
95+
11196 // package x field should exist and be a number
112- EXPECT_NE (package.x , NAN); // Check x position
113- EXPECT_NE (package.y , NAN); // Check y position
97+ EXPECT_NE (package.x , NAN); // Check x position
98+ EXPECT_NE (package.y , NAN); // Check y position
11499 EXPECT_NE (package.orientation , NAN); // Check orientation
115- EXPECT_NE (package.x_vel , NAN); // Check x velocity
116- EXPECT_NE (package.y_vel , NAN); // Check y velocity
100+ EXPECT_NE (package.x_vel , NAN); // Check x velocity
101+ EXPECT_NE (package.y_vel , NAN); // Check y velocity
117102 }
118103 else
119104 {
0 commit comments