%% Expérience pour récupérer données
% Ces données expérimentales permettront ensuite de créer le modèle

close all; clc;

try
    fclose(s);
catch
    warning('s ne peut pas être fermé');
end

s = serial('COM5');
set(s,'BaudRate',9600,'Parity','none')
set(s,'DataBits',8)
set(s,'StopBits',1)

fopen(s);
pause(2);

%periode d'echantillonnage
time_min = 0.7; % exprimee en minutes 
time_sec = time_min*60;  %=42sec

%duree de la manip
t1 = [20,4,4,15,15,8,8,10,10+5]*60;
t2 = [20+5,4,4,15,15,8,8,10,10]*60;
tcum1= zeros(1,length(t1));
tcum2= zeros(1,length(t2)); % en sec
tcum1(1)=t1(1);
tcum2(1)=t2(1);
for i=2:length(t1)
    tcum1(i)=tcum1(i-1)+t1(i);
    tcum2(i)=tcum2(i-1)+t2(i);
end


TotalTime_min = max(tcum1)/60;
TotalTime_sec = TotalTime_min*60;
coups = round(TotalTime_sec/time_sec);
T1 = zeros(1,coups);
T2 = zeros(1,coups);
u1 = zeros(1,coups);
u2 = zeros(1,coups);

figure(1);
subplot(2,1,1);
axis([0 TotalTime_min 0 150]); 
ani1=animatedline('Color','r');
xlabel('temps (min)')
ylabel('température (°C)')
title('capteur 1') 
subplot(2,1,2);
axis([0 TotalTime_min 0 150]);
ani2=animatedline('Color','b');
xlabel('temps (min)')
ylabel('température (°C)')
title('capteur 2') 

figure(2);
subplot(2,1,1);
axis([0 TotalTime_min 0 255]); 
ani3=animatedline('Color','r');
xlabel('temps (min)')
ylabel('valeur de la commande')
title('U1') 
subplot(2,1,2);
axis([0 TotalTime_min 0 255]);
ani4=animatedline('Color','b');
xlabel('temps (min)')
ylabel('valeur de la commande')
title('U2')

x = 0:time_min:TotalTime_min;
U1 = check_u(100);
U2 = check_u(100);
w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));

%%%------- Experience ---------------------
TSTART = tic;
eTime = 0;
delta_u = 60;

for k = 1:coups
    while(eTime<k*time_sec)
        eTime = toc(TSTART);
    end
    % Valeur de U1/U2 
    switch true
        case eTime<tcum1(1) % 20 min
            U1 = check_u(127);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum1(2) % 4 min
            U1 = check_u(127+delta_u);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum1(3)
            U1 = check_u(127-delta_u);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum1(4) % 15 min
            U1 = check_u(127+delta_u);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum1(5)
            U1 = check_u(127-delta_u);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum1(6) % 8 min
            U1 = check_u(127+delta_u);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum1(7) 
            U1 = check_u(127-delta_u);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum1(8) % 10 min
            U1 = check_u(127+delta_u);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum1(9) % 10+5
            U1 = check_u(127-delta_u);
            u1(k)=U1;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));      
        otherwise
            break;
    end        
    
    switch true
        case eTime<tcum2(1) % 5 min
            U2 = check_u(127);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum2(2) % 4 min
            U2 = check_u(127+delta_u);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum2(3)
            U2 = check_u(127-delta_u);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum2(4) % 15 min
            U2 = check_u(127+delta_u);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum2(5)
            U2 = check_u(127-delta_u);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum2(6) % 8 min
            U2 = check_u(127+delta_u);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum2(7) 
            U2 = check_u(127-delta_u);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum2(8) % 10 min
            U2 = check_u(127+delta_u);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));
        case eTime<tcum2(9) 
            U2 = check_u(127-delta_u);
            u2(k)=U2;
            w = strcat('0',num2str(U1,'%03d'),num2str(U2,'%03d'));  
        otherwise
            break;
    end

    % Ecriture de w sur aduino + Lecture de T1/T2 de l'arduino
    fwrite(s,w,'uint8');
    temp = fscanf(s,'%s', 9);
    T1(k) = str2num(temp(:,1:4))/10;
    T2(k) = str2num(temp(:,6:9))/10;
    
    % affichage dynamique (tt les deltaT)
    addpoints(ani1,x(k),T1(k));    
    addpoints(ani2,x(k),T2(k));
    drawnow
    addpoints(ani3,x(k),U1);
    addpoints(ani4,x(k),U2);
    drawnow
end

fclose(s);

d = datestr(clock, 'yyyy-mm-dd-HH-MM');
save('sauvegarde.mat', 'd', 'T1', 'T2', 'u1', 'u2');


