nathankellenicki / node-poweredup

A Javascript module to interface with LEGO Powered Up components.
https://nathankellenicki.github.io/node-poweredup/
MIT License
483 stars 61 forks source link

possible fix for #118, always return resolve #121

Closed Debenben closed 3 years ago

Debenben commented 3 years ago

I have done only a few tests with my technic hubs and motors, but I would expect this to work with all devices. A better solution than this could be to register callbacks for all commands. Then it is possible to only resolve those that completed and reject the discarded ones. For my current project I do not need this, but it might be handy for others.

Below all combinations of feedback bits I managed to produce:

    const feedback0x01 = () => { // normal operation, one command in progress sends 0x01 followed by 0x0a when finished
    setInterval(() => {
        motorA.rotateByDegrees(1,100);
    }, 1000);
    }

    const feedback0x05 = () => { // current command(s) discarded, replaced with new command which is in progress
    setInterval(() => {
            motorA.rotateByDegrees(100,10);
    }, 100);
    }

    const feedback0x0a = () => { // command(s) succeeded
        setInterval(() => {
            motorA.setPower(0);
    }, 100);
    }

    const feedback0x0c = () => { // add some mechanical load to make sure motor is not able to rotate and command fails
        setInterval(() => {
        motorA.rotateByDegrees(100,1);
        }, 10000);
    }

    const feedback0x0e = () => { // command(s) which were in progress (= rotateByDegrees) discarded, interrupted by new command (= setPower) which succeeded
    setInterval(() => {
            motorA.rotateByDegrees(100,10);
            motorA.setPower(0);
    }, 100);
    }
nathankellenicki commented 3 years ago

Thanks for the PR, merged!