forked from Karlkorv/dependable-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
270 lines (232 loc) · 9.15 KB
/
Copy pathRobot.java
File metadata and controls
270 lines (232 loc) · 9.15 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
public class Robot {
double armHeightExpected = 0;
double armLengthExpected = 0;
boolean grippingBoxExpected = false;
int detectedErrors = 0;
static final int MAX_DETECTED_ERRORS = 5;
double armHeight = 0;
double armLength = 0;
boolean grippingBox = false;
double physicalFailureRate = 0.4;
double softwareFailureRate = 0.1;
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);
}
// ANSI color helpers
String red(String s) {
return "\u001B[31m" + s + "\u001B[0m";
}
String green(String s) {
return "\u001B[32m" + s + "\u001B[0m";
}
// Trigger these when we move something
boolean checkArmHeight = false;
boolean checkArmLength = false;
boolean checkGrippingBox = false;
// ===== Sensors =====
double getArmHeight() {
printProgress("Getting arm height ", 3, 150);
sleepMs(1000);
if (Math.random() < softwareFailureRate) {
System.out.println(red("ERROR: Software failure in getArmHeight"));
printProgress(red("ERROR: Returning incorrect arm height "), 2, 120);
sleepMs(1000);
return armHeight * Math.random();
}
System.out.println("-> Arm height: " + String.format("%.3f", armHeight));
return armHeight;
}
double getArmLength() {
printProgress("Getting arm length ", 3, 120);
sleepMs(1000);
if (Math.random() < softwareFailureRate) {
System.out.println(red("ERROR: Software failure in getArmLength"));
printProgress(red("ERROR: Returning incorrect arm length "), 2, 100);
sleepMs(1000);
return armLength * Math.random();
}
System.out.println("-> Arm length: " + String.format("%.3f", armLength));
return armLength;
}
boolean isGrippingBox() {
printProgress("Checking gripper state ", 3, 140);
sleepMs(1000);
if (Math.random() < softwareFailureRate) {
System.out.println(red("ERROR: Software failure in isGrippingBox"));
printProgress(red("ERROR: Returning incorrect gripping status "), 2, 110);
sleepMs(1000);
return !grippingBox;
}
System.out.println("-> Gripping box: " + grippingBox);
return grippingBox;
}
// ===== Actuators =====
void setArmHeight(double height) {
checkArmHeight = true;
armHeightExpected = height;
// Simulate gradual movement
double start = armHeight;
double delta = height - start;
int steps = 4;
System.out.print("Setting arm height to " + armHeightExpected + " ");
for (int i = 1; i <= steps; i++) {
double interim = start + delta * ((double) i / steps);
System.out.print(String.format("[%.2f]", interim));
System.out.flush();
sleepMs(1000);
}
System.out.println();
if (Math.random() < physicalFailureRate) {
System.out.println(red("ERROR: Physical failure in setArmHeight"));
printProgress(red("ERROR: Arm height set incorrectly "), 2, 120);
sleepMs(1000);
armHeight = height * Math.random();
} else {
armHeight = height;
}
}
void setArmLength(double length) {
checkArmLength = true;
armLengthExpected = length;
// Simulate extension/retraction with dots
String action = length > armLength ? "Extending" : "Retracting";
printProgress(action + " arm length to " + armLengthExpected + " ", 4, 140);
if (Math.random() < physicalFailureRate) {
System.out.println(red("ERROR: Physical failure in setArmLength"));
printProgress(red("ERROR: Arm length set incorrectly "), 2, 100);
sleepMs(1000);
armLength = length * Math.random();
} else {
armLength = length;
}
}
void setBoxGripState(boolean grip) {
checkGrippingBox = true;
grippingBoxExpected = grip;
String verb = grippingBoxExpected ? "Closing" : "Opening";
printProgress(verb + " gripper ", 3, 160);
if (Math.random() < physicalFailureRate) {
System.out.println(red("Physical failure in switchBoxGrip"));
printProgress(red("Box grip state not changed "), 2, 130);
return;
}
grippingBox = grippingBoxExpected;
System.out.println("-> Gripper now: " + grippingBox);
}
void verifySystem() {
System.out.println("Verifying system integrity");
if (detectedErrors > MAX_DETECTED_ERRORS) {
throw new RuntimeException("Too many errors detected! System shutting down.");
}
if (checkArmHeight) {
double measured = getArmHeight();
if (Double.isNaN(measured) || measured != armHeightExpected) {
detectedErrors++;
System.out.println(
red("Error detected in arm height: expected=" + armHeightExpected + " measured=" + measured));
System.out.println(red("Resetting arm height to expected value"));
sleepMs(1000);
setArmHeight(armHeightExpected);
System.out.println("Re-verifying system after correction");
verifySystem();
} else {
System.out.println(green("Arm height OK"));
}
}
if (checkArmLength) {
double measured = getArmLength();
if (Double.isNaN(measured) || measured != armLengthExpected) {
detectedErrors++;
System.out.println(
red("Error detected in arm length: expected=" + armLengthExpected + " measured=" + measured));
System.out.println(red("Resetting arm length to expected value"));
sleepMs(1000);
setArmLength(armLengthExpected);
System.out.println("Re-verifying system after correction");
verifySystem();
} else {
System.out.println(green("Arm length OK"));
}
}
if (checkGrippingBox) {
boolean measured = isGrippingBox();
if (measured != grippingBoxExpected) {
detectedErrors++;
System.out.println(red("Error detected in gripping status: expected=" + grippingBoxExpected
+ " measured=" + measured));
System.out.println(red("Resetting gripping status to expected value"));
sleepMs(1000);
setBoxGripState(grippingBoxExpected);
System.out.println("Re-verifying system after correction");
verifySystem();
} else {
System.out.println(green("Gripping status OK"));
}
}
checkArmHeight = false;
checkArmLength = false;
checkGrippingBox = false;
if (detectedErrors > MAX_DETECTED_ERRORS) {
throw new RuntimeException(red("Too many errors detected! System shutting down."));
}
// All checks passed
System.out.println(green("System integrity verified"));
}
void fetchBoxFromHeight(double height) {
System.out.println("Starting box fetch operation");
printProgress("Raising arm to " + height + "m ", 5, 140);
setArmHeight(height);
sleepMs(1000);
verifySystem();
printProgress("Extending arm to reach box ", 5, 120);
setArmLength(1.0);
sleepMs(1000);
verifySystem();
printProgress("Closing gripper to pick box ", 4, 140);
setBoxGripState(true);
sleepMs(1000);
verifySystem();
printProgress("Retracting arm with box ", 5, 110);
setArmLength(0.0);
sleepMs(1000);
verifySystem();
printProgress("Lowering arm to home position ", 5, 140);
setArmHeight(0.0);
sleepMs(1000);
verifySystem();
printProgress("Opening gripper to release box ", 4, 140);
setBoxGripState(false);
sleepMs(1000);
verifySystem();
}
public static void main(String[] args) {
Robot robot = new Robot();
System.out.println();
System.out.println("Running robot with physical/sofware failure rate:");
System.out.println(robot.physicalFailureRate * 100 + "% physical failure rate"
+ " and " + robot.softwareFailureRate * 100 + "% software failure rate.");
System.out.println();
try {
robot.fetchBoxFromHeight(2.0);
System.out.println("Box fetched successfully!");
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
}