May 2013

Archive for May 2013

DIY Spectrometer



Some time ago I had some time, a spare webcam, Matlab and some Legos lying around, so I began building a Spectrometer from it. Now, to the hardware, you just need a dark box, with no light infusing in it, and a piece of dvd to use as diffraction grid. This website will help to answer most questions: http://publiclaboratory.org/tool/spectrometer

At the time, their software didn't ran on my computer so I started prototyping my own in matlab. The code is below:

% Copyright 2014 Érico Porto

% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at

%     http://www.apache.org/licenses/LICENSE-2.0

% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.

vid = videoinput('winvideo', 1, 'YUY2_640x480');
preview(vid);
pause(4);
color_spectrum = ycbcr2rgb(getsnapshot(vid));

%correct the orientation
binImg = im2bw(color_spectrum,0.24);
I = bwlabel(binImg);
a= regionprops(I,'MajorAxisLength','Area','Orientation');
[areaMaxArea, indexMaxArea]= max([a(:).Area]);
angle = a(indexMaxArea).Orientation;
color_spectrum = imrotate(color_spectrum,-angle,'bilinear','crop');
%now I want to get the main rectangle
binImg = im2bw(color_spectrum,0.08);
I = bwlabel(binImg);
a= regionprops(I,'BoundingBox','Area');
[areaMaxArea, iMA]= max([a(:).Area]);
subImage = imcrop(color_spectrum,round(a(iMA).BoundingBox));

%here I'm guessing the light wavelength!
%I think I need to use 380nm and 750nm as my bounding
XAxis=zeros(size(subImage,2),1);
XAxis(1)=380.0;
XAxis(length(XAxis))=750.0;

%find blue - 472nm
[intensity, blueI] = max(mean(subImage(:,:,3)));
XAxis(blueI)=472.0;

%find green - 532nm
[intensity, greenI] = max(mean(subImage(:,:,2)));
XAxis(greenI)=532.0;

%find red - 685nm
[intensity, redI] = max(mean(subImage(:,:,1)));
XAxis(redI)=685.0;

%Now I will run and fill the zeros...
beginI=1; endI=blueI; 
for i = (beginI+1):(endI-1)
    XAxis(i)= XAxis(beginI) + ((XAxis(endI)-XAxis(beginI))/(endI-beginI)) * (i-beginI);
end
beginI=blueI; endI=greenI; 
for i = (beginI+1):(endI-1)
    XAxis(i)= XAxis(beginI) + ((XAxis(endI)-XAxis(beginI))/(endI-beginI)) * (i-beginI);
end
beginI=greenI; endI=redI;
for i = (beginI+1):(endI-1)
    XAxis(i)= XAxis(beginI) + ((XAxis(endI)-XAxis(beginI))/(endI-beginI)) * (i-beginI);
end
beginI=redI; endI=length(XAxis); 
for i = (beginI+1):(endI-1)
    XAxis(i)= XAxis(beginI) + ((XAxis(endI)-XAxis(beginI))/(endI-beginI)) * (i-beginI);
end
%this ends, the wavelength vector is ready for use!

figure(2);
colormap(gray(256));
gray_spectrum=((double(subImage(:,:,1))+double(subImage(:,:,2))+double(subImage(:,:,3)))/3.0);
image(gray_spectrum);
figure(3);
colormap(gray(256));
Grayimg_reversed=flipud(gray_spectrum);
graph=mean( Grayimg_reversed ,1);

%Finally I can plot a nice graph with meaningfull axis
plot(XAxis,graph);
pause(3);
delete(vid);

For improvement, I would suggest being able to save the resulting vector with easy to some sort of table, to later use this big table to guess what I have in front of it - using Hungarian or other solver method.

Virtualbox and old Windows



I think it's always cool to once in a while boot your old computer inside your new one, just to keep track of the old stuff you've done.

So here is a tip on how to run Windows XP for free inside Virtual Box.

Import Windows XP Mode into VirtualBox #while maintaining product activation:
  1. Download Windows XP Mode (WindowsXPMode_en-us.exe | MD5: bf3726d684d3acb98185665123c9efcf)
  2. Extract xpm from WindowsXPMode_en-us.exe with a file archiver like PeaZip Portable.
  3. Add .rar extension to xpm
  4. Extract VirtualXPVHD from xpm.rar
  5. Add .vdi extension to VirtualXPVHD
  6. Create new machine in VirtualBox, specifying VirtualXPVHD.vdi as the hard disk
  7. Once initial setup is complete, uninstall Virtual PC Integration Components from Add/Remove Control Panel
  8. Install Guest Additions (Devices > Install Guest Additions...)
  9. Shut down virtual machine
  10. Download pcbios.bin (MD5: 12ccdc652b30c6d1e307c6f7deff5d24) from VMLite and copy to a directory on host computer (e.g., C:\vm\)
  11. "C:\Program Files\Oracle\VirtualBox\VBoxMange.exe" setextradata vm-name "VBoxInternal/Devices/pcbios/0/Config/BiosRom" "c:\vm\pcbios.bin"


If your games start bugging, activate the Stretch after the Guest Addition installation, and everything will run fine.

If you are looking onto Windows 3.11 and MS-DOS, well, just download those from the web and install them in a clean machine. On my case I couldn't get Windows 3.11 to auto boot after the installation, so when my machine start, I type win [enter].

Using GCALCLI to access Google Calendar



So, I had a netbook lying around, an old ASUS SURF 4G, and thought "hey, it would be cool to look my calendar on this, and make a key hanger with it's screen". So I thought, ok, I will let the browser open in google calendar and run to the street to find something to build the key hanger. Unfortunately, my ASUS has only 4GB SSD HD, so right now it's loaded with a custom Gentoo that will never update, that I've built long ago. This custom Gentoo runs an old Opera Browser. And my Opera Browser can load everything, except Google Calendar.

So I should just stop here and go outside walk on the beach... Nah, I HAVE to fix this. So how? Doing some Google Search I've discovered that there is a command line way of getting the calendar, using a python script called gcalcli. I have Python 2.7.1 already built into, so it's easy right? Uhm, wait, it depends on other things... Ok, no problem, just download everything with Pip. Oh, Pip is not Installed?

Ok. Run this:

 curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py  
 python get-pip.py  

Then you need to get Google API Client for Python. Type this:

 pip install --upgrade google-api-python-client  

Now you need python-dateutil. Go pip:

 pip install python-dateutil  

and vobject and parsedatetime Python module. It's time for:

 pip install vobject  
 pip install parsedatetime  

Ok, now you are prepared. Right now, all you need to do is download gcalcli:

 curl -O https://raw.github.com/insanum/gcalcli/master/gcalcli  

And if everything is fine, just make it executable:

 chmod +x gcalcli  

Now you can run it.

 ./gcalcli agenda  

Everything should be self-explanatory from here on. You can't validate the Oauth2 through links because Google will ask for javascript before giving you the authentication code. With gcalcli you can do much more than just seeing your agenda. Have fun accessing Google Calendar from the command line.
Powered by Blogger.