Skip to content

Commit 2e48ce0

Browse files
committed
Improved error handling which sends error messages to UI.
1 parent e3c4050 commit 2e48ce0

File tree

3 files changed

+107
-38
lines changed

3 files changed

+107
-38
lines changed

backend/server/programs.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ Runtime = function() {
115115
messageType: 'std_msgs/Bool',
116116
latch: true
117117
});
118+
var errorTopic = new ROSLIB.Topic({
119+
ros: ROS,
120+
name: 'code_it/errors',
121+
messageType: 'std_msgs/String'
122+
});
123+
124+
var publishError = function(message) {
125+
var msg = new ROSLIB.Message({data: message});
126+
console.log('Publishing error: ' + message);
127+
errorTopic.publish(msg);
128+
};
118129

119130
var onProgramEnd = function() {
120131
var msg = new ROSLIB.Message({data: false});
@@ -139,6 +150,11 @@ Runtime = function() {
139150
}
140151
try {
141152
if (interpreter.step()) {
153+
var error = Robot.getError();
154+
if (error) {
155+
publishError(error);
156+
Robot.setError('');
157+
}
142158
var feedback = {block_id: interpreter.blockId};
143159
if (action.currentGoal) {
144160
action.sendFeedback(feedback);
@@ -154,10 +170,11 @@ Runtime = function() {
154170
onProgramEnd();
155171
}
156172
} catch(e) {
157-
console.log('Error running program ' + program);
173+
console.log('Error: ');
158174
console.log(e);
159-
console.log(e.stack);
175+
e.stack && console.log(e.stack);
160176
action.setAborted(e.toString());
177+
publishError('There was an error while running the program: ' + e.toString());
161178
onProgramEnd();
162179
}
163180
}

backend/server/robot.js

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// The implementation of the primitives.
22
Robot = function() {
3+
var error = ''; // Most recent error message, empty string for no error.
4+
5+
var getError = function() {
6+
return error;
7+
}
8+
9+
var setError = function(message) {
10+
error = message;
11+
}
12+
313
var askMultipleChoice = Meteor.wrapAsync(function(question, choices, timeout, callback) {
414
console.log('Asking: ' + question + ', choices: ' + choices);
515
var client = new ROSLIB.Service({
@@ -18,12 +28,16 @@ Robot = function() {
1828
client.callService(request, function(result) {
1929
if (funcCall.isRunning) {
2030
funcCall.isRunning = false;
31+
setError(result.error);
32+
if (result.error) {
33+
callback(result.error, null); // Must quit program, need user input to proceed.
34+
}
2135
callback(null, result.choice);
2236
}
2337
}, function(error) {
2438
if (funcCall.isRunning) {
2539
funcCall.isRunning = false;
26-
callback(error ? error : true, null);
40+
callback(error ? error : 'Failed to ask multiple choice question.', null);
2741
}
2842
});
2943

@@ -55,10 +69,13 @@ Robot = function() {
5569
funcCall.isRunning = true;
5670

5771
client.callService(request, function(result) {
72+
funcCall.isRunning = false;
73+
setError(result.error); // Failure is probaby not critical enough to quit over.
74+
callback(null, null);
5875
}, function(error) {
5976
if (funcCall.isRunning) {
6077
funcCall.isRunning = false;
61-
callback(error ? error : true, null);
78+
callback(error ? error : 'Failed to display message.', null);
6279
}
6380
});
6481

@@ -69,11 +86,6 @@ Robot = function() {
6986
callback(null, null); // err, result
7087
}
7188
}, timeout * 1000);
72-
} else {
73-
if (funcCall.isRunning) {
74-
funcCall.isRunning = false;
75-
callback(null, null);
76-
}
7789
}
7890
});
7991

@@ -87,9 +99,14 @@ Robot = function() {
8799

88100
var request = new ROSLIB.ServiceRequest({});
89101
client.callService(request, function(result) {
90-
callback(null, result.objects);
102+
setError(result.error);
103+
if (result.error) {
104+
callback(null, []); // Return empty list on error.
105+
} else {
106+
callback(null, result.objects);
107+
}
91108
}, function() {
92-
callback(true, []);
109+
callback(error ? error : 'Failed to look for objects.', []);
93110
});
94111
});
95112

@@ -108,15 +125,13 @@ Robot = function() {
108125
client.callService(request, function(result) {
109126
console.log('Done navigating to ' + location + ', result:');
110127
console.log(result);
128+
setError(result.error);
111129
if (result.error !== '') { // Navigation failed
112130
callback(null, false); // result = false
113131
}
114132
callback(null, true); // Success
115133
}, function(error) {
116-
// Failure callback
117-
console.log('GoTo service call failed.');
118-
console.log(error);
119-
callback(error ? error : true, null);
134+
callback(error ? error : 'Failed to run navigation.', null);
120135
});
121136
});
122137

@@ -134,15 +149,13 @@ Robot = function() {
134149
client.callService(request, function(result) {
135150
console.log('Done docking');
136151
console.log(result);
152+
setError(result.error);
137153
if (result.error !== '') { // Docking failed
138154
callback(null, false); // result = false
139155
}
140156
callback(null, true); // Success
141157
}, function(error) {
142-
// Failure callback
143-
console.log('GoToDock service call failed.');
144-
console.log(error);
145-
callback(error ? error : true, null);
158+
callback(error ? error : 'Failed to run navigation to dock.', null);
146159
});
147160
});
148161

@@ -168,12 +181,14 @@ Robot = function() {
168181
});
169182

