function varargout = rep_moteur(varargin)
% REP_MOTEUR M-file for rep_moteur.fig
%      REP_MOTEUR, by itself, creates a new REP_MOTEUR or raises the existing
%      singleton*.
%
%      H = REP_MOTEUR returns the handle to a new REP_MOTEUR or the handle to
%      the existing singleton*.
%
%      REP_MOTEUR('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in REP_MOTEUR.M with the given input arguments.
%
%      REP_MOTEUR('Property','Value',...) creates a new REP_MOTEUR or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before rep_moteur_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to rep_moteur_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help rep_moteur

% Last Modified by GUIDE v2.5 02-May-2010 16:52:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @rep_moteur_OpeningFcn, ...
                   'gui_OutputFcn',  @rep_moteur_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before rep_moteur is made visible.
function rep_moteur_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to rep_moteur (see VARARGIN)

% Choose default command line output for rep_moteur
% Valeur par défaut pour l'essai en boucle ouverte
handles.output = hObject;
handles.data.duree = .5;  %Durée d'un essai en BO pour identification du moteur
handles.data.amplitude = 12; %Amplitude de l'echelon d'entrée en BO

% Valeur par défaut pour l'essai en boucle fermée
handles.data.duree_bf = 2; %Durée d'un essai en bf
handles.data.puls_bf=6.28; %Pulsation du sinus d'entrée en BF
handles.data.amp_bf = 500;  %Amplitude de l'entree en bf
handles.data.moyenne_bf = 0; %Moyenne du sinus d'entrée en bf
%set(radiobutton_sin_bf,'Value',1)
%set(radiobutton_ech_bf,'Value',0)

%set(radiobutton_pid_bf,'Value',1)
handles.data.kp=50;
handles.data.ki=1;
handles.data.kd=1;

handles.data.trace_vit=1;
handles.data.trace_cons=1;
handles.data.trace_ecart=0;
handles.data.trace_com=0;


% Update handles structure
guidata(hObject, handles);

% UIWAIT makes rep_moteur wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = rep_moteur_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function edit_amplitude_Callback(hObject, eventdata, handles)
% hObject    handle to edit_amplitude (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_amplitude as text
%        str2double(get(hObject,'String')) returns contents of edit_amplitude as a double
amplitude = str2double(get(hObject, 'String'));
if isnan(amplitude)
    set(hObject, 'String', 12);
    errordlg('L amplitude doit être un nombre','Erreur');
end

% Save the new amplitude value
handles.data.amplitude = amplitude;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_amplitude_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_amplitude (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String','12')



function edit_duree_Callback(hObject, eventdata, handles)
% hObject    handle to edit_duree (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_duree as text
%        str2double(get(hObject,'String')) returns contents of edit_duree as a double
duree = str2double(get(hObject, 'String'));
if isnan(duree)
    set(hObject, 'String', 0.5);
    errordlg('La duree doit être un nombre','Error');
end

if duree>0.5
    set(hObject, 'String', 0.5);
    errordlg('La durée de l essai ne peut excéder 0.5 secondes','Erreur');
    duree = str2double(get(hObject, 'String'));
end

% Save the new volume value
handles.data.duree = duree;
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function edit_duree_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_duree (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String','0.5');


% --- Executes on button press in pushbutton_start.
function pushbutton_start_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h = waitbar(0,'Merci de patienter...');
ea09com_init;
ea09com_bind( 8809 );
ea09com_setAddress( '172.26.254.254', 8809 );
send_RFV;
ea09com_update;
takeFirst_FV

send_SetMMONITOR( 0, 1 );
send_SetMCTRL( 0, 2 );


duration=handles.data.duree
Amp=handles.data.amplitude/23.46*255
if Amp>255;
    Amp=255;
end

waitbar(0.25)

tstart = clock;
elapsed = 0;
while( elapsed < duration )
    ea09com_update;
    elapsed = etime(clock,tstart);
    send_SetControlPoint(Amp,0,0,0 );
    
%     if elapsed <duration/10
%         send_SetControlPoint(0,0,0,0 );
%     else
%         send_SetControlPoint(Amp,0,0,0 );
%     end
end;

%set monitoring of all motors off
send_SetMMONITOR( 0, 0 );
send_SetMCTRL( 0, 1 );

messageA = takeFirst_MessageA(0);

index = 1;
startTime = messageA(2);

while( [num_MessageA(0)] > 0 )
    messageA = takeFirst_MessageA(0);
    ea09time(index) = messageA(2) - startTime; 
    speed_setpoint(index) = messageA(3);
    actual_speed(index) = messageA(4);
    current(index) = messageA(5);
    control_point(index) = messageA(6);
    index = index + 1;
end;

ea09com_close;

waitbar(.5)

%Détermination du temps de début de l'échelon
ind=1

while control_point(ind)==0,
    ind=ind+1;
end

waitbar(.6)

figure
%plot( ea09time, speed_setpoint / 255, 'Color', [1 0 0] );
hold on
plot( ea09time(ind:length(ea09time))-ea09time(ind), actual_speed(ind:length(ea09time))*900*60/2000, 'Color', [0 1 0]  );
%plot( ea09time, current / 4096, 'Color', [0 0 1]  );
plot( ea09time(ind:length(ea09time))-ea09time(ind), min(24,control_point(ind:length(ea09time)) / 255*23.5), 'Color', [1 0 1]  );
legend('Vitesse de rotation moteur 1 (tours/min)', 'Tension d alimentation (V)' ,'Location','SE');
%legend( 'speed set-point', 'actual speed', 'current', 'control point' );
grid on
xlabel('Temps (ms)')
title('Unités SI')


close(h)


% --- Executes on button press in pushbutton_exec_bf.
function pushbutton_exec_bf_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_exec_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h = waitbar(0,'Merci de patienter...');

entree_sin=get(handles.radiobutton_sin_bf,'Value');
entree_ech=get(handles.radiobutton_ech_bf,'Value');

cor_p=get(handles.radiobutton_p_bf,'Value');
cor_i=get(handles.radiobutton_i_bf,'Value');
cor_pi=get(handles.radiobutton_pi_bf,'Value');
cor_pid=get(handles.radiobutton_pid_bf,'Value');

if cor_p==1
    correcteur='P';
elseif cor_i==1
    correcteur='I';
elseif cor_pi==1
    correcteur='PI';
else
    correcteur='PID';
end

ea09com_init;
ea09com_bind( 8809 );
ea09com_setAddress( '172.26.254.254', 8809 );
send_RFV;
ea09com_update;
takeFirst_FV

send_SetMMONITOR( 0, 1 );
send_SetMCTRL( 0, 2 );

duration=str2double(get(handles.edit_duree_bf, 'String')); %handles.data.duree_bf;
amp=handles.data.amp_bf;
moy_sin=handles.data.moyenne_bf;
puls_sin=handles.data.puls_bf;
kp=handles.data.kp;
ki=handles.data.ki;
kd=handles.data.kd;


waitbar(0.25)
switch correcteur
    case 'P'
        tstart = clock;
        elapsed = 0;
        ea09com_update;
        messageA = last_MessageA(0);
        starttime=messageA(2)/1000;
        while( elapsed < duration )
            ea09com_update;
            elapsed = etime(clock,tstart);
            %consigne = amp*entree_ech*2000/900/60;
            messageA = last_MessageA(0);
            time=messageA(2)/1000-starttime;
            consigne = (amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*time)))*2000/900/60;
            actual_speed = messageA(4);
            e = consigne - actual_speed;
            %i = i + e;
            % u = kp * ( e + ki * i );
            u=0.75/0.0325*kp*e;
            if( u > 255 )
                u = 255;
            elseif( u < -255 )
                u = -255;
            end;
            send_SetControlPoint( u, 0, 0, 0 );
          end;

    case 'I'
        tstart = clock;
        %tstart2=clock;
        elapsed = 0;
        i=0;  
        ea09com_update;
        messageA = last_MessageA(0);
        starttime=messageA(2)/1000;
        time=0;
        while( elapsed < duration )
            ea09com_update;
            elapsed = etime(clock,tstart);
            %consigne = amp*entree_ech*2000/900/60;
            messageA = last_MessageA(0);
            Te=-time;
            time=messageA(2)/1000-starttime;
            Te=Te+time;
            consigne = (amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*time)))*2000/900/60;
            actual_speed = messageA(4);
            e = consigne - actual_speed;
            %Te=etime(clock,tstart2);
            i = i + Te*e;
            %tstart2=clock;
            % u = kp * ( e + ki * i );
            u=0.75/0.0325*ki*i;
            if( u > 255 )
                u = 255;
            elseif( u < -255 )
                u = -255;
            end;
            send_SetControlPoint( u, 0, 0, 0 );
           end;
        
            case 'PI'
        
        i=0;
        ea09com_update;
        messageA = last_MessageA(0);
        starttime=messageA(2)/1000;
        time=0;
        tstart = clock;
        %tstart2=clock;
        elapsed = 0;
        while( elapsed < duration )
            ea09com_update;
            elapsed = etime(clock,tstart);
            messageA = last_MessageA(0);
            Te=-time;
            time=messageA(2)/1000-starttime;
            Te=Te+time;
            consigne = (amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*time)))*2000/900/60;
            %consigne = amp*entree_ech*2000/900/60;
            %consigne = (amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*elapsed)))*2000/900/60;
            actual_speed = messageA(4);
            e = consigne - actual_speed;
            i = i + Te*e;
            %tstart2=clock;
            % u = kp * ( e + ki * i );
            u=0.75/0.0325*kp*(e+ki*i);
            if( u > 255 )
                u = 255;
            elseif( u < -255 )
                u = -255;
            end;
            send_SetControlPoint( u, 0, 0, 0 );
        end;
        
        case 'PID'
