venerdì 30 settembre 2016

OPTOTUNE lens MATLAB communication script

In our photonics laboratory facilities at UCC we have the Optotune controllable lens along with its lens driver controller 4 from Optotune.
I have been using this lens for several experiment and the GUI developed by Optotune works just fine.
However, sometimes I needed to have better control and play with the lens. For a couple of experiments we used this lens as a key component working in an automated environment on where a software was dynamically changing the lens parameter (focal length, therefore electronic lens current) according to different scenarios. This first software was written in C++ code.

Next I have decided to move to the development in MATLAB. To do that I have written a code that is able to simply control the electronic lens (current setting).
Following the instructions of the Optotune datasheet I have used the crc16ibm.

I would like to share this, since so many times I have been on the other side and is nice to give back.

Hopefully someone might find it useful.
Note that the code is not fully optimized and is very simple though but it does the job and was the result of a couple of hours.

It has 3 functions and 1 main.


function  reverse_16:

function [data] = reverse_16(message_byte)
dr = bin2dec(fliplr(dec2bin(message_byte, 16)))
data = dr;
end


function  reverse_8:

function [data] = reverse_8(message_byte)
dr = bin2dec(fliplr(dec2bin(message_byte, 8)))
data = dr;
end

function crc16ibm:

function [data] = crc16ibm(message, n)
% crc16ibm returns a 16 bit number value.
remainder = 0; remainder_bin = '0000000000000000'; %initializations
polynomial = 32773; % is the polynomail used in the CRCIBM16 - in hexadecimal is 0x8005.
%message = [hex2dec('41'), hex2dec('77'), 5, 117, 0, 0]; %
for i=1 : n
    if i == 3;
    end
    a = reverse_8(message(i));
    a_bin = dec2bin(a);
    a_bin_str = num2str(a_bin);
    size_a = length(a_bin_str);
    remainder = a_bin_str;
    while size_a <16 font="">
        remainder = strcat(remainder,'0');
        size_a = length(remainder);
    end;
    remainder_array= []; remainder_bin_array = []; %need to define these for the EXOR operation
    for k = 1 : 16
        remainder_array(k) = str2num(remainder(k));
        remainder_bin_array(k) = str2num(remainder_bin(k));
    end;
    remainder_bin_array = xor(remainder_array , remainder_bin_array);
    remainder_bin='';
    for i = 1 :length(remainder_bin_array)
        remainder_bin = strcat(remainder_bin,num2str(remainder_bin_array(i)));
    end;
    
    for j = 1 : 8
        if remainder_bin(1) == '1'
            remainder_bin = strcat(remainder_bin,'0'); %is a shift to the left therefore adding '0'
            remainder_bin = remainder_bin(2:end); %need to remove the first value to keep it as a 16-bit number
            %need to perform the exor operation
            polynomial_bin = dec2bin(polynomial);
            pol_array= []; remainder_array = [];
            for k=1:length(polynomial_bin)
                pol_array(k) = str2num(polynomial_bin(k));
                remainder_array(k) = str2num(remainder_bin(k));
            end;
            C = xor(remainder_array, pol_array); % the result of the EXOR operation
            remainder_bin = ''; %need to transfrom back to char string...
            for i = 1 :length(C)
                remainder_bin = strcat(remainder_bin,num2str(C(i)));
            end;
        else
            remainder_bin = strcat(remainder_bin,'0'); %is a shift to the left therefore adding '0'
            remainder_bin = remainder_bin(2:end); %need to remove the first value to keep it as a 16-bit number
        end
        pippo(j) = bin2dec(remainder_bin); %temp variable, debug purposes
    end
end
remainder_bin;

% need to make a reverse 16..
remainder = bin2dec(remainder_bin);
data= reverse_16(remainder);

end


Main:

% ECVFL control using matlab the Optotune Lensdriver 

clear all; close all; clc; %clear all

s = serial('COM6'); %depends could change 
set(s,'BaudRate',115200); %specfication according to Optotune datasheet
fopen(s); %open COM port
fprintf(s,'Start'); %initial command just to check the ECVFL
out = fscanf(s) %prompt print of 'Ready' string in command window

%% processing
current_value = 0; %something between 0 and 200
current_value =(current_value  * 4096) / 293; %from the ECVFL datasheet
low = 0; high = 0; %variable definition
current_value_bin = dec2bin(current_value); %convertion to binary
size_number = length(current_value_bin);
while size_number <16 font="">
    current_value_bin = strcat('0',current_value_bin);
    size_number = length(current_value_bin);
