It was an obligatory task during my studies to develop an Arduino based project. I am really interested in robotics so I decided to make a robot of my own. The idea was to build a desktop robotic arm possible to control with computer mouse. After I achieved that, I came to a conclusion that the project was too simple - that's just controlling 4 servos with a computer mouse - not funny at all, so I started my work on image processing program. I am familiar with Matlab software so I decided to use it for this purpose.Video
In the video you can see the final effect. This video was made for Matlab's contest and it also includes some explanations which I may duplicate in further description:How it works?
When it comes to communication between Arduino and Matlab, there are 2 ways to deal with it. First is by using Matlab support package for Arduino, it's quite handy when you want to access some Arduino's variables from Matlab to show them on graphs or do math calculations on them. The second way (the one I used) is using serial comunication.Automatic mode - image processing
If you haven't watched the video above - please do now. Below i will explain some of the most important functions that I used in Matlab. However to truly understand how this image processing works, you have to see what the robot sees.
The functions worth mentioning are:
- snapshot - to acquire images from webcam
- rgb2gray - to convert image to greyscale
- bwareaopen - to remove noise from binary picture
- regionprops - to measure white spot and find its location
From the last function, Matlab is able to find centerpoint of the tape (its position). It then calculates on which out of 3 possible locations the tape is. What is left to do is to send the location number to Arduino with fprintf function. Then Arduino (depending on what value it recieved) decides which location to approach.
As you may have noticed - this vision system is not able to recognize any location but only one out of three predefined.Serial communication trick
When we open serial communication on our Arduino, the controller will reset. You can notice that it even resets when you open the Serial Monitor in IDE. To deal with it, I soldered a 10uF capacitor between RESET and GND pins of Arduoino (+ goes to RESET). Details here.Using Matlab Support Package for Arduino
I haven't used it in this project but I did in another one and I think it's worth mentioning. To install the package, go to: Add-Ons -> Get Hardware Support Packages -> Install from Internet -> Arduino.
There are many many functions to use with this packege, you can find the list here.
You can write whole program on Arduino using Matlab thanks to this package, but it doesn't seem conveninet to me. What I used it for is when I had to watch and analyze some voltage signals and had no osciloscope to do it. Instead, I used this package and a simple code which you can find in Code section. The results were great:
Real time AnalogRead plot.MATLAB
a=arduino; i = 1; tic while (toc<10) b(i) = a.readVoltage(0); plot(b); drawnow; i=i+1; end