%         i=0;
%         e=0;
%         ea09com_update;
%         messageA = last_MessageA(0);
%         starttime=messageA(2)/1000;
%         tstart = clock;
%         %tstart2=clock;
%         elapsed = 0;
%         time=0;
%         d=0
%         while( elapsed < duration )
%             ea09com_update;
%             elapsed = etime(clock,tstart);
%             messageA = last_MessageA(0);
%             Te=-time;
%             time=messageA(2)/1000-starttime;
%             Te=Te+time;
%             consigne = (amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*time)))*2000/900/60;
%             %consigne = amp*entree_ech*2000/900/60;
%             %consigne = (amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*elapsed)))*2000/900/60;
%             messageA = last_MessageA(0);
%             actual_speed = messageA(4);
%             %d=-e;
%             e = consigne - actual_speed;
%             %Te=etime(clock,tstart2);
%             i = i + Te*e;
%             %d=(d+e)/Te;
%             %tstart2=clock;
%             % u = kp * ( e + ki * i );
%             u=kp*(e+ki*i);%+kd*d);
%             if( u > 255 )
%                 u = 255;
%             elseif( u < -255 )
%                 u = -255;
%             end;
%             send_SetControlPoint( u, 0, 0, 0 );
%         end;
%%
i=0;
        e=0;
        d=0;
        ea09com_update;
        messageA = last_MessageA(0);
        starttime=messageA(2)/1000;
        time=0;
        tstart = clock;
        %tstart2=clock;
        elapsed = 0;
        while( elapsed < duration )
            ea09com_update;
            elapsed = etime(clock,tstart);
            messageA = last_MessageA(0);
            Te=-time;
            time=messageA(2)/1000-starttime;
            Te=Te+time;
            consigne = (amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*time)))*2000/900/60;
            %consigne = amp*entree_ech*2000/900/60;
            %consigne = (amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*elapsed)))*2000/900/60;
            actual_speed = messageA(4);
            d=-e;
            e = consigne - actual_speed;
            d=(d+e)/Te;
            i = i + Te*e;
            u=0.75/0.0325*kp*(e+ki*i+kd*d);
            if( u > 255 )
                u = 255;
            elseif( u < -255 )
                u = -255;
            end;
            send_SetControlPoint( u, 0, 0, 0 );
        end;

