From a47a06ac194f0e3d28a3b19542270b0274b2c5e6 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 21 Aug 2020 06:06:59 +0200 Subject: [PATCH] Cooperate (#985) * cooperate pause * fix math * update lastPause before pausing * faster cooperation Co-authored-by: Peli de Halleux --- docs/coding/ignition.md | 2 -- libs/core/cooperate.ts | 11 +++++++++++ libs/core/input.ts | 3 +-- libs/core/output.ts | 10 +++++----- libs/core/pxt.json | 3 ++- 5 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 libs/core/cooperate.ts diff --git a/docs/coding/ignition.md b/docs/coding/ignition.md index 298d3111..109ed42b 100644 --- a/docs/coding/ignition.md +++ b/docs/coding/ignition.md @@ -27,7 +27,6 @@ while (true) { music.playSoundEffectUntilDone(sounds.mechanicalMotorStart) music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle); } - pause(1); } ``` @@ -43,6 +42,5 @@ while (true) { music.playSoundEffectUntilDone(sounds.mechanicalMotorStart) music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle); } - pause(1); } ``` \ No newline at end of file diff --git a/libs/core/cooperate.ts b/libs/core/cooperate.ts new file mode 100644 index 00000000..850a3a92 --- /dev/null +++ b/libs/core/cooperate.ts @@ -0,0 +1,11 @@ +namespace control { + let lastPause = 0; + const COOPERATION_INTERVAL = 20 + export function cooperate() { + const now = control.millis() + if (now - lastPause > COOPERATION_INTERVAL) { + lastPause = now + pause(1) + } + } +} \ No newline at end of file diff --git a/libs/core/input.ts b/libs/core/input.ts index c1f8d715..fa0d6af8 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -21,8 +21,7 @@ namespace sensors.internal { const now = control.millis(); if (now - this.lastQuery >= this.interval * 2) this.queryAndUpdate(); // sensor poller is not allowed to run - if (now - this.lastPause >= this.interval * 5) - pause(1); // allow events to trigger + control.cooperate(); // allow events to trigger } private queryAndUpdate() { diff --git a/libs/core/output.ts b/libs/core/output.ts index ea431c00..5cb2fede 100644 --- a/libs/core/output.ts +++ b/libs/core/output.ts @@ -124,7 +124,7 @@ namespace motors { export function stopAll() { const b = mkCmd(Output.ALL, DAL.opOutputStop, 0) writePWM(b); - pause(1); + control.cooperate(); } /** @@ -136,7 +136,7 @@ namespace motors { //% help=motors/reset-all export function resetAll() { reset(Output.ALL) - pause(1); + control.cooperate(); } interface MoveSchedule { @@ -269,7 +269,7 @@ namespace motors { if (this._brake && this._brakeSettleTime > 0) pause(this._brakeSettleTime); else { - pause(1); + control.cooperate(); } } @@ -280,7 +280,7 @@ namespace motors { // allow robot to settle this.settle(); } else { - pause(1); + control.cooperate(); } } @@ -357,7 +357,7 @@ namespace motors { // special: 0 is infinity if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) { this._run(schedule.speed); - pause(1); + control.cooperate(); return; } diff --git a/libs/core/pxt.json b/libs/core/pxt.json index b392b009..fbb190d4 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -26,7 +26,8 @@ "icons.jres", "ns.ts", "platform.h", - "integrator.ts" + "integrator.ts", + "cooperate.ts" ], "testFiles": [ "test.ts"