@@ -16,7 +16,7 @@ var Controllers = {
16
16
var state = priv . get ( this ) ;
17
17
18
18
this . address = opts . address || 0x40 ;
19
- this . pwmRange = opts . pwmRange || [ 544 , 2400 ] ;
19
+ this . pwmRange = opts . pwmRange || [ 1000 , 2000 ] ;
20
20
this . frequency = opts . frequency || 50 ;
21
21
22
22
state . expander = Expander . get ( {
@@ -32,9 +32,9 @@ var Controllers = {
32
32
} ,
33
33
write : {
34
34
writable : true ,
35
- value : function ( pin , microseconds ) {
35
+ value : function ( pin , us ) {
36
36
var state = priv . get ( this ) ;
37
- state . expander . servoWrite ( pin , microseconds ) ;
37
+ state . expander . servoWrite ( pin , us ) ;
38
38
}
39
39
}
40
40
} ,
@@ -56,20 +56,19 @@ var Controllers = {
56
56
} ,
57
57
write : {
58
58
writable : true ,
59
- value : function ( pin , microseconds ) {
60
- microseconds |= 0 ;
61
- this . io . servoWrite ( pin , microseconds ) ;
59
+ value : function ( pin , us ) {
60
+ this . io . servoWrite ( pin , us | 0 ) ;
62
61
}
63
62
}
64
63
}
65
64
} ;
66
65
66
+
67
67
var Devices = {
68
68
FORWARD : {
69
69
deviceName : {
70
- get : function ( ) {
71
- return "FORWARD" ;
72
- }
70
+ value : "FORWARD" ,
71
+ writable : false ,
73
72
} ,
74
73
dir : {
75
74
value : function ( speed , dir ) {
@@ -81,9 +80,8 @@ var Devices = {
81
80
} ,
82
81
FORWARD_REVERSE : {
83
82
deviceName : {
84
- get : function ( ) {
85
- return "FORWARD_REVERSE" ;
86
- }
83
+ value : "FORWARD_REVERSE" ,
84
+ writable : false ,
87
85
} ,
88
86
dir : {
89
87
value : function ( speed , dir ) {
@@ -97,9 +95,8 @@ var Devices = {
97
95
} ,
98
96
FORWARD_BRAKE_REVERSE : {
99
97
deviceName : {
100
- get : function ( ) {
101
- return "FORWARD_BRAKE_REVERSE" ;
102
- }
98
+ value : "FORWARD_BRAKE_REVERSE" ,
99
+ writable : false ,
103
100
} ,
104
101
dir : {
105
102
value : function ( speed , dir ) {
@@ -140,14 +137,7 @@ function ESC(opts) {
140
137
var pinValue ;
141
138
var device ;
142
139
var state = {
143
- // All speed history for this ESC
144
- // history = [
145
- // {
146
- // timestamp: Date.now(),
147
- // speed: speed
148
- // }
149
- // ];
150
- history : [ ] ,
140
+ last : { speed : null } ,
151
141
value : 0
152
142
} ;
153
143
@@ -157,12 +147,19 @@ function ESC(opts) {
157
147
158
148
priv . set ( this , state ) ;
159
149
160
- this . startAt = typeof opts . startAt !== "undefined" ? opts . startAt : null ;
161
- this . neutral = opts . neutral ;
162
150
this . range = opts . range || [ 0 , 100 ] ;
163
- this . pwmRange = opts . pwmRange || [ 544 , 2400 ] ;
151
+ this . pwmRange = opts . pwmRange || [ 1000 , 2000 ] ;
152
+ this . neutral = opts . neutral || this . pwmRange [ 0 ] ;
164
153
this . interval = null ;
165
154
155
+ // Scale to pwm range
156
+ if ( typeof this . neutral !== "undefined" && this . neutral <= 100 ) {
157
+ this . neutral = Fn . scale ( this . neutral , 0 , 100 , this . pwmRange [ 0 ] , this . pwmRange [ 1 ] ) ;
158
+ }
159
+
160
+ // Enforce pwm range on neutral point
161
+ this . neutral = Fn . constrain ( this . neutral , this . pwmRange [ 0 ] , this . pwmRange [ 1 ] ) ;
162
+
166
163
// StandardFirmata on Arduino allows controlling
167
164
// servos from analog pins.
168
165
// If we're currently operating with an Arduino
@@ -209,34 +206,22 @@ function ESC(opts) {
209
206
return state . value ;
210
207
}
211
208
} ,
212
- history : {
213
- get : function ( ) {
214
- return state . history . slice ( - 5 ) ;
215
- }
216
- } ,
217
209
last : {
218
210
get : function ( ) {
219
- return state . history [ state . history . length - 1 ] || {
220
- last : null
221
- } ;
211
+ return state . last ;
222
212
}
223
213
}
224
214
} ) ) ;
225
215
226
216
this . initialize ( opts ) ;
227
217
228
218
if ( this . deviceName !== "FORWARD" ) {
229
- if ( Number . isNaN ( + this . neutral ) ) {
230
- throw new Error ( "Directional speed controllers require a neutral point from 0-100 (number) " ) ;
219
+ if ( this . neutral === this . pwmRange [ 0 ] ) {
220
+ throw new Error ( "Bidirectional speed controllers require a non-zero neutral point" ) ;
231
221
}
232
-
233
- this . startAt = this . neutral ;
234
222
}
235
223
236
- // Match either null or undefined, but not 0
237
- if ( this . startAt !== null && this . startAt !== undefined ) {
238
- this . speed ( this . startAt ) ;
239
- }
224
+ this . throttle ( this . neutral ) ;
240
225
}
241
226
242
227
util . inherits ( ESC , Emitter ) ;
@@ -249,11 +234,11 @@ util.inherits(ESC, Emitter);
249
234
* @param {Float } speed 0...100 (full range)
250
235
*
251
236
* @return {ESC } instance
237
+ * @deprecated Will be deleted in version 1.0.0. Use throttle(us) instead.
252
238
*/
253
-
254
- ESC . prototype . speed = function ( speed ) {
239
+ /* istanbul ignore next */
240
+ ESC . prototype . speed = util . deprecate ( function ( speed ) {
255
241
var state = priv . get ( this ) ;
256
- var history = state . history ;
257
242
var noInterval = false ;
258
243
var steps = 0 ;
259
244
var lspeed , hspeed ;
@@ -263,7 +248,7 @@ ESC.prototype.speed = function(speed) {
263
248
if ( this . interval ) {
264
249
// Bail out if speed is the same as whatever was
265
250
// last _provided_
266
- if ( this . value === speed ) {
251
+ if ( state . value === speed ) {
267
252
return this ;
268
253
} else {
269
254
clearInterval ( this . interval ) ;
@@ -276,18 +261,18 @@ ESC.prototype.speed = function(speed) {
276
261
// This is the very first speed command being received.
277
262
// Safe to assume that the ESC and Brushless motor are
278
263
// not yet moving.
279
- if ( history . length === 0 ) {
264
+ if ( state . last . speed === null ) {
280
265
noInterval = true ;
266
+ state . last . speed = this . neutral ;
267
+ } else {
268
+ // Bail out if speed is the same as whatever was
269
+ // last _written_
270
+ if ( state . last . speed === speed ) {
271
+ return this ;
272
+ }
281
273
}
282
274
283
- // Bail out if speed is the same as whatever was
284
- // last _written_
285
-
286
- if ( this . last . speed === speed ) {
287
- return this ;
288
- }
289
-
290
- lspeed = this . last . speed ;
275
+ lspeed = state . last . speed ;
291
276
hspeed = speed ;
292
277
steps = Math . ceil ( Math . abs ( lspeed - hspeed ) ) ;
293
278
@@ -298,10 +283,7 @@ ESC.prototype.speed = function(speed) {
298
283
if ( noInterval ) {
299
284
this . write ( this . pin , Fn . fscale ( speed , 0 , 100 , this . pwmRange [ 0 ] , this . pwmRange [ 1 ] ) ) ;
300
285
301
- history . push ( {
302
- timestamp : Date . now ( ) ,
303
- speed : speed
304
- } ) ;
286
+ state . last . speed = speed ;
305
287
return this ;
306
288
}
307
289
@@ -317,10 +299,7 @@ ESC.prototype.speed = function(speed) {
317
299
318
300
this . write ( this . pin , Fn . fscale ( throttle , 0 , 100 , this . pwmRange [ 0 ] , this . pwmRange [ 1 ] ) ) ;
319
301
320
- history . push ( {
321
- timestamp : Date . now ( ) ,
322
- speed : throttle
323
- } ) ;
302
+ state . last . speed = throttle ;
324
303
325
304
if ( steps ) {
326
305
steps -- ;
@@ -333,24 +312,29 @@ ESC.prototype.speed = function(speed) {
333
312
} . bind ( this ) , 1 ) ;
334
313
335
314
return this ;
336
- } ;
315
+ } , "ESC.prototype.speed: Use `throttle(μs)` (544-2400μs) instead" ) ;
316
+
337
317
318
+ /**
319
+ * throttle
320
+ *
321
+ * Throttle the ESC's speed by setting the pulse
322
+ *
323
+ * @param {Integer } throttle pwmRange[0]...pwmRange[1] (full usec range)
324
+ *
325
+ * @return {ESC } instance
326
+ */
327
+ ESC . prototype . throttle = function ( pulse ) {
328
+ this . write ( this . pin , Fn . constrain ( pulse , this . pwmRange [ 0 ] , this . pwmRange [ 1 ] ) ) ;
329
+ return this ;
330
+ } ;
338
331
339
332
/**
340
333
* brake Stop the ESC by hitting the brakes ;)
341
334
* @return {Object } instance
342
335
*/
343
336
ESC . prototype . brake = function ( ) {
344
- var state = priv . get ( this ) ;
345
- var speed = this . neutral || 0 ;
346
-
347
- this . speed ( speed ) ;
348
-
349
- state . history . push ( {
350
- timestamp : Date . now ( ) ,
351
- speed : speed
352
- } ) ;
353
-
337
+ this . write ( this . pin , this . neutral ) ;
354
338
return this ;
355
339
} ;
356
340
@@ -394,17 +378,7 @@ ESC.prototype.brake = function() {
394
378
* @return {Object } instance
395
379
*/
396
380
ESC . prototype . stop = function ( ) {
397
- var state = priv . get ( this ) ;
398
- var history = state . history ;
399
- var speed = this . type === "bidirectional" ? this . neutral : 0 ;
400
-
401
- this . write ( this . pin , Fn . fscale ( speed , 0 , 100 , this . pwmRange [ 0 ] , this . pwmRange [ 1 ] ) ) ;
402
-
403
- history . push ( {
404
- timestamp : Date . now ( ) ,
405
- speed : speed
406
- } ) ;
407
-
381
+ this . write ( this . pin , this . neutral ) ;
408
382
return this ;
409
383
} ;
410
384
@@ -428,32 +402,6 @@ function ESCs(numsOrObjects) {
428
402
429
403
util . inherits ( ESCs , Collection ) ;
430
404
431
- /**
432
- *
433
- * ESCs, speed(0-100%)
434
- *
435
- * set all escs to the specified speed from 0-100%
436
- *
437
- * eg. array.min();
438
-
439
- * ESCs, min()
440
- *
441
- * set all escs to the minimum throttle
442
- *
443
- * eg. array.min();
444
-
445
- * ESCs, max()
446
- *
447
- * set all escs to the maximum throttle
448
- *
449
- * eg. array.max();
450
-
451
- * ESCs, stop()
452
- *
453
- * stop all escs
454
- *
455
- * eg. array.stop();
456
- */
457
405
458
406
Collection . installMethodForwarding (
459
407
ESCs . prototype , ESC . prototype
0 commit comments