07Apr 2011

Electronic projects and Matlab

I have to admit, I am usually not the biggest fan of interpreted languages and high level programming. But in this case, they were very convenient.
This is thus a small article about what you can do in 3-4 hours with Matlab and a FTDI USB connectivity to your project. As an example, here is a project where my webcam output (well, kind of) gets displayed on my led matrix:

Led matrix with webcam

For quite some time now, on my projects, I have been using Matlab for various data processing operations, picture conversions...
and also communication with my embedded systems using FTDI USB to RS232 converters. Nothing more convenient that a simple fread() and fwrite() to import/export your data to/from your computer!
If you have read my blog, you may know that I have a 96*64 bi-color led matrix at my disposal. Until now, I was generating all the screen data on the fly and still hadn't implemented the possibility to directly write to the led matrix frame buffer through its USB port.
So yesterday I finally got the motivation to modify my FPSLIC code and optimize it to receive streamed data. As double-buffering was already implemented, the coding part was pretty quick!
To have a clear and simple application of this added functionality, I thought it would be nice to display some data coming from my computer... What kind of data? The obvious choice was the webcam feed.
This is where Matlab was very useful. In only 50 lines of code, I was able to show the edges detected from my webcam feed:

   function output_args = webcam( input_args )
   s = serial('COM3', 'Parity', 'none', 'ByteOrder', 'BigEndian', 'InputBufferSize', 2000000, 'OutputBufferSize', 1000000 , 'BaudRate', 520833, 'DataBits', 8, 'RequestToSend', 'off', 'StopBits', 1, 'ReadAsyncMode', 'continuous', 'FlowControl', 'none');
   vid = videoinput('winvideo', 1, 'RGB24_160x120');
   himage = preview(vid);
   buffer = uint8(zeros(768, 1));
   for i=1:2000
       data = getsnapshot(vid);
       data = imresize(data, 64 96);
       data = rgb2gray(data);
       BW = edge(data,'canny');
       index_store = 1;
       for x=1:96
           for y=1:8
               A = 0;
               for j=1:8
                   val = BW((y-1)*8 + j, 97 - x);
                   A = A + val * 2^(8-j);
               buffer(index_store) = A;
               indexstore = indexstore + 1;
       while(strcmp(s.TransferStatus, 'idle') == 0)
           indexstore = indexstore + 1;
       fwrite(s,buffer,'uint8', 'async');  
   while(strcmp(s.TransferStatus, 'idle') == 0)
       indexstore = indexstore + 1;

As you see, everything is pretty simple here. Snapshots getting acquired, image resizing, conversion to B&W for edge detection and some compression for direct write to the frame buffer.
Of course, this code is based on the assumption that picture generation will not be faster than the link to the led matrix (which in my case is true). Actually in this mode, my led matrix constantly output its SYNC signal so frame synchronization could be implemented in the future.
Asynchronous sending to the serial port is used, and the whole buffer is put in the queue at once. Not doing so would considerably increase the loop execution time in synchronous mode, and in asynchronous mode active waiting should be used between each call to fwrite.
RS232 link speed is set to allow for 520833 / (96 * 64 * 2) = 42Hz refresh rate which is enough for what i want to do. Of course, the display is only bi-color with monochrome data for each color, so everything is simplified! Anyway, here is the final result:

Led matrix with webcam

This result is obtained with only using Matlab pre-existing functions, leaving the door opened to additional improvements. Anyway, I just wanted to let you know that with this kind of high level tools, it is very easy & quick to get nice results!


1. On Friday, April 8 2011, 11:44 by xa4

Impressive ! as usual..

2. On Tuesday, April 12 2011, 07:48 by TechnoMan

Very cool app. Well done.

3. On Friday, April 15 2011, 14:48 by Joachim

Cool stuff! Do you have a concrete application that drove the idea or is it just for fun?

I could think of using it for some live art installation at a museum or concert, or for advertising uses. What do you think?

4. On Friday, April 15 2011, 14:53 by shubham

awsm dude!!!

5. On Friday, April 15 2011, 15:06 by limpkin
@Joachim : Only for fun! And yes, this project would have a lot of applications in the domains you mentioned. Actually, led panels are already used for advertising purposes (especially in japan).
6. On Saturday, April 16 2011, 06:37 by Andy

Just Wow man! I aspire to be like u! :)

7. On Thursday, November 3 2011, 20:39 by vadim

Great project. Where can I find your AVR embedded code ?

8. On Friday, November 4 2011, 09:04 by limpkin

@vadim : thanks! you can find it here

9. On Saturday, November 5 2011, 20:22 by shweta_sharma

gud evening sir thanks for acknowledging persons like me with the marvellous works in matlab
regarding code
1)i couldnt get to know on what basis have the calculation of "A" in the loop been done?.and what is the significance of loop computation?.....
i.will be grateful to hav ur guidance.

10. On Saturday, November 5 2011, 20:26 by limpkin

@shweta_sharma : Hi Shweta! Well, A is just a value which contains 8 pixels :)

This post's comments feed

Add a comment

Comments can be formatted using a simple wiki syntax.

They posted on the same topic

Trackback URL : https://www.limpkin.fr/index.php?trackback/65