A few years back, I picked up a Lego Mindstorms kit when they went on sale. My family had fun building all of the robots that the kit provides instructions for. We also tried several of the designs people have published on-line.
Mindstorms are comprised of Lego bricks, gears, shafts, wheels, and other various pieces similar to those found in the Technic sets. In addition to the bricks they also have motors, sensors, wiring, a remote, and a brain box to programmatically control your creations via a simple to use drag-n-drop app on your mobile or PC.
Once we’d had our fun with them, they went back in the box. Fast forward a few years, my now teenage son, approached me saying that he wanted to learn how to code in Python. He asked if I had any ideas for a project he could use for motivation. Having just gone through a bunch of items to put in the neighborhood garage sale, I thought of the Mindstorms. I had recently read an article regarding a Python runtime someone had created for the control box. “Let’s build a robot.”, I answered.
He thought of building a Roomba like bot, that could navigate its environment. We would need to write software to tell the machine to make a reverse J-turn when either the forward or downward facing sensors detected an obstacle. He used the TRACK3R plans from LEGO as the basis for the vehicle. Basically, we built a tank with a sonic sensor facing downward and an IR sensor facing forward.
Once he had assembled the bot, the next step was to load the Linux based OS onto a MicroSD card. We followed the well written instructions on the Lego education site and at Ev3Dev.org to get our controller to dual boot Linux with an embedded Python framework. If you are planning a similar project, the best advice I can give is to be patient, go slow, and follow each instruction exactly.
There is a extension for Microsoft’s Visual Code IDE that makes getting up and coding a breeze. The Ev3DevPython site has some great documentation for installing and using the extension. They also have some sample code, video instructions, and a lot of other useful information. I would suggest visiting the site before starting your own build.
After all the moving parts were sorted, we began testing various solutions for the code that would allow our bot to autonomously drive around the house. At first, we struggled trying to get the tank to run straight. We were trying to use the “MoveTank” method, but couldn’t keep it running in a loop. We solved the problem by running each motor, individually, in a while loop. The finished “Roomba” code is below. Feel free to use it in your own projects. You may need to change the Output ports to match the way your bot is wired.
#!/usr/bin/env python3
from ev3dev2.auto import *
us = UltrasonicSensor(INPUT_3)
mr = LargeMotor(OUTPUT_A)
ml = LargeMotor(OUTPUT_B)
ir = InfraredSensor(INPUT_4)
dist=us.value()/10
sound = Sound()
sound.speak("zoo wee mama")
while us.distance_centimeters < 8:
mr.run_timed(time_sp=100, speed_sp=500)
ml.run_timed(time_sp=100, speed_sp=500)
if ir.proximity < 14:
mr.on_for_rotations(SpeedPercent(70), -3)
if us.distance_centimeters >8:
mr.stop()
ml.stop()
sound.speak("help me I am stuck")
break