|
|
AVR Robot Controller 1.1 |
| Home>AVR Robot Controller->LEGO | |
|
|
This article appeared in the April 2004 issue of Servo Magazine.I already read the article, Skip to the sample code now! Why LEGO sensors?Lego construction kits are amazing toys. With a few standardized parts one can quickly build fairly rugged structures. Add a computer, motors and sensors; one can build up prototype or final robots. The LEGO Mindstorm kits, with the LEGO RCX Brick computer add the final pieces needed for building robots: motors, gears and sensors that easily snap together to rapidly build robots. The standard LEGO brick, however, has serious limitations. No more than three sensors can be connected at a time (not strictly true, but true if you want full functionality from each sensor) and the programming language has limited access to the capabilities of the internal CPU. I am used to programming in C and assembly and find the expressive power of the native LEGO languages (Robotics Invention System and Robolab) restrictive. Because of the popularity of the LEGO Mindstorm kits (I suspect many adults purchased them for their kids, but ended up owning them; that is my excuse!) there has been considerable activity in extending the basic LEGO system. There are web sites detailing how LEGO sensors work and how to create your own special sensors. There are alternative firmware and high level languages available that liberate the creator from the limitations of the stock LEGO RCX controller. For more sophisticated LEGO based robots, more powerful computers and home-brew sensors replace the LEGO Brick and Mindstorm sensors. This combination is popular for teaching robotics at the college level. In short, it seems that either one uses the LEGO RCX brick and tries to get around it’s limitations through alternate software, or one dumps all the Mindstorm parts and uses custom controllers & sensors with the LEGO parts when building robots. I never saw a third option of using stock LEGO sensors with alternate computers. This didn’t seem right since the LEGO Mindstorm sensors are rugged, inexpensive, easily available, and are designed to work with LEGO parts. Having an extended Mindstorm kit at home, with plenty of extra sensors, I became intrigued by the idea of using the stock LEGO parts with my own AVR Robot Control board (www.Barello.net/ARC); I wondered if the standard 9v LEGO sensors would work with +5v logic of a simple microcontroller. After performing some quick tests I found that LEGO sensors work fine with a +5v system. All one needs is an Analog input (ADC) and the ability to control whether an I/O port is an input or output. This article describes interfacing LEGO Mindstorm sensors with an Atmel AVR microprocessor (www.atmel.com) using the BASCOM (www.mcselec.com) basic compiler. The techniques described should work with PIC and HC11 style microcontrollers using a variety of languages as long as they support switching an analog I/O port from an output to an input. How LEGO sensors workLEGO sensors are very clever two wire devices. They are non-polarized so that they can be connected either way and still work. The standard LEGO connector is a 2x2 block with connecting plates on either side. The plates are cross-wired so connecting in any of the four possible orientations will make the circuit work.
Figure 1 LEGO Connector Figure 2 illustrates a generalized schematic of the LEGO sensor interface. A voltage applied to the terminals charges capacitor C1 to power to the circuitry. The four diodes, D1 through D4 form a classic bridge rectifier so that C1 is charged properly regardless of how the device is connected.
Figure 2 Generalized LEGO sensor schematic Reading the output signal of a LEGO sensor is also very clever. A 0-400ua current sink, in the form of an NPN transistor provides the output signal. A current sink will draw a specified amount of current regardless of the applied voltage. While under power, the output draws a fraction of a milliamp. When power is removed, the capacitor, C1, continues to power the device and the amount of current being sunk can be measured as a voltage across a 10k load resistor. Note that diodes D5 and D6 connected to the current sink from each terminal: they route the current to the correct pins regardless of connector orientation. Figure 3. Shows the Microcontroller interface to a LEGO sensor. As the output current varies from 0-400ua, the voltage read by the ADC input will vary from 5 to 1v. With a 10 bit ADC (typical) the count range will be ~200 – 1000.
Figure 3 CPU LEGO Interface Figure 4 shows a time/voltage graph of powering and reading a LEGO sensor. After powering the sensor for a minimum of 2-3 ms, the power is removed and the I/O port re-configured as an ADC input. The output of the sensor will settle down within 10us and can be measured. After measuring, the port is re-configured as an output to power the sensor.
Figure 4 General LEGO sensor time/voltage graph LEGO SensorsSwitchThe LEGO switch sensor is the simplest sensor. It is a switch in series with an 800-ohm resistor. It takes no special software or even an ADC to read. However, it can be read with the same software as the LEGO light sensor, but will read 100% light when closed. The 800 ohm resistor solves several problems: a) when powered a closed switch won’t short the I/O pin to ground, and b) the sensor can be paralleled across a LEGO light sensor. When the switch closes, the 800-ohm resistor easily overwhelms the output of the light sensor to bring the output voltage near zero.
Figure 5 LEGO Switch Schematic LightThe LEGO light sensor is pretty straightforward. It has an output that is the inverse of the light level. However, the output is restricted to a fairly narrow current range. All the results obtained here and used in the software were experimentally determined. I found that the LEGO light sensor returned from 600-900 counts from a 10bit ADC @ 5volts from brightest to darkest light levels. The brighter the light, the lower the value measured.
Figure 6 LEGO Light Sensor Voltage Graph The sample code modifies the measured value to return a byte wide value limited in range from 1 to 255.There is a special case for a switch connected across the sensor: because it returns a value much lower than the brightest light, the software can tell it is a switch and returns 0 when closed. Angle (Rotation)The third sensor I tried, the LEGO rotation sensor, was a bit tricky. The rotation sensor implements a quadrature encoder with four distinct states for each slot. The sensor has a four-slot encoder disk to give 16 counts per revolution. The rotation sensor communicates this information as four distinct output levels. Figure 7 shows the four levels, their corresponding states, and how to decode those states into encoder counts. Note that when moving forward, states change in one order, and in reverse, the other order. By examining the previous and current state, the software can determine whether the encoder is moving forward or backwards. The level for each state was determined experimentally.
Figure 7 LEGO Rotation Sensor Voltage Graph TemperatureThe fourth LEGO sensor available is the temperature probe. I did not have a sample of this probe, but the current output vs. temperature can be found in the references or experimentally determined. SoftwareI used an ARC control board and the BASCOM AVR basic compiler for all my experiments. The AVR CPU has a simplified, flexible I/O structure that facilitates controlling and reading LEGO sensors; it is also very fast, compared to other common 8 bit controllers. Using BASIC allowed me to generate test code very quickly. BASCOM has structured basic syntax (if/then/else if/end) and that combined with the straightforward I/O and interrupt structure of the AVR chip makes for a thoroughly pleasant programming experience. The basic logic is to first power the device, then wait for 3-8 ms, then remove power, and measure the amount of current the device draws. In the sample code I chose a fast ADC conversion time to minimized the time-spent reading and left the device powered when done.  This insures C1 is fully charged the next time the device is read. LEGO sensors can be read as fast as 300 Hz. The AVR microprocessor controls the Analog I/O port through three registers: Data Direction Register (DDRA), PORT output register (PORTA) and Pin input register (PINA). Each register is a byte wide, corresponding to the 8 bits in each I/O port. By setting the DDRA bit to 0 the I/O pin becomes an input. By setting the DDRA bit to 1 the pin becomes an output. When configured as an output, writing a 1 to the PORT bit will drive the pin to +5v and that powers the LEGO sensor. When reading the sensor both the DDR and PORT bits must be zero. The AVR CPU has an internal pull-up resistor for each I/O pin that activates when DDR = 0 and PORT = 1. Reading the LEGO sensor requires a 10k load resistor, we don’t want to add another, unknown, resistor by enabling the internal pull-up. The code sidebar has a simplified example of reading the light sensor. The function returns the raw ADC reading after powering the sensor for eight milliseconds. The sensor port is fixed as bit 0 and the sensor is left powered off at the end, which saves power.
Sample CodeThe following three files comprise the LEGO sample code. The sample application Simple_lego.bas includes the forward declarations file legolibdefine.bas near the beginning of code and the actual LEGO interace routines legolib.bas at the end. The library routines take a constant indicating which I/O port the sensor is attached. This allows the programmer to connect up to eight different LEGO sensors to PORTA. The sensors can be any mix of light, temperature, rotation or switch. The sample application simply calls each interface routine and prints out the results. Here is a photo of the modified ARC board analog inputs. Note the 10k resistors replacing the normal 33k resistors (for the floor sensors of the Mini-sumo high school project) and the LEGO sensors connected to the left two interface pins (stage left!) Where to purchase LEGO sensorsPITSCO is the USA LEGO educational distributor. You can either order over the web, or, call them up. Their web site is a little difficult to navigate and find stuff, but if you know the magic part numbers, they are easy to find. I included the connecting cables as well. Order the long ones, cut in half and use them to connect the sensors to your controller. www.pldstore.com - Pitsco - 1-800-362-4308
ReferencesAVR Robot Control Board: LEGO software sample code http://www.barello.net/ARC/LEGO/Simple_Lego.bas http://www.barello.net/ARC/LEGO/legolib.bas http://www.barello.net/ARC/LEGO/legolibdefine.bas AVR Robot Control Board support mailing list http://groups.yahoo.com/group/AvrRobotControl BASCOM basic compiler, IDE, simulator and programmer http://www.mcselec.com/download_avr.htm LEGO Sensors explained http://personal.pitnet.net/usr/gasperi/lego.htm Atmel AVR Microcontrollers http://www.atmel.com/products/avr
Last update 16-Mar-2007 |