-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequenced_interruptable_system.cpp
216 lines (173 loc) · 7.14 KB
/
sequenced_interruptable_system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <memory>
#include <list>
#include <vector>
#include <chrono>
#include "pico/stdlib.h"
#include "../controller.h"
#include "../trafficlight_group.h"
#include "../sequence.h"
#include "../common_sequences.h"
#include "sequenced_interruptable_system.h"
SequencedInterruptableSystem::SequencedInterruptableSystem()
{
}
SequencedInterruptableSystem::SequencedInterruptableSystem(LightType lightType, CrossingType crossingType, SequenceType sequenceType) : SequencedInterruptableSystem()
{
setLightType(lightType);
setCrossingType(crossingType);
setSequenceType(sequenceType);
}
SequencedInterruptableSystem::SequencedInterruptableSystem(std::vector<std::shared_ptr<TrafficLight>> trafficLights, SequenceType sequenceType, LightType lightType, CrossingType crossingType) : SequencedInterruptableSystem(lightType, crossingType, sequenceType)
{
std::vector<std::shared_ptr<TrafficLightGroup>> groups;
for (auto trafficLight : trafficLights) {
auto group = std::make_shared<TrafficLightGroup>();
group->addTrafficLight(trafficLight);
groups.push_back(group);
}
setGroups(groups);
setUp();
}
SequencedInterruptableSystem::SequencedInterruptableSystem(std::vector<std::shared_ptr<TrafficLightGroup>> trafficLightGroups, SequenceType sequenceType, LightType lightType, CrossingType crossingType) : SequencedInterruptableSystem(lightType, crossingType, sequenceType)
{
setGroups(trafficLightGroups);
setUp();
}
void SequencedInterruptableSystem::requestCrossing()
{
_crossingRequested = true;
}
void SequencedInterruptableSystem::requestNextGroup()
{
_nextGroupRequested = true;
}
void SequencedInterruptableSystem::setLightType(LightType lightType)
{
_lightType = lightType;
}
void SequencedInterruptableSystem::setCrossingType(CrossingType crossingType)
{
_crossingType = crossingType;
}
void SequencedInterruptableSystem::setSequenceType(SequenceType sequenceType)
{
_sequenceType = sequenceType;
}
void SequencedInterruptableSystem::setTiming(SequencedInterruptableSystemTimings timing, std::chrono::milliseconds time)
{
setTimingInternal(timing, time);
}
void SequencedInterruptableSystem::setTiming(SequencedInterruptableSystemTimings timing, std::chrono::milliseconds time, unsigned int groupId)
{
setTimingInternal(timing, time, groupId);
}
void SequencedInterruptableSystem::reset()
{
_currentGroup = 0;
}
void SequencedInterruptableSystem::run()
{
reset();
do {
doRedToGreen();
awaitNextGroupRequested();
doGreenToRed();
doCrossingIfRequested();
}
while (advanceToNextGroup());
}
void SequencedInterruptableSystem::setGroups(std::vector<std::shared_ptr<TrafficLightGroup>> groups)
{
_groups = groups;
}
void SequencedInterruptableSystem::setUp()
{
setUpAllLightsGroup();
}
void SequencedInterruptableSystem::setUpAllLightsGroup()
{
_allLightsGroup = std::make_shared<TrafficLightGroup>();
for (auto group : _groups) {
auto groupLights = group->getTrafficLights();
for (auto trafficLight : groupLights) {
_allLightsGroup->addTrafficLight(trafficLight);
}
}
}
void SequencedInterruptableSystem::awaitNextGroupRequested()
{
while (_sequenceType == SequenceType::Manual && !_nextGroupRequested) {
sleep_ms(100);
}
sleep_ms(100);
_nextGroupRequested = false;
}
void SequencedInterruptableSystem::doRedToGreen()
{
auto group = getCurrentGroup();
auto redToGreenController = std::make_shared<Controller>();
auto staticRedSequence = std::make_shared<StaticSequence>(getTimingForCurrentGroup(SequencedInterruptableSystemTimings::DelayUntilGreenLight));
auto redToGreenSequence = std::make_shared<RedToGreenSequence>(getTimingForCurrentGroup(SequencedInterruptableSystemTimings::MinimumTimeUntilRedLight), _lightType == LightType::Red_Green ? RedToGreenSequence::SequenceType::Red_Green : RedToGreenSequence::SequenceType::Red_Yellow_Green);
redToGreenController->addTrafficLightGroup(_allLightsGroup, 0);
redToGreenController->addTrafficLightGroup(group, 1);
redToGreenController->addSequence(staticRedSequence, 0);
redToGreenController->addSequence(redToGreenSequence, 1);
redToGreenController->run();
}
void SequencedInterruptableSystem::doGreenToRed()
{
auto group = getCurrentGroup();
auto greenToRedController = std::make_shared<Controller>();
auto greenToRedSequence = std::make_shared<GreenToRedSequence>(std::chrono::milliseconds(0));
greenToRedController->addTrafficLightGroup(_allLightsGroup, 0);
greenToRedController->addTrafficLightGroup(group, 1);
greenToRedController->addSequence(greenToRedSequence, 1);
greenToRedController->run();
}
void SequencedInterruptableSystem::doCrossingIfRequested()
{
if (_crossingRequested && _crossingType != CrossingType::None) {
auto crossingController = std::make_shared<Controller>();
auto staticRedSequence = std::make_shared<StaticSequence>(getTimingForCurrentGroup(SequencedInterruptableSystemTimings::DelayUntilGreenCrossing));
auto redCrossingToGreenCrossingSequence = std::make_shared<RedCrossingToGreenCrossingSequence>(getTimingForCurrentGroup(SequencedInterruptableSystemTimings::CrossingTime));
auto staticOffSequence = std::make_shared<StaticSequence>(getTimingForCurrentGroup(SequencedInterruptableSystemTimings::OffTimeBetweenGreenAndRedCrossing), TrafficLight::Light::Red);
auto greenCrossingToRedCrossingSequence = std::make_shared<GreenCrossingToRedCrossingSequence>(std::chrono::milliseconds(0));
crossingController->addTrafficLightGroup(_allLightsGroup, 0);
crossingController->addSequence(staticRedSequence, 0);
crossingController->addSequence(redCrossingToGreenCrossingSequence, 0);
crossingController->addSequence(staticOffSequence, 0);
crossingController->addSequence(greenCrossingToRedCrossingSequence, 0);
_crossingRequested = false;
crossingController->run();
}
}
bool SequencedInterruptableSystem::advanceToNextGroup()
{
if (++_currentGroup < _groups.size()) {
return true;
}
return false;
}
std::chrono::milliseconds SequencedInterruptableSystem::getTimingForCurrentGroup(SequencedInterruptableSystemTimings timing) const
{
return getTiming(timing, _currentGroup);
}
std::chrono::milliseconds SequencedInterruptableSystem::getStandardTiming(SequencedInterruptableSystemTimings timing) const
{
switch (timing) {
case SequencedInterruptableSystemTimings::CrossingTime:
return std::chrono::seconds(8);
case SequencedInterruptableSystemTimings::DelayUntilGreenCrossing:
case SequencedInterruptableSystemTimings::DelayUntilGreenLight:
return std::chrono::seconds(2);
case SequencedInterruptableSystemTimings::MinimumTimeUntilRedLight:
return std::chrono::seconds(10);
case SequencedInterruptableSystemTimings::OffTimeBetweenGreenAndRedCrossing:
return std::chrono::seconds(4);
}
return std::chrono::milliseconds(0);
}
std::shared_ptr<TrafficLightGroup> SequencedInterruptableSystem::getCurrentGroup() const
{
return _groups[_currentGroup];
}