From 60c09809e7d9cbd97acbca27f0f8b042ccf5fa2c Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Tue, 17 Sep 2019 15:14:14 -0700 Subject: [PATCH] Stall (#897) * stall detection * arrange blocks * updated blocks * tested on hw --- libs/core/output.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/libs/core/output.ts b/libs/core/output.ts index 984772c1..e220ba2a 100644 --- a/libs/core/output.ts +++ b/libs/core/output.ts @@ -502,7 +502,7 @@ namespace motors { */ //% blockId=motorPauseUntilRead block="pause until %motor|ready" //% motor.fieldEditor="motors" - //% weight=90 + //% weight=90 blockGap=8 //% group="Move" pauseUntilReady(timeOut?: number) { pauseUntil(() => this.isReady(), timeOut); @@ -610,6 +610,33 @@ namespace motors { toString(): string { return `${this._large ? "" : "M"}${this._portName} ${this.speed()}% ${this.angle()}>`; } + + /** + * Pauses the program until the motor is stalled. + */ + //% blockId=motorPauseUntilStall block="pause until %motor|stalled" + //% motor.fieldEditor="motors" + //% weight=89 + //% group="Move" + //% help=motors/motor/pause-until-stalled + pauseUntilStalled(timeOut?: number): void { + // let it start + pause(50); + let previous = this.angle(); + let stall = 0; + pauseUntil(() => { + let current = this.angle(); + if (Math.abs(current - previous) < 1) { + if (stall++ > 2) { + return true; // not moving + } + } else { + stall = 0; + previous = current; + } + return false; + }, timeOut) + } } //% whenUsed fixedInstance block="large motor A" jres=icons.portA