Sonar sensor stops working when in mBot mode


#1

Refer to the images. The following works i.e. when an obstruction is 5cm from the mBot the lights come one

Screenshot of working senario

However when it’s uploaded to the mBot it does not. Basically what happens now is any obstruction up to about 40cm away causes the light to come one

Screenshot of mBot upload

I am giving a a class in 2 days using 10 mBots and really need to work this out before then.


#2

Hi @abasel -

I was interested in your comment, so tried and was able to replicate your observations … quite strange. I’d also be interested in hearing what the Makeblock folks have to say about this.

In an attempt to try to display the actual ultrasonic reading in the mBot program mode I was playing around and inserted a ‘send mBot message’ statement at the beginning of the program … here’s the screenshot …

.

(To be honest, I actually have no idea yet of what the send message statement does … I was just fooling around).

Curiously, running this modified program, the program seems to work as intended again … reliably discriminating objects at about the 5cm threshold.

Anyway, again, I have no idea why this works, or what ‘send message’ does … it was just an interesting accidental find that I thought I’d pass along. Will await Makeblock’s take on what might be happening here.


#3

Further to my last post …

I compared the actual Arduino code generated by your mBot example (with the ‘set dist to ultrasonic’ statement) to mine (with the ‘send mBot message to ultrasonic’ statement). The results were interesting, and I think perhaps shed some light on why your version isn’t working.

I note in my code example that several initializers related to the ultrasonic module are included which are missing within the Arduino code your program generated, specifically - the MeIR.h header, and the declaration and begin/use of an ir variable. I’ve extracted the relevant code snippets below …

So, it seems what is happening (still for some unknown reason) is that your attempted inclusion of the ultrasonic sensor via the ‘set dist to ultrasonic’ sensor statement really isn’t including any reference at all to the sensor.

Anyway, I thought this was really interesting and relevant … hopefully it can help the Makeblock folks formulate a response.


#4

Many thanks… at least I can get it working again… yes it does seem that the mBlock interface is a bit buggy but as long as their is a work around so the kids can get it working :smile:

Cheers


#5

Agreed … glad it will work as a workaround for you.

Not sure if this particular one is a ‘buggy’ issue as much as perhaps simply a lack of documentation (or at least documentation I haven’t found yet). The use of variables, the corresponding use of the set/show/hide commands, the ‘send mBot message’ commands, etc. … it’s all a bit vague as to what they do and their proper use. I find myself doing a bit of (educated) guessing as well as trial and error to figure things out.

Hopefully this will get worked through with time as well as questions from the user community.


#6

I ran into the same problem and played with it in the Arduino software. It seems to be related to when the sonar sensors don’t sense anything or the distance is very long. When running in mBlock mode, the returned value from ultrasonic sensor will be a large number, maxing out at about 400 or so. When running compiled code on the mBot it appears that this causes a data overrun or type conversion issue or something similar and gets returned as zero, exactly the opposite of the desired value.
My work around was:

Set dist1 to ultrasonic sensor port1 distance
if (dist1=0) set dist1 to 255

then it all worked fine.


#7

Further digging into the code…

When running from the computer in Mblock, the function called on the mbot device uses the following code:
case ULTRASONIC_SENSOR:{
if(us.getPort()!=port){
us.reset(port);
}
value = (float)us.distanceCm(50000);
sendFloat(value);
}

while the compiled version uses this command:
dist1 = ultrasonic_1.distanceCm();

the noted difference is the input to the distanceCm of 50000 instead of nothing. This value is the maximum amount of distance(time for sound to bounce back) to wait for the ultrasonic sensor before the distance sensor function bails out. The 50000 is 50000 cm, while the default value when nothing is provided is 400 cm. So I think in the compiled code, the sensor bails out (timeout of the pulsein function) if no bounce is detected is signifcantly lower, and my guess is returns a time of 0 if nothing is detected.

Once i get time, i’ll add the 50000 to the compiled code to confirm.


#8

At first, I thought my ultrasonic sensor was not working. After some tinkering, I found a way to make it read out distances. I’ve calibrated it with a tape measure and it’s fairly accurate. I’m guessing that the problem comes from header declarations (most sample code I’ve seen so far are using MeOrion.h rather than MeMCore.h so I was inadvertently using those.) Also, the sensor initialization that worked for me was:
MeUltrasonicSensor ultrasonic(3);

Posted the code here: http://forum.makeblock.cc/t/mbot-arduino-code-that-works-for-the-ultrasonic-sensor/4828

Hope this helps!


#9