%%
end

%set monitoring of all motors off
send_SetMMONITOR( 0, 0 );
send_SetMCTRL( 0, 1 );

messageA = takeFirst_MessageA(0);
index = 1;
startTime = messageA(2);

while( [num_MessageA(0)] > 0 )
    messageA = takeFirst_MessageA(0);
    ea09time(index) = messageA(2) - startTime; 
    speed_setpoint(index) = messageA(3);
    actual_speed(index) = messageA(4);
    current(index) = messageA(5);
    control_point(index) = messageA(6);
    index = index + 1;
end;

ea09com_close;
waitbar(.5)

%Détermination du temps de début de l'échelon
ind=1
while control_point(ind)==0,
    ind=ind+1;
end
waitbar(.6)

figure

if get(handles.checkbox_cons,'Value')==1
%plot( ea09time, speed_setpoint*900*60/2000, 'Color', [1 0 0] );
cons_the=amp*entree_ech+entree_sin*(moy_sin+amp*sin(puls_sin*ea09time*1e-3));
plot(ea09time(ind:length(ea09time))-ea09time(ind),cons_the(ind:length(ea09time)))
end

hold on
if get(handles.checkbox_vit,'Value')==1
plot( ea09time(ind:length(ea09time))-ea09time(ind), actual_speed(ind:length(ea09time))*900*60/2000, 'Color', [0 1 0]  );
end
hold on
%plot( ea09time, current / 4096, 'Color', [0 0 1]  );
grid on
legend('Consigne de vitesse (tr/min)', 'Vitesse de rotation moteur 1 (tours/min)','Location','SE');


