Saturday, January 14, 2012

Robots!

I've been wanting to branch out my skills recently and ended up getting the robotics bug. It combines mechanical engineering, electrical engineering and software engineering all into one awesome package. I finally bit the bullet and ordered my first Arduino micro-controller from http://www.trossenrobotics.com/ located up in Chicago and started building my first robot. I'm going to start keeping a record of what I'm doing. This first post is just going to be a mass update to where I am today.

Chassis Design

I mocked up a simple chassis design using Maya, it's nothing special but gave me a general idea about proportions and the basic mechanics. Here's the basic sketch.

Like I said, nothing special but it helped me get a basic idea of how this thing is gunna work. As of right now, it's a cardboard and wooden dowel chassis. The chassis is about 18" long. The chassis is being driven by one powered wheel in the center, the 2 wheels on the back are unpowered, as are the two wheels in the front. I'm rigging up a pretty basic steering column for turning. I got the idea from a RC car I took apart. The two wheels in the back are really only there for stability.

The red and black mess up front is the pan and tilt camera kit with a salvaged Droid attached. More on that later.

Light Blue are the two servos I'll be using.

The green rectangles are the Arduino Microcontroller and the breadboards for wiring

The purple cubes are two 3300 mAh 6v rechargeable NiMH batteries used for powering the servos and 1 9v for powering the Arduino.

Electronics
For my micro-controller I'm using the Arduino Uno R3 with the Xbee wireless shield for remote communication. As for my servos I'm using Hitec 322HD Delux with Karbonite Gears. The pan and tilt requires 2 unmodified servos, the drive train requires one modified servo that allows it to rotate a full 360 degrees. Modifying my first servo took some time and it's nowhere near perfect. The potentiometer is glued in at roughly 2 degrees off from center, thankfully enough it's easy to fix in code on the micro-controller. As for sensors, I have some photosensors, a gyro and sound sensor. I bought the grove shield which allows me to plug in the gyro and sound sensor fairly easily. I plan on purchasing an IR sensor for collision detection.

One of the main things I wanted my robot to do was to be able to communicate back to my host computer with minimal lag, I also wanted to be able to remotely control it as well as get live video feedback. The first two parts are fairly simple, the 3rd gets a little more tricky. After searching for quite some time on a decent robotics camera, the best I came up with is low-res camera that is only capable of taking still shots, not exactly what I wanted. It seems the major limitation to this is the processing speed of the micro-controller. Because of this, I had to get creative. My first idea was to use a wireless webcam. As I started googling a Android app popped up that allowed me to turn my Android phone into a wireless webcam. The app is called IP Webcam https://market.android.com/details?id=com.pas.webcam&hl=en. I turned on wifi on my phone, and was able to connect instantly to my Android powered webcam. The app starts up a webserver for you, and if you open up the correct ports on your router, anyone can view it. It's also capable of being password protected. The developers website also has a link to a PC side application that allows you to use your new webcam stream as a simulated hardlinked webcam. I'll explain that more later.

Because I plan on expanding this in the future, I'm using breadboards for the majority of the development. This first model is an Alpha so I don't plan on getting too rough with it, so the breadboards should be fine. I wanna keep this as modular as possible for future expansion.

I started doing a mechanics and electronics test. Here are my results

Drive Servo and Steering Servo Test, no loads
I accidentally turned on a weird filter, so the video colors are weird but you can get the idea

Drive Servo Torque Test
Here I'm testing my general controls as well as the torque and movement of my drive servo. It appears to work well until I get it stuck on my headset. I also wanted to test the wireless responsiveness and to ensure my settings were all connect. You can see that the Arduino is not physically connected to the host PC.

Pan and Tilt Camera Test
Here I'm testing my controls and mechanics for controlling the pan and tilt camera. I wanted to ensure that the servos were setup correctly and powerful enough to move the phone. As you can see, no problems at all.


Programming
My primary development is going to take place in the Arduino language for the micro-controller and python for logic processing and control. Arduino is based on C and is fairly straightforward.

Arduino has a large following in the robotics community and there are a multitude of code examples and tutorials, and the documentation is pretty robust. To communicate with the micro-controller I am using SoftwareSerial which sets up a pretty simple serial server which I can connect to using python. For right now I won't post any code examples as I'm still developing my libraries and the code is pretty much just for testing purposes right now, as I get farther along I will post code updates

As far as Python goes I'm using a few non-standard modules to achieve what I want.
First and foremost to make the serial connection I am using a module called pySerial, found at http://pyserial.sourceforge.net/. This module is the basis for my Arduino library I'm building to do common functions.

For video processing I am using the Python modules of OpenCV ( Open Source Computer Vision ) http://opencv.willowgarage.com/wiki/. OpenCV is a pretty powerful library specifically designed for computer vision processing. It has built in methods for shape and edge detection as well as the Haar Object Detection Algorithm, which allows me to quickly process and find shapes such as faces, eyes, or other human body parts.

For webcam communication I am using the VideoCapture module http://videocapture.sourceforge.net/.

So a small caveat to using VideoCapture and OpenCV. OpenCV has built in methods for retrieving webcam feeds, but unfortunately only appears to work with DirectShow enabled cameras. Enabling other capture methods requires a full rebuild and recompile of OpenCV, which kinda seemed like overkill. That's when I ran into the VideoCapture module which works on most webcam feeds outta the box. Now, VideoCapture uses PIL to return images. When you request an image from the camera, PIL automatically flips the blue and red channels for you when you are on windows, unfortunately OpenCV does the same thing. So when I would feed the processed image back to the OpenCV frame, my colors would be reversed. Scouring the internet, I finally found a solution. I needed to convert the PIL image into a string anyway, so this method worked out for me.

img.rotate( 180 ).tostring( )[::-1]

The key point here is [::-1]. That flips the channels back to the original state, allowing OpenCV to correctly flip them for when it is output to my window.


Finally, if you noticed in my videos I am using an Xbox360 controller to control my servos. To receive input from the controller I am using pyGame http://pygame.org/news.html. Once I receive the input from the joysticks, I simply round the values and fit them into the 0 to 180 range.

Next Steps
My next steps revolve around me finishing the cardboard chassis and getting this things moving around. Based on some of my strength tests I need to reinforce the body by using multiple layers of cardboard. Instead of one solid piece, I may end up breaking the body into segments in order to strengthen the frame.

Once that is completed I need to finish connection all of my electronics and finishing the Arduino program to support all of them. I also plan on building an H-Bridge for my DC Motor so I can beef up the drive on the robot so that it will support a heavier, sturdier chassis in the future. Cardboard is great but it isn't exactly the strongest of materials.

Following that I need to combine my applications for control and input in python.


Conclusion
Well, there it is, my huge post on my beginning adventures into robotics. Look for more in the future as I continue to build this.