The following material has been reprinted with permission from Steve's site.
About the LegWay
The LegWay is a Lego robot, built to stand on two wheels and balance, follow a black line, and/or spin in place.
Many people have tried to build balancing robots, but few have been able to make it work. I managed to find the correct combination of hardware and software to allow the robot to balance. Once I was able to make it balance, it was a simple task to make it move forward and follow a line.
Many people have asked if it will work on a slope. Actually, it will, for the same reason it will work on different color surfaces. LegWay constantly attempts to adjust its balance point. I've been able to put it on a table, and tilt the table, and LegWay continues to maintain its balance.
I've succeeded in making LegWay work with the standard Lego remote control. Using the motor control buttons, the robot can move forward, backward, or turn right or left while balancing.
Also, using an Acrobot from the Lego Constructipedia, I've made a robot that can stand up on its own. I also attempted to make LegWay balance using NQC, but the response of the software was just too slow. I still have some ideas for making it work with a standard light sensor, but I haven't been able to complete that, yet.
Watch the LegWay in motion
LegWay program
The program for LegWay was written in brickOS (LegOS) and uses EOPDs to maintain a constant distance from the ground. As the distance decreases, LegWay moves forward. As the distance increases, LegWay moves backward.
Every 50 ms, LegWay attempts to recalculate the balance point by measuring the current distance and motor speed.
To move forward for line following, LegWay actually sets the motors to run backward, causing a tilt, which it automatically corrects by moving forward. When one sensor is over the line, it stops that motor, and LegWay balances using only the other motor, causing it to turn.
To spin in place, both motors are shifted "off center" in opposite directions, the same amount, but they still correct for tilting.
In the pictures, you may notice the second sensor is disconnected. The main LegWay program will follow a line if two sensors are attached (always moving forward) or attempt to stand still, if only one sensor is attached.
![](http://uphotos.eepw.com.cn/fetch/forrob/legway.gif)
If the motors are set at full power in either direction for more than one second, LegWay assumes it has fallen over and the program ends.
LegWay EOPD sensors
LegWay uses two electro-optical proximity detectors (EOPDs) from HiTechnic Sensors to balance and detect lines.
The EOPDs are based on the infrared proximity detector (IRPD) circuit, but use visible light to determine distance by checking the detector, sending a pulse of light, and checking the detector again to calculate the amount of light reflected. The value returned will change based on the distance to an object and the color of the object. IRPDs can be used in place of the EOPDs, but they will not work as well for detecting and following lines. These sensors are available now. I used red EOPDs.
For this application, a small change in the value will usually indicate a change in the distance to the surface, while a large change will represent a change in the surface color (white to black) The EOPDs usually do not return a usable distance to a black surface, because the light is absorbed.
LegWay past
I attempted to build LegWay using an accelerometer to detect tilt, but soon realized the acceleration due to gravity would not change as the robot tilts, because the robot is accelerating at 9.8 m/s^2. So the reading will be zero until it smashes into the floor.
Several people have said they tried without success to build something like this with standard Lego light sensors, but I don't believe they have the resolution required (not even close).
LegWay future
My next project will be to make LegWay work with a remote control. (I've done this.) I have a couple ideas, which include using a SpyBot remote to drive LegWay around, or use a regular Lego remote to run some pre-programmed moves.
Another idea is to make LegWay stand up from a lying position on its own. It can't do that right now, because the center of mass is below the axle when it's on its side.
LegWay response
I've had a TON of people contact me about my LegWay. I believe it's been popular because it seems easy, and many people have tried it. However, as far as I know, no one's been able to do it with Lego Mindstorms before.
LegWay was voted into the Hall of Fame at Lego Mindstorms Monthly. LegWay was also selected as the Cool Robot of the Week by NASA's Space Telerobotics Program for November 18, 2002.
Be like Steve
Want to build your own LegWay? Here's all the information you'll need:
Originally posted January 16, 2003