-
Notifications
You must be signed in to change notification settings - Fork 13
/
hubAsync.ts
328 lines (301 loc) · 12.4 KB
/
hubAsync.ts
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
import { Hub } from './hub';
import { Motor } from '../types'
const CALLBACK_TIMEOUT_MS = 1000 / 3;
export const DEFAULT_CONFIG = {
METRIC_MODIFIER: 28.5,
TURN_MODIFIER: 2.56,
DRIVE_SPEED: 25,
TURN_SPEED: 20,
DEFAULT_STOP_DISTANCE: 105,
DEFAULT_CLEAR_DISTANCE: 120,
LEFT_MOTOR: 'A' as Motor,
RIGHT_MOTOR: 'B' as Motor,
VALID_MOTORS: ['A' as Motor, 'B' as Motor],
};
const validateConfiguration = (configuration: BoostConfiguration) => {
configuration.leftMotor = configuration.leftMotor || DEFAULT_CONFIG.LEFT_MOTOR;
configuration.rightMotor = configuration.rightMotor || DEFAULT_CONFIG.RIGHT_MOTOR;
// @ts-ignore
if (!DEFAULT_CONFIG.VALID_MOTORS.includes(configuration.leftMotor)) throw Error('Define left port port correctly');
// @ts-ignore
if (!DEFAULT_CONFIG.VALID_MOTORS.includes(configuration.rightMotor)) throw Error('Define right port port correctly');
if (configuration.leftMotor === configuration.rightMotor) throw Error('Left and right motor can not be same');
configuration.distanceModifier = configuration.distanceModifier || DEFAULT_CONFIG.METRIC_MODIFIER;
configuration.turnModifier = configuration.turnModifier || DEFAULT_CONFIG.TURN_MODIFIER;
configuration.driveSpeed = configuration.driveSpeed || DEFAULT_CONFIG.DRIVE_SPEED;
configuration.turnSpeed = configuration.turnSpeed || DEFAULT_CONFIG.TURN_SPEED;
configuration.defaultStopDistance = configuration.defaultStopDistance || DEFAULT_CONFIG.DEFAULT_STOP_DISTANCE;
configuration.defaultClearDistance = configuration.defaultClearDistance || DEFAULT_CONFIG.DEFAULT_CLEAR_DISTANCE;
};
const waitForValueToSet = function(
valueName,
compareFunc = valueNameToCompare => this[valueNameToCompare],
timeoutMs = 0
) {
if (compareFunc.bind(this)(valueName)) return Promise.resolve(this[valueName]);
return new Promise((resolve, reject) => {
setTimeout(
async () => resolve(await waitForValueToSet.bind(this)(valueName, compareFunc, timeoutMs)),
timeoutMs + 100
);
});
};
export interface BoostConfiguration {
distanceModifier?: any;
turnModifier?: any;
defaultClearDistance?: any;
defaultStopDistance?: any;
leftMotor?: Motor;
rightMotor?: Motor;
driveSpeed?: number;
turnSpeed?: number;
}
export class HubAsync extends Hub {
hubDisconnected: boolean;
configuration: BoostConfiguration;
portData: any;
useMetric: boolean;
modifier: number;
distance: number;
constructor(bluetooth: BluetoothRemoteGATTCharacteristic, configuration: BoostConfiguration) {
super(bluetooth);
validateConfiguration(configuration);
this.configuration = configuration;
}
/**
* Disconnect Hub
* @method Hub#disconnectAsync
* @returns {Promise<boolean>} disconnection successful
*/
disconnectAsync(): Promise<boolean> {
this.setDisconnected();
return waitForValueToSet.bind(this)('hubDisconnected');
}
/**
* Execute this method after new instance of Hub is created
* @method Hub#afterInitialization
*/
afterInitialization() {
this.hubDisconnected = null;
this.portData = {
A: { angle: 0 },
B: { angle: 0 },
AB: { angle: 0 },
C: { angle: 0 },
D: { angle: 0 },
LED: { angle: 0 },
};
this.useMetric = true;
this.modifier = 1;
this.emitter.on('rotation', rotation => (this.portData[rotation.port].angle = rotation.angle));
this.emitter.on('disconnect', () => (this.hubDisconnected = true));
this.emitter.on('distance', distance => (this.distance = distance));
}
/**
* Control the LED on the Move Hub
* @method Hub#ledAsync
* @param {boolean|number|string} color
* If set to boolean `false` the LED is switched off, if set to `true` the LED will be white.
* Possible string values: `off`, `pink`, `purple`, `blue`, `lightblue`, `cyan`, `green`, `yellow`, `orange`, `red`,
* `white`
* @returns {Promise}
*/
ledAsync(color: boolean | number | string): Promise<any> {
return new Promise((resolve, reject) => {
this.led(color, () => {
// Callback is executed when command is sent and it will take some time before MoveHub executes the command
setTimeout(resolve, CALLBACK_TIMEOUT_MS);
});
});
}
/**
* Run a motor for specific time
* @method Hub#motorTimeAsync
* @param {string|number} port possible string values: `A`, `B`, `AB`, `C`, `D`.
* @param {number} seconds
* @param {number} [dutyCycle=100] motor power percentsage from `-100` to `100`. If a negative value is given rotation
* is counterclockwise.
* @param {boolean} [wait=false] will promise wait unitll motorTime run time has elapsed
* @returns {Promise}
*/
motorTimeAsync(port: string | number, seconds: number, dutyCycle: number = 100, wait: boolean = false): Promise<any> {
return new Promise((resolve, _) => {
this.motorTime(port, seconds, dutyCycle, () => {
setTimeout(resolve, wait ? CALLBACK_TIMEOUT_MS + seconds * 1000 : CALLBACK_TIMEOUT_MS);
});
});
}
/**
* Run both motors (A and B) for specific time
* @method Hub#motorTimeMultiAsync
* @param {number} seconds
* @param {number} [dutyCycleA=100] motor power percentage from `-100` to `100`. If a negative value is given rotation
* is counterclockwise.
* @param {number} [dutyCycleB=100] motor power percentage from `-100` to `100`. If a negative value is given rotation
* is counterclockwise.
* @param {boolean} [wait=false] will promise wait unitll motorTime run time has elapsed
* @returns {Promise}
*/
motorTimeMultiAsync(seconds: number, dutyCycleA: number = 100, dutyCycleB: number = 100, wait: boolean = false): Promise<any> {
return new Promise((resolve, _) => {
this.motorTimeMulti(seconds, dutyCycleA, dutyCycleB, () => {
setTimeout(resolve, wait ? CALLBACK_TIMEOUT_MS + seconds * 1000 : CALLBACK_TIMEOUT_MS);
});
});
}
/**
* Turn a motor by specific angle
* @method Hub#motorAngleAsync
* @param {string|number} port possible string values: `A`, `B`, `AB`, `C`, `D`.
* @param {number} angle - degrees to turn from `0` to `2147483647`
* @param {number} [dutyCycle=100] motor power percentage from `-100` to `100`. If a negative value is given
* rotation is counterclockwise.
* @param {boolean} [wait=false] will promise wait unitll motorAngle has turned
* @returns {Promise}
*/
motorAngleAsync(port: string | number, angle: number, dutyCycle: number = 100, wait: boolean = false): Promise<any> {
return new Promise((resolve, _) => {
this.motorAngle(port, angle, dutyCycle, async () => {
if (wait) {
let beforeTurn;
do {
beforeTurn = this.portData[port].angle;
await new Promise(res => setTimeout(res, CALLBACK_TIMEOUT_MS));
} while (this.portData[port].angle !== beforeTurn);
resolve();
} else {
setTimeout(resolve, CALLBACK_TIMEOUT_MS);
}
});
});
}
/**
* Turn both motors (A and B) by specific angle
* @method Hub#motorAngleMultiAsync
* @param {number} angle degrees to turn from `0` to `2147483647`
* @param {number} [dutyCycleA=100] motor power percentage from `-100` to `100`. If a negative value is given
* rotation is counterclockwise.
* @param {number} [dutyCycleB=100] motor power percentage from `-100` to `100`. If a negative value is given
* rotation is counterclockwise.
* @param {boolean} [wait=false] will promise wait unitll motorAngle has turned
* @returns {Promise}
*/
motorAngleMultiAsync(angle: number, dutyCycleA: number = 100, dutyCycleB: number = 100, wait: boolean = false): Promise<any> {
return new Promise((resolve, _) => {
this.motorAngleMulti(angle, dutyCycleA, dutyCycleB, async () => {
if (wait) {
let beforeTurn;
do {
beforeTurn = this.portData['AB'].angle;
await new Promise(res => setTimeout(res, CALLBACK_TIMEOUT_MS));
} while (this.portData['AB'].angle !== beforeTurn);
resolve();
} else {
setTimeout(resolve, CALLBACK_TIMEOUT_MS);
}
});
});
}
/**
* Use metric units (default)
* @method Hub#useMetricUnits
*/
useMetricUnits() {
this.useMetric = true;
}
/**
* Use imperial units
* @method Hub#useImperialUnits
*/
useImperialUnits() {
this.useMetric = false;
}
/**
* Set friction modifier
* @method Hub#setFrictionModifier
* @param {number} modifier friction modifier
*/
setFrictionModifier(modifier: number) {
this.modifier = modifier;
}
/**
* Drive specified distance
* @method Hub#drive
* @param {number} distance distance in centimeters (default) or inches. Positive is forward and negative is backward.
* @param {boolean} [wait=true] will promise wait untill the drive has completed.
* @returns {Promise}
*/
drive(distance: number, wait: boolean = true): Promise<any> {
const angle =
Math.abs(distance) *
((this.useMetric ? this.configuration.distanceModifier : this.configuration.distanceModifier / 4) *
this.modifier);
const dutyCycleA =
this.configuration.driveSpeed * (distance > 0 ? 1 : -1) * (this.configuration.leftMotor === 'A' ? 1 : -1);
const dutyCycleB =
this.configuration.driveSpeed * (distance > 0 ? 1 : -1) * (this.configuration.leftMotor === 'A' ? 1 : -1);
return this.motorAngleMultiAsync(angle, dutyCycleA, dutyCycleB, wait);
}
/**
* Turn robot specified degrees
* @method Hub#turn
* @param {number} degrees degrees to turn. Negative is to the left and positive to the right.
* @param {boolean} [wait=true] will promise wait untill the turn has completed.
* @returns {Promise}
*/
turn(degrees: number, wait: boolean = true): Promise<any> {
const angle = Math.abs(degrees) * this.configuration.turnModifier;
const turnMotorModifier = this.configuration.leftMotor === 'A' ? 1 : -1;
const leftTurn = this.configuration.turnSpeed * (degrees > 0 ? 1 : -1) * turnMotorModifier;
const rightTurn = this.configuration.turnSpeed * (degrees > 0 ? -1 : 1) * turnMotorModifier;
const dutyCycleA = this.configuration.leftMotor === 'A' ? leftTurn : rightTurn;
const dutyCycleB = this.configuration.leftMotor === 'A' ? rightTurn : leftTurn;
return this.motorAngleMultiAsync(angle, dutyCycleA, dutyCycleB, wait);
}
/**
* Drive untill sensor shows object in defined distance
* @method Hub#driveUntil
* @param {number} [distance=0] distance in centimeters (default) or inches when to stop. Distance sensor is not very sensitive or accurate.
* By default will stop when sensor notices wall for the first time. Sensor distance values are usualy between 110-50.
* @param {boolean} [wait=true] will promise wait untill the bot will stop.
* @returns {Promise}
*/
async driveUntil(distance: number = 0, wait: boolean = true): Promise<any> {
const distanceCheck =
distance !== 0 ? (this.useMetric ? distance : distance * 2.54) : this.configuration.defaultStopDistance;
const direction = this.configuration.leftMotor === 'A' ? 1 : -1;
const compareFunc = direction === 1 ? () => distanceCheck >= this.distance : () => distanceCheck <= this.distance;
this.motorTimeMulti(60, this.configuration.driveSpeed * direction, this.configuration.driveSpeed * direction);
if (wait) {
await waitForValueToSet.bind(this)('distance', compareFunc);
await this.motorAngleMultiAsync(0);
} else {
return waitForValueToSet
.bind(this)('distance', compareFunc)
.then(_ => this.motorAngleMulti(0, 0, 0));
}
}
/**
* Turn until there is no object in sensors sight
* @method Hub#turnUntil
* @param {number} [direction=1] direction to turn to. 1 (or any positive) is to the right and 0 (or any negative) is to the left.
* @param {boolean} [wait=true] will promise wait untill the bot will stop.
* @returns {Promise}
*/
async turnUntil(direction: number = 1, wait: boolean = true): Promise<any> {
const directionModifier = direction > 0 ? 1 : -1;
this.turn(360 * directionModifier, false);
if (wait) {
await waitForValueToSet.bind(this)('distance', () => this.distance >= this.configuration.defaultClearDistance);
await this.turn(0, false);
} else {
return waitForValueToSet
.bind(this)('distance', () => this.distance >= this.configuration.defaultClearDistance)
.then(_ => this.turn(0, false));
}
}
updateConfiguration(configuration: BoostConfiguration): void {
validateConfiguration(configuration);
this.configuration = configuration;
}
}