pxt-microbit icon indicating copy to clipboard operation
pxt-microbit copied to clipboard

sonar doesn't work in V2 board

Open kimlabtoy opened this issue 1 year ago • 14 comments

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

kimlabtoy avatar Jul 25 '23 17:07 kimlabtoy

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

jwunderl avatar Jul 25 '23 17:07 jwunderl

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

kimlabtoy avatar Jul 25 '23 17:07 kimlabtoy

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...

JohnVidler avatar Jul 25 '23 18:07 JohnVidler

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 ;
}

jwunderl avatar Jul 25 '23 18:07 jwunderl

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 avatar Jul 25 '23 18:07 kimlabtoy

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

kimlabtoy avatar Jul 25 '23 19:07 kimlabtoy

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

abchatra avatar Oct 20 '23 21:10 abchatra

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.

horssenp avatar Apr 11 '24 09:04 horssenp

Thanks @horssenp

abchatra avatar Apr 11 '24 15:04 abchatra

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

abchatra avatar Jul 26 '24 21:07 abchatra

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.

microbit-carlos avatar Jul 30 '24 18:07 microbit-carlos

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

martinwork avatar Jul 31 '24 15:07 martinwork

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

abchatra avatar Aug 23 '24 16:08 abchatra

For the post-release hotfix, yes.

microbit-carlos avatar Aug 23 '24 16:08 microbit-carlos