From 1c8a3d24d66b5596da93707c13569db3b6d2c190 Mon Sep 17 00:00:00 2001 From: Siobhan Duncan Date: Mon, 20 Jun 2022 17:03:17 +0100 Subject: [PATCH] Changed step/slide/turn to allow for zero step value Changed step/slide/turn to allow for zero step value and sets the timeout for a single step so that it doesn't look like the block was skipped in the UI. Pauses to show the robot doesn't respond before moving on --- src/blocks/scratch3_mv2.js | 40 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/blocks/scratch3_mv2.js b/src/blocks/scratch3_mv2.js index 0e29d8609c..a483556dd5 100644 --- a/src/blocks/scratch3_mv2.js +++ b/src/blocks/scratch3_mv2.js @@ -377,45 +377,58 @@ class Scratch3Mv2Blocks { walk_fw (args, util) { const moveTime = 1500; + let timeOut = moveTime; let steps = parseInt(args.STEPS); - steps = Math.min(Math.max(steps, 1), 20); + steps = Math.min(Math.max(steps, 0), 20); const stepLength = 25; console.log(`traj/step/${steps}/?moveTime=${moveTime}&stepLength=${stepLength}`); mv2.send_REST(`traj/step/${steps}/?moveTime=${moveTime}&stepLength=${stepLength}`); + if(steps){ + timeOut = timeOut * steps; + } return new Promise(resolve => - setTimeout(resolve, moveTime * steps)); + setTimeout(resolve, timeOut)); } walk_bw (args, util) { const moveTime = 1500; + let timeOut = moveTime; let steps = parseInt(args.STEPS); - steps = Math.min(Math.max(steps, 1), 20); + steps = Math.min(Math.max(steps, 0), 20); const stepLength = -25; console.log(`traj/step/${steps}/?moveTime=${moveTime}&stepLength=${stepLength}`); mv2.send_REST(`traj/step/${steps}/?moveTime=${moveTime}&stepLength=${stepLength}`); + if(steps){ + timeOut = timeOut * steps; + } return new Promise(resolve => - setTimeout(resolve, moveTime * steps)); + setTimeout(resolve, timeOut)); } walk (args, util) { let moveTime = parseFloat(args.MOVETIME) * 1000; moveTime = Math.min(Math.max(moveTime, 1), 10000); + let timeOut = moveTime; let stepLength = parseInt(args.STEPLEN); stepLength = Math.min(Math.max(stepLength, -50), 50); let steps = parseInt(args.STEPS); - steps = Math.min(Math.max(steps, 1), 20); + steps = Math.min(Math.max(steps, 0), 20); let turn = parseInt(args.TURN); turn = Math.min(Math.max(turn, -25), 25); console.log(`traj/step/${steps}/?stepLength=${stepLength}&moveTime=${moveTime}&turn=${turn}`); mv2.send_REST(`traj/step/${steps}/?stepLength=${stepLength}&moveTime=${moveTime}&turn=${turn}`); + if(steps){ + timeOut = timeOut * steps; + } return new Promise(resolve => - setTimeout(resolve, moveTime * steps)); + setTimeout(resolve, timeOut)); } turn (args, util) { const moveTime = 1500; + let timeOut = moveTime; let steps = parseInt(args.STEPS); - steps = Math.min(Math.max(steps, 1), 20); + steps = Math.min(Math.max(steps, 0), 20); let turn = 20; const side = args.SIDE; if (side === '1'){ @@ -423,8 +436,11 @@ class Scratch3Mv2Blocks { } console.log(`traj/step/${steps}/?moveTime=${moveTime}&turn=${turn}&stepLength=1`); mv2.send_REST(`traj/step/${steps}/?moveTime=${moveTime}&turn=${turn}&stepLength=1`); + if(steps){ + timeOut = timeOut * steps; + } return new Promise(resolve => - setTimeout(resolve, moveTime * steps)); + setTimeout(resolve, timeOut)); } wiggle (args, util) { @@ -466,13 +482,17 @@ class Scratch3Mv2Blocks { slide (args, util) { const moveTime = 1000; + let timeOut = moveTime; let steps = parseInt(args.STEPS); - steps = Math.min(Math.max(steps, 1), 20); + steps = Math.min(Math.max(steps, 0), 20); const side = args.SIDE; console.log(`traj/sidestep/${steps}/?side= ${side}&moveTime=${moveTime}`); mv2.send_REST(`traj/sidestep/${steps}/?side=${side}&moveTime=${moveTime}`); + if(steps){ + timeOut = timeOut * steps; + } return new Promise(resolve => - setTimeout(resolve, moveTime * steps)); + setTimeout(resolve, timeOut)); } eyes (args, util) {