Escaping code


#1

I need to develop a code that lets Mbot to exit from an enclosed rectangular area. One of the smallest side is opened and i need mbot to leave from there. Any guess?


#2

How will the mBot know that it left the enclosed area? Line on the ground at the exit?


#3

No line on the ground at the exit. I actually didnt think that it is necessary. Just want Mbot to find the free space. But i am no t sure how to code it


#4

So I ask because its difficult to tell the difference between open space inside the enclosed space (if its larger than 40cm) and open space outside. Lets assume the bot faces the exit by chance and just heads out, looking for a wall, but there is none. That’s why I thing a line marking the exit makes programming easier


#5

@MbotUser,

Please describe the rectangle better. Does it have walls, or lines? This will help to determine which sensor to would be best to use.

Mike


#6

The rectangle is like a maze. It has walls. One side is 200cm and the other side is 40cm. The exit is 20cm which is on one of the small wall.


#7

Ok in this particular setup, the mBot will always be able to detect both long walls with the ultrasonic sensor because they are closer than 40cm.

Solution idea:
0. set variable exit_found to false

  1. turn bot on the spot until the US detects a wall <40cm
  2. go straight until distance to wall <10cm
  3. Make a full turn on the spot and memorize smallest distance to the wall.
  4. Keep turning until US shows the same smallest distance again. (now the bot should face the wall)
  5. Turn right 90 degrees (you need to use timing for that unless you have a compass or 3d acceleration sensor)
  6. Repeat until exit_found is true:
    6.1 reset timer
    6.2 set motors to forward
    6.3 Repeat until timer is > 2s (or whatever time it takes the robot to travel 10cm, you do the tweaking) or
    if there is a wall <10cm in front of bot
    6.4 stop motors
    6.5 if there is a wall <10cm in front of bot, turn right 90 degrees (bot is in a corner and needs to follow the next wall)
    6.6 else turn left 90 degree
    6.7 if US distance <9 cm, turn right 105 degree ( bot is too close to wall and needs to increase)
    else if US distance is between 9 and 11cm, turn right 90 degree (bot was parallel to wall and can go on)
    else if US distance is between 11 and 20cm, turn right 75 degree (bot is getting too far from the wall)
    else if US distance > 20cm, then set exit_found to true.
  7. go straight ahead for 40cm (this should leave the area through the exit)

robot will need to turn less if you turn the US using a servo


#8

@MbotUser … did that help?


#9

Hi Mbotuser,

we solve a similar challenge and came out with the following solution.

No exit lines. Just trying to correct trajectory by getting closer or farther from the wall.

Turning time should be adjusted depending on the type of floor.

According to my 10 years old son, it is funnier if you play some sound when you just found the exit!


#10