Visual Servoing using a Webcam, Arduino and OpenCV

Last weekend, I ‘ve decided to experiment a little with the OpenCV library. I wanted to use it for tracking the movement of a single object using a camera on an rotating mount, and closed loop control. I have found a similar project on youtube but it used windows instead of linux and a servo motor instead of a stepper one.

So, I started playing with OpenCV, and quite easily managed to automatically detect an object through the webcam using color detection. I also found out that I had an old ultra-low quality usb webcam, that nevertheless could be used. The next thing I needed was a servo motor. But tough luck… I didn’t have one. At first I considered using a simple DC motor for controlling the camera’s direction. But, that was quite difficult to do, due to nonlinearities and sensitivity of the motor. Therefore this option was quickly abandoned. At this point I remembered that I had a spare stepper motor that I once taken out of an old broken scanner. Great! Exactly what I needed! But… I didn’t have any special chip to control it with, or at least I thought so. It turns out that you can control a stepper motor, using the exact same L293D chip that you normally use to control an ordinary DC motor, just with a different assembly! Ha! The puzzle is complete!

I played a bit with the stepper motor and after some inspection, managed to control it through the Arduino microcontroller. Then, I added some code to transfer the data back and forth from the PC to the arduino through serial communication. Finally, I wrote the program for the microcontroller implementing a simple negative feedback control loop. Et voila! With a little tuning, the automatic tracking system was working surprisingly well!!!

Here it is:

The info and code that I used was based on other related projects. Here are the links:

  1. Color Detection and Object Tracking & OpenCV:
  2. Stepper motor control with an H-Bridge (L293D):
  3. PC-Arduino serial communication in C:
  4. A similar project on youtube:

You can find the code that I used for my setup here:

Some more info on how to build it from scratch on an Ubuntu Linux:

  1. Install OpenCV as instructed here.
  2. Connect the webcam
  3. Connect arduino and load it with the code for the microcontroller
  4. Build the tracking software as instructed in trackncontrol_pc.cpp
  5. Run the program: LD_PRELOAD=/usr/lib/i386-linux-gnu/libv4l/ ./trackncontrol_pc

Feel free to ask questions or add suggestions in the comments below!


2 thoughts on “Visual Servoing using a Webcam, Arduino and OpenCV

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s