Skip to content

Commit 5ee5b48

Browse files
committed
Update output file stream usage
This allows for more granular and consistent control over where logs and output are sent.
1 parent 656b5c3 commit 5ee5b48

File tree

5 files changed

+225
-156
lines changed

5 files changed

+225
-156
lines changed

lib/Command_Interpreter.cpp

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
#include <ctime>
77
#include <utility>
88

9-
DigitalPin::DigitalPin(int gpioNumber, EnableType enableType): Pin(gpioNumber), enableType(enableType) {}
10-
11-
void DigitalPin::initialize(WiringControl& wiringControl) {
9+
void DigitalPin::initialize(WiringControl &wiringControl) {
1210
switch (enableType) {
1311
case ActiveLow:
1412
wiringControl.setPinType(gpioNumber, DigitalActiveLow);
@@ -17,12 +15,12 @@ void DigitalPin::initialize(WiringControl& wiringControl) {
1715
wiringControl.setPinType(gpioNumber, DigitalActiveHigh);
1816
break;
1917
default:
20-
std::cerr << "Impossible digital pin type " << enableType << "! Exiting." << std::endl;
18+
errorLog << "Impossible digital pin type " << enableType << "! Exiting." << std::endl;
2119
exit(42);
2220
}
2321
}
2422

25-
void DigitalPin::enable(WiringControl& wiringControl) {
23+
void DigitalPin::enable(WiringControl &wiringControl) {
2624
switch (enableType) {
2725
case ActiveHigh:
2826
wiringControl.digitalWrite(gpioNumber, High);
@@ -31,12 +29,12 @@ void DigitalPin::enable(WiringControl& wiringControl) {
3129
wiringControl.digitalWrite(gpioNumber, Low);
3230
break;
3331
default:
34-
std::cerr << "Impossible pin mode!" << std::endl;
32+
errorLog << "Impossible pin mode!" << std::endl;
3533
exit(42);
3634
}
3735
}
3836

39-
void DigitalPin::disable(WiringControl& wiringControl) {
37+
void DigitalPin::disable(WiringControl &wiringControl) {
4038
switch (enableType) {
4139
case ActiveHigh:
4240
wiringControl.digitalWrite(gpioNumber, Low);
@@ -45,122 +43,126 @@ void DigitalPin::disable(WiringControl& wiringControl) {
4543
wiringControl.digitalWrite(gpioNumber, High);
4644
break;
4745
default:
48-
std::cerr << "Impossible pin mode!" << std::endl;
46+
errorLog << "Impossible pin mode!" << std::endl;
4947
exit(42);
5048
}
5149
}
5250

53-
bool DigitalPin::enabled(WiringControl& wiringControl) {
51+
bool DigitalPin::enabled(WiringControl &wiringControl) {
5452
switch (enableType) {
5553
case ActiveHigh:
5654
return wiringControl.digitalRead(gpioNumber) == High;
5755
case ActiveLow:
5856
return wiringControl.digitalRead(gpioNumber) == Low;
5957
default:
60-
std::cerr << "Impossible pin enable type! Exiting." << std::endl;
58+
errorLog << "Impossible pin enable type! Exiting." << std::endl;
6159
exit(42);
6260
}
6361
}
6462

65-
int DigitalPin::read(WiringControl& wiringControl) {
63+
int DigitalPin::read(WiringControl &wiringControl) {
6664
return wiringControl.digitalRead(gpioNumber);
6765
}
6866

69-
void PwmPin::setPwm(int pulseWidth, WiringControl& wiringControl, std::ofstream &logFile) {
67+
void PwmPin::setPwm(int pulseWidth, WiringControl &wiringControl) {
7068
setPowerAndDirection(pulseWidth, wiringControl);
7169
std::time_t currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
72-
logFile << "Current time: " << std::ctime(&currentTime) << std::endl;
73-
logFile << "Thruster at pin " << gpioNumber << ": " << pulseWidth << std::endl;
70+
outLog << "Current time: " << std::ctime(&currentTime) << std::endl;
71+
outLog << "Thruster at pin " << gpioNumber << ": " << pulseWidth << std::endl;
7472
}
7573

76-
HardwarePwmPin::HardwarePwmPin(int gpioNumber): PwmPin(gpioNumber) {}
7774

78-
void HardwarePwmPin::initialize(WiringControl& wiringControl) {
75+
void HardwarePwmPin::initialize(WiringControl &wiringControl) {
7976
wiringControl.setPinType(gpioNumber, HardwarePWM);
8077
}
8178

82-
void HardwarePwmPin::enable(WiringControl& wiringControl) {
79+
void HardwarePwmPin::enable(WiringControl &wiringControl) {
8380
wiringControl.pwmWriteMaximum(gpioNumber);
8481
}
8582

86-
void HardwarePwmPin::disable(WiringControl& wiringControl) {
83+
void HardwarePwmPin::disable(WiringControl &wiringControl) {
8784
wiringControl.pwmWriteOff(gpioNumber);
8885
}
8986

90-
bool HardwarePwmPin::enabled(WiringControl& wiringControl) {
87+
bool HardwarePwmPin::enabled(WiringControl &wiringControl) {
9188
return wiringControl.pwmRead(gpioNumber).pulseWidth != 1500;
9289
}
9390

94-
void HardwarePwmPin::setPowerAndDirection(int pwmValue, WiringControl& wiringControl) {
91+
void HardwarePwmPin::setPowerAndDirection(int pwmValue, WiringControl &wiringControl) {
9592
wiringControl.pwmWrite(gpioNumber, pwmValue);
9693
}
9794

98-
int HardwarePwmPin::read(WiringControl& wiringControl) {
95+
int HardwarePwmPin::read(WiringControl &wiringControl) {
9996
return wiringControl.pwmRead(gpioNumber).pulseWidth;
10097
}
10198

102-
SoftwarePwmPin::SoftwarePwmPin(int gpioNumber): PwmPin(gpioNumber) {}
10399

104-
void SoftwarePwmPin::initialize(WiringControl& wiringControl) {
100+
void SoftwarePwmPin::initialize(WiringControl &wiringControl) {
105101
wiringControl.setPinType(gpioNumber, SoftwarePWM);
106102
}
107103

108-
void SoftwarePwmPin::enable(WiringControl& wiringControl) {
104+
void SoftwarePwmPin::enable(WiringControl &wiringControl) {
109105
wiringControl.pwmWriteMaximum(gpioNumber);
110106
}
111107

112-
void SoftwarePwmPin::disable(WiringControl& wiringControl) {
108+
void SoftwarePwmPin::disable(WiringControl &wiringControl) {
113109
wiringControl.pwmWriteOff(gpioNumber);
114110
}
115111

116-
bool SoftwarePwmPin::enabled(WiringControl& wiringControl) {
112+
bool SoftwarePwmPin::enabled(WiringControl &wiringControl) {
117113
return wiringControl.pwmRead(gpioNumber).pulseWidth != 1500;
118114
}
119115

120-
void SoftwarePwmPin::setPowerAndDirection(int pwmValue, WiringControl& wiringControl) {
116+
void SoftwarePwmPin::setPowerAndDirection(int pwmValue, WiringControl &wiringControl) {
121117
wiringControl.pwmWrite(gpioNumber, pwmValue);
122118
}
123119

124-
int SoftwarePwmPin::read(WiringControl& wiringControl) {
120+
int SoftwarePwmPin::read(WiringControl &wiringControl) {
125121
return wiringControl.pwmRead(gpioNumber).pulseWidth;
126122
}
127123

128-
Command_Interpreter_RPi5::Command_Interpreter_RPi5(std::vector<PwmPin*> thrusterPins, std::vector<DigitalPin*> digitalPins):
129-
thrusterPins(std::move(thrusterPins)), digitalPins(std::move(digitalPins)) {
124+
Command_Interpreter_RPi5::Command_Interpreter_RPi5(std::vector<PwmPin *> thrusterPins,
125+
std::vector<DigitalPin *> digitalPins,
126+
const WiringControl &wiringControl, std::ostream &output,
127+
std::ostream &outLog, std::ostream &errorLog) :
128+
thrusterPins(std::move(thrusterPins)), digitalPins(std::move(digitalPins)), wiringControl(wiringControl),
129+
errorLog(errorLog),
130+
outLog(outLog), output(output) {
130131
if (this->thrusterPins.size() != 8) {
131-
std::cerr << "Incorrect number of thruster pwm pins given! Need 8, given " << this->thrusterPins.size() << std::endl;
132+
errorLog << "Incorrect number of thruster pwm pins given! Need 8, given " << this->thrusterPins.size()
133+
<< std::endl;
132134
exit(42);
133135
}
134136
}
135137

136-
std::vector<Pin*> Command_Interpreter_RPi5::allPins() {
137-
auto allPins = std::vector<Pin*>{};
138+
std::vector<Pin *> Command_Interpreter_RPi5::allPins() {
139+
auto allPins = std::vector<Pin *>{};
138140
allPins.insert(allPins.end(), this->thrusterPins.begin(), this->thrusterPins.end());
139141
allPins.insert(allPins.end(), this->digitalPins.begin(), this->digitalPins.end());
140142
return allPins;
141143
}
142144

143145
void Command_Interpreter_RPi5::initializePins() {
144146
if (!wiringControl.initializeGPIO()) {
145-
std::cerr << "Failure to configure GPIO pins!" << std::endl;
147+
errorLog << "Failure to configure GPIO pins!" << std::endl;
146148
exit(42);
147149
}
148-
for (Pin* pin : allPins()) {
150+
for (Pin *pin: allPins()) {
149151
pin->initialize(wiringControl);
150152
}
151153
}
152154

153-
void Command_Interpreter_RPi5::execute(pwm_array thrusterPwms, std::ofstream& logFile) {
155+
void Command_Interpreter_RPi5::execute(pwm_array thrusterPwms) {
154156
int i = 0;
155-
for (int pulseWidth : thrusterPwms.pwm_signals) {
156-
thrusterPins.at(i)->setPwm(pulseWidth, wiringControl, logFile);
157+
for (int pulseWidth: thrusterPwms.pwm_signals) {
158+
thrusterPins.at(i)->setPwm(pulseWidth, wiringControl);
157159
i++;
158160
}
159161
}
160162

161163
std::vector<int> Command_Interpreter_RPi5::readPins() {
162164
std::vector<int> pinValues;
163-
for (auto pin : allPins()) {
165+
for (auto pin: allPins()) {
164166
pinValues.push_back(pin->read(wiringControl));
165167
}
166168
return pinValues;
@@ -172,24 +174,24 @@ Command_Interpreter_RPi5::~Command_Interpreter_RPi5() {
172174
}
173175
}
174176

175-
//move_forwards(float distance, std::ofstream logfile) {
177+
//move_forwards(float distance, std::ostream logfile) {
176178
// command = calculate_stuff(distance, current_robot_data);
177179
// blind_execute(command.accel_command, logfile); //accel
178180
// blind_execute(command.ss_command, logfile); //stead-state
179181
// blind_execute(command.dec_command, logfile); //decel
180182
// // At this point, a new thread/command sends new commands. This can be stop, or it can be a new direction, etc.
181183
//}
182184

183-
void Command_Interpreter_RPi5::blind_execute(const CommandComponent& commandComponent, std::ofstream& logfile) {
185+
void Command_Interpreter_RPi5::blind_execute(const CommandComponent &commandComponent) {
184186
auto endTime = std::chrono::system_clock::now() + commandComponent.duration;
185187
auto currentTime = std::chrono::system_clock::now();
186-
execute(commandComponent.thruster_pwms, logfile);
188+
execute(commandComponent.thruster_pwms);
187189
while (currentTime < endTime) {
188190
currentTime = std::chrono::system_clock::now();
189191
}
190192
}
191193

192-
//void Command_Interpreter_RPi5::corrective_execute(command_component command, std::ofstream logfile) {
194+
//void Command_Interpreter_RPi5::corrective_execute(command_component command, std::ostream logfile) {
193195
// adjusted_command = copy(command)
194196
// while (!time_is_up()) {
195197
// adjusted_command = correct_command();

0 commit comments

Comments
 (0)