Skip to content

Commit 46ed2e6

Browse files
authored
Merge pull request #1 from Cyclone-Robosub/updateCodeBranch
Update code branch
2 parents b731ba9 + 24d6dfb commit 46ed2e6

File tree

2 files changed

+103
-12
lines changed

2 files changed

+103
-12
lines changed

lib/Wiring.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ void printToSerial(std::string message, int serial) {
2727

2828
#else
2929

30-
#include <wiringPi.h>
3130
#include <wiringSerial.h>
3231

3332
bool WiringControl::initializeGPIO() {
34-
if (wiringPiSetupGpio() < 0 || ((serial = serialOpen("/dev/ttyACM0", 115200)) < 0)) {
33+
if ((serial = serialOpen("/dev/serial/by-id/usb-MicroPython_Board_in_FS_mode_e6614864d3798738-if00", 115200)) < 0) {
3534
return false;
3635
}
3736
return true;

testing/Command_Interpreter_Testing.cpp

+102-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
11
#include "Command_Interpreter.h"
22
#include <gtest/gtest.h>
33

4+
#ifndef MOCK_RPI
5+
6+
#include <wiringSerial.h>
7+
8+
void echoOn(int serial) {
9+
serialPuts(serial, "echo on\n");
10+
}
11+
12+
bool initializeSerial(int* serial) {
13+
if ((*serial = serialOpen("/dev/serial/by-id/usb-MicroPython_Board_in_FS_mode_e6614864d3798738-if00", 115200)) < 0) {
14+
return false;
15+
}
16+
echoOn(*serial);
17+
return true;
18+
}
19+
20+
int getSerialChar(int* serial) {
21+
if (*serial == -1) {
22+
if (!initializeSerial
23+
(serial)) {
24+
std::cerr << "Unable to open serial port! Exiting." << std::endl;
25+
exit(42);
26+
}
27+
}
28+
return serialGetchar(*serial); // from wiringSerial
29+
}
30+
31+
#else
32+
33+
bool initializeSerial(int* serial) {
34+
return true;
35+
}
36+
37+
int getSerialChar(int* serial) {
38+
return EOF;
39+
}
40+
41+
#endif
42+
443
TEST(CommandInterpreterTest, CreateCommandInterpreter) {
544
testing::internal::CaptureStdout();
45+
int serial = -1;
46+
initializeSerial(&serial);
647

748
auto pinNumbers = std::vector<int>{4, 5, 2, 3, 9, 7, 8, 6};
849

@@ -28,13 +69,27 @@ TEST(CommandInterpreterTest, CreateCommandInterpreter) {
2869
expectedOutput.append(" PWM 1500\n");
2970
}
3071

72+
int charRead = EOF;
73+
std::string serialOutput;
74+
while ((charRead = getSerialChar(&serial)) != EOF) {
75+
serialOutput.push_back((char)charRead);
76+
}
77+
3178
ASSERT_EQ(pinStatus.size(), 8);
3279
ASSERT_EQ(pinStatus, (std::vector<int>{1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500}));
33-
ASSERT_EQ(output, expectedOutput);
80+
if (serialOutput.empty()) {
81+
ASSERT_EQ(output, expectedOutput);
82+
}
83+
else {
84+
expectedOutput.insert(0, "echo on\n");
85+
ASSERT_EQ(serialOutput, expectedOutput);
86+
}
3487
}
3588

3689
TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
3790
testing::internal::CaptureStdout();
91+
int serial = -1;
92+
initializeSerial(&serial);
3893

3994
auto pinNumbers = std::vector<int>{4, 5, 2, 3, 9, 7, 8, 6};
4095

@@ -66,15 +121,28 @@ TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
66121
expectedOutput.append("Configure 8 Digital\nSet 8 Digital High\n");
67122
expectedOutput.append("Configure 9 Digital\nSet 9 Digital Low\n");
68123

124+
int charRead = EOF;
125+
std::string serialOutput;
126+
while ((charRead = getSerialChar(&serial)) != EOF) {
127+
serialOutput.push_back((char)charRead);
128+
}
69129
ASSERT_EQ(pinStatus.size(), 10);
70130
ASSERT_EQ(pinStatus, (std::vector<int>{1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1, 0}));
71-
ASSERT_EQ(output, expectedOutput);
131+
if (serialOutput.empty()) {
132+
ASSERT_EQ(output, expectedOutput);
133+
}
134+
else {
135+
expectedOutput.insert(0, "echo on\n");
136+
ASSERT_EQ(serialOutput, expectedOutput);
137+
}
72138
}
73139

74140
TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
75141
testing::internal::CaptureStdout();
142+
int serial = -1;
143+
initializeSerial(&serial);
76144

77-
const CommandComponent acceleration = {1500, 1900, 1100,
145+
const CommandComponent acceleration = {1900, 1900, 1100,
78146
1250, 1300, 1464, 1535,
79147
1536, std::chrono::milliseconds(2000)};
80148

@@ -106,7 +174,7 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
106174
expectedOutput.append(std::to_string(pinNumber));
107175
expectedOutput.append(" PWM 1500\n");
108176
}
109-
expectedOutput.append("Set 4 PWM 1500\n");
177+
expectedOutput.append("Set 4 PWM 1900\n");
110178
expectedOutput.append("Set 5 PWM 1900\n");
111179
expectedOutput.append("Set 2 PWM 1100\n");
112180
expectedOutput.append("Set 3 PWM 1250\n");
@@ -115,16 +183,29 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
115183
expectedOutput.append("Set 8 PWM 1535\n");
116184
expectedOutput.append("Set 6 PWM 1536\n");
117185

186+
int charRead = EOF;
187+
std::string serialOutput;
188+
while ((charRead = getSerialChar(&serial)) != EOF) {
189+
serialOutput.push_back((char)charRead);
190+
}
118191
ASSERT_NEAR((endTime - startTime) / std::chrono::milliseconds(1), std::chrono::milliseconds(2000) /
119192
std::chrono::milliseconds(1), std::chrono::milliseconds(10) / std::chrono::milliseconds(1));
120-
ASSERT_EQ(pinStatus, (std::vector<int>{1500, 1900, 1100, 1250, 1300, 1464, 1535, 1536}));
121-
ASSERT_EQ(output, expectedOutput);
193+
ASSERT_EQ(pinStatus, (std::vector<int>{1900, 1900, 1100, 1250, 1300, 1464, 1535, 1536}));
194+
if (serialOutput.empty()) {
195+
ASSERT_EQ(output, expectedOutput);
196+
}
197+
else {
198+
expectedOutput.insert(0, "echo on\n");
199+
ASSERT_EQ(serialOutput, expectedOutput);
200+
}
122201
}
123202

124203
TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
125204
testing::internal::CaptureStdout();
205+
int serial = -1;
206+
initializeSerial(&serial);
126207

127-
const CommandComponent acceleration = {1500, 1900, 1100,
208+
const CommandComponent acceleration = {1100, 1900, 1100,
128209
1250, 1300, 1464, 1535,
129210
1536, std::chrono::milliseconds(2000)};
130211

@@ -156,7 +237,7 @@ TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
156237
expectedOutput.append(std::to_string(pinNumber));
157238
expectedOutput.append(" PWM 1500\n");
158239
}
159-
expectedOutput.append("Set 4 PWM 1500\n");
240+
expectedOutput.append("Set 4 PWM 1100\n");
160241
expectedOutput.append("Set 5 PWM 1900\n");
161242
expectedOutput.append("Set 2 PWM 1100\n");
162243
expectedOutput.append("Set 3 PWM 1250\n");
@@ -165,10 +246,21 @@ TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
165246
expectedOutput.append("Set 8 PWM 1535\n");
166247
expectedOutput.append("Set 6 PWM 1536\n");
167248

249+
int charRead = EOF;
250+
std::string serialOutput;
251+
while ((charRead = getSerialChar(&serial)) != EOF) {
252+
serialOutput.push_back((char)charRead);
253+
}
168254
ASSERT_NEAR((endTime - startTime) / std::chrono::milliseconds(1), std::chrono::milliseconds(2000) /
169255
std::chrono::milliseconds(1), std::chrono::milliseconds(10) / std::chrono::milliseconds(1));
170256

171-
ASSERT_EQ(pinStatus, (std::vector<int>{1500, 1900, 1100, 1250, 1300, 1464, 1535, 1536}));
172-
ASSERT_EQ(output, expectedOutput);
257+
ASSERT_EQ(pinStatus, (std::vector<int>{1100, 1900, 1100, 1250, 1300, 1464, 1535, 1536}));
258+
if (serialOutput.empty()) {
259+
ASSERT_EQ(output, expectedOutput);
260+
}
261+
else {
262+
expectedOutput.insert(0, "echo on\n");
263+
ASSERT_EQ(serialOutput, expectedOutput);
264+
}
173265
}
174266

0 commit comments

Comments
 (0)