@@ -127,18 +127,12 @@ class EV3 {
127
127
this . speed = 50 ;
128
128
this . _sensors = {
129
129
distance : 0 ,
130
- brightness : 0
131
- } ;
132
- this . _motorPositions = {
133
- 1 : 0 ,
134
- 2 : 0 ,
135
- 4 : 0 ,
136
- 8 : 0
130
+ brightness : 0 ,
131
+ buttons : [ 0 , 0 , 0 , 0 ]
137
132
} ;
133
+ this . _motorPositions = [ 0 , 0 , 0 , 0 ] ;
138
134
this . _sensorPorts = [ ] ;
139
135
this . _motorPorts = [ ] ;
140
- this . _sensorPortsWaiting = [ false , false , false , false ] ;
141
- this . _motorPortsWaiting = [ false , false , false , false ] ;
142
136
this . _pollingIntervalID = null ;
143
137
144
138
/**
@@ -177,8 +171,16 @@ class EV3 {
177
171
disconnectSession ( ) {
178
172
this . _bt . disconnectSession ( ) ;
179
173
window . clearInterval ( this . _pollingIntervalID ) ; // TODO: window?
174
+ this . speed = 50 ;
175
+ this . _sensors = {
176
+ distance : 0 ,
177
+ brightness : 0 ,
178
+ buttons : [ 0 , 0 , 0 , 0 ]
179
+ } ;
180
+ this . _motorPositions = [ 0 , 0 , 0 , 0 ] ;
180
181
this . _sensorPorts = [ ] ;
181
182
this . _motorPorts = [ ] ;
183
+ this . _pollingIntervalID = null ;
182
184
}
183
185
184
186
/**
@@ -201,8 +203,9 @@ class EV3 {
201
203
// Accurate to +/- 1 cm (+/- .394 in.)
202
204
let value = this . _sensors . distance > 100 ? 100 : this . _sensors . distance ;
203
205
value = value < 0 ? 0 : value ;
206
+ value = Math . round ( 100 * value ) / 100 ;
204
207
205
- return Math . round ( value ) ;
208
+ return value ;
206
209
}
207
210
208
211
get brightness ( ) {
@@ -214,13 +217,17 @@ class EV3 {
214
217
getMotorPosition ( port ) {
215
218
if ( ! this . connected ) return ;
216
219
217
- return this . _motorPositions [ port ] ;
220
+ let value = this . _motorPositions [ port ] ;
221
+ value = value % 360 ;
222
+ value = value < 0 ? value * - 1 : value ;
223
+
224
+ return value ;
218
225
}
219
226
220
- isButtonPressed ( /* args */ ) {
227
+ isButtonPressed ( port ) {
221
228
if ( ! this . connected ) return ;
222
229
223
- return this . _sensors . button ;
230
+ return this . _sensors . buttons [ port ] ;
224
231
}
225
232
226
233
beep ( ) {
@@ -244,17 +251,23 @@ class EV3 {
244
251
BTCommand . LONGRAMP
245
252
) ) ;
246
253
247
- // Send message
254
+ // Send turn message
248
255
this . _bt . sendMessage ( {
249
256
message : Base64Util . arrayBufferToBase64 ( cmd ) ,
250
257
encoding : 'base64'
251
258
} ) ;
252
259
253
- // Yield for time
260
+ // Send coast message
261
+ const coastTime = 100 ;
262
+ setTimeout ( ( ) => {
263
+ this . motorCoast ( port ) ;
264
+ } , time + coastTime ) ;
265
+
266
+ // Yield for turn time + brake time
254
267
return new Promise ( resolve => {
255
268
setTimeout ( ( ) => {
256
269
resolve ( ) ;
257
- } , time ) ;
270
+ } , time + coastTime ) ;
258
271
} ) ;
259
272
}
260
273
@@ -270,17 +283,57 @@ class EV3 {
270
283
BTCommand . LONGRAMP
271
284
) ) ;
272
285
273
- // Send message
286
+ // Send turn message
274
287
this . _bt . sendMessage ( {
275
288
message : Base64Util . arrayBufferToBase64 ( cmd ) ,
276
289
encoding : 'base64'
277
290
} ) ;
278
291
292
+ // Send coast message
293
+ const coastTime = 100 ;
294
+ setTimeout ( ( ) => {
295
+ this . motorCoast ( port ) ;
296
+ } , time + coastTime ) ;
297
+
279
298
// Yield for time
280
299
return new Promise ( resolve => {
281
300
setTimeout ( ( ) => {
282
301
resolve ( ) ;
283
- } , time ) ;
302
+ } , time + coastTime ) ;
303
+ } ) ;
304
+ }
305
+
306
+ motorCoast ( port ) {
307
+ const cmd = [ ] ;
308
+ // MOTOR COAST
309
+ /*
310
+ 0x09 [ 9] length
311
+ 0x00 [ 0] length
312
+ 0x01 [ 1]
313
+ 0x00 [ 0]
314
+ 0x00 [ 0]
315
+ 0x00 [ 0]
316
+ 0x00 [ 0]
317
+ 0xA3 [163] Coast motor command
318
+ 0x00 [ 0] layer
319
+ 0x03 [ ] port
320
+ 0x00 [ 0] float = coast = 0
321
+ */
322
+ cmd [ 0 ] = 9 ;
323
+ cmd [ 1 ] = 0 ;
324
+ cmd [ 2 ] = 1 ;
325
+ cmd [ 3 ] = 0 ;
326
+ cmd [ 4 ] = 0 ;
327
+ cmd [ 5 ] = 0 ;
328
+ cmd [ 6 ] = 0 ;
329
+ cmd [ 7 ] = 163 ;
330
+ cmd [ 8 ] = 0 ;
331
+ cmd [ 9 ] = port ;
332
+ cmd [ 10 ] = 0 ;
333
+
334
+ this . _bt . sendMessage ( {
335
+ message : Base64Util . uint8ArrayToBase64 ( cmd ) ,
336
+ encoding : 'base64'
284
337
} ) ;
285
338
}
286
339
@@ -578,8 +631,12 @@ class EV3 {
578
631
array [ offset + 2 ] ,
579
632
array [ offset + 3 ]
580
633
] ) ;
581
- log . info ( `sensor at port ${ i } ${ this . _sensorPorts [ i ] } value: ${ value } ` ) ;
582
- this . _sensors [ EV_DEVICE_LABELS [ this . _sensorPorts [ i ] ] ] = value ;
634
+ // log.info(`sensor at port ${i} ${this._sensorPorts[i]} value: ${value}`);
635
+ if ( EV_DEVICE_LABELS [ this . _sensorPorts [ i ] ] === 'button' ) {
636
+ this . _sensors . buttons [ i ] = value ;
637
+ } else {
638
+ this . _sensors [ EV_DEVICE_LABELS [ this . _sensorPorts [ i ] ] ] = value ;
639
+ }
583
640
offset += 4 ;
584
641
}
585
642
}
@@ -594,8 +651,8 @@ class EV3 {
594
651
if ( value > 0x7fffffff ) {
595
652
value = value - 0x100000000 ;
596
653
}
597
- log . info ( `motor at port ${ i } ${ this . _motorPorts [ i ] } value: ${ value } ` ) ;
598
- this . _motorPositions [ MOTOR_PORTS [ i ] . value ] = value ;
654
+ // log.info(`motor at port ${i} ${this._motorPorts[i]} value: ${value}`);
655
+ this . _motorPositions [ i ] = value ;
599
656
offset += 4 ;
600
657
}
601
658
}
@@ -836,17 +893,39 @@ class Scratch3Ev3Blocks {
836
893
}
837
894
838
895
motorTurnClockwise ( args ) {
839
- const port = Cast . toNumber ( args . PORT ) ;
896
+ const port = Cast . toNumber ( args . PORT ) ; // TODO: fix
840
897
const time = Cast . toNumber ( args . TIME ) * 1000 ;
841
898
842
- return this . _device . motorTurnClockwise ( port , time ) ;
899
+ let p = null ;
900
+ if ( port === 1 ) {
901
+ p = 1 ;
902
+ } else if ( port === 2 ) {
903
+ p = 2 ;
904
+ } else if ( port === 3 ) {
905
+ p = 4 ;
906
+ } else if ( port === 4 ) {
907
+ p = 8 ;
908
+ }
909
+
910
+ return this . _device . motorTurnClockwise ( p , time ) ;
843
911
}
844
912
845
913
motorTurnCounterClockwise ( args ) {
846
- const port = Cast . toNumber ( args . PORT ) ;
914
+ const port = Cast . toNumber ( args . PORT ) ; // TODO: fix
847
915
const time = Cast . toNumber ( args . TIME ) * 1000 ;
848
916
849
- return this . _device . motorTurnCounterClockwise ( port , time ) ;
917
+ let p = null ;
918
+ if ( port === 1 ) {
919
+ p = 1 ;
920
+ } else if ( port === 2 ) {
921
+ p = 2 ;
922
+ } else if ( port === 3 ) {
923
+ p = 4 ;
924
+ } else if ( port === 4 ) {
925
+ p = 8 ;
926
+ }
927
+
928
+ return this . _device . motorTurnCounterClockwise ( p , time ) ;
850
929
}
851
930
852
931
motorRotate ( args ) {
@@ -874,13 +953,13 @@ class Scratch3Ev3Blocks {
874
953
}
875
954
876
955
getMotorPosition ( args ) {
877
- const port = Cast . toNumber ( args . PORT ) ;
956
+ const port = Cast . toNumber ( args . PORT - 1 ) ; // TODO: Fix MOTOR_PORTS
878
957
879
958
return this . _device . getMotorPosition ( port ) ;
880
959
}
881
960
882
961
whenButtonPressed ( args ) {
883
- const port = Cast . toNumber ( args . PORT ) ;
962
+ const port = Cast . toNumber ( args . PORT - 1 ) ; // TODO: Fix SENSOR PORTS
884
963
885
964
return this . _device . isButtonPressed ( port ) ;
886
965
}
@@ -898,7 +977,7 @@ class Scratch3Ev3Blocks {
898
977
}
899
978
900
979
buttonPressed ( args ) {
901
- const port = Cast . toNumber ( args . PORT ) ;
980
+ const port = Cast . toNumber ( args . PORT - 1 ) ; // TODO: fix SENSOR_PORTS
902
981
903
982
return this . _device . isButtonPressed ( port ) ;
904
983
}
0 commit comments