if get(handles.checkbox_com,'Value')==1
figure
plot( ea09time(ind:length(ea09time))-ea09time(ind), control_point(ind:length(ea09time)), 'Color', [1 0 1]  );
xlabel('Temps (ms)')
legend('Commande réel')
ylabel('Commande')
grid on
end

if get(handles.checkbox_ecart,'Value')==1
figure
plot( ea09time(ind:length(ea09time))-ea09time(ind), cons_the(ind:length(ea09time))*2000/900/60-actual_speed(ind:length(ea09time)), 'Color', [1 0 1]  );
xlabel('Temps (ms)')
ylabel('ecart (inc/900eme de s)')
grid on
end
  
close(h)



function edit_kp_Callback(hObject, eventdata, handles)
% hObject    handle to edit_kp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kp = str2double(get(hObject, 'String'));
if isnan(kp)
    set(hObject, 'String', 50);
    errordlg('Le paramètre Kp doit être un nombre','Erreur');
end

% Save the new amplitude value
handles.data.kp = kp;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit_kp as text
%        str2double(get(hObject,'String')) returns contents of edit_kp as a double


% --- Executes during object creation, after setting all properties.
function edit_kp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_kp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String','50')



function edit_kd_Callback(hObject, eventdata, handles)
% hObject    handle to edit_kd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_kd as text
%        str2double(get(hObject,'String')) returns contents of edit_kd as a double
kd = str2double(get(hObject, 'String'));
if isnan(kd)
    set(hObject, 'String', 1);
    errordlg('Kd doit être un nombre','Erreur');
end

% Save the new amplitude value
handles.data.kd = kd;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_kd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_kd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String',1)