170183
client.callService(request, function(result) {
171-
callback(null, result.is_open);
184+
setError(result.error);
185+
if (result.error) {
186+
callback(result.error, null); // No recovery if error with gripper service.
187+
} else {
188+
callback(null, result.is_open);
189+
}
172190
}, function(error) {
173-
// Failure callback
174-
console.log('IsGripperOpen service call failed.');
175-
console.log(error);
176-
callback(error ? error : true, null);
191+
callback(error ? error : 'Failed to check gripper state.', null);
177192
});
178193
});
179194

@@ -199,9 +214,13 @@ Robot = function() {
199214
});
200215

201216
client.callService(request, function(result) {
217+
setError(result.error);
218+
if (result.error) {
219+
callback(result.error, null); // No recovery.
220+
}
202221
callback(null, null);
203222
}, function(error) {
204-
callback(error ? error : true, null);
223+
callback(error ? error : 'Failed to look at target.', null);
205224
});
206225
});
207226

@@ -232,9 +251,14 @@ Robot = function() {
232251
});
233252

234253
client.callService(request, function(result) {
235-
callback(null, true);
254+
setError(result.error);
255+
if (result.error) {
256+
callback(null, false);
257+
} else {
258+
callback(null, true);
259+
}
236260
}, function(error) {
237-
callback(error ? error : true, false);
261+
callback(error ? error : 'Failed to run object picking.', false);
238262
});
239263
});
240264

@@ -253,9 +277,14 @@ Robot = function() {
253277
});
254278

255279
client.callService(request, function(result) {
256-
callback(null, true);
280+
setError(result.error);
281+
if (result.error) {
282+
callback(null, false);
283+
} else {
284+
callback(null, true);
285+
}
257286
}, function(error) {
258-
callback(error ? error : true, false);
287+
callback(error ? error : 'Failed to run object placing.', false);
259288
});
260289
});
261290

@@ -274,9 +303,10 @@ Robot = function() {
274303
// Some implementations may not know when the sound has finished.
275304
// Keep in mind that this block may not be synchronous.
276305
client.callService(request, function(result) {
306+
setError(result.error); // Sound not playing is probably not fatal enough to quit the program over.
277307
callback(null, null);
278308
}, function(error) {
279-
callback(error ? error : true, null);
309+
callback(error ? error : 'Speech failed to run.', null);
280310
});
281311
});
282312

@@ -297,11 +327,14 @@ Robot = function() {
297327
});
298328

299329
client.callService(request, function(result) {
300-
callback(null, null);
330+
setError(result.error);
331+
if (result.error) {
332+
callback(result.error, null); // No recovery.
333+
} else {
334+
callback(null, null);
335+
}
301336
}, function(error) {
302-
console.log('Error calling SetGripper');
303-
console.log(error);
304-
callback(error ? error : true, null);
337+
callback(error ? error : 'Failed to set gripper.', null);
305338
});
306339
});
307340

@@ -319,17 +352,22 @@ Robot = function() {
319352
});
320353

321354
client.callService(request, function(result) {
322-
callback(null, null);
355+
setError(result.error);
356+
if (result.error) {
357+
callback(result.error, null);
358+
} else {
359+
callback(null, null);
360+
}
323361
}, function(error) {
324-
console.log(error);
325-
callback(null, null);
362+
callback(error ? error : 'Failed to tuck arms.', null);
326363
});
327364
});
328365

329366
return {
330367
askMultipleChoice: askMultipleChoice,
331368
displayMessage: displayMessage,
332369
findObjects: findObjects,
370+
getError: getError,
333371
goTo: goTo,
334372
goToDock: goToDock,
335373
isGripperOpen: isGripperOpen,
@@ -338,6 +376,7 @@ Robot = function() {
338376
pick: pick,
339377
place: place,
340378
say: say,
379+
setError: setError,
341380
setGripper: setGripper,
342381
tuckArms: tuckArms,
343382
};

frontend/app/elements/code-it-blockly/code-it-blockly.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<link rel="import" href="../../bower_components/polymer/polymer.html">
2+
<link rel="import" href="../../bower_components/paper-toast/paper-toast.html">
23
<link rel="import" href="blockly-import.html">
34
<dom-module id="code-it-blockly">
45
<template>
@@ -80,6 +81,7 @@
8081
</section>
8182
</iron-pages>
8283
</div>
84+
<paper-toast id="toast"></paper-toast>
8385
</template>
8486
<script>
8587
(function() {
@@ -112,7 +114,8 @@
112114
isRunning: {
113115
type: Boolean,
114116
value: false,
115-
}
117+
},
118+
_errorTopic: Object,
116119
},
117120

118121
ready: function() {
@@ -125,6 +128,16 @@
125128
this.isRunningTopic.subscribe(function(msg) {
126129
that._handleIsRunningMsg(that, msg);
127130
});
131+
132+
this._errorTopic = new ROSLIB.Topic({
133+
ros: ROS,
134+
name: '/code_it/errors',
135+
type: 'std_msgs/String'
136+
});
137+
this._errorTopic.subscribe(function(msg) {
138+
that.$.toast.text = msg.data;
139+
that.$.toast.show();
140+
});
128141
},
129142

130143
_handleIsRunningMsg: function(that, msg) {

0 commit comments

Comments
 (0)