Wednesday, December 30, 2009

Thanks, Santa !

After a period of turkey dinners and unwrapping gifts, there is finally time for a new challenge. Don’t misunderstand me, I don’t hate opening presents at all, and certainly not when the presents turn out to be Lego! Shrewdly I started mentioning that I fancy the new Lego Technic Crane Truck (8258) several months ago. On December 25th, when I opened my present, there it was!

So I started building the box and in the mean time I also discovered the technical functions of the crane and how they work. Pretty cool, I thought. I took some pictures of the original truck. 
PICT4830PICT4831PICT4829 PICT4828
The only thing I don’t like is that there is only one motor inside. But wait a minute !!! Somewhere in the house, there should be a NXT lying about. Perhaps I can use that one ?

Stay tuned !

Tuesday, December 29, 2009

Mindsensors’ PowerMeter as Morse Decoder

After finishing the multimeter project and seeing some images other creations from Xander S. and Andy M., I thought we had used the Mindsensors’ PowerMeter to its limits : reading voltage/current, logging it or displaying it graphically. However, during a walk on the beach, it dawned on me: this is not ‘just’ a power meter, it is the sensor of all sensors, the must-have for all Robotics and Electronics fanatics. With the PowerMeter extending the NXT with a special sensor becomes a no-brainer. Most electronic sensors work by varying voltage or current. The PowerMeter is actually a generic  A/D converter for the NXT which allows you to quickly and safely interface your home-made sensor to the NXT. It is even a bit more than that, since it has a built-in timer you can use as well and in this example we will. Measuring distance, temperature, humidity, … all becomes easy with the PowerMeter.

lightsensor Once I realized this, I was up for a new challenge. To keep the electronics simple, I made a light sensor for the PowerMeter with a photodiode and a resistor. As transmitter I use a simple flashlight. The circuit shown here is not very critical. In fact I left out C2 and R4. I first used the multimeter program from the previous project and learned that -depending on the surrounding light- the ‘dark’ value was 1350 mV, the ‘light’ value was 20 mV. I decided to use 20% of the dark value as threshold between light and dark.


Instead of using the RobotC drivers, which I created for my previous  PowerMeter project, I wanted to use LeJOS for this one. LeJOS is a Java Virtual Machine for the NXT. Java promises cross-platform portability ("Write Once, Run Anywhere") and this is partially true. LeJOS allows to create programs in Java that run entirely on the NXT, partly on the NXT and partly on the PC or completely on the PC. This means that instead of needing different programming environments if you want to communicate between NXT and PC, all can be done from the same environment. However, there is a catch. Some parts of LeJOS are very sophisticated, while other parts are very basic.  Most notably, LeJOS has no IDE. It is all command line based and even the firmware for the NXT has this same basic look-and-feel. Most of this is not a real issue and you can use your preferred development environment. For me that is Eclipse.

First I created a new Java class ‘PowerMeter’ that contains all MorseCode the code to interface with the sensor. This class is the basis for all other programs.  I must admit that interfacing with I²C turned out to be very simple in LeJOS. Then I rewrote the multimeter program in Java as proof-of-concept. Now was the time for the real work. Several hours, crashes and lots of coffee later, the Morse decoder was born. The usage is simple. First you learn the NXT what a ‘short flash’ and a ‘long flash’ is. Then the NXT starts waiting and every time a short pulse is received it takes is as a DOT, a long pulse as a DASH. Once a full character is received – the end of a character is denoted a dark period equal to 3 short pulses in time - the character is decoded using a simple lookup table.


The code is available here.

Tuesday, December 22, 2009

Mindsensors' NXT PowerMeter


Mindsensors currently has a new sensor in beta.  It is a sensor which is a bit out of the ordinary. It is not to make robots or automated devices, but it is more a tool to help in electronics. It allows to measure current and voltage used either by the NXT brick itself (using the provided probes) or an external circuit (without using the probes). The firmware of the sensor is not completely finished yet which means some of the features are not available yet. However, even in its current state it is a marvel ! It can measure up to 13.5 volts and 3 amps.

I decided to start slowly and created a simple circuit with a 9V battery, one fixed resistor of 50 ohm and one variable resistor of 1K. Since the battery is a rechargeable battery I measure around 7-8V, which is normal and the current depends on the variable resistor. After trying the provided NXC program written by Deepak, I decided to convert it into a RobotC library.  I used the same naming convention as Deepak's NXC library. Once I got it working, I added all routines to read all registers of the NXT PowerMeter, so that the library is ready for future expansion (meaning when Mindsensors adds features to the firmware)

Finally I wrote a program that turns the NXT into a simple multimeter. It shows the device ID, vendor ID, firmware version of the PowerMeter along with the measured current and voltage. Using Ohm's Law it is easy to calculate the resistance from this. The NXT shows those 3 values but by pressing the buttons you can change which one is shown in a big font. The other 2 are shown in small fonts. It also displays the internal timer. The PowerMeter has an internal timer in msec which can be reset.

The video is available on YouTube, the software on the Mindsensors website.

CommLib + GCC + QT4 = NXT Remote control


By coincidence I stumbled on Anders' Mindstorms website where a library was available to communicate between the PC and NXT via Bluetooth. There was a testprogram available so I decided to give it a spin. I downloaded the MinGW framework  -GCC/G++ compiler for Windows -made a simple testprogram based on his example. After changing the code to the correct COM port, the example worked instantaneously. However it used an ugly console window.

So I decided to push my luck a bit and make a graphical userinterface for it. Graphical as in a GUI ? Yep. But coding a windows UI from scratch is not for those with a weak heart, so I needed a framework that would handle the nitty-gritty details. Several exist and I used a few of them : wxWidgets, Qt and -yes even- the MFC.  Over time I became a fan of QT. This is a crossplatform application platform which includes a GUI (amongst others) and works on Windows, Linux, Mac, Windows CE, Symbian, .... . It was created by TrollTech and the first version was released in 1991. A few years ago, Nokia bought the company. The frameworks remained open-source.  In the old days I used it only on Linux, because the version for Windows was a commercial license. However, since version 4 it is now also free of charge on Windows. Maybe it has nothing to do with the version as such, maybe it is like this since Nokia bought TrollTech. Not sure ...

One of the neat things of Qt is that it is a very consistent framework.  At first glance it looks scary, but in fact it is very neat, clean and straightforward. The key to understanding it is 'signals' and 'slots'. An object can generate signals and other objects can intercept those signals by connecting one of their methods to a so-called slot. When the first object generates a signal, the framework will call all routines that are connected to it.  This mechanism is used throughout the entire framework (not just for the GUI, but also for system routines) and once you understand it, it makes all the rest a walk in the park. A lot of examples are provided to guide you through the maze from simple to complex. Amazing how easy it is to use.

Here is a simple example : A pushbutton that calls one of my methods when clicked
1:     QPushButton *leftFwd = new QPushButton(this);
2:     leftFwd->setIconSize (QSize (60,60)) ;
3:     leftFwd->setIcon (QIcon("images/leftForward.png")) ;
4:     leftFwd->setGeometry (10, 40, 60, 60) ;
5:     connect(leftFwd, SIGNAL(clicked()), this, SLOT(leftForwardEvent()));

On line 5 it reads : connect the signal ‘clicked()’ of the object ‘leftFwd’ to the slot ‘leftForwardEvent()’ of ‘this’ object.

Anyhow... To cut a long story short. After a day of coding and tweaking I ended up with a simple graphical UI for my robot  that works pretty neat !

The source code can be found here