-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssignment2.java
More file actions
296 lines (259 loc) · 10.6 KB
/
Assignment2.java
File metadata and controls
296 lines (259 loc) · 10.6 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
public class Assignment2 {
static class WorldState {
static volatile boolean waitingPositionAvailable = true;
static volatile boolean loadingPositionAvailable = true;
static volatile boolean boxInFetchingPosition = false;
static volatile boolean boxOnBack = false;
}
static class CB {
double signalRate = 0.6;
double softwareFailureRate = 0.08;
boolean signalRobotToMoveToLoadingPosition() {
printProgress("CB: Attempting to signal robot to move to loading position ", 3, 300);
if (Math.random() < softwareFailureRate) {
System.out.println(red("CB: software glitch, not sending correct signal"));
return false;
}
boolean s = Math.random() < signalRate;
System.out.println("CB: signal sent? " + s);
return s;
}
boolean signalGrabBox() {
printProgress("CB: Attempting to signal robot to grab box ", 3, 300);
boolean s = Math.random() < signalRate;
System.out.println("CB: grab signal sent? " + s);
return s;
}
boolean signalRobotAtLoadingPosition() {
printProgress("CB: Attempting to acknowledge robot at loading position ", 3, 300);
boolean s = Math.random() < signalRate;
System.out.println("CB: ack signal sent? " + s);
return s;
}
boolean signalRobotToDeliverBox() {
printProgress("CB: Attempting to signal robot to deliver box ", 3, 300);
boolean s = Math.random() < signalRate;
System.out.println("CB: deliver signal sent? " + s);
return s;
}
boolean deliverBoxToFetchingPosition() {
printProgress("CB: Attempting to move box to belt ", 3, 300);
boolean s = Math.random() < 0.9;
System.out.println("CB: box moved to belt? " + s);
return s;
}
}
static class Robot {
boolean inWaitingPosition = false;
boolean inLoadingPosition = false;
boolean grippingBox = false;
double softwareFailureRate = 0.05;
void sleepMs(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
void printDots(int count, long betweenMs) {
for (int i = 0; i < count; i++) {
System.out.print(".");
System.out.flush();
sleepMs(betweenMs);
}
System.out.println();
}
void printProgress(String message, int dots, long betweenMs) {
System.out.print(message);
printDots(dots, betweenMs);
}
String red(String s) {
return "\u001B[31m" + s + "\u001B[0m";
}
boolean inWaitingPosition() {
return inWaitingPosition;
}
boolean inLoadingPosition() {
return inLoadingPosition;
}
void moveToWaitingPosition() {
printProgress("Robot: Moving to waiting position ", 4, 300);
sleepMs(600);
inWaitingPosition = true;
inLoadingPosition = false;
System.out.println("Robot: inWaitingPosition=" + inWaitingPosition);
}
void moveToLoadingPosition() {
printProgress("Robot: Moving to loading position ", 4, 300);
sleepMs(600);
inWaitingPosition = false;
inLoadingPosition = true;
System.out.println("Robot: inLoadingPosition=" + inLoadingPosition);
}
boolean awaitSignalInWaitingPosition(boolean incomingSignal) {
printProgress("Robot: Awaiting signal in waiting position ", 3, 300);
sleepMs(400);
boolean received = incomingSignal && Math.random() < 0.9;
System.out.println("Robot: signal received? " + received);
if (received) {
boolean available = radarCheckWaiting();
System.out.println("Robot: waiting position available? " + available);
return available;
}
return false;
}
boolean awaitSignalToMoveToLoadingPosition(boolean incomingSignal) {
printProgress("Robot: Awaiting move-to-loading signal ", 3, 300);
sleepMs(400);
boolean received = incomingSignal && Math.random() < 0.9;
System.out.println("Robot: move signal received? " + received);
if (received) {
boolean available = radarCheckLoading();
System.out.println("Robot: loading position available? " + available);
return available;
}
return false;
}
boolean awaitSignalGrabBox(boolean incomingSignal) {
printProgress("Robot: Awaiting grab-box signal from CB ", 3, 300);
sleepMs(400);
boolean received = incomingSignal && Math.random() < 0.9;
System.out.println("Robot: grab signal received? " + received);
return received;
}
boolean radarCheckWaiting() {
printProgress("Robot: Checking waiting position with radar ", 3, 100);
sleepMs(400);
if (Math.random() < softwareFailureRate) {
System.out.println(red("Robot: radar false negative/positive"));
return !WorldState.waitingPositionAvailable;
}
return WorldState.waitingPositionAvailable;
}
boolean radarCheckLoading() {
printProgress("Robot: Checking loading position with radar ", 3, 100);
sleepMs(400);
if (Math.random() < softwareFailureRate) {
System.out.println(red("Robot: radar false reading"));
return !WorldState.loadingPositionAvailable;
}
return WorldState.loadingPositionAvailable;
}
boolean weightCheckBack() {
printProgress("Robot: Checking back weight sensor ", 3, 100);
sleepMs(300);
if (Math.random() < softwareFailureRate) {
System.out.println(red("Robot: weight sensor unreliable"));
return !WorldState.boxOnBack;
}
return WorldState.boxOnBack;
}
void signalCBLoadingEntered() {
printProgress("Robot: Signalling CB that loading position entered ", 3, 300);
sleepMs(200);
WorldState.loadingPositionAvailable = false;
}
void signalCBOperationComplete() {
printProgress("Robot: Signalling CB operation complete ", 3, 300);
sleepMs(200);
WorldState.loadingPositionAvailable = true;
}
void pickBoxFromFetching() {
printProgress("Robot: Closing gripper to pick box ", 3, 300);
sleepMs(400);
grippingBox = true;
WorldState.boxInFetchingPosition = false;
WorldState.boxOnBack = true;
System.out.println("Robot: grippingBox=" + grippingBox + " boxOnBack=" + WorldState.boxOnBack);
}
}
static void printDots(int count, long betweenMs) {
for (int i = 0; i < count; i++) {
System.out.print(".");
System.out.flush();
try {
Thread.sleep(betweenMs);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.println();
}
static void printProgress(String message, int dots, long betweenMs) {
System.out.print(message);
printDots(dots, betweenMs);
}
static String red(String s) {
return "\u001B[31m" + s + "\u001B[0m";
}
static String green(String s) {
return "\u001B[32m" + s + "\u001B[0m";
}
public static void main(String[] args) {
Robot robot = new Robot();
CB cb = new CB();
System.out.println();
System.out.println("Starting simulation");
System.out.println();
robot.moveToWaitingPosition();
boolean signal = false;
int waitAttempts = 0;
int maxWaitAttempts = 8;
while (robot.inWaitingPosition() && waitAttempts < maxWaitAttempts) {
boolean handled = robot.awaitSignalInWaitingPosition(signal);
if (handled) {
printProgress("Main: robot accepted signal and waiting position verified ", 3, 300);
break;
}
signal = cb.signalRobotToMoveToLoadingPosition();
waitAttempts++;
}
if (!robot.inWaitingPosition() || waitAttempts >= maxWaitAttempts) {
System.out.println(red("Main: Timeout waiting for move-to-loading signal or position not available"));
return;
}
boolean moveAllowed = robot.awaitSignalToMoveToLoadingPosition(signal);
if (!moveAllowed) {
System.out.println(red("Main: Move to loading denied after verification"));
return;
}
robot.moveToLoadingPosition();
robot.signalCBLoadingEntered();
int deliverAttempts = 0;
do {
WorldState.boxInFetchingPosition = cb.deliverBoxToFetchingPosition();
deliverAttempts++;
} while (!WorldState.boxInFetchingPosition && deliverAttempts < 5);
boolean grabSignal = false;
int grabAttempts = 0;
int maxGrabAttempts = 6;
while (robot.inLoadingPosition() && grabAttempts < maxGrabAttempts) {
boolean received = robot.awaitSignalGrabBox(grabSignal);
if (received) {
if (WorldState.boxInFetchingPosition) {
robot.pickBoxFromFetching();
boolean weightOk = robot.weightCheckBack();
System.out.println("Main: weight check says boxOnBack=" + weightOk);
if (!weightOk) {
System.out.println(red("Main: Weight sensor disagrees, aborting fetch"));
return;
}
robot.signalCBOperationComplete();
System.out.println(green("Main: Box has been fetched"));
break;
} else {
System.out.println(red("Main: No box in fetching position"));
return;
}
}
grabSignal = cb.signalGrabBox();
grabAttempts++;
}
if (grabAttempts >= maxGrabAttempts) {
System.out.println(red("Main: Timeout waiting for grab-box signal"));
return;
}
System.out.println();
System.out.println(green("Simulation finished"));
}
}