classdef pointage_laser_vBeta < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                      matlab.ui.Figure
        GridLayout                    matlab.ui.container.GridLayout
        TabGroup                      matlab.ui.container.TabGroup
        ConnectionTab                 matlab.ui.container.Tab
        PortDropDownLabel             matlab.ui.control.Label
        PortDropDown                  matlab.ui.control.DropDown
        ConnectionButton              matlab.ui.control.Button
        RefreshListButton             matlab.ui.control.Button
        Image                         matlab.ui.control.Image
        AsservissementenpositiondunLaseren2DLabel  matlab.ui.control.Label
        Image3                        matlab.ui.control.Image
        BadfeedbackLabel              matlab.ui.control.Label
        Image3_2                      matlab.ui.control.Image
        GoodcontrolLabel              matlab.ui.control.Label
        Image4                        matlab.ui.control.Image
        PhotodetectorTab              matlab.ui.container.Tab
        GridLayout2                   matlab.ui.container.GridLayout
        GridLayout3                   matlab.ui.container.GridLayout
        InvertXCheckBox               matlab.ui.control.CheckBox
        InvertYCheckBox               matlab.ui.control.CheckBox
        GetButton                     matlab.ui.control.Button
        StopButton                    matlab.ui.control.Button
        Image2                        matlab.ui.control.Image
        UIAxes                        matlab.ui.control.UIAxes
        MotorTab                      matlab.ui.container.Tab
        GridLayout4                   matlab.ui.container.GridLayout
        GridLayout5                   matlab.ui.container.GridLayout
        InvertXCheckBox_2             matlab.ui.control.CheckBox
        InvertYCheckBox_2             matlab.ui.control.CheckBox
        StopButton_2                  matlab.ui.control.Button
        SettoInitialPositionButton    matlab.ui.control.Button
        MotorPanel                    matlab.ui.container.Panel
        XSliderLabel                  matlab.ui.control.Label
        XSlider                       matlab.ui.control.Slider
        XValueLabel                   matlab.ui.control.Label
        YSliderLabel                  matlab.ui.control.Label
        YSlider                       matlab.ui.control.Slider
        YValueLabel                   matlab.ui.control.Label
        SetXMaxButton                 matlab.ui.control.Button
        SetXMinButton                 matlab.ui.control.Button
        SetYMaxButton                 matlab.ui.control.Button
        SetYMinButton                 matlab.ui.control.Button
        MultiplicatorDropDownLabel    matlab.ui.control.Label
        MultiplicatorDropDown         matlab.ui.control.DropDown
        UIAxes2                       matlab.ui.control.UIAxes
        PhotodetectorPanel            matlab.ui.container.Panel
        UIAxes3                       matlab.ui.control.UIAxes
        OpenLoopTab                   matlab.ui.container.Tab
        GridLayout7                   matlab.ui.container.GridLayout
        GridLayout8                   matlab.ui.container.GridLayout
        StartButton                   matlab.ui.control.Button
        XminEditFieldLabel            matlab.ui.control.Label
        XminEditField                 matlab.ui.control.EditField
        XmaxEditFieldLabel            matlab.ui.control.Label
        XmaxEditField                 matlab.ui.control.EditField
        YminEditFieldLabel            matlab.ui.control.Label
        YminEditField                 matlab.ui.control.EditField
        YmaxEditFieldLabel            matlab.ui.control.Label
        YmaxEditField                 matlab.ui.control.EditField
        FeHzEditField_2Label          matlab.ui.control.Label
        FeHzEditField_2               matlab.ui.control.EditField
        UIAxes4                       matlab.ui.control.UIAxes
        PIDTab                        matlab.ui.container.Tab
        GridLayout6                   matlab.ui.container.GridLayout
        GainXSliderLabel              matlab.ui.control.Label
        GainXSlider                   matlab.ui.control.Slider
        xGainxDropDownLabel           matlab.ui.control.Label
        xGainxDropDown                matlab.ui.control.DropDown
        InvertXCheckBox_3             matlab.ui.control.CheckBox
        InvertYCheckBox_3             matlab.ui.control.CheckBox
        GainYSliderLabel              matlab.ui.control.Label
        GainYSlider                   matlab.ui.control.Slider
        xGainyDropDownLabel           matlab.ui.control.Label
        xGainyDropDown                matlab.ui.control.DropDown
        IntegralXSliderLabel          matlab.ui.control.Label
        IntegralXSlider               matlab.ui.control.Slider
        xIntegralxDropDownLabel       matlab.ui.control.Label
        xIntegralxDropDown            matlab.ui.control.DropDown
        xIntegralyDropDownLabel       matlab.ui.control.Label
        xIntegralyDropDown            matlab.ui.control.DropDown
        IntegralYSliderLabel          matlab.ui.control.Label
        IntegralYSlider               matlab.ui.control.Slider
        XGainValueLabel               matlab.ui.control.Label
        YGainValueLabel               matlab.ui.control.Label
        XIntegralValueLabel           matlab.ui.control.Label
        YIntegralValueLabel           matlab.ui.control.Label
        DerivativeXSliderLabel        matlab.ui.control.Label
        DerivativeXSlider             matlab.ui.control.Slider
        DerivativeYSliderLabel        matlab.ui.control.Label
        DerivativeYSlider             matlab.ui.control.Slider
        xDerivativexDropDownLabel     matlab.ui.control.Label
        xDerivativexDropDown          matlab.ui.control.DropDown
        xDerivativeyDropDownLabel     matlab.ui.control.Label
        xDerivativeyDropDown          matlab.ui.control.DropDown
        XDerivativeValueLabel         matlab.ui.control.Label
        YDerivativeValueLabel         matlab.ui.control.Label
        XYCheckBox                    matlab.ui.control.CheckBox
        FeHzEditFieldLabel            matlab.ui.control.Label
        FeHzEditField                 matlab.ui.control.EditField
        SendButton                    matlab.ui.control.Button
        AutoCheckBox                  matlab.ui.control.CheckBox
        ProportionnalControllerLabel  matlab.ui.control.Label
        IntegralControllerLabel       matlab.ui.control.Label
        DerivativeControllerLabel     matlab.ui.control.Label
        GlobalparametersLabel         matlab.ui.control.Label
        NumDataLabel                  matlab.ui.control.Label
        DataLabel                     matlab.ui.control.Label
    end

    
    properties (Access = private)
        s % liaison serie
        connected = false % true if serial is connected
        dataS
        dataX   % step response on X
        dataY   % step response on Y
        data_T  % time
    end
    
    methods (Access = private)
        
        % IT Communication function
        function boardCommIT(app, src, ~)
            rLine = readline(src);   
            rLineChar = char(rLine);
            if rLineChar(1:1) == 'A'
                stepC = sscanf(rLine, 'A_%lf_%lf_!');
                if(app.InvertXCheckBox.Value == 1) stepC(1) = -stepC(1); end;
                if(app.InvertYCheckBox.Value == 1) stepC(2) = -stepC(2); end;
                plot(app.UIAxes, round(stepC(1)), round(stepC(2)),'+b', 'MarkerSize',25, 'LineWidth',10);
            elseif rLineChar(1:1) == 'M'
                stepC = sscanf(rLine, 'M_%lf_%lf_%lf_%lf_!');
                if(strcmp(app.TabGroup.SelectedTab.Title,'Alignment'))
                    if(app.InvertXCheckBox.Value == 1) stepC(3) = -stepC(3); end;
                    if(app.InvertYCheckBox.Value == 1) stepC(4) = -stepC(4); end;
                    plot(app.UIAxes, round(stepC(3)*100)/100, round(stepC(4)*100)/100,'+b', 'MarkerSize',25, 'LineWidth',10);
                elseif(strcmp(app.TabGroup.SelectedTab.Title,'Motor'))
                    if(app.InvertXCheckBox_2.Value == 1) stepC(3) = -stepC(3); end;
                    if(app.InvertYCheckBox_2.Value == 1) stepC(4) = -stepC(4); end;
                    plot(app.UIAxes2, round(stepC(1)*100)/100, round(stepC(2)*100)/100,'+b', 'MarkerSize',10, 'LineWidth',3);
                    plot(app.UIAxes3, round(stepC(3)*100)/100, round(stepC(4)*100)/100,'+b', 'MarkerSize',10, 'LineWidth',3);
                end
            elseif rLineChar(1:1) == 'S'
                if rLineChar(3:3) ~= 'E'
                    stepC = sscanf(rLineChar, 'S_%c_%d_%lf_!');
                    if char(stepC(1)) == 'x'
                        if stepC(2) > 0
                            app.dataX(stepC(2)) = stepC(3);
                        else
                            disp('Erreur nombre X '+string(stepC(2)))
                        end
                    elseif char(stepC(1)) == 'y'
                        if stepC(2) > 0
                            app.dataY(stepC(2)) = stepC(3);
                        else
                            disp('Erreur nombre Y '+string(stepC(2)))
                        end
                    elseif char(stepC(1)) == 's'
                        if stepC(2) > 0
                            app.dataS(stepC(2)) = stepC(3);
                        else
                            disp('Erreur nombre S '+string(stepC(2)))
                        end
                    end
                    app.DataLabel.Text = "Data---";
                    app.StartButton.Enable = false;
                else
                    FeHz = str2num(app.FeHzEditField_2.Value);
                    app.data_T = linspace(0,256/FeHz-1/FeHz, 256);
                    plot(app.UIAxes4,app.data_T, app.dataS,'-r',app.data_T, app.dataX,'+b',app.data_T, app.dataY,'g');
                    legend(app.UIAxes4, 'Step X', 'Detector X', 'Detector Y');
                    drawnow;
                    app.DataLabel.Text = "";
                    app.StartButton.Enable = true;
                end                
            end
            %app.DataLabel.Text = rLine;
            app.NumDataLabel.Text = num2str(app.s.NumBytesAvailable);
        end
        
        % Sending PID information
        function sendPID(app, ~)
            if(app.connected) 
                if(str2num(app.FeHzEditField.Value) <= 10000)
                    if(str2num(app.FeHzEditField.Value) >= 10)
                        sendingData = strcat("D_"+app.XGainValueLabel.Text+"_"+app.YGainValueLabel.Text+ ...
                            "_"+app.XIntegralValueLabel.Text+"_"+app.YIntegralValueLabel.Text+ ...
                            "_"+app.XDerivativeValueLabel.Text+"_"+app.YDerivativeValueLabel.Text+ ...
                            "_"+app.FeHzEditField.Value+"_!");
                        app.DataLabel.Text = sendingData;
                        writeline(app.s, sendingData);
                    else
                        msgbox('Warning ! Sampling frequency must be between 10Hz to 10kHz !');
                    end
                else
                    msgbox('Warning ! Sampling frequency must be between 10Hz to 10kHz !');
                end
            else 
                msgbox('You must be connected to the board...','Error','error');  
            end;
        end
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            app.PortDropDown.Items = serialportlist();
            
            % Step Tab
            app.FeHzEditField.Value = '10000';
            app.StartButton.BackgroundColor = 'g';
            % PID Tab
            app.FeHzEditField_2.Value = '10000';
            app.XGainValueLabel.Text = '0';
            app.YGainValueLabel.Text = '0';
            app.XIntegralValueLabel.Text = '0';
            app.YIntegralValueLabel.Text = '0';
            app.XDerivativeValueLabel.Text = '0';
            app.YDerivativeValueLabel.Text = '0';
            app.xGainxDropDown.Value = '1';
            app.xGainyDropDown.Value = '1';
            app.xIntegralxDropDown.Value = '1';
            app.xIntegralyDropDown.Value = '1';
            app.xDerivativexDropDown.Value = '1';
            app.xDerivativeyDropDown.Value = '1';
            app.XYCheckBox.Value = true;
            app.XYCheckBoxValueChanged();
            % Motor Tab
            app.SettoInitialPositionButton.BackgroundColor = 'c';
            app.StopButton_2.BackgroundColor = 'r';
            app.StopButton_2.FontColor = 'w';
            app.SetXMinButton.BackgroundColor = 'm';
            app.SetXMaxButton.BackgroundColor = 'm';
            app.SetXMinButton.FontColor = 'w';
            app.SetXMaxButton.FontColor = 'w';
            app.SetYMinButton.BackgroundColor = 'b';
            app.SetYMaxButton.BackgroundColor = 'b';
            app.SetYMinButton.FontColor = 'w';
            app.SetYMaxButton.FontColor = 'w';
            app.XminEditField.Value = '-1';
            app.XmaxEditField.Value = '1';
            app.YminEditField.Value = '-1';
            app.YmaxEditField.Value = '1';
            app.GainXSlider.Value = 1;
            app.GainYSlider.Value = 1;
            app.XGainValueLabel.Text = '1';
            app.YGainValueLabel.Text = '1';
            app.UIAxes2.DataAspectRatio = [1 1 1];
            app.UIAxes2.XLim= [-100 100];
            app.UIAxes2.YLim= [-100 100];
            app.UIAxes3.DataAspectRatio = [1 1 1];
            app.UIAxes3.XLim= [-100 100];
            app.UIAxes3.YLim= [-100 100];
            % Alignment Tab
            app.GetButton.BackgroundColor = 'g';
            app.StopButton.Enable = false;
            app.UIAxes.DataAspectRatio = [1 1 1];
            app.MultiplicatorDropDown.Value = '100';            
            app.UIAxes.XLim= [-100 100];
            app.UIAxes.YLim= [-100 100];
            
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            delete(app)
            clear app.s
        end

        % Button pushed function: GetButton
        function GetButtonPushed(app, event)
            if(app.connected)
                writeline(app.s, 'A_!');
                app.GetButton.BackgroundColor = [197/255.0 176/255.0 171/255.0];
                app.GetButton.Enable = false;
                app.StopButton.BackgroundColor = 'r';
                app.StopButton.Enable = true;
            else
                msgbox('You must be connected to the board...','Error','error');
            end;
        end

        % Button pushed function: StopButton, StopButton_2
        function StopButtonPushed(app, event)
            if(app.connected)
                writeline(app.s, 'O_!');
                app.GetButton.BackgroundColor = 'g';
                app.GetButton.Enable = true;
                app.StopButton.BackgroundColor = [197/255.0 176/255.0 171/255.0];
                app.StopButton.Enable = false;
            else
                msgbox('You must be connected to the board...','Error','error');
            end;
        end

        % Button pushed function: ConnectionButton
        function ConnectionButtonPushed(app, event)
            try
                app.s = serialport(num2str(app.PortDropDown.Value), 115200);
                configureTerminator(app.s, "CR/LF");
                configureCallback(app.s, "terminator", @app.boardCommIT);
                app.ConnectionButton.Enable = false;
                app.ConnectionButton.BackgroundColor = 'g';
                app.connected = true;
                app.RefreshListButton.Enable = false;
            catch
                msgbox('Problem using serial.');
            end
        end

        % Value changed function: XSlider, YSlider
        function XSliderValueChanged(app, event)
            valueX = app.XSlider.Value;
            valueY = app.YSlider.Value;
            valueX = valueX * str2num(app.MultiplicatorDropDown.Value);
            valueY = valueY * str2num(app.MultiplicatorDropDown.Value);
            app.XValueLabel.Text = num2str(round(valueX*100)/100);
            app.YValueLabel.Text = num2str(round(valueY*100)/100);
            sendingData = strcat("M_"+num2str(valueX)+"_"+num2str(valueY)+"_!");
            if(app.connected) writeline(app.s, sendingData);
            else msgbox('You must be connected to the board...','Error','error');  end;     
            app.UIAxes2.XLim= [-str2num(app.MultiplicatorDropDown.Value) str2num(app.MultiplicatorDropDown.Value)];
            app.UIAxes2.YLim= [-str2num(app.MultiplicatorDropDown.Value) str2num(app.MultiplicatorDropDown.Value)];
        end

        % Value changing function: XSlider
        function XSliderValueChanging(app, event)
            changingValue = event.Value;
            valueY = app.YSlider.Value;
            app.XValueLabel.Text = num2str(round(changingValue));
            sendingData = strcat("M_"+num2str(changingValue)+"_"+num2str(valueY)+"_!");
            if(app.connected) writeline(app.s, sendingData);
            else msgbox('You must be connected to the board...','Error','error');  end;
        end

        % Value changing function: YSlider
        function YSliderValueChanging(app, event)
            changingValue = event.Value;
            valueX = app.XSlider.Value;
            app.YValueLabel.Text = num2str(round(changingValue));            
            sendingData = strcat("M_"+num2str(valueX)+"_"+num2str(changingValue)+"_!");
            if(app.connected) writeline(app.s, sendingData);
            else msgbox('You must be connected to the board...','Error','error');  end;
        end

        % Value changing function: GainXSlider
        function GainXSliderValueChanging(app, event)
            changingValue = event.Value;
            value = round(changingValue)*str2num(app.xGainxDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XGainValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YGainValueLabel.Text = num2str(valueY); 
            end;
            if(app.AutoCheckBox.Value) app.sendPID(); end;
        end

        % Value changed function: XYCheckBox
        function XYCheckBoxValueChanged(app, event)
            value = app.XYCheckBox.Value;
            if(value)
                app.GainYSlider.Enable = false;
                app.IntegralYSlider.Enable = false;
                app.DerivativeYSlider.Enable = false;
                app.xGainyDropDown.Enable = false;
                app.xIntegralyDropDown.Enable = false;
                app.xDerivativeyDropDown.Enable = false;                
            else
                app.GainYSlider.Enable = true;
                app.IntegralYSlider.Enable = true;
                app.DerivativeYSlider.Enable = true;    
                app.xGainyDropDown.Enable = true;
                app.xIntegralyDropDown.Enable = true;
                app.xDerivativeyDropDown.Enable = true;            
            end
        end

        % Button pushed function: SendButton
        function SendButtonPushed(app, event)
            value = round(app.GainXSlider.Value)*str2num(app.xGainxDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XGainValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YGainValueLabel.Text = num2str(valueY); 
            else
                value = round(app.GainYSlider.Value)*str2num(app.xGainyDropDown.Value);
                if(app.InvertYCheckBox_3.Value) valueY = -value; 
                else valueY = value; end;
                app.YGainValueLabel.Text = num2str(valueY); 
            end;     

            value = round(app.IntegralXSlider.Value)*str2num(app.xIntegralxDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XIntegralValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YIntegralValueLabel.Text = num2str(valueY); 
            else
                value = round(app.IntegralYSlider.Value)*str2num(app.xIntegralyDropDown.Value);
                if(app.InvertYCheckBox_3.Value) valueY = -value; 
                else valueY = value; end;
                app.YIntegralValueLabel.Text = num2str(valueY); 
            end;  
            
            value = round(app.DerivativeXSlider.Value)*str2num(app.xDerivativexDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XDerivativeValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YDerivativeValueLabel.Text = num2str(valueY); 
            else
                value = round(app.DerivativeYSlider.Value)*str2num(app.xDerivativeyDropDown.Value);
                if(app.InvertYCheckBox_3.Value) valueY = -value; 
                else valueY = value; end;
                app.YDerivativeValueLabel.Text = num2str(valueY); 
            end;  
            
            app.sendPID();
        end

        % Value changed function: xGainxDropDown
        function xGainxDropDownValueChanged(app, event)
            value = round(app.GainXSlider.Value)*str2num(app.xGainxDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XGainValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YGainValueLabel.Text = num2str(valueY); 
            end;
            if(app.AutoCheckBox.Value) app.sendPID(); end;
        end

        % Value changing function: IntegralXSlider
        function IntegralXSliderValueChanging(app, event)
            changingValue = event.Value;
            value = round(changingValue)*str2num(app.xIntegralxDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XIntegralValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YIntegralValueLabel.Text = num2str(valueY); 
            end;
            if(app.AutoCheckBox.Value) app.sendPID(); end;            
        end

        % Value changed function: xIntegralxDropDown
        function xIntegralxDropDownValueChanged(app, event)
            value = round(app.IntegralXSlider.Value)*str2num(app.xIntegralxDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XIntegralValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YIntegralValueLabel.Text = num2str(valueY); 
            end;
            if(app.AutoCheckBox.Value) app.sendPID(); end;            
        end

        % Value changing function: DerivativeXSlider
        function DerivativeXSliderValueChanging(app, event)
            changingValue = event.Value;
            value = round(changingValue)*str2num(app.xDerivativexDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XDerivativeValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YDerivativeValueLabel.Text = num2str(valueY); 
            end;
            if(app.AutoCheckBox.Value) app.sendPID(); end;              
        end

        % Value changed function: xDerivativexDropDown
        function xDerivativexDropDownValueChanged(app, event)
            value = round(app.DerivativeXSlider.Value)*str2num(app.xDerivativexDropDown.Value);
            if(app.InvertXCheckBox_3.Value) valueX = -value; 
            else valueX = value; end;
            app.XDerivativeValueLabel.Text = num2str(valueX);
            if(app.XYCheckBox.Value) 
                if(app.InvertYCheckBox_3.Value) valueY = -value;
                else valueY = value; end;
                app.YDerivativeValueLabel.Text = num2str(valueY); 
            end;
            if(app.AutoCheckBox.Value) app.sendPID(); end;                 
        end

        % Key press function: UIFigure
        function UIFigureKeyPress(app, event)
            key = event.Key;
            if(strcmp(key,'return')) % enter key
                if(strcmp(app.TabGroup.SelectedTab.Title,'PID'))
                    app.sendPID();
                end
            end
        end

        % Value changed function: FeHzEditField
        function FeHzEditFieldValueChanged(app, event)
            value = app.FeHzEditField.Value;
            app.sendPID();
        end

        % Button pushed function: StartButton
        function StartButtonPushed(app, event)
            if((app.FeHzEditField_2.Value >= 10) & (app.FeHzEditField_2.Value <= 10000))
                if app.connected
                    if strcmp(app.TabGroup.SelectedTab.Title,'OpenLoop')
                        app.dataS = zeros(1,256);
                        app.dataX = zeros(1,256);
                        app.dataY = zeros(1,256);
                        sendingData = strcat("S_"+app.XminEditField.Value+"_"+app.XmaxEditField.Value+ ...
                            "_"+app.YminEditField.Value+"_"+app.YmaxEditField.Value+"_"+ ...
                            app.FeHzEditField_2.Value+"_!");
                        app.DataLabel.Text = sendingData;
                        writeline(app.s, sendingData); 
                    end
                end
            else
                msgbox('Warning ! Sampling frequency must be between 10Hz to 10kHz !');
            end
        end

        % Button pushed function: SetYMaxButton
        function SetYMaxButtonPushed(app, event)
            app.YmaxEditField.Value = num2str(app.YSlider.Value);
        end

        % Button pushed function: SetYMinButton
        function SetYMinButtonPushed(app, event)
            app.YminEditField.Value = num2str(app.YSlider.Value);
        end

        % Button pushed function: SetXMaxButton
        function SetXMaxButtonPushed(app, event)
            app.XmaxEditField.Value = num2str(app.XSlider.Value);
        end

        % Button pushed function: SetXMinButton
        function SetXMinButtonPushed(app, event)
            app.XminEditField.Value = num2str(app.XSlider.Value);
        end

        % Button pushed function: SettoInitialPositionButton
        function SettoInitialPositionButtonPushed(app, event)
            sendingData = strcat("M_0_0_!");
            app.XValueLabel.Text = '0';
            app.YValueLabel.Text = '0';
            app.XSlider.Value = 0;
            app.YSlider.Value = 0;
            if(app.connected) writeline(app.s, sendingData);
            else msgbox('You must be connected to the board...','Error','error');  end;
        end

        % Button pushed function: RefreshListButton
        function RefreshListButtonPushed(app, event)
            app.PortDropDown.Items = serialportlist();
        end

        % Value changing function: GainYSlider
        function GainYSliderValueChanging(app, event)
            changingValue = event.Value;
            value = round(changingValue)*str2num(app.xGainyDropDown.Value);
            if(app.InvertYCheckBox_3.Value) valueY = -value; 
            else valueY = value; end;
            app.YGainValueLabel.Text = num2str(valueY);
            if(app.AutoCheckBox.Value) app.sendPID(); end;
        end

        % Value changing function: IntegralYSlider
        function IntegralYSliderValueChanging(app, event)
            changingValue = event.Value;
            value = round(changingValue)*str2num(app.xIntegralyDropDown.Value);
            if(app.InvertYCheckBox_3.Value) valueY = -value; 
            else valueY = value; end;
            app.YIntegralValueLabel.Text = num2str(valueY);
            if(app.AutoCheckBox.Value) app.sendPID(); end;
        end

        % Value changing function: DerivativeYSlider
        function DerivativeYSliderValueChanging(app, event)
            changingValue = event.Value;
            value = round(changingValue)*str2num(app.xDerivativeyDropDown.Value);
            if(app.InvertYCheckBox_3.Value) valueY = -value; 
            else valueY = value; end;
            app.YDerivativeValueLabel.Text = num2str(valueY);
            if(app.AutoCheckBox.Value) app.sendPID(); end;
        end

        % Value changed function: xGainyDropDown
        function xGainyDropDownValueChanged(app, event)
            value = round(app.GainYSlider.Value)*str2num(app.xGainyDropDown.Value);
            if(app.InvertYCheckBox_3.Value) valueY = -value; 
            else valueY = value; end;
            app.YGainValueLabel.Text = num2str(valueY);
            if(app.AutoCheckBox.Value) app.sendPID(); end;
        end

        % Value changed function: xIntegralyDropDown
        function xIntegralyDropDownValueChanged(app, event)
            value = round(app.IntegralYSlider.Value)*str2num(app.xIntegralyDropDown.Value);
            if(app.InvertYCheckBox_3.Value) valueY = -value; 
            else valueY = value; end;
            app.YIntegralValueLabel.Text = num2str(valueY);
            if(app.AutoCheckBox.Value) app.sendPID(); end;
        end

        % Value changed function: xDerivativeyDropDown
        function xDerivativeyDropDownValueChanged(app, event)
            value = round(app.DerivativeYSlider.Value)*str2num(app.xDerivativeyDropDown.Value);
            if(app.InvertYCheckBox_3.Value) valueY = -value; 
            else valueY = value; end;
            app.YDerivativeValueLabel.Text = num2str(valueY);
            if(app.AutoCheckBox.Value) app.sendPID(); end;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 1071 641];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
            app.UIFigure.KeyPressFcn = createCallbackFcn(app, @UIFigureKeyPress, true);

            % Create GridLayout
            app.GridLayout = uigridlayout(app.UIFigure);
            app.GridLayout.ColumnWidth = {'3x', '1x'};
            app.GridLayout.RowHeight = {'12x', '1x'};

            % Create TabGroup
            app.TabGroup = uitabgroup(app.GridLayout);
            app.TabGroup.Layout.Row = 1;
            app.TabGroup.Layout.Column = [1 2];

            % Create ConnectionTab
            app.ConnectionTab = uitab(app.TabGroup);
            app.ConnectionTab.Title = 'Connection';

            % Create PortDropDownLabel
            app.PortDropDownLabel = uilabel(app.ConnectionTab);
            app.PortDropDownLabel.HorizontalAlignment = 'right';
            app.PortDropDownLabel.FontSize = 16;
            app.PortDropDownLabel.Position = [62 444 35 22];
            app.PortDropDownLabel.Text = 'Port';

            % Create PortDropDown
            app.PortDropDown = uidropdown(app.ConnectionTab);
            app.PortDropDown.FontSize = 16;
            app.PortDropDown.Position = [112 444 222 22];

            % Create ConnectionButton
            app.ConnectionButton = uibutton(app.ConnectionTab, 'push');
            app.ConnectionButton.ButtonPushedFcn = createCallbackFcn(app, @ConnectionButtonPushed, true);
            app.ConnectionButton.FontSize = 16;
            app.ConnectionButton.FontWeight = 'bold';
            app.ConnectionButton.FontColor = [1 0 0];
            app.ConnectionButton.Position = [54 399 280 26];
            app.ConnectionButton.Text = 'Connection';

            % Create RefreshListButton
            app.RefreshListButton = uibutton(app.ConnectionTab, 'push');
            app.RefreshListButton.ButtonPushedFcn = createCallbackFcn(app, @RefreshListButtonPushed, true);
            app.RefreshListButton.FontSize = 16;
            app.RefreshListButton.FontWeight = 'bold';
            app.RefreshListButton.FontColor = [0.149 0.149 0.149];
            app.RefreshListButton.Position = [50 26 280 26];
            app.RefreshListButton.Text = 'Refresh List';

            % Create Image
            app.Image = uiimage(app.ConnectionTab);
            app.Image.Position = [412 193 515 270];
            app.Image.ImageSource = 'systBoucle.png';

            % Create AsservissementenpositiondunLaseren2DLabel
            app.AsservissementenpositiondunLaseren2DLabel = uilabel(app.ConnectionTab);
            app.AsservissementenpositiondunLaseren2DLabel.FontSize = 20;
            app.AsservissementenpositiondunLaseren2DLabel.FontWeight = 'bold';
            app.AsservissementenpositiondunLaseren2DLabel.FontColor = [0 0.4471 0.7412];
            app.AsservissementenpositiondunLaseren2DLabel.Position = [489 492 445 25];
            app.AsservissementenpositiondunLaseren2DLabel.Text = 'Asservissement en position d''un Laser en 2D';

            % Create Image3
            app.Image3 = uiimage(app.ConnectionTab);
            app.Image3.Position = [402 66 176 105];
            app.Image3.ImageSource = 'bad.jpg';

            % Create BadfeedbackLabel
            app.BadfeedbackLabel = uilabel(app.ConnectionTab);
            app.BadfeedbackLabel.HorizontalAlignment = 'center';
            app.BadfeedbackLabel.Position = [450 35 79 22];
            app.BadfeedbackLabel.Text = 'Bad feedback';

            % Create Image3_2
            app.Image3_2 = uiimage(app.ConnectionTab);
            app.Image3_2.Position = [669 66 176 105];
            app.Image3_2.ImageSource = 'better.jpg';

            % Create GoodcontrolLabel
            app.GoodcontrolLabel = uilabel(app.ConnectionTab);
            app.GoodcontrolLabel.HorizontalAlignment = 'center';
            app.GoodcontrolLabel.Position = [720 40 74 22];
            app.GoodcontrolLabel.Text = 'Good control';

            % Create Image4
            app.Image4 = uiimage(app.ConnectionTab);
            app.Image4.Position = [865 7 175 102];
            app.Image4.ImageSource = 'goodtoo.jpg';

            % Create PhotodetectorTab
            app.PhotodetectorTab = uitab(app.TabGroup);
            app.PhotodetectorTab.Title = 'Photodetector';

            % Create GridLayout2
            app.GridLayout2 = uigridlayout(app.PhotodetectorTab);
            app.GridLayout2.ColumnWidth = {'3x', '1x'};
            app.GridLayout2.RowHeight = {'1x'};

            % Create GridLayout3
            app.GridLayout3 = uigridlayout(app.GridLayout2);
            app.GridLayout3.ColumnWidth = {'1x'};
            app.GridLayout3.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x'};
            app.GridLayout3.Layout.Row = 1;
            app.GridLayout3.Layout.Column = 2;

            % Create InvertXCheckBox
            app.InvertXCheckBox = uicheckbox(app.GridLayout3);
            app.InvertXCheckBox.Text = 'Invert X';
            app.InvertXCheckBox.Layout.Row = 1;
            app.InvertXCheckBox.Layout.Column = 1;

            % Create InvertYCheckBox
            app.InvertYCheckBox = uicheckbox(app.GridLayout3);
            app.InvertYCheckBox.Text = 'Invert Y';
            app.InvertYCheckBox.Layout.Row = 2;
            app.InvertYCheckBox.Layout.Column = 1;

            % Create GetButton
            app.GetButton = uibutton(app.GridLayout3, 'push');
            app.GetButton.ButtonPushedFcn = createCallbackFcn(app, @GetButtonPushed, true);
            app.GetButton.Layout.Row = 4;
            app.GetButton.Layout.Column = 1;
            app.GetButton.Text = 'Get';

            % Create StopButton
            app.StopButton = uibutton(app.GridLayout3, 'push');
            app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
            app.StopButton.Layout.Row = 5;
            app.StopButton.Layout.Column = 1;
            app.StopButton.Text = 'Stop';

            % Create Image2
            app.Image2 = uiimage(app.GridLayout3);
            app.Image2.Layout.Row = 3;
            app.Image2.Layout.Column = 1;
            app.Image2.ImageSource = '4q_photodiode.png';

            % Create UIAxes
            app.UIAxes = uiaxes(app.GridLayout2);
            title(app.UIAxes, 'Photodiode')
            xlabel(app.UIAxes, 'X')
            ylabel(app.UIAxes, 'Y')
            app.UIAxes.XLim = [-100 100];
            app.UIAxes.YLim = [-100 100];
            app.UIAxes.XGrid = 'on';
            app.UIAxes.YGrid = 'on';
            app.UIAxes.Layout.Row = 1;
            app.UIAxes.Layout.Column = 1;

            % Create MotorTab
            app.MotorTab = uitab(app.TabGroup);
            app.MotorTab.Title = 'Motor';

            % Create GridLayout4
            app.GridLayout4 = uigridlayout(app.MotorTab);
            app.GridLayout4.ColumnWidth = {'2x', '1x', '2x'};
            app.GridLayout4.RowHeight = {'1x'};

            % Create GridLayout5
            app.GridLayout5 = uigridlayout(app.GridLayout4);
            app.GridLayout5.ColumnWidth = {'1x'};
            app.GridLayout5.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x'};
            app.GridLayout5.Layout.Row = 1;
            app.GridLayout5.Layout.Column = 2;

            % Create InvertXCheckBox_2
            app.InvertXCheckBox_2 = uicheckbox(app.GridLayout5);
            app.InvertXCheckBox_2.Text = 'Invert X';
            app.InvertXCheckBox_2.Layout.Row = 1;
            app.InvertXCheckBox_2.Layout.Column = 1;

            % Create InvertYCheckBox_2
            app.InvertYCheckBox_2 = uicheckbox(app.GridLayout5);
            app.InvertYCheckBox_2.Text = 'Invert Y';
            app.InvertYCheckBox_2.Layout.Row = 2;
            app.InvertYCheckBox_2.Layout.Column = 1;

            % Create StopButton_2
            app.StopButton_2 = uibutton(app.GridLayout5, 'push');
            app.StopButton_2.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
            app.StopButton_2.Layout.Row = 6;
            app.StopButton_2.Layout.Column = 1;
            app.StopButton_2.Text = 'Stop';

            % Create SettoInitialPositionButton
            app.SettoInitialPositionButton = uibutton(app.GridLayout5, 'push');
            app.SettoInitialPositionButton.ButtonPushedFcn = createCallbackFcn(app, @SettoInitialPositionButtonPushed, true);
            app.SettoInitialPositionButton.FontSize = 15;
            app.SettoInitialPositionButton.FontWeight = 'bold';
            app.SettoInitialPositionButton.Layout.Row = 4;
            app.SettoInitialPositionButton.Layout.Column = 1;
            app.SettoInitialPositionButton.Text = 'Set to Initial Position';

            % Create MotorPanel
            app.MotorPanel = uipanel(app.GridLayout4);
            app.MotorPanel.Title = 'Motor';
            app.MotorPanel.Layout.Row = 1;
            app.MotorPanel.Layout.Column = 1;

            % Create XSliderLabel
            app.XSliderLabel = uilabel(app.MotorPanel);
            app.XSliderLabel.HorizontalAlignment = 'right';
            app.XSliderLabel.FontSize = 15;
            app.XSliderLabel.FontWeight = 'bold';
            app.XSliderLabel.Position = [73 211 25 22];
            app.XSliderLabel.Text = 'X';

            % Create XSlider
            app.XSlider = uislider(app.MotorPanel);
            app.XSlider.Limits = [-1 1];
            app.XSlider.MajorTicks = [-1 -0.6 -0.2 0 0.2 0.6 1];
            app.XSlider.ValueChangedFcn = createCallbackFcn(app, @XSliderValueChanged, true);
            app.XSlider.ValueChangingFcn = createCallbackFcn(app, @XSliderValueChanging, true);
            app.XSlider.Position = [126 258 222 3];

            % Create XValueLabel
            app.XValueLabel = uilabel(app.MotorPanel);
            app.XValueLabel.HorizontalAlignment = 'center';
            app.XValueLabel.Position = [73 182 43 22];
            app.XValueLabel.Text = 'XValue';

            % Create YSliderLabel
            app.YSliderLabel = uilabel(app.MotorPanel);
            app.YSliderLabel.HorizontalAlignment = 'right';
            app.YSliderLabel.FontSize = 15;
            app.YSliderLabel.FontWeight = 'bold';
            app.YSliderLabel.Position = [15 252 25 22];
            app.YSliderLabel.Text = 'Y';

            % Create YSlider
            app.YSlider = uislider(app.MotorPanel);
            app.YSlider.Limits = [-1 1];
            app.YSlider.MajorTicks = [-1 -0.6 -0.2 0 0.2 0.6 1];
            app.YSlider.Orientation = 'vertical';
            app.YSlider.ValueChangedFcn = createCallbackFcn(app, @XSliderValueChanged, true);
            app.YSlider.ValueChangingFcn = createCallbackFcn(app, @YSliderValueChanging, true);
            app.YSlider.Position = [15 289 3 192];

            % Create YValueLabel
            app.YValueLabel = uilabel(app.MotorPanel);
            app.YValueLabel.HorizontalAlignment = 'center';
            app.YValueLabel.Position = [10 228 43 22];
            app.YValueLabel.Text = 'YValue';

            % Create SetXMaxButton
            app.SetXMaxButton = uibutton(app.MotorPanel, 'push');
            app.SetXMaxButton.ButtonPushedFcn = createCallbackFcn(app, @SetXMaxButtonPushed, true);
            app.SetXMaxButton.Position = [270 63 100 22];
            app.SetXMaxButton.Text = 'SetXMax';

            % Create SetXMinButton
            app.SetXMinButton = uibutton(app.MotorPanel, 'push');
            app.SetXMinButton.ButtonPushedFcn = createCallbackFcn(app, @SetXMinButtonPushed, true);
            app.SetXMinButton.Position = [270 29 100 22];
            app.SetXMinButton.Text = 'SetXMin';

            % Create SetYMaxButton
            app.SetYMaxButton = uibutton(app.MotorPanel, 'push');
            app.SetYMaxButton.ButtonPushedFcn = createCallbackFcn(app, @SetYMaxButtonPushed, true);
            app.SetYMaxButton.Position = [39 63 100 22];
            app.SetYMaxButton.Text = 'SetYMax';

            % Create SetYMinButton
            app.SetYMinButton = uibutton(app.MotorPanel, 'push');
            app.SetYMinButton.ButtonPushedFcn = createCallbackFcn(app, @SetYMinButtonPushed, true);
            app.SetYMinButton.Position = [39 29 100 22];
            app.SetYMinButton.Text = 'SetYMin';

            % Create MultiplicatorDropDownLabel
            app.MultiplicatorDropDownLabel = uilabel(app.MotorPanel);
            app.MultiplicatorDropDownLabel.HorizontalAlignment = 'right';
            app.MultiplicatorDropDownLabel.Position = [163 161 70 22];
            app.MultiplicatorDropDownLabel.Text = 'Multiplicator';

            % Create MultiplicatorDropDown
            app.MultiplicatorDropDown = uidropdown(app.MotorPanel);
            app.MultiplicatorDropDown.Items = {'1', '10', '100'};
            app.MultiplicatorDropDown.Position = [248 161 100 22];
            app.MultiplicatorDropDown.Value = '1';

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.MotorPanel);
            title(app.UIAxes2, 'Motor')
            xlabel(app.UIAxes2, 'X')
            ylabel(app.UIAxes2, 'Y')
            app.UIAxes2.Position = [97 269 222 246];

            % Create PhotodetectorPanel
            app.PhotodetectorPanel = uipanel(app.GridLayout4);
            app.PhotodetectorPanel.Title = 'Photodetector';
            app.PhotodetectorPanel.Layout.Row = 1;
            app.PhotodetectorPanel.Layout.Column = 3;

            % Create UIAxes3
            app.UIAxes3 = uiaxes(app.PhotodetectorPanel);
            title(app.UIAxes3, 'Photodetector')
            xlabel(app.UIAxes3, 'X')
            ylabel(app.UIAxes3, 'Y')
            app.UIAxes3.Position = [1 197 401 318];

            % Create OpenLoopTab
            app.OpenLoopTab = uitab(app.TabGroup);
            app.OpenLoopTab.Title = 'OpenLoop';

            % Create GridLayout7
            app.GridLayout7 = uigridlayout(app.OpenLoopTab);
            app.GridLayout7.ColumnWidth = {'1x'};
            app.GridLayout7.RowHeight = {'8x', '1x'};

            % Create GridLayout8
            app.GridLayout8 = uigridlayout(app.GridLayout7);
            app.GridLayout8.ColumnWidth = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
            app.GridLayout8.RowHeight = {'1x'};
            app.GridLayout8.Layout.Row = 2;
            app.GridLayout8.Layout.Column = 1;

            % Create StartButton
            app.StartButton = uibutton(app.GridLayout8, 'push');
            app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
            app.StartButton.Layout.Row = 1;
            app.StartButton.Layout.Column = 11;
            app.StartButton.Text = 'Start';

            % Create XminEditFieldLabel
            app.XminEditFieldLabel = uilabel(app.GridLayout8);
            app.XminEditFieldLabel.HorizontalAlignment = 'right';
            app.XminEditFieldLabel.Layout.Row = 1;
            app.XminEditFieldLabel.Layout.Column = 1;
            app.XminEditFieldLabel.Text = 'Xmin';

            % Create XminEditField
            app.XminEditField = uieditfield(app.GridLayout8, 'text');
            app.XminEditField.Layout.Row = 1;
            app.XminEditField.Layout.Column = 2;

            % Create XmaxEditFieldLabel
            app.XmaxEditFieldLabel = uilabel(app.GridLayout8);
            app.XmaxEditFieldLabel.HorizontalAlignment = 'right';
            app.XmaxEditFieldLabel.Layout.Row = 1;
            app.XmaxEditFieldLabel.Layout.Column = 3;
            app.XmaxEditFieldLabel.Text = 'Xmax';

            % Create XmaxEditField
            app.XmaxEditField = uieditfield(app.GridLayout8, 'text');
            app.XmaxEditField.Layout.Row = 1;
            app.XmaxEditField.Layout.Column = 4;

            % Create YminEditFieldLabel
            app.YminEditFieldLabel = uilabel(app.GridLayout8);
            app.YminEditFieldLabel.HorizontalAlignment = 'right';
            app.YminEditFieldLabel.Layout.Row = 1;
            app.YminEditFieldLabel.Layout.Column = 5;
            app.YminEditFieldLabel.Text = 'Ymin';

            % Create YminEditField
            app.YminEditField = uieditfield(app.GridLayout8, 'text');
            app.YminEditField.Layout.Row = 1;
            app.YminEditField.Layout.Column = 6;

            % Create YmaxEditFieldLabel
            app.YmaxEditFieldLabel = uilabel(app.GridLayout8);
            app.YmaxEditFieldLabel.HorizontalAlignment = 'right';
            app.YmaxEditFieldLabel.Layout.Row = 1;
            app.YmaxEditFieldLabel.Layout.Column = 7;
            app.YmaxEditFieldLabel.Text = 'Ymax';

            % Create YmaxEditField
            app.YmaxEditField = uieditfield(app.GridLayout8, 'text');
            app.YmaxEditField.Layout.Row = 1;
            app.YmaxEditField.Layout.Column = 8;

            % Create FeHzEditField_2Label
            app.FeHzEditField_2Label = uilabel(app.GridLayout8);
            app.FeHzEditField_2Label.HorizontalAlignment = 'right';
            app.FeHzEditField_2Label.Layout.Row = 1;
            app.FeHzEditField_2Label.Layout.Column = 9;
            app.FeHzEditField_2Label.Text = 'Fe (Hz)';

            % Create FeHzEditField_2
            app.FeHzEditField_2 = uieditfield(app.GridLayout8, 'text');
            app.FeHzEditField_2.Layout.Row = 1;
            app.FeHzEditField_2.Layout.Column = 10;

            % Create UIAxes4
            app.UIAxes4 = uiaxes(app.GridLayout7);
            title(app.UIAxes4, 'Step Response')
            xlabel(app.UIAxes4, 'Time')
            ylabel(app.UIAxes4, 'Voltage')
            app.UIAxes4.Layout.Row = 1;
            app.UIAxes4.Layout.Column = 1;

            % Create PIDTab
            app.PIDTab = uitab(app.TabGroup);
            app.PIDTab.Title = 'PID';

            % Create GridLayout6
            app.GridLayout6 = uigridlayout(app.PIDTab);
            app.GridLayout6.ColumnWidth = {'1x', '3x', '1x', '1x', '3x', '1x'};
            app.GridLayout6.RowHeight = {'1x', '2x', '1x', '1x', '2x', '1x', '1x', '2x', '1x', '1x', '1x', '1x'};

            % Create GainXSliderLabel
            app.GainXSliderLabel = uilabel(app.GridLayout6);
            app.GainXSliderLabel.HorizontalAlignment = 'right';
            app.GainXSliderLabel.Layout.Row = 2;
            app.GainXSliderLabel.Layout.Column = 1;
            app.GainXSliderLabel.Text = 'Gain X';

            % Create GainXSlider
            app.GainXSlider = uislider(app.GridLayout6);
            app.GainXSlider.ValueChangingFcn = createCallbackFcn(app, @GainXSliderValueChanging, true);
            app.GainXSlider.Layout.Row = 2;
            app.GainXSlider.Layout.Column = 2;

            % Create xGainxDropDownLabel
            app.xGainxDropDownLabel = uilabel(app.GridLayout6);
            app.xGainxDropDownLabel.HorizontalAlignment = 'right';
            app.xGainxDropDownLabel.Layout.Row = 3;
            app.xGainxDropDownLabel.Layout.Column = 1;
            app.xGainxDropDownLabel.Text = 'x Gain (x)';

            % Create xGainxDropDown
            app.xGainxDropDown = uidropdown(app.GridLayout6);
            app.xGainxDropDown.Items = {'1e-4', '1e-2', '1', '1e2', '1e4'};
            app.xGainxDropDown.ValueChangedFcn = createCallbackFcn(app, @xGainxDropDownValueChanged, true);
            app.xGainxDropDown.Layout.Row = 3;
            app.xGainxDropDown.Layout.Column = 2;
            app.xGainxDropDown.Value = '1e-4';

            % Create InvertXCheckBox_3
            app.InvertXCheckBox_3 = uicheckbox(app.GridLayout6);
            app.InvertXCheckBox_3.Text = 'Invert X';
            app.InvertXCheckBox_3.Layout.Row = 11;
            app.InvertXCheckBox_3.Layout.Column = 2;

            % Create InvertYCheckBox_3
            app.InvertYCheckBox_3 = uicheckbox(app.GridLayout6);
            app.InvertYCheckBox_3.Text = 'Invert Y';
            app.InvertYCheckBox_3.Layout.Row = 11;
            app.InvertYCheckBox_3.Layout.Column = 5;

            % Create GainYSliderLabel
            app.GainYSliderLabel = uilabel(app.GridLayout6);
            app.GainYSliderLabel.HorizontalAlignment = 'right';
            app.GainYSliderLabel.Layout.Row = 2;
            app.GainYSliderLabel.Layout.Column = 4;
            app.GainYSliderLabel.Text = 'Gain Y';

            % Create GainYSlider
            app.GainYSlider = uislider(app.GridLayout6);
            app.GainYSlider.ValueChangingFcn = createCallbackFcn(app, @GainYSliderValueChanging, true);
            app.GainYSlider.Layout.Row = 2;
            app.GainYSlider.Layout.Column = 5;

            % Create xGainyDropDownLabel
            app.xGainyDropDownLabel = uilabel(app.GridLayout6);
            app.xGainyDropDownLabel.HorizontalAlignment = 'right';
            app.xGainyDropDownLabel.Layout.Row = 3;
            app.xGainyDropDownLabel.Layout.Column = 4;
            app.xGainyDropDownLabel.Text = 'x Gain (y)';

            % Create xGainyDropDown
            app.xGainyDropDown = uidropdown(app.GridLayout6);
            app.xGainyDropDown.Items = {'1e-4', '1e-2', '1', '1e2', '1e4'};
            app.xGainyDropDown.ValueChangedFcn = createCallbackFcn(app, @xGainyDropDownValueChanged, true);
            app.xGainyDropDown.Layout.Row = 3;
            app.xGainyDropDown.Layout.Column = 5;
            app.xGainyDropDown.Value = '1e-4';

            % Create IntegralXSliderLabel
            app.IntegralXSliderLabel = uilabel(app.GridLayout6);
            app.IntegralXSliderLabel.HorizontalAlignment = 'right';
            app.IntegralXSliderLabel.Layout.Row = 5;
            app.IntegralXSliderLabel.Layout.Column = 1;
            app.IntegralXSliderLabel.Text = 'Integral X';

            % Create IntegralXSlider
            app.IntegralXSlider = uislider(app.GridLayout6);
            app.IntegralXSlider.ValueChangingFcn = createCallbackFcn(app, @IntegralXSliderValueChanging, true);
            app.IntegralXSlider.Layout.Row = 5;
            app.IntegralXSlider.Layout.Column = 2;

            % Create xIntegralxDropDownLabel
            app.xIntegralxDropDownLabel = uilabel(app.GridLayout6);
            app.xIntegralxDropDownLabel.HorizontalAlignment = 'right';
            app.xIntegralxDropDownLabel.Layout.Row = 6;
            app.xIntegralxDropDownLabel.Layout.Column = 1;
            app.xIntegralxDropDownLabel.Text = 'x Integral (x)';

            % Create xIntegralxDropDown
            app.xIntegralxDropDown = uidropdown(app.GridLayout6);
            app.xIntegralxDropDown.Items = {'1e-4', '1e-2', '1', '1e2', '1e4'};
            app.xIntegralxDropDown.ValueChangedFcn = createCallbackFcn(app, @xIntegralxDropDownValueChanged, true);
            app.xIntegralxDropDown.Layout.Row = 6;
            app.xIntegralxDropDown.Layout.Column = 2;
            app.xIntegralxDropDown.Value = '1e-4';

            % Create xIntegralyDropDownLabel
            app.xIntegralyDropDownLabel = uilabel(app.GridLayout6);
            app.xIntegralyDropDownLabel.HorizontalAlignment = 'right';
            app.xIntegralyDropDownLabel.Layout.Row = 6;
            app.xIntegralyDropDownLabel.Layout.Column = 4;
            app.xIntegralyDropDownLabel.Text = 'x Integral (y)';

            % Create xIntegralyDropDown
            app.xIntegralyDropDown = uidropdown(app.GridLayout6);
            app.xIntegralyDropDown.Items = {'1e-4', '1e-2', '1', '1e2', '1e4'};
            app.xIntegralyDropDown.ValueChangedFcn = createCallbackFcn(app, @xIntegralyDropDownValueChanged, true);
            app.xIntegralyDropDown.Layout.Row = 6;
            app.xIntegralyDropDown.Layout.Column = 5;
            app.xIntegralyDropDown.Value = '1e-4';

            % Create IntegralYSliderLabel
            app.IntegralYSliderLabel = uilabel(app.GridLayout6);
            app.IntegralYSliderLabel.HorizontalAlignment = 'right';
            app.IntegralYSliderLabel.Layout.Row = 5;
            app.IntegralYSliderLabel.Layout.Column = 4;
            app.IntegralYSliderLabel.Text = 'Integral Y';

            % Create IntegralYSlider
            app.IntegralYSlider = uislider(app.GridLayout6);
            app.IntegralYSlider.ValueChangingFcn = createCallbackFcn(app, @IntegralYSliderValueChanging, true);
            app.IntegralYSlider.Layout.Row = 5;
            app.IntegralYSlider.Layout.Column = 5;

            % Create XGainValueLabel
            app.XGainValueLabel = uilabel(app.GridLayout6);
            app.XGainValueLabel.HorizontalAlignment = 'center';
            app.XGainValueLabel.FontWeight = 'bold';
            app.XGainValueLabel.Layout.Row = 2;
            app.XGainValueLabel.Layout.Column = 3;
            app.XGainValueLabel.Text = 'XGainValue';

            % Create YGainValueLabel
            app.YGainValueLabel = uilabel(app.GridLayout6);
            app.YGainValueLabel.HorizontalAlignment = 'center';
            app.YGainValueLabel.FontWeight = 'bold';
            app.YGainValueLabel.Layout.Row = 2;
            app.YGainValueLabel.Layout.Column = 6;
            app.YGainValueLabel.Text = 'YGainValue';

            % Create XIntegralValueLabel
            app.XIntegralValueLabel = uilabel(app.GridLayout6);
            app.XIntegralValueLabel.HorizontalAlignment = 'center';
            app.XIntegralValueLabel.FontWeight = 'bold';
            app.XIntegralValueLabel.Layout.Row = 5;
            app.XIntegralValueLabel.Layout.Column = 3;
            app.XIntegralValueLabel.Text = 'XIntegralValue';

            % Create YIntegralValueLabel
            app.YIntegralValueLabel = uilabel(app.GridLayout6);
            app.YIntegralValueLabel.HorizontalAlignment = 'center';
            app.YIntegralValueLabel.FontWeight = 'bold';
            app.YIntegralValueLabel.Layout.Row = 5;
            app.YIntegralValueLabel.Layout.Column = 6;
            app.YIntegralValueLabel.Text = 'YIntegralValue';

            % Create DerivativeXSliderLabel
            app.DerivativeXSliderLabel = uilabel(app.GridLayout6);
            app.DerivativeXSliderLabel.HorizontalAlignment = 'right';
            app.DerivativeXSliderLabel.Layout.Row = 8;
            app.DerivativeXSliderLabel.Layout.Column = 1;
            app.DerivativeXSliderLabel.Text = 'Derivative X';

            % Create DerivativeXSlider
            app.DerivativeXSlider = uislider(app.GridLayout6);
            app.DerivativeXSlider.ValueChangingFcn = createCallbackFcn(app, @DerivativeXSliderValueChanging, true);
            app.DerivativeXSlider.Layout.Row = 8;
            app.DerivativeXSlider.Layout.Column = 2;

            % Create DerivativeYSliderLabel
            app.DerivativeYSliderLabel = uilabel(app.GridLayout6);
            app.DerivativeYSliderLabel.HorizontalAlignment = 'right';
            app.DerivativeYSliderLabel.Layout.Row = 8;
            app.DerivativeYSliderLabel.Layout.Column = 4;
            app.DerivativeYSliderLabel.Text = 'Derivative Y';

            % Create DerivativeYSlider
            app.DerivativeYSlider = uislider(app.GridLayout6);
            app.DerivativeYSlider.ValueChangingFcn = createCallbackFcn(app, @DerivativeYSliderValueChanging, true);
            app.DerivativeYSlider.Layout.Row = 8;
            app.DerivativeYSlider.Layout.Column = 5;

            % Create xDerivativexDropDownLabel
            app.xDerivativexDropDownLabel = uilabel(app.GridLayout6);
            app.xDerivativexDropDownLabel.HorizontalAlignment = 'right';
            app.xDerivativexDropDownLabel.Layout.Row = 9;
            app.xDerivativexDropDownLabel.Layout.Column = 1;
            app.xDerivativexDropDownLabel.Text = 'x Derivative (x)';

            % Create xDerivativexDropDown
            app.xDerivativexDropDown = uidropdown(app.GridLayout6);
            app.xDerivativexDropDown.Items = {'1e-4', '1e-2', '1', '1e2', '1e4'};
            app.xDerivativexDropDown.ValueChangedFcn = createCallbackFcn(app, @xDerivativexDropDownValueChanged, true);
            app.xDerivativexDropDown.Layout.Row = 9;
            app.xDerivativexDropDown.Layout.Column = 2;
            app.xDerivativexDropDown.Value = '1e-4';

            % Create xDerivativeyDropDownLabel
            app.xDerivativeyDropDownLabel = uilabel(app.GridLayout6);
            app.xDerivativeyDropDownLabel.HorizontalAlignment = 'right';
            app.xDerivativeyDropDownLabel.Layout.Row = 9;
            app.xDerivativeyDropDownLabel.Layout.Column = 4;
            app.xDerivativeyDropDownLabel.Text = 'x Derivative (y)';

            % Create xDerivativeyDropDown
            app.xDerivativeyDropDown = uidropdown(app.GridLayout6);
            app.xDerivativeyDropDown.Items = {'1e-4', '1e-2', '1', '1e2', '1e4'};
            app.xDerivativeyDropDown.ValueChangedFcn = createCallbackFcn(app, @xDerivativeyDropDownValueChanged, true);
            app.xDerivativeyDropDown.Layout.Row = 9;
            app.xDerivativeyDropDown.Layout.Column = 5;
            app.xDerivativeyDropDown.Value = '1e-4';

            % Create XDerivativeValueLabel
            app.XDerivativeValueLabel = uilabel(app.GridLayout6);
            app.XDerivativeValueLabel.HorizontalAlignment = 'center';
            app.XDerivativeValueLabel.FontWeight = 'bold';
            app.XDerivativeValueLabel.Layout.Row = 8;
            app.XDerivativeValueLabel.Layout.Column = 3;
            app.XDerivativeValueLabel.Text = 'XDerivativeValue';

            % Create YDerivativeValueLabel
            app.YDerivativeValueLabel = uilabel(app.GridLayout6);
            app.YDerivativeValueLabel.HorizontalAlignment = 'center';
            app.YDerivativeValueLabel.FontWeight = 'bold';
            app.YDerivativeValueLabel.Layout.Row = 8;
            app.YDerivativeValueLabel.Layout.Column = 6;
            app.YDerivativeValueLabel.Text = 'YDerivativeValue';

            % Create XYCheckBox
            app.XYCheckBox = uicheckbox(app.GridLayout6);
            app.XYCheckBox.ValueChangedFcn = createCallbackFcn(app, @XYCheckBoxValueChanged, true);
            app.XYCheckBox.Text = '|X|=|Y|';
            app.XYCheckBox.Layout.Row = 12;
            app.XYCheckBox.Layout.Column = 2;

            % Create FeHzEditFieldLabel
            app.FeHzEditFieldLabel = uilabel(app.GridLayout6);
            app.FeHzEditFieldLabel.HorizontalAlignment = 'right';
            app.FeHzEditFieldLabel.Layout.Row = 12;
            app.FeHzEditFieldLabel.Layout.Column = 4;
            app.FeHzEditFieldLabel.Text = 'Fe (Hz)';

            % Create FeHzEditField
            app.FeHzEditField = uieditfield(app.GridLayout6, 'text');
            app.FeHzEditField.ValueChangedFcn = createCallbackFcn(app, @FeHzEditFieldValueChanged, true);
            app.FeHzEditField.Layout.Row = 12;
            app.FeHzEditField.Layout.Column = 5;

            % Create SendButton
            app.SendButton = uibutton(app.GridLayout6, 'push');
            app.SendButton.ButtonPushedFcn = createCallbackFcn(app, @SendButtonPushed, true);
            app.SendButton.Layout.Row = 12;
            app.SendButton.Layout.Column = 6;
            app.SendButton.Text = 'Send';

            % Create AutoCheckBox
            app.AutoCheckBox = uicheckbox(app.GridLayout6);
            app.AutoCheckBox.Text = 'Auto';
            app.AutoCheckBox.Layout.Row = 12;
            app.AutoCheckBox.Layout.Column = 3;

            % Create ProportionnalControllerLabel
            app.ProportionnalControllerLabel = uilabel(app.GridLayout6);
            app.ProportionnalControllerLabel.BackgroundColor = [0.651 0.651 0.651];
            app.ProportionnalControllerLabel.HorizontalAlignment = 'center';
            app.ProportionnalControllerLabel.FontWeight = 'bold';
            app.ProportionnalControllerLabel.Layout.Row = 1;
            app.ProportionnalControllerLabel.Layout.Column = [1 6];
            app.ProportionnalControllerLabel.Text = 'Proportionnal Controller';

            % Create IntegralControllerLabel
            app.IntegralControllerLabel = uilabel(app.GridLayout6);
            app.IntegralControllerLabel.BackgroundColor = [0.651 0.651 0.651];
            app.IntegralControllerLabel.HorizontalAlignment = 'center';
            app.IntegralControllerLabel.FontWeight = 'bold';
            app.IntegralControllerLabel.Layout.Row = 4;
            app.IntegralControllerLabel.Layout.Column = [1 6];
            app.IntegralControllerLabel.Text = 'Integral Controller';

            % Create DerivativeControllerLabel
            app.DerivativeControllerLabel = uilabel(app.GridLayout6);
            app.DerivativeControllerLabel.BackgroundColor = [0.651 0.651 0.651];
            app.DerivativeControllerLabel.HorizontalAlignment = 'center';
            app.DerivativeControllerLabel.FontWeight = 'bold';
            app.DerivativeControllerLabel.Layout.Row = 7;
            app.DerivativeControllerLabel.Layout.Column = [1 6];
            app.DerivativeControllerLabel.Text = 'Derivative Controller';

            % Create GlobalparametersLabel
            app.GlobalparametersLabel = uilabel(app.GridLayout6);
            app.GlobalparametersLabel.BackgroundColor = [0.8 0.8 0.8];
            app.GlobalparametersLabel.HorizontalAlignment = 'center';
            app.GlobalparametersLabel.FontWeight = 'bold';
            app.GlobalparametersLabel.Layout.Row = 10;
            app.GlobalparametersLabel.Layout.Column = [1 6];
            app.GlobalparametersLabel.Text = 'Global parameters';

            % Create NumDataLabel
            app.NumDataLabel = uilabel(app.GridLayout);
            app.NumDataLabel.HorizontalAlignment = 'center';
            app.NumDataLabel.Layout.Row = 2;
            app.NumDataLabel.Layout.Column = 2;
            app.NumDataLabel.Text = 'NumData';

            % Create DataLabel
            app.DataLabel = uilabel(app.GridLayout);
            app.DataLabel.HorizontalAlignment = 'center';
            app.DataLabel.FontSize = 15;
            app.DataLabel.FontWeight = 'bold';
            app.DataLabel.Layout.Row = 2;
            app.DataLabel.Layout.Column = 1;
            app.DataLabel.Text = 'Data';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = pointage_laser_vBeta

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end