end;

high_bin = current_value_bin(1:8);
low_bin = current_value_bin(9:end);
high = bin2dec(high_bin);
low = bin2dec(low_bin);

data_command = [hex2dec('41'), hex2dec('77'), high, low, 0, 0];

crc_value = crc16ibm(data_command, 4); %4=data length
% crc16ibm returns a 16 bits number. As before need to split it in 2 8 bit
% number
crc_value_bin = dec2bin(crc_value); %convertion to binary
size_number = length(crc_value_bin);
while size_number <16 font="">
    crc_value_bin = strcat('0',crc_value_bin);
    size_number = length(crc_value_bin);
end;
crc_value_high_bin = crc_value_bin(1:8); %upper part
crc_value_low_bin = crc_value_bin(9:end); %lower part

crc_value_high = bin2dec(crc_value_high_bin);
crc_value_low = bin2dec(crc_value_low_bin);

data_command = [hex2dec('41'), hex2dec('77') high low crc_value_low crc_value_high];

%% Send data throught the serial COM port communication

fwrite(s, data_command, 'uint8');

%test
%fprintf(s,'Start');
%out = fscanf(s)
%%
fclose(s); delete(s); clear s;
delete(instrfindall); % to make sure we really close the COM port

giovedì 29 maggio 2014

Latex - Reference capital title

While writing an latex article one issue that I faced regards the bibliography.
In particular, I was looking for a way to include a multiple reference such as [1-4] with the dash in the middle. To do that I found out two options:

  1. \usepackage[numbers,sort&compress]{natbib} % multiple reference
    That works but it has a small issue. I noticed that the "reference" title was written in lower case, left-aligned and not anymore in capital at it was before including this package.

    or
  2. \usepackage{cite} % for multiple references with the dash as separator
    This was the answer I was looking for.  
By the way to write multiple references:
\cite{ref1, ref2, ref3, ref4}

Another small hint. While writing:

\begin{thebibliography}{number}
*
*
\end{thebibliography}

the number represents how many \bibitem  entries you want to include. If this number if less than the real entries you will have a space formatting issue. 

Hope it helps.

giovedì 3 aprile 2014

Matlab function to c++ (one tip)

I'm not going to describe how to transform the MATLAB function to c++ code, there is a nice video that already explain how to do it, what is not explained is what  to do when you get an error while your typing the Matlab command
>>coder
??? Undefined function or variable 'coder'.

Struggling to understand what was the problem after a while I found out that was my Matlab version,
>> ver
-------------------------------------------------------------------------------------
MATLAB Version 7.9.0.529 (R2009b)
MATLAB License Number: xxxxxxxxx
Operating System: Microsoft Windows Vista Version 6.1 (Build 7600)
Java VM Version: Java 1.6.0_12-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
-------------------------------------------------------------------------------------

By installing a new version of Matlab
>> ver
---------------------------------------------------------------------------------------------
MATLAB Version: 8.2.0.701 (R2013b)
MATLAB License Number: xxxxxxxx
Operating System: Microsoft Windows 7 Version 6.1 (Build 7600)
Java Version: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) Client VM mixed mode
---------------------------------------------------------------------------------------------

The problem is gone.
Hope this helps.

sabato 8 marzo 2014

Ranking '98 (Radio Onda Cero - Marzo 2014)

Soy un gran seguidor de la radio Onda Cero1. Cuando en las mañanas tengo que trabajar me encanta escuchar esta radio. Para mi que estoy en Europa es un horario excelente porque escucho con tranquilidad el "club 90" al aire desde las 4 a.m. hasta las 6 a.m. hora peruana.
El sabado 8 Marzo 2014 (feliz dia a todas las mujeres ^^ ) ha sido mandado en onda el ranking de el año 1998.
No estoy tanto deacuerdo con este ranking pero' bueno gustos son gustos no?
Personalmente me gustan mas las que hey marcando con "!".
Les dejo la lista (casi) completa2, espero les guste.
25 - Shakira - Ciega sordomuda !
24 - Chichi Peralta - Ciguapa
23 - Brother Louie (feat. Eric Singleton) - Modern Talking
22 - Enrique Iglesias - Cosas del amor
21 - Backstreet boys - Everybody
20 - Christian Meier - Quien sabe
19 - Fool's Garden - Lemon tree ! (dance remix version)
18 - Chayanne - Lo dejaria todo
17 - Space Girls - If you wanna be my lover
16 - Leon Gieco - Ojos con los Orozco !
15 - Dj Factoria - El chocho loco (remix version)
14 - Backstreet Boys - As long as you love me
13 - Fito Paez - El amor despues del amor
12 - Ilegales - Sueño contigo 
11 - Modern Talking - Cheri cheri lady
10 - Ricky Martin - Vuelve
9 - Nek - Laura no esta
8 - Chichi Peralta - Procura
7 - Batucada n2 - DJ Dero
6 - Alejandro Sanz - Amiga mia !
5 - Soda Stereo - Musica ligera !
4 - Ricky Martin - La copa de la vida
3 - Celine Dion - My heart will go on (with movie dialogues) !
2 - Modern Talking - You're my heart you're my soul
1 - El Chombo - Los Cuentos de la Cripta Parte 2