function edit_ki_Callback(hObject, eventdata, handles)
% hObject    handle to edit_ki (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ki = str2double(get(hObject, 'String'));
if isnan(ki)
    set(hObject, 'String', 1);
    errordlg('Ki doit être un nombre','Erreur');
end

% Save the new amplitude value
handles.data.ki = ki;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit_ki as text
%        str2double(get(hObject,'String')) returns contents of edit_ki as a double


% --- Executes during object creation, after setting all properties.
function edit_ki_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_ki (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String',1);


function edit_amplitude_bf_Callback(hObject, eventdata, handles)
% hObject    handle to edit_amplitude_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
amp_bf = str2double(get(hObject, 'String'));
if isnan(amp_bf)
    set(hObject, 'String', 500);
    errordlg('L amplitude doit être un nombre','Erreur');
end

% Save the new amplitude value
handles.data.amp_bf = amp_bf;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit_amplitude_bf as text
%        str2double(get(hObject,'String')) returns contents of edit_amplitude_bf as a double


% --- Executes during object creation, after setting all properties.
function edit_amplitude_bf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_amplitude_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String','500')



function edit_duree_bf_Callback(hObject, eventdata, handles)
% hObject    handle to edit_duree_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
duree_bf = str2double(get(hObject, 'String'));
if isnan(duree_bf)
    set(hObject, 'String', 2);
    errordlg('La durée doit être un nombre','Erreur');
    duree_bf=2;
end

% Save the new amplitude value
handles.data.buree_bf = duree_bf;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit_duree_bf as text
%        str2double(get(hObject,'String')) returns contents of edit_duree_bf as a double


% --- Executes during object creation, after setting all properties.
function edit_duree_bf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_duree_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String','2')


function edit_puls_bf_Callback(hObject, eventdata, handles)
% hObject    handle to edit_puls_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
puls_bf = str2double(get(hObject, 'String'));
if isnan(puls_bf)
    set(hObject, 'String', 6.28);
    errordlg('La pulsation doit être un nombre','Erreur');
end

% Save the new amplitude value
handles.data.puls_bf = puls_bf;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit_puls_bf as text
%        str2double(get(hObject,'String')) returns contents of edit_puls_bf as a double


% --- Executes during object creation, after setting all properties.
function edit_puls_bf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_puls_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String','6.28')


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in checkbox_cons.
function checkbox_cons_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_cons (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
trace_cons=get(hObject,'Value');
handles.data.trace_cons = trace_cons;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of checkbox_cons


% --- Executes on button press in checkbox_vit.
function checkbox_vit_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_vit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
trace_vit=get(hObject,'Value');
handles.data.trace_vit = trace_vit;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of checkbox_vit


% --- Executes on button press in checkbox_com.
function checkbox_com_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_com (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
trace_com=get(hObject,'Value');
handles.data.trace_com = trace_com;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of checkbox_com


% --- Executes on button press in checkbox_ecart.
function checkbox_ecart_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_ecart (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
trace_ecart=get(hObject,'Value');
handles.data.trace_ecart = trace_ecart;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of checkbox_ecart


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in checkbox6.
function checkbox6_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox6



function edit_moy_bf_Callback(hObject, eventdata, handles)
% hObject    handle to edit_moy_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
moyenne_bf = str2double(get(hObject, 'String'));
if isnan(moyenne_bf)
    set(hObject, 'String', 0);
    errordlg('La moyenne doit être un nombre','Erreur');
end

% Save the new amplitude value
handles.data.moyenne_bf = moyenne_bf;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit_moy_bf as text
%        str2double(get(hObject,'String')) returns contents of edit_moy_bf as a double


% --- Executes during object creation, after setting all properties.
function edit_moy_bf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_moy_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String','0');


% --- Executes on button press in radiobutton_ech_bf.
function radiobutton_ech_bf_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_ech_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit_moy_bf,'Enable','off')
set(handles.edit_puls_bf,'Enable','off')
% Hint: get(hObject,'Value') returns toggle state of radiobutton_ech_bf


% --- Executes on button press in radiobutton_sin_bf.
function radiobutton_sin_bf_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_sin_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit_moy_bf,'Enable','on')
set(handles.edit_puls_bf,'Enable','on')
% Hint: get(hObject,'Value') returns toggle state of radiobutton_sin_bf


% --- Executes on button press in radiobutton_p_bf.
function radiobutton_p_bf_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_p_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit_kp,'Enable','on')
set(handles.edit_ki,'Enable','off')
set(handles.edit_kd,'Enable','off')
% Hint: get(hObject,'Value') returns toggle state of radiobutton_p_bf


% --- Executes on button press in radiobutton_i_bf.
function radiobutton_i_bf_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_i_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit_kp,'Enable','off')
set(handles.edit_ki,'Enable','on')
set(handles.edit_kd,'Enable','off')
% Hint: get(hObject,'Value') returns toggle state of radiobutton_i_bf


% --- Executes on button press in radiobutton_pi_bf.
function radiobutton_pi_bf_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_pi_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit_kp,'Enable','on')
set(handles.edit_ki,'Enable','on')
set(handles.edit_kd,'Enable','off')
% Hint: get(hObject,'Value') returns toggle state of radiobutton_pi_bf


% --- Executes on button press in radiobutton_pid_bf.
function radiobutton_pid_bf_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_pid_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit_kp,'Enable','on')
set(handles.edit_ki,'Enable','on')
set(handles.edit_kd,'Enable','on')
% Hint: get(hObject,'Value') returns toggle state of radiobutton_pid_bf


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_n.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes1


% --- Executes during object creation, after setting all properties.
function axes7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_ne.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes7


% --- Executes during object creation, after setting all properties.
function axes5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('stop.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes5


% --- Executes during object creation, after setting all properties.
function axes8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_se.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes8


% --- Executes during object creation, after setting all properties.
function axes4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_e.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes4


% --- Executes during object creation, after setting all properties.
function axes6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_no.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes6


% --- Executes during object creation, after setting all properties.
function axes3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_o.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes3


% --- Executes during object creation, after setting all properties.
function axes9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_so.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes9


% --- Executes during object creation, after setting all properties.
function axes10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_r+.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes10


% --- Executes during object creation, after setting all properties.
function axes11_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_r-.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes11


% --- Executes during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('fleche_s.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes2


% --- Executes during object creation, after setting all properties.
function pushbutton4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function pushbutton_exec_bf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_exec_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called



% --- Executes during object creation, after setting all properties.
function pushbutton_start_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object deletion, before destroying properties.
function edit_amplitude_bf_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to edit_amplitude_bf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
%set monitoring of all motors off
%send_SetMMONITOR( 0, 0 );
%send_SetMCTRL( 0, 1 );
%ea09com_close;
delete(hObject);


% --------------------------------------------------------------------
function uipanel6_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to uipanel6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function axe_n_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axe_n (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axe_n


% --- Executes during object creation, after setting all properties.
function axes13_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
I=imread('robotino.jpg');
imshow(I)
% Hint: place code in OpeningFcn to populate axes13


