Cooperate (#985)

* cooperate pause

* fix math

* update lastPause before pausing

* faster cooperation

Co-authored-by: Peli de Halleux <peli@DESKTOP-5B7QRAM.corp.microsoft.com>
This commit is contained in:
Peli de Halleux 2020-08-21 06:06:59 +02:00 committed by GitHub
parent 3f598a3eee
commit a47a06ac19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 10 deletions

View File

@ -27,7 +27,6 @@ while (true) {
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart) music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle); music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
} }
pause(1);
} }
``` ```
@ -43,6 +42,5 @@ while (true) {
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart) music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle); music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
} }
pause(1);
} }
``` ```

11
libs/core/cooperate.ts Normal file
View File

@ -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)
}
}
}

View File

@ -21,8 +21,7 @@ namespace sensors.internal {
const now = control.millis(); const now = control.millis();
if (now - this.lastQuery >= this.interval * 2) if (now - this.lastQuery >= this.interval * 2)
this.queryAndUpdate(); // sensor poller is not allowed to run this.queryAndUpdate(); // sensor poller is not allowed to run
if (now - this.lastPause >= this.interval * 5) control.cooperate(); // allow events to trigger
pause(1); // allow events to trigger
} }
private queryAndUpdate() { private queryAndUpdate() {

View File

@ -124,7 +124,7 @@ namespace motors {
export function stopAll() { export function stopAll() {
const b = mkCmd(Output.ALL, DAL.opOutputStop, 0) const b = mkCmd(Output.ALL, DAL.opOutputStop, 0)
writePWM(b); writePWM(b);
pause(1); control.cooperate();
} }
/** /**
@ -136,7 +136,7 @@ namespace motors {
//% help=motors/reset-all //% help=motors/reset-all
export function resetAll() { export function resetAll() {
reset(Output.ALL) reset(Output.ALL)
pause(1); control.cooperate();
} }
interface MoveSchedule { interface MoveSchedule {
@ -269,7 +269,7 @@ namespace motors {
if (this._brake && this._brakeSettleTime > 0) if (this._brake && this._brakeSettleTime > 0)
pause(this._brakeSettleTime); pause(this._brakeSettleTime);
else { else {
pause(1); control.cooperate();
} }
} }
@ -280,7 +280,7 @@ namespace motors {
// allow robot to settle // allow robot to settle
this.settle(); this.settle();
} else { } else {
pause(1); control.cooperate();
} }
} }
@ -357,7 +357,7 @@ namespace motors {
// special: 0 is infinity // special: 0 is infinity
if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) { if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) {
this._run(schedule.speed); this._run(schedule.speed);
pause(1); control.cooperate();
return; return;
} }

View File

@ -26,7 +26,8 @@
"icons.jres", "icons.jres",
"ns.ts", "ns.ts",
"platform.h", "platform.h",
"integrator.ts" "integrator.ts",
"cooperate.ts"
], ],
"testFiles": [ "testFiles": [
"test.ts" "test.ts"