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!

3 DOF Heli-Robot (2012)

This was a semester project semester project for the Mechatronics class of the MSc. on Automation Systems and Robotics (NTUA). It started out as an idea from an earlier weekend project of mine that can be found here. Along with two other colleagues from NTUA, we implemented this robot.

Briefly, this is a 3 DOF mechanism with two brushless motors as actuators. The angles are measured with potentiometers, and the closed loop control is implemented using an arduino microcontroller. Pretty fun project to do!

More details can be found in the presentation (english), and in the corresponding report (greek).

PID controled Heli-Robot (2011)

Another old weekend project, started out at control systems design class of NTUA, where I saw a robot like this, but more complicated with three DOFs. I was fascinated, so I said, I want to have one too! Or maybe even better, make one! So I spent all three days in a row to build it. Luckily, I already had an arduino board, therefore I had only to buy an L293D chip to control the motor. The rest of the parts are mostly junk. So, here it is.

helirobot1 helirobot2