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: http://opencv-srf.blogspot.gr/2010/09/object-detection-using-color-seperation.html
  2. Stepper motor control with an H-Bridge (L293D): https://learn.adafruit.com/adafruit-arduino-lesson-16-stepper-motors/breadboard-layout
  3. PC-Arduino serial communication in C: http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/
  4. A similar project on youtube: https://www.youtube.com/watch?v=O6j02lN5gDw

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/v4l1compat.so ./trackncontrol_pc

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

Fractal Set Plotters

Sometime ago, I’ve decided to write some small programs to plot the Mandelbrot and the Julia sets. These simple applications are written in the Processing language. Here’s what they look like. By clicking somewhere you can zoom in and out respectively. Moreover, in the Julia set plotter, you can choose the values for the quadratic polynomial’s c parameter.
Screenshot-juliaset3 Screenshot-mandelbrot3
You can download the Julia and Mandelbrot set plotters here:
Julia set plotter
Mandelbrot set plotter

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

Trajectory prediction of objects interacting with an attracting force following the inverse square law.

This is an old project from back in 2011. It was a weekend project when I decided to write a small program which would plot out the paths of interacting objects, under the influence of gravitational field. By implementing numerical modelling techniques with an application such as this one, one may easily determine the trajectories over time of several moving objects. Whether these are planets, satellites, or charged particles the same principles apply.

As you can see, slightly different initial conditions may lead to very divergent trajectories and states.
This seems like chaotic behaviour!
🙂

1collision 2collision 3nocollision