-
Notifications
You must be signed in to change notification settings - Fork 3
/
MotionService.js
343 lines (297 loc) · 12.2 KB
/
MotionService.js
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'use strict';
/** @namespace services.motion */
const TaskError = Mep.require('strategy/TaskError');
const EventEmitter = require('events').EventEmitter;
const Point = Mep.require('misc/geometry/Point');
const MotionDriver = Mep.require('drivers/motion/MotionDriver');
const MotionTargetQueue = require('./MotionTargetQueue');
const Line = Mep.require('misc/geometry/Line');
const Misc = Mep.require('misc/geometry/Misc');
const TAG = 'MotionService';
/**
* Provides a very abstract way to control and estimate robot position
* @fires services.motion.MotionService#pathObstacleDetected
* @memberOf services.position
* @author Darko Lukic <[email protected]>
*/
class MotionService extends EventEmitter {
init(config) {
this.config = Object.assign({
hazardObstacleDistance: 400,
maxBypassTolerance: 50,
targetLineOffset: 150,
hazardAngleFront: [-70, 70],
hazardAngleBack: [110, -110],
epsilonRadius: 30
}, config);
this.motionDriver = Mep.getDriver('MotionDriver');
// Important for simulation
this._targetQueue = new MotionTargetQueue((targets) => {
Mep.Telemetry.send(TAG, 'TargetQueueChanged', targets);
});
Mep.Telemetry.send(TAG, 'HazardObstacleDistanceSet', {
hazardObstacleDistance: this.config.hazardObstacleDistance
});
// Global resolve and reject used outside (strategies)
this._resolve = null;
this._reject = null;
this._paused = false;
this._lastDirection = MotionDriver.DIRECTION_UNDEFINED;
this._obstacleDetectedTimeout = null;
this._pathObstacleDetectedTimeout = null;
// Event method configuration
this._goToNextQueuedTarget = this._goToNextQueuedTarget.bind(this);
this._onObstacleDetected = this._onObstacleDetected.bind(this);
this._onPathObstacleDetected = this._onPathObstacleDetected.bind(this);
// Subscribe on sensors that can provide obstacles on the robot's terrain
Mep.Terrain.on('obstacleDetected', this._onObstacleDetected);
}
isPaused() {
return this._paused;
}
_onObstacleDetected(params) {
if (this._targetQueue.getTargetFront() === null) return;
let hazardAngle = (this.motionDriver.getDirection() === MotionDriver.DIRECTION_FORWARD) ?
this.config.hazardAngleFront : this.config.hazardAngleBack;
// Hazard region
if (Misc.isAngleInRange(hazardAngle[0], hazardAngle[1], params.relativePoi.getAngleFromZero()) &&
params.relativePoi.getDistance(new Point(0, 0)) < this.config.hazardObstacleDistance) {
let friend = Mep.Share.getLastFriendPosition() !== null &&
params.polygon.isPointInside(Mep.Share.getLastFriendPosition());
if (this._obstacleDetectedTimeout !== null) {
clearTimeout(this._obstacleDetectedTimeout);
} else {
this._onPathObstacleDetected(true, friend);
}
this._obstacleDetectedTimeout = setTimeout(() => {
this._obstacleDetectedTimeout = null;
this._onPathObstacleDetected(false, friend);
}, Mep.Config.get('obstacleMaxPeriod') + 100);
} else {
/*
TODO: Try to redesign a path in runtime
if (target.getParams().rerouting === true) {
this.tryRerouting();
}
*/
}
}
_onPathObstacleDetected(detected, friend) {
if (detected === true) {
let target = this._targetQueue.getTargetFront();
let timeout = (friend === true) ? target.getParams().friend : target.getParams().obstacle;
Mep.Motion.stop();
Mep.Log.debug(TAG, 'Obstacle detected, robot stopped');
this._pathObstacleDetectedTimeout = setTimeout(() => {
Mep.Motion.forceReject(friend);
}, timeout);
} else {
if (this._pathObstacleDetectedTimeout !== null) {
clearTimeout(this._pathObstacleDetectedTimeout);
}
Mep.Motion.resume();
}
}
tryRerouting() {
let motionService = this;
let pfTarget = this._targetQueue.getTargetBack();
if (pfTarget !== null) {
// Redesign path
let points = Mep.Terrain.findPath(Mep.Position.getPosition(), pfTarget.getPoint());
if (points.length > 0) {
let params = pfTarget.getParams();
// Reduce tolerance to get better to get better bypass
params.tolerance = (params.tolerance > this.config.maxBypassTolerance) ?
this.config.maxBypassTolerance :
params.tolerance;
// Redesign a path
this._targetQueue.empty();
this._targetQueue.addPointsBack(points, params);
if (params.tolerance === -1) {
this.stop().then(() => {
motionService.resume();
});
} else {
this.motionDriver.finishCommand();
this.resume();
}
} else {
Mep.Log.warn(TAG, 'Cannot redesign path, possible crash!');
// There will be no crash if obstacle move away or
// if robot stop thanks to `pathObstacleDetected` sensors
}
}
}
/**
* Move the robot, set new position of the robot
* @param {TunedPoint} tunedPoint Point that should be reached
* @param {Object} [parameters] Configuration options.
* @param {Boolean} [parameters.pf] Use terrain finding algorithm.
* @param {Boolean} [parameters.backward] Set backward robot moving.
* @param {Boolean} [parameters.rerouting] Enable rerouting during the movement.
* @param {Boolean} [parameters.relative] Use relative to previous position.
* @param {Number} [parameters.tolerance] Position will consider as reached if Euclid's distance between current
* and required position is less than tolerance.
* @param {Number} [parameters.speed] Speed of the robot movement in range (0, 255).
* @param {Number} [parameters.obstacle] Time [ms] after command will be rejected (with TaskError.action === 'obstacle')
* if obstacle is detected in hazard region
* @param {Number} [parameters.friend] Time [ms] after command will be rejected (with TaskError.action === 'friend')
* if friend is detected in hazard region
* @returns {Promise}
*/
go(tunedPoint, parameters) {
let point = tunedPoint.getPoint();
let params = Object.assign({}, this.config.moveOptions, parameters);
// Update last moving direction
if (this.motionDriver.getDirection() !== MotionDriver.DIRECTION_UNDEFINED) {
this._lastDirection = this.motionDriver.getDirection();
}
this._targetQueue.empty();
// Apply relative
if (params.relative === true) {
point.translate(Mep.Position.getPosition());
}
// Apply path finding algorithm
if (params.pf === true) {
let currentPoint = Mep.Position.getPosition();
this._targetQueue.addPointsBack(Mep.Terrain.findPath(currentPoint, point), params);
Mep.Log.debug(TAG, 'Start path finding from', currentPoint, 'to', this._targetQueue.getTargets());
} else {
this._targetQueue.addPointBack(point, params)
}
return new Promise((resolve, reject) => {
if (this._targetQueue.isEmpty()) {
reject(new TaskError(TAG, 'pf', 'Cannot go to required position, no path found'));
return;
}
this._resolve = resolve;
this._reject = reject;
this._goToNextQueuedTarget();
});
}
_goToNextQueuedTarget() {
let motionService = this;
if (this._targetQueue.isEmpty()) {
if (this._resolve !== null) {
this._resolve();
}
} else {
let target = this._targetQueue.getTargetFront();
this._goSingleTarget(target.getPoint(), target.getParams()).then(() => {
motionService._targetQueue.removeFront();
motionService._goToNextQueuedTarget();
}).catch((e) => {
if (e.action !== 'break') {
Mep.Log.error(TAG, e);
motionService._reject(e);
}
});
}
}
/**
* Go to single point without advanced features
* @param {misc.Point} point Target point
* @param {Object} params Additional options
* @param {Boolean} [params.backward] Move robot backward
* @param {Number} [params.tolerance] Max radius
* @param {Number} [params.speed] Speed
* @return {Promise}
* @private
*/
_goSingleTarget(point, params) {
Mep.Log.debug(TAG, 'Simple target go', point);
this._paused = false;
// Set speed
if (params.speed !== undefined && this.motionDriver.getActiveSpeed() !== params.speed) {
this.motionDriver.setSpeed(params.speed);
}
// Move the robot
if (params.tolerance < 0) {
return this.motionDriver.moveToPosition(
point,
params.backward ? -1 : 1
);
} else {
return this.motionDriver.moveToCurvilinear(
point,
params.backward ? -1 : 1,
params.radius,
params.tolerance
);
}
}
/**
* Stop the robot
* @param {Boolean} softStop If true robot will turn of motors
*/
stop(softStop = false) {
this.pause();
if (softStop === true) {
return this.motionDriver.softStop();
} else {
return this.motionDriver.stop();
}
}
pause() {
this._paused = true;
}
resume() {
if (this._paused === true) {
this._paused = false;
this._goToNextQueuedTarget();
}
}
/**
* Move robot forward or backward depending on param `millimeters`
* @param {Number} millimeters Path that needs to be passed. If negative robot will go backward
* @param {Object} params
* @param {Number} [params.speed] Speed
* @params {Boolean} [params.opposite] Move in opposite direction to previous command
* @returns {Promise}
*/
straight(millimeters, params) {
// Update last moving directiObstacle is too long in front of roboton
if (this.motionDriver.getDirection() !== MotionDriver.DIRECTION_UNDEFINED) {
this._lastDirection = this.motionDriver.getDirection();
}
// Apply params
if (params !== undefined) {
// Apply speed
if (params.speed !== undefined && this.motionDriver.getActiveSpeed() !== params.speed) {
this.motionDriver.setSpeed(params.speed);
}
// Apply inverse
if (params.opposite !== undefined) {
switch (this._lastDirection) {
case MotionDriver.DIRECTION_FORWARD:
millimeters = (-1) * Math.abs(millimeters);
break;
case MotionDriver.DIRECTION_BACKWARD:
millimeters = Math.abs(millimeters);
break;
}
}
}
return this.motionDriver.goForward(millimeters);
}
/**
* Rotate robot for an angle
* @param {TunedAngle} tunedAngle Angle to rotate
* @param {Object} options Additional options
* @returns {Promise}
*/
rotate(tunedAngle, options) {
return this.motionDriver.rotateTo(tunedAngle.getAngle());
}
forceReject(friend) {
if (this._reject !== null) {
this._targetQueue.empty();
if (friend === true) {
this._reject(new TaskError(TAG, 'friend', 'Friend is too long in front of robot'));
} else {
this._reject(new TaskError(TAG, 'obstacle', 'Obstacle is too long in front of robot'));
}
}
}
}
module.exports = MotionService;