Mbot/MBlock Obstacle Avoidance + Memory Code


Ive been working on some code for a long time that is the obstacle avoidance code but extensively modified to remember where the obstacles are and then avoid them. Ive been working on it for freaking ever and can’t quite get it to work. Obstacle Avoidance Remember.sb2 (78.4 KB)

All the code is commented to the best of my 5 min ability do so. Ill can clear things up if you ask.

Also if you have really bad hardware this code is freaking massive in mblock and it lags out my MacBook air to like 5 fps when coding in it so keep that in mind.


Pls help finish the code :joy: I’m legit dying from working on it


I am pretty much a beginner in this arena so I am not the right man for this job, but this sounds very cool and is something I’m interested in myself. Looking at the code will not mean much to me yet, but I am very curious to know how you generate and store the “map” during a physical scan of the area. I assume you are using data from the encoder motors to know where you have been (or are you using the compass sensor, or both?) so that seems like the easy part, tough part being how to get this poor blind thing to remember where everything is. In fairly broad strokes, can you describe how your system works? (I realize this may be a lot to ask and will gladly accept “F-OFF” as a valid input value.)


the biggest problem with the code is the motors actually don’t have encoders in them as they do not output any values for their rotation. So i had to measure the circumference of the wheels and measure how many rotations/second the wheels turned at speed 255, which turned out to be around 2/s, but that value changes based on how full the battery is so your mileage may change. i figured out that every 0.1s it moves 1.57in at a pretty low battery power when i tested it. I set 1.57 to just a basic unit of distance 1, so in the code every 0.1s it changes its Y-Position by 1. The graph it creates has the Y axis as the forward-backward sides of the robot when the button is pressed, and the X-axis as the side to side.
In a hypothetical situation If it sees an object that is 15cm in front of it and the robot hasn’t rotated at all, so the positive end of the Y axis is still forward, then it would set the objects Y-position to the correct y-position of the robot + 3.76 units (1.57in (1 unit) is 3.988cm, 15cm/3.988 is 3.76). If you want any more info id be glad to give it because i want this code to work.


I forgot about the “Mbot” in the title! Yes, I guess encoders would have made your life a lot easier.

So your code plots an X/Y of each obstacle and stores those co-ordinates in a variable? I’m not clear how the grid is stored/constructed/accessed. I would guess these co-ordinates are all stored individually in variables? Does the robot essentially map out a perimeter using something like a maze solving or wall-following algorithm and then fill it in with a systematic search for obstacles? (Eat your heart out, Roomba.) Am I asking too many annoying questions?

Thinking about this is giving me ideas, hoping it might be useful to you in terms of fresh thinking about this system. Did you make a sort of logical flowchart of what is going on? Where do you think it might be breaking?


Yes there are, in my code, 18 XY coordinates, 2 for the robot ( X Position, Y Position), and 2 for each obstacle, and since i have 8 obstacles, there are 16 coordinates for Obstacles. (1 X, 1 Y, 2 X, 2 Y, 3X, 3 Y…). basically it drives forward until it sees an object, then once it senses an object, it places the first objects 1X,1Y coordinates, then turns left or right, then if it sees another one, it does the same except the coordinates are 2X, 2Y for the 2nd obstacle… all the way until 8 obstacles, since there is no “create variable” code that i can place, i have to pre-determine how many obstacles its capable of seeing.