Les dejo tambien una playlist de youtube de estas canciones.

1 - Web Radio Onda Cero - Link
2 - La numero 7 era imposible de entender, era una cancion brasileña sin palabras. - gracias a blasterjaxx fernando por el link.

============================================================
Mas canciones en este link de youtube, canciones randoms de club 90 Onda Cero Peru'.
Gracias a blasterjaxx fernando por el link.

lunedì 22 luglio 2013

Un tuffo con l'Iphone

Una persona di mia conoscenza ha condiviso con me una sua disavventura. Il suo adorato Iphone 5 è andato a farsi un tuffo in mare (scemo lui), ora si sta consolando con il suo vecchio 3gs. Ovviamente non funziona più come prima ma presenta dei grossi problemi. Consultandosi in giro ha scoperto che è tutto un problema legata all'umidità, diciamo che dentro il telefono è presente ancora dell'acqua.
Sembra che un buon metodo, quelli della nonna per capirci, per soluzionare questo tipo di problema sia quello di far fare all'Iphone un tuffo in un po di riso, sembra che in questo modo si possa togliere facilmente l'umidità. Pazzesco no?
La cosa triste in questa storia è che questo metodo si dici che funzioni più o meno bene nel caso in cui il telefono finisca in acqua dolce. Nel caso di acqua salata come è quella del mare, le cose stanno diversamente, poiché il salino presente è fonte di ulteriori problemi.
Diciamo che questa è un po una soluzione da ultima spiaggia, ovviamente il telefono non è più in garanzia, ma a quanto sembra, l'assistenza Apple sembra che comunque possa ritirare il suo vecchio Iphone ottendo cosi un buono sconto per un successivo acquisto. Se cosi fosse, questo confermerebbe ancora una volta che:

  • l'assistenza Apple è una delle migliori che abbia mai visto. Vendono dei prodotti decisamente sovrapprezzati ma almeno hai una buona assistenza, consolateVi (si, io non sono un apple-fan)

sabato 13 luglio 2013

Win7 loader 2.1 by Daz

Segnalo per conoscenza Windows 7 loader 2.1 by Daz. Programmino "magico" in grado di darvi una seriale per il vostro sistema operativo in modo che non sia più in versione genuine ma passi alla versione attiva.
I passi da seguire sono ben spiegati nei link qui in basso.
Se vi chiedete come posso fare con gli aggiornamenti automatici di Windows a quanto pare questa procedura non da nessun tipo di problema: Windows loader è compattibile con gli aggiornamenti. Per maggiori informazioni vi lascio i link.

Guida come scaricare Windows Loader + pass: qui.
Forum discussione compattibilità aggiornamenti: 1 e 2.


mercoledì 5 giugno 2013

Viber desktop - versione beta

Chi conosce Viber penso possa apprezzare questa novità (insomma). La novità introdotta con questa versione è la videochiamata (stile skype). Personalmente l'ho trovata abbastanza fluida (confermato anche dal mio interlocutore), ma magari ancora da migliorare. Chi è incollato al pc per molte ore ha la possibilità di settare un bel riquadro di notifica in diverse parti del proprio schermo.
Piccola pecca:

  • scordatevi gli sticker almeno per il momento (personalmente li trovo molto belli, se si potessero acquistare ci farei un pensiero) cosi come le emoticon. (nel video qui in basso sono visibili ma credetemi non so come fare)
Pro:
  • salvataggio automatico dei file (es. foto) nella cartella ViberDownloads tra i vostri documenti (consiglio di rinominare i file il prima possibile per evitare di avere nomi come sigle incomprensibili)
  • sincronizzazione tra versione desktop e app nel telefono senza nessun problema