Friday, April 29, 2011

Pocket Solver

Greetings.

For the past few days, I have been busy working on an iPhone Application. I will let you know the details later, but first let me narrate my journey in the mysterious and interesting world of iPhone application development.
    Being a generally Windows/Linux guy, I had no way to get my hands on a Mac. I wasn't willing to spend in the excess of $600 for a Mac(Mini). But thanks to the hacker community, I had a cheaper option. I used VMware Workstation and bought a copy of Mac OS 10.6. Then I proceeded to create a .iso file of the 10.6. Using a preconfigured Virtual Machine along with with custom built audio and video drivers and my Mac OS 10.6, I was able to run 10.6 on a virtual machine on Windows! I will create a blog post explaining how to do this, but tutorials can be found online.
    Once I got my "Mac" up and running, it was only a short while before I downloaded Xcode and the iOS SDK and started working on my app. It is a simple app that solves math equation AND outputs the full written solution. It is thus called Pocket Solver. It is a free application that my company Affordable Software Solutions released.  You can download it later find out about its features. Until then, here are some pics.





Anyway, this post is not to discuss the various features of my app or to promote it. Okay, maybe a little. But in this post, I will share some of the algorithms I  came up with to solve linear equations in two and three variables that will make your life much easier (if your life consists of solving linear equations...)

1) Linear Equation in two variables

Suppose you have a system of two equations that are in the form:

ax  + by = c    
dx + ey = f

then your x value will be equal to:

((c*d)-(f*a))/((b*d)-(e*a))

and your y value will be equal to:

(c*d-(x*b*d))/(a*d)

where x = ((c*d)-(f*a))/((b*d)-(e*a)).

looks complex, doesn't it?  All these algorithms actually do is eliminate the y variable and solve for x, and solve for y by substituting the newly found value of x in the equation.

If that didn't explode your head, here is another one...

2) Linear Equation in three variables

Suppose you have a system of three equations that are in the form:

ax  + by + cz = d    
ex  + fy + gz = h    
ix  + jy + kz = l 

Let us create six variables that contain a certain series of functions that we will use the most.
Let

    ae1 = (b*e)-(f*a)
    be1 = (c*e)-(g*a)
    ce1 = (d*e)-(h*a)   
    ae2 = (b*i)-(j*a)
    be2 = (c*i)-(k*a)
    ce2 = (d*i)-(l*a)

Now we will use these substitutions to find out the values of x, y and z.

So now, your z value will be equal to:

((ce1*ae2)-(ce2*ae1))/((be1*ae2)-(be2*ae1))

and your y value will be equal to:

((ce1-(z*be1))/ae1)

where z is equal to the equation above.

And your x value will be equal to:

(d-(z*c)-(b*((ce1-(z*be1))/ae1)))/a

where z can be found using the first equation.

Once again, I systematically eliminate x, create two equations with only y and z variables, eliminate y, find the value of z, substitute for z in one of the two newly created equations and find y, and substitute for y and z in the given equation and find x.

Wow, that was a lot! I am sure that you can further simply the algorithms, but  I haven't tried to do so. I encourage you to tinker with the algorithms, simplify them and make them more elegant. You have permission to use this algorithms in your projects, but with proper credit given to Aravind Rao.

This is not the end....





Tuesday, April 12, 2011

CyclopsNXT

Greetings

It has been a long time since I posted any of my creations. This is because for the past few days, I had been working on possibly my most entertaining project to date: A NXT cyclops that can track faces! Before I get started, here are some pics.


Front View, base and cam

Side view, cam holder in detail

Gearing to turn the camera

I see you!

So to make this creation, I used the OpenCV wrapper for Java (JavaCV), Lejos as well as Java SE. This creations consists of two motors. One motor drives a set of gears which can tilt the camera up or down. Another set of gears rotates a geared base CW or CCW, which in turn rotates the camera. The camera itself is connected to a terminal, which is the only wired connection to an external terminal in the whole project. You can use Bluetooth webcams and do away with wires. The geared base, rotating motor and the NXT brick all sit on a pretty sturdy base, which rests on four wheels. The whole project is about 20x15x20 cm. So basically the camera has four degrees of freedom. So what does this project achieve and how does it do that? I wanted for a long time to be able to build a robot that could track objects in real time and this robot does just that. The camera gets input from the environment and sends it to the computer for processing. A Java/JavaCV program running on the computer processes this input video and draws a rectangle on all detected faces (program works with only one face though).
       Now a method in the program computes the distance of the edges of the box around the face to the edges of the video frame. If the distance from the left edge of the box to the left edge of the frame is different by more than 80px compared to the distance from the right edge of the box to the right edge of the frame, the JavaCV program on the computer sends a command to the NXT Brick to either turn left or right, so as to position the face in the center of the video frame. The same works in the y axis, where the distances from the top and bottom edges of the box around the face to the top and bottom edges of the video frame are compared. Again if the difference is more than 60px, the JavaCV program sends a command to the NXT to tilt the camera up or down depending on the scenario.
       So that is pretty much how the program works. I have posted links to the code below, from both the NXT and the Computer. It is annotated and licensed under the MIT OSI License. Feel free to go over the coed and edit it. Hopefully it is educational. If you are having problems building the robot itself, send me a message, and I can help you out.  I have posted pics above that can guide you. Oh and a huge thanks to Nashruddin whose blog taught me how to program face recognition in OpenCV! And finally here is a video showing the robot in action.



*LINKS*
Code on the NXT
Code on the computer
Classifier to detect faces, must be in the folder from which comp.class is running.

This is not the end...