Sunday, March 20, 2011

Open CVhat I can do!

Greetings:

I have decided to take a short break from Lego building(let us just say that I stepped on one too many Lego bricks) and decided to learn something new. I always wanted to incorporate vision in my programs, so I decided to learn a little bit of OpenCV. I actually want to use OpenCV with Lego Mindstorms, but since I am using OpenCV for c++, I would have to use JNI to get my LeJOS Mindstorms to successfully communicate with my C++ OpenCV program. (EDIT: I just found out that a Java Wrapper for OpenCV exists, so I may be able to give my Mindstorms "vision" after all!) Anyway, What I really was interested in was to use OpenCV to track eyeball movement. I will tell you why in the next post. Now how OpenCV tracks face and eye movements is relatively complex. I do not understand all of it, just enough to make my small eye tracking program work. Pretty much the face detection algorithm scans an image for Haar like Features, and if the image satisfies the condition of having a face, the algorithm further subdivides the face into many categories. For example in a typical face, the eye is darker than the cheek, and so when the algorithm compares the intensities of the pixels that make up the eye and cheek, it finds the difference in intensities and is able to distinguish between the eye and a cheek in a face.
          This is a very watered down explanation of what is *really* happening behind the scenes, and in reality, the face and eye detection algorithms in openCV are clunky at best without proper optimization. So I decided to create  small c++ program that takes in input from a webcam ans tracks the face and eyes in real time. It also superimposes images over the face. Face detection was pretty much straightforward. But the eye detection algorithm required further optimization. How it worked was that it all eyes that it discovered in an image(or video frame) it would store in a array. But the problem was that it detected 6-7 eyes at once! Now I only have 2 eyes(4 with glasses, but that not the point), so I had to make the algorithm pick only two eye objects from all that it had discovered. The optimization that I used was to pick the two eye objects with the largest area, and see how their x and y coordinates match up relative to themselves and the face(This is better explained in the annotated code posted below). After hours of tinkering, I have a created a program that tracks human eyes with movement in all 3 dimensions with ~85% accuracy in real time. It is good enough to start out with, but I figure I will have to do much more of optimization if I want to carry out a certain project(more on this later).
              I have posted the links to the annotated code below which explains how my optimizations work. They are no work of art, but they work satisfactorily. Also here is short video of the eye tracking in progress in real time.


Link to download the folder with code, .exe, classifiers and the other good stuff.

This is not the end......

Saturday, March 12, 2011

Mindstorms liquid dispenser

Greetings

Here is a simple project that I had been working on for quite some time. It is a Mindstorms powered liquid dispenser. Essentially it is a simple liquid dispenser that works on the principal of a diaphragm pump. Here are some pics which will describe the apparatus better than I ever can hope to by writing
Side View

Front View

Rear View

Close up of the water delivery system

        So the principle behind the dispenser is simple. A small compressor is driven directly by two NXT motors, converting circular motion to linear motion needed to compress the air in the compressor( think pistons.) The compressed air gets pushed into an airtight water container via a small rubber tube. As the air gets pushed in, the pressure gets increased inside the container. Now there is a secondary rubber tube with one end inside the water and the other end connected to the nozzle through which water is dispensed. When the pressure inside the container increases, water gets pushed out of the container through the non air carrying tube into the open, via the nozzle. Therefore it is very important for the water container to be absolutely air tight to be able to maintain a constant pressure through expulsion of water.
        The design of the project too is nothing too complex, although it was made to make space use as efficient as possible. In the back, a small rectangular platform houses the water container, secured by two elastic restrainers. Right after that are the two motors in an upright position, directly driving the compressor. The NXT brick is situated right in front of these motors. Finally in the front is the push handle with touch sensor, along with a nozzle. Whenever the push handle is pressed, the touch sensor is also pressed, which causes the NXT to drive the motors, leading to flow of water through the nozzle. The handle returns back in place due to rubber bands. I designed the whole front setup to mimic and actual water dispenser.
        There is not a lot of programming involved in this project. The NXT runs the motors whenever the touch sensor is pressed. However, a lot of other sensors could be used. For example, the motors could start running whenever a cup is placed in the platform, with input provided to the NXT by an ultrasonic sensor. Or using a sound sensor, water could be dispensed by clapping of the hands and stopped again by doing the same. Like the Mindstorms motto, the possibilities are indeed endless!
        I have included links for the code for the NXT down below. There are three different files, one for using a touch sensor as input, the others for using a sound sensor and ultrasonic sensors as inputs. The code in all files is annotated. To compile and run these files, use the nxjc and nxj commands. If you are unsure on how to do this, here is a link that can help you out. As for the model itself, I might upload an LDD file of the model in the near future. In the mean time, you can look at the video below of the Mindstorms water dispenser in action and try to build your own version.




