Skip to content

Commit 5a0815b

Browse files
authored
Merge pull request scratchfoundation#1300 from LLK/feature/device-connection-ev3-fixes-1
EV3 Extension fixes
2 parents 55944bc + 394b57e commit 5a0815b

File tree

1 file changed

+108
-29
lines changed

1 file changed

+108
-29
lines changed

src/extensions/scratch3_ev3/index.js

Lines changed: 108 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,12 @@ class EV3 {
127127
this.speed = 50;
128128
this._sensors = {
129129
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]
137132
};
133+
this._motorPositions = [0, 0, 0, 0];
138134
this._sensorPorts = [];
139135
this._motorPorts = [];
140-
this._sensorPortsWaiting = [false, false, false, false];
141-
this._motorPortsWaiting = [false, false, false, false];
142136
this._pollingIntervalID = null;
143137

144138
/**
@@ -177,8 +171,16 @@ class EV3 {
177171
disconnectSession () {
178172
this._bt.disconnectSession();
179173
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];
180181
this._sensorPorts = [];
181182
this._motorPorts = [];
183+
this._pollingIntervalID = null;
182184
}
183185

184186
/**
@@ -201,8 +203,9 @@ class EV3 {
201203
// Accurate to +/- 1 cm (+/- .394 in.)
202204
let value = this._sensors.distance > 100 ? 100 : this._sensors.distance;
203205
value = value < 0 ? 0 : value;
206+
value = Math.round(100 * value) / 100;
204207

205-
return Math.round(value);
208+
return value;
206209
}
207210

208211
get brightness () {
@@ -214,13 +217,17 @@ class EV3 {
214217
getMotorPosition (port) {
215218
if (!this.connected) return;
216219

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;
218225
}
219226

220-
isButtonPressed (/* args */) {
227+
isButtonPressed (port) {
221228
if (!this.connected) return;
222229

223-
return this._sensors.button;
230+
return this._sensors.buttons[port];
224231
}
225232

226233
beep () {
@@ -244,17 +251,23 @@ class EV3 {
244251
BTCommand.LONGRAMP
245252
));
246253

247-
// Send message
254+
// Send turn message
248255
this._bt.sendMessage({
249256
message: Base64Util.arrayBufferToBase64(cmd),
250257
encoding: 'base64'
251258
});
252259

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
254267
return new Promise(resolve => {
255268
setTimeout(() => {
256269
resolve();
257-
}, time);
270+
}, time + coastTime);
258271
});
259272
}
260273

@@ -270,17 +283,57 @@ class EV3 {
270283
BTCommand.LONGRAMP
271284
));
272285

273-
// Send message
286+
// Send turn message
274287
this._bt.sendMessage({
275288
message: Base64Util.arrayBufferToBase64(cmd),
276289
encoding: 'base64'
277290
});
278291

292+
// Send coast message
293+
const coastTime = 100;
294+
setTimeout(() => {
295+
this.motorCoast(port);
296+
}, time + coastTime);
297+
279298
// Yield for time
280299
return new Promise(resolve => {
281300
setTimeout(() => {
282301
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'
284337
});
285338
}
286339

@@ -578,8 +631,12 @@ class EV3 {
578631
array[offset + 2],
579632
array[offset + 3]
580633
]);
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+
}
583640
offset += 4;
584641
}
585642
}
@@ -594,8 +651,8 @@ class EV3 {
594651
if (value > 0x7fffffff) {
595652
value = value - 0x100000000;
596653
}
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;
599656
offset += 4;
600657
}
601658
}
@@ -836,17 +893,39 @@ class Scratch3Ev3Blocks {
836893
}
837894

838895
motorTurnClockwise (args) {
839-
const port = Cast.toNumber(args.PORT);
896+
const port = Cast.toNumber(args.PORT); // TODO: fix
840897
const time = Cast.toNumber(args.TIME) * 1000;
841898

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);
843911
}
844912

845913
motorTurnCounterClockwise (args) {
846-
const port = Cast.toNumber(args.PORT);
914+
const port = Cast.toNumber(args.PORT); // TODO: fix
847915
const time = Cast.toNumber(args.TIME) * 1000;
848916

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);
850929
}
851930

852931
motorRotate (args) {
@@ -874,13 +953,13 @@ class Scratch3Ev3Blocks {
874953
}
875954

876955
getMotorPosition (args) {
877-
const port = Cast.toNumber(args.PORT);
956+
const port = Cast.toNumber(args.PORT - 1); // TODO: Fix MOTOR_PORTS
878957

879958
return this._device.getMotorPosition(port);
880959
}
881960

882961
whenButtonPressed (args) {
883-
const port = Cast.toNumber(args.PORT);
962+
const port = Cast.toNumber(args.PORT - 1); // TODO: Fix SENSOR PORTS
884963

885964
return this._device.isButtonPressed(port);
886965
}
@@ -898,7 +977,7 @@ class Scratch3Ev3Blocks {
898977
}
899978

900979
buttonPressed (args) {
901-
const port = Cast.toNumber(args.PORT);
980+
const port = Cast.toNumber(args.PORT - 1); // TODO: fix SENSOR_PORTS
902981

903982
return this._device.isButtonPressed(port);
904983
}

0 commit comments

Comments
 (0)