6
6
#include < ctime>
7
7
#include < utility>
8
8
9
- DigitalPin::DigitalPin (int gpioNumber, EnableType enableType): Pin(gpioNumber), enableType(enableType) {}
10
-
11
- void DigitalPin::initialize (WiringControl& wiringControl) {
9
+ void DigitalPin::initialize (WiringControl &wiringControl) {
12
10
switch (enableType) {
13
11
case ActiveLow:
14
12
wiringControl.setPinType (gpioNumber, DigitalActiveLow);
@@ -17,12 +15,12 @@ void DigitalPin::initialize(WiringControl& wiringControl) {
17
15
wiringControl.setPinType (gpioNumber, DigitalActiveHigh);
18
16
break ;
19
17
default :
20
- std::cerr << " Impossible digital pin type " << enableType << " ! Exiting." << std::endl;
18
+ errorLog << " Impossible digital pin type " << enableType << " ! Exiting." << std::endl;
21
19
exit (42 );
22
20
}
23
21
}
24
22
25
- void DigitalPin::enable (WiringControl& wiringControl) {
23
+ void DigitalPin::enable (WiringControl & wiringControl) {
26
24
switch (enableType) {
27
25
case ActiveHigh:
28
26
wiringControl.digitalWrite (gpioNumber, High);
@@ -31,12 +29,12 @@ void DigitalPin::enable(WiringControl& wiringControl) {
31
29
wiringControl.digitalWrite (gpioNumber, Low);
32
30
break ;
33
31
default :
34
- std::cerr << " Impossible pin mode!" << std::endl;
32
+ errorLog << " Impossible pin mode!" << std::endl;
35
33
exit (42 );
36
34
}
37
35
}
38
36
39
- void DigitalPin::disable (WiringControl& wiringControl) {
37
+ void DigitalPin::disable (WiringControl & wiringControl) {
40
38
switch (enableType) {
41
39
case ActiveHigh:
42
40
wiringControl.digitalWrite (gpioNumber, Low);
@@ -45,122 +43,126 @@ void DigitalPin::disable(WiringControl& wiringControl) {
45
43
wiringControl.digitalWrite (gpioNumber, High);
46
44
break ;
47
45
default :
48
- std::cerr << " Impossible pin mode!" << std::endl;
46
+ errorLog << " Impossible pin mode!" << std::endl;
49
47
exit (42 );
50
48
}
51
49
}
52
50
53
- bool DigitalPin::enabled (WiringControl& wiringControl) {
51
+ bool DigitalPin::enabled (WiringControl & wiringControl) {
54
52
switch (enableType) {
55
53
case ActiveHigh:
56
54
return wiringControl.digitalRead (gpioNumber) == High;
57
55
case ActiveLow:
58
56
return wiringControl.digitalRead (gpioNumber) == Low;
59
57
default :
60
- std::cerr << " Impossible pin enable type! Exiting." << std::endl;
58
+ errorLog << " Impossible pin enable type! Exiting." << std::endl;
61
59
exit (42 );
62
60
}
63
61
}
64
62
65
- int DigitalPin::read (WiringControl& wiringControl) {
63
+ int DigitalPin::read (WiringControl & wiringControl) {
66
64
return wiringControl.digitalRead (gpioNumber);
67
65
}
68
66
69
- void PwmPin::setPwm (int pulseWidth, WiringControl& wiringControl, std::ofstream &logFile ) {
67
+ void PwmPin::setPwm (int pulseWidth, WiringControl &wiringControl ) {
70
68
setPowerAndDirection (pulseWidth, wiringControl);
71
69
std::time_t currentTime = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now ());
72
- logFile << " Current time: " << std::ctime (¤tTime) << std::endl;
73
- logFile << " Thruster at pin " << gpioNumber << " : " << pulseWidth << std::endl;
70
+ outLog << " Current time: " << std::ctime (¤tTime) << std::endl;
71
+ outLog << " Thruster at pin " << gpioNumber << " : " << pulseWidth << std::endl;
74
72
}
75
73
76
- HardwarePwmPin::HardwarePwmPin (int gpioNumber): PwmPin(gpioNumber) {}
77
74
78
- void HardwarePwmPin::initialize (WiringControl& wiringControl) {
75
+ void HardwarePwmPin::initialize (WiringControl & wiringControl) {
79
76
wiringControl.setPinType (gpioNumber, HardwarePWM);
80
77
}
81
78
82
- void HardwarePwmPin::enable (WiringControl& wiringControl) {
79
+ void HardwarePwmPin::enable (WiringControl & wiringControl) {
83
80
wiringControl.pwmWriteMaximum (gpioNumber);
84
81
}
85
82
86
- void HardwarePwmPin::disable (WiringControl& wiringControl) {
83
+ void HardwarePwmPin::disable (WiringControl & wiringControl) {
87
84
wiringControl.pwmWriteOff (gpioNumber);
88
85
}
89
86
90
- bool HardwarePwmPin::enabled (WiringControl& wiringControl) {
87
+ bool HardwarePwmPin::enabled (WiringControl & wiringControl) {
91
88
return wiringControl.pwmRead (gpioNumber).pulseWidth != 1500 ;
92
89
}
93
90
94
- void HardwarePwmPin::setPowerAndDirection (int pwmValue, WiringControl& wiringControl) {
91
+ void HardwarePwmPin::setPowerAndDirection (int pwmValue, WiringControl & wiringControl) {
95
92
wiringControl.pwmWrite (gpioNumber, pwmValue);
96
93
}
97
94
98
- int HardwarePwmPin::read (WiringControl& wiringControl) {
95
+ int HardwarePwmPin::read (WiringControl & wiringControl) {
99
96
return wiringControl.pwmRead (gpioNumber).pulseWidth ;
100
97
}
101
98
102
- SoftwarePwmPin::SoftwarePwmPin (int gpioNumber): PwmPin(gpioNumber) {}
103
99
104
- void SoftwarePwmPin::initialize (WiringControl& wiringControl) {
100
+ void SoftwarePwmPin::initialize (WiringControl & wiringControl) {
105
101
wiringControl.setPinType (gpioNumber, SoftwarePWM);
106
102
}
107
103
108
- void SoftwarePwmPin::enable (WiringControl& wiringControl) {
104
+ void SoftwarePwmPin::enable (WiringControl & wiringControl) {
109
105
wiringControl.pwmWriteMaximum (gpioNumber);
110
106
}
111
107
112
- void SoftwarePwmPin::disable (WiringControl& wiringControl) {
108
+ void SoftwarePwmPin::disable (WiringControl & wiringControl) {
113
109
wiringControl.pwmWriteOff (gpioNumber);
114
110
}
115
111
116
- bool SoftwarePwmPin::enabled (WiringControl& wiringControl) {
112
+ bool SoftwarePwmPin::enabled (WiringControl & wiringControl) {
117
113
return wiringControl.pwmRead (gpioNumber).pulseWidth != 1500 ;
118
114
}
119
115
120
- void SoftwarePwmPin::setPowerAndDirection (int pwmValue, WiringControl& wiringControl) {
116
+ void SoftwarePwmPin::setPowerAndDirection (int pwmValue, WiringControl & wiringControl) {
121
117
wiringControl.pwmWrite (gpioNumber, pwmValue);
122
118
}
123
119
124
- int SoftwarePwmPin::read (WiringControl& wiringControl) {
120
+ int SoftwarePwmPin::read (WiringControl & wiringControl) {
125
121
return wiringControl.pwmRead (gpioNumber).pulseWidth ;
126
122
}
127
123
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) {
130
131
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;
132
134
exit (42 );
133
135
}
134
136
}
135
137
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 *>{};
138
140
allPins.insert (allPins.end (), this ->thrusterPins .begin (), this ->thrusterPins .end ());
139
141
allPins.insert (allPins.end (), this ->digitalPins .begin (), this ->digitalPins .end ());
140
142
return allPins;
141
143
}
142
144
143
145
void Command_Interpreter_RPi5::initializePins () {
144
146
if (!wiringControl.initializeGPIO ()) {
145
- std::cerr << " Failure to configure GPIO pins!" << std::endl;
147
+ errorLog << " Failure to configure GPIO pins!" << std::endl;
146
148
exit (42 );
147
149
}
148
- for (Pin* pin : allPins ()) {
150
+ for (Pin * pin: allPins ()) {
149
151
pin->initialize (wiringControl);
150
152
}
151
153
}
152
154
153
- void Command_Interpreter_RPi5::execute (pwm_array thrusterPwms, std::ofstream& logFile ) {
155
+ void Command_Interpreter_RPi5::execute (pwm_array thrusterPwms) {
154
156
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);
157
159
i++;
158
160
}
159
161
}
160
162
161
163
std::vector<int > Command_Interpreter_RPi5::readPins () {
162
164
std::vector<int > pinValues;
163
- for (auto pin : allPins ()) {
165
+ for (auto pin: allPins ()) {
164
166
pinValues.push_back (pin->read (wiringControl));
165
167
}
166
168
return pinValues;
@@ -172,24 +174,24 @@ Command_Interpreter_RPi5::~Command_Interpreter_RPi5() {
172
174
}
173
175
}
174
176
175
- // move_forwards(float distance, std::ofstream logfile) {
177
+ // move_forwards(float distance, std::ostream logfile) {
176
178
// command = calculate_stuff(distance, current_robot_data);
177
179
// blind_execute(command.accel_command, logfile); //accel
178
180
// blind_execute(command.ss_command, logfile); //stead-state
179
181
// blind_execute(command.dec_command, logfile); //decel
180
182
// // At this point, a new thread/command sends new commands. This can be stop, or it can be a new direction, etc.
181
183
// }
182
184
183
- void Command_Interpreter_RPi5::blind_execute (const CommandComponent& commandComponent, std::ofstream& logfile ) {
185
+ void Command_Interpreter_RPi5::blind_execute (const CommandComponent &commandComponent ) {
184
186
auto endTime = std::chrono::system_clock::now () + commandComponent.duration ;
185
187
auto currentTime = std::chrono::system_clock::now ();
186
- execute (commandComponent.thruster_pwms , logfile );
188
+ execute (commandComponent.thruster_pwms );
187
189
while (currentTime < endTime) {
188
190
currentTime = std::chrono::system_clock::now ();
189
191
}
190
192
}
191
193
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) {
193
195
// adjusted_command = copy(command)
194
196
// while (!time_is_up()) {
195
197
// adjusted_command = correct_command();
0 commit comments