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:
- Color Detection and Object Tracking & OpenCV: http://opencv-srf.blogspot.gr/2010/09/object-detection-using-color-seperation.html
- Stepper motor control with an H-Bridge (L293D): https://learn.adafruit.com/adafruit-arduino-lesson-16-stepper-motors/breadboard-layout
- PC-Arduino serial communication in C: http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/
- A similar project on youtube: https://www.youtube.com/watch?v=O6j02lN5gDw
You can find the code that I used for my setup here:
- C++ code for Object Tracking and Serial communication on the PC: trackncontrol_pc.cpp
- C code for the Arduino microcontroller: trackncontrol_arduino.ino
Some more info on how to build it from scratch on an Ubuntu Linux:
- Install OpenCV as instructed here.
- Connect the webcam
- Connect arduino and load it with the code for the microcontroller
- Build the tracking software as instructed in trackncontrol_pc.cpp
- 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!