Link to the NXT program with the Touch Sensor
Link to the NXT program with the Sound Sensor
Link to the NXT program with the Ultrasonic Sensor

This is not the end.....

Wednesday, March 2, 2011

The NXT step

Greetings

For a long time, I wanted to use my programming abilities in robotics. I wanted to create a robot and program it myself to respond to stimuli, unlike the pre-programmed ones that were available in the market. This meant that I had to buy motors, PC boards, micro-controllers, switches and sensors independently, assemble them and then program them.  While I searched the net to buy these parts (A lot of companies did not ship to the Middle East, where I used to live), I started getting disheartened because of the nonavailability of the most basic micro-controllers and motors.
                Fortunately for me, Lego and MIT released the Lego Mindstorms Kit. While the NXT-G programming language was sufficient, Visual Programming lacked a certain something that had drawn me to textual programming languages. When the good guys at LeJOS released LeJOS v0.85, I immediately flashed my NXT to that version(The latest version at the time of writing is 0.90, refer to this page to see how to flash your NXT and get the LeJOS environment running on your computer) and began coding in sweet, sweet Java. I had always been fascinated by remote controlled cars, so the first thing I set out to do was to create a program to control my NXT vehicle via input from the keyboard. To do this, I created a small GUI based Java program(send.java) which acted as the control center. The program had a command log, connect, disconnect, shutdown and options(The options button has no functionality currently) buttons, and a text area to receive the input form the keyboard. The program sent commands from the keyboard to the NXT(running rec.java) via Bluetooth. The NXT acts as a slave(The main thread is pretty much stopped from executing, until a Bluetooth connection opens) and waits for input from the computer and acts accordingly. My program works in a simple manner. When I press the top arrow key, the key-code of that key specified by Java is sent to the NXT and the NXT knows to move forward, and so on. The idea is simple in concept, but it does have a few problems. One of the problems is that in this model, the sending of two commands simultaneously is a bit of a hassle. Right now, the robot can turn or move, but not at the same time. One possible solution would be to multiply the key-codes of the two arrows and  return the product as input for the NXT because for the key-codes 37, 38, 39, 40, the product of any two numbers is different from the product of any two other numbers in the set(note that the same cannot be said for addition.) In any case, the program does the work of successfully sending commands to the NXT, albeit not with a lot of options. This is just a test program, and I am working on another one with greater functionality(one that has a working options button!)
                 In any case I will put the files that run on the NXT and on the computer up for download. Please note that to run these files, you should have LeJOS installed on both the NXT and the Computer. The files to be run on the NXT will be called rec.java and the file to run on the computer will be called send.java.Here is what you have to do. Open up command prompt and and navigate to the folder where you have placed the two files. Then type in the following commands:

nxjc rec.java              (compile the rec.java file)
nxj rec                    (upload the rec.class file to the NXT)
Now go the NXT and execute the rec.nxj file. Go back to command prompt.
nxjpcc send.java           (compile send.java)
nxjpc send                 (run send.class, AFTER starting rec.nxj on the NXT)

I have once again annotated the code, explaining how and why it works. Please edit the code, add your own functionality and experiment with it. I am once again releasing the code under the MIT license.  You can use any NXT car creation you want to test out the program, but the model has to turn via a difference in the speeds of the different sets of wheels, like in a tank. I used the 30 minute model from Lego Mindstorms for testing purposes, here is a link to the instructions to build a similar model. You can replace the tracks with wheels if you want. Also, here is a video of the program and the NXT in action.

Here are the links for the two.java files. I have not created any JARs as they are really not needed.

rec.java (On the NXT)
send.java (On the computer)

This is not the end.......