microsoft / pxt-microbit

A Blocks / JavaScript code editor for the micro:bit built on Microsoft MakeCode
https://makecode.microbit.org
Other
720 stars 590 forks source link

sonar doesn't work in V2 board #5327

Open kimlabtoy opened 1 year ago

kimlabtoy commented 1 year ago

I run my program use V1 in v6.0.15 build, its work well, and run the same program use V2 in v6.0.15 build, it can only detect <10cm and quickly show error 030. Run this program use V2 in v6.0.16 build will show error 914. A2 363023312_1714468798974788_4826183488006116484_n Aa3

jwunderl commented 1 year ago

Could you create a share link to the exact program that reproduces this using the image button?

kimlabtoy commented 1 year ago

@jwunderl https://makecode.microbit.org/S93364-44504-66711-82927

JohnVidler commented 1 year ago

Just pinging myself here to track this in case there's a deeper CODAL cause. The 030 would indicate a memory fault on the heap...

jwunderl commented 1 year ago

I don't see anything in pxt-sonar that feels likely to induce an error but will see if I can get a minimal repro, feels more likely it could be stemming from one of the packages handling the display or potentially some edge case in codal. for ref pxt-codal is just https://github.com/microsoft/pxt-sonar/blob/master/main.ts; so relevant code basically just comes out to

pins.setPull(trig, PinPullMode.PullNone);
pins.digitalWritePin(trig, 0);
control.waitMicros(2);
pins.digitalWritePin(trig, 1);
control.waitMicros(10);
pins.digitalWritePin(trig, 0);

// read pulse
const d = pins.pulseIn(echo, PulseValue.High, maxCmDistance * 58);

switch (unit) {
    case PingUnit.Centimeters /** 1 **/: return Math.idiv(d, 58);
    case PingUnit.Inches /** 2 **/: return Math.idiv(d, 148);
    default: return d ;
}
kimlabtoy commented 1 year ago

I found that sonar program can work well in v5.1.40, but doesn't work in v6.0.1~v6.0.17. (V2 board) In v6.0.17 sonar (use SR04) can only detect <10cm, but no error code. In v6.0.16 show error code 030 in a few second.

kimlabtoy commented 1 year ago

@jwunderl I use another program that remove oled12864, but it looks like a same situation. https://makecode.microbit.org/S93364-44504-66711-82927 A4

abchatra commented 1 year ago

@JohnVidler @carlosperate this one may be important to address.

horssenp commented 6 months ago

Variable 'Centimeters' in pxt-sonar is duplicated in maqueen.ts version 1.7.2 Hence duplicate error....

Just my 2 cents in trying to get it working.

abchatra commented 6 months ago

Thanks @horssenp

abchatra commented 3 months ago

@carlosperate @jaustin this one is good to address for this release.

microbit-carlos commented 3 months ago

We'll see if we can fit it in the next CODAL tag, but will depend on external factors as we don't have a dedicated codal person at the moment.

martinwork commented 3 months ago

I tried V1 and V2 in MakeCode v5 and live (6.0.28) measuring a distance of about 92cm with the sonar sensor on a :MOVE MOTOR.

Eventually, I noticed that every test started with a single high reading that was in fact correct, followed by varying lower readings.

The lower readings seem to be fixed by adding a digital read of the trigger pin after the ping pulse in, with a pause before the next reading.

I modified the example from https://github.com/microsoft/pxt-microbit/issues/5327#issuecomment-1650376757 to set the trigger pin back to digital input. I changed the pins to 13/14 for the buggy sensor. https://makecode.microbit.org/_ELdeFJHz88fs Note this has OLED and sensors extensions plus an old version of sonar, but it works for me with the reset of the trigger pin

Here's a fresh new project https://makecode.microbit.org/_JziM4K2wyRHj

And a :MOVE MOTOR one. https://makecode.microbit.org/_9k5WjWP1zdwb

abchatra commented 2 months ago

@carlosperate does this needs to be tracked for this release?

microbit-carlos commented 2 months ago

For the post-release hotfix, yes.