classdef pointage_laser_1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        Panel                          matlab.ui.container.Panel
        GridLayout                     matlab.ui.container.GridLayout
        GridLayout6                    matlab.ui.container.GridLayout
        GridLayout7                    matlab.ui.container.GridLayout
        ConnLamp                       matlab.ui.control.Lamp
        ConnectionLabel                matlab.ui.control.Label
        Help                           matlab.ui.control.Button
        GridLayout8                    matlab.ui.container.GridLayout
        NucleoBoardPanel               matlab.ui.container.Panel
        GridLayout11                   matlab.ui.container.GridLayout
        Refresh                        matlab.ui.control.Button
        PortLabel                      matlab.ui.control.Label
        PortList                       matlab.ui.control.DropDown
        GridLayout20                   matlab.ui.container.GridLayout
        GridLayout21                   matlab.ui.container.GridLayout
        FeenHzEditFieldLabel           matlab.ui.control.Label
        FeenHzEditField                matlab.ui.control.NumericEditField
        Frquencedchantillonnageentre1Hzet10kHzLabel  matlab.ui.control.Label
        BCon                           matlab.ui.control.Button
        NumDataLabel                   matlab.ui.control.Label
        GridLayout12                   matlab.ui.container.GridLayout
        TabGroup                       matlab.ui.container.TabGroup
        StartTab                       matlab.ui.container.Tab
        AlignmentTab                   matlab.ui.container.Tab
        StartAlignButton               matlab.ui.control.Button
        StopAlignButton                matlab.ui.control.Button
        UIAxes2_2                      matlab.ui.control.UIAxes
        MotorTab                       matlab.ui.container.Tab
        XmotorSliderLabel              matlab.ui.control.Label
        XmotorSlider                   matlab.ui.control.Slider
        YmotorSliderLabel              matlab.ui.control.Label
        YmotorSlider                   matlab.ui.control.Slider
        Button                         matlab.ui.control.Button
        UIAxes2                        matlab.ui.control.UIAxes
        UIAxes3                        matlab.ui.control.UIAxes
        OpenLoopTab                    matlab.ui.container.Tab
        GridLayout22                   matlab.ui.container.GridLayout
        StepResponseAcquisitionButton  matlab.ui.control.Button
        UIAxes                         matlab.ui.control.UIAxes
        PIDTab                         matlab.ui.container.Tab
        GridLayout18                   matlab.ui.container.GridLayout
        GridLayout19                   matlab.ui.container.GridLayout
        KpXEditFieldLabel              matlab.ui.control.Label
        KpXEditField                   matlab.ui.control.NumericEditField
        KpYEditFieldLabel              matlab.ui.control.Label
        KpYEditField                   matlab.ui.control.NumericEditField
        AxeYLabel                      matlab.ui.control.Label
        AxeXLabel                      matlab.ui.control.Label
        KiXEditFieldLabel              matlab.ui.control.Label
        KiXEditField                   matlab.ui.control.NumericEditField
        KiYEditFieldLabel              matlab.ui.control.Label
        KiYEditField                   matlab.ui.control.NumericEditField
        KdXEditFieldLabel              matlab.ui.control.Label
        KdXEditField                   matlab.ui.control.NumericEditField
        KdYEditFieldLabel              matlab.ui.control.Label
        KdYEditField                   matlab.ui.control.NumericEditField
        GainCheckBox                   matlab.ui.control.CheckBox
        IntegCheckBox                  matlab.ui.control.CheckBox
        DerivCheckBox                  matlab.ui.control.CheckBox
        FormeidaledePIDLabel           matlab.ui.control.Label
        SendingCoeffPButton            matlab.ui.control.Button
        ReceivedDataLabel              matlab.ui.control.Label
        SendedDataLabel                matlab.ui.control.Label
    end

    
    properties (Access = private)
        GREEN_COLOR = [0 1 0];
        RED_COLOR = [1 0 0];
        s % serial port
        color_plot % color plot handlers
        is_connected = false;
        k_pX = 1.0;
        k_pY = 1.0;
        k_iX = 0.0;
        k_iY = 0.0;
        k_dX = 0.0;
        k_dY = 0.0;
        k_Fe = 1e4;
        g_Ux=0.0;
        g_Uy=0.0;
        g_DetX=0.0;
        g_DetY=0.0;
        dataX = zeros(1,256);
        dataY = zeros(1,256);
        dataS = zeros(1,256);
        data_T = zeros(1,256);
    end
    
    methods (Access = private)
        
        function updateGUI(app, led_state)
            % plot color value in GUI
            app.color_plot.YData = [app.color_plot.YData(2:end) led_state];
            % change color of GUI lamp
            app.LEDLamp.Color = [0 led_state 0]; % scale color values to 0..1 range
        end
        
        function sendData(app, mode)
            if strcmp(app.TabGroup.SelectedTab.Title,'PID')
                switch mode
                    case 'P'
                        str_to_send = strcat('P_',num2str(app.k_pX),'_',num2str(app.k_pY),'_',num2str(app.k_Fe),'_!');
                        
                    case 'I'
                        str_to_send = strcat('I_',num2str(app.k_pX),'_',num2str(app.k_pY),'_',num2str(app.k_iX),'_',num2str(app.k_iY),'_',num2str(app.k_Fe),'_!');
                    case 'D'
                        str_to_send = strcat('D_',num2str(app.k_pX),'_',num2str(app.k_pY),'_',num2str(app.k_iX),'_',num2str(app.k_iY),'_',num2str(app.k_dX),'_',num2str(app.k_dY),'_',num2str(app.k_Fe),'_!');
                    case 'K'
                        str_to_send = 'P_1.0_1.0_10000_!';
                    otherwise
                        str_to_send = '0_!';
                end
            elseif strcmp(app.TabGroup.SelectedTab.Title,'OpenLoop')
                str_to_send = strcat('S_',num2str(app.k_Fe),'_!');
            elseif strcmp(app.TabGroup.SelectedTab.Title,'Alignment')
                str_to_send = strcat('A_!');
            elseif strcmp(app.TabGroup.SelectedTab.Title,'Motor')
                str_to_send = strcat('M_',num2str(app.XmotorSlider.Value),'_',num2str(app.YmotorSlider.Value),'_1_!');
            end
            app.SendedDataLabel.Text = str_to_send;
            app.NumDataLabel.Text = num2str(app.s.NumBytesAvailable)
            drawnow;
            writeline(app.s, str_to_send);
        end  
        
        function resetApp(app, ~)
            % Communications
            app.is_connected = false;
            app.PortList.Items = serialportlist();
            app.ReceivedDataLabel.Text = 'Disconnected';
            % Default Coefficients
            app.KpXEditField.Value = 0.5;
            app.KpYEditField.Value = 0.5;
            app.FeenHzEditField.Value = 1e4;
            % GUI
            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];
            app.YmotorSlider.Orientation='vertical';
            app.YmotorSlider.Limits=[-100 100];
            app.XmotorSlider.Limits=[-100 100];
            app.GainCheckBox.Value = true;
            app.IntegCheckBox.Value = false;
            app.DerivCheckBox.Value = false;
            app.KiXEditField.Enable = false;
            app.KiYEditField.Enable = false;
            app.KdXEditField.Enable = false;
            app.KdYEditField.Enable = false;
            app.KpXEditField.Value = app.k_pX;
            app.KpYEditField.Value = app.k_pY;
            app.KiXEditField.Value = app.k_iX;
            app.KiYEditField.Value = app.k_iY;
            app.KdXEditField.Value = app.k_dX;
            app.KdYEditField.Value = app.k_dY;
            app.FeenHzEditField.Value = app.k_Fe;
            app.XmotorSlider.Value = app.g_Ux;
            app.YmotorSlider.Value = app.g_Uy;
        end
        
        function boardCommIT(app, src, ~)
            rLine = readline(src);
            app.ReceivedDataLabel.Text = rLine;
            drawnow;
            
            rLineChar = char(rLine);
            if 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
                else
                    app.data_T = linspace(0,256/app.k_Fe-1/app.k_Fe, 256);
                    plot(app.UIAxes,app.data_T, app.dataS,'-r',app.data_T, app.dataX,'+b',app.data_T, app.dataY,'g');
                    drawnow;
                end                
            end
            if (rLineChar(1:1) == 'A')
                   stepC = sscanf(rLineChar, 'A_%lf_%lf_!');
                   app.g_DetX=stepC(1);
                   app.g_DetY=stepC(2);
                   plot(app.UIAxes2_2, app.g_DetX, app.g_DetY,'+b', 'MarkerSize',10, 'LineWidth',3); 
                   %drawnow;
            end
        end
    end
    

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

        % Code that executes after component creation
        function startupFcn(app)
            app.resetApp();
            app.data_T = linspace(0,256/app.k_Fe-1/app.k_Fe, 256);
            plot(app.UIAxes,app.data_T, app.dataS,'-r',app.data_T, app.dataX,'+b',app.data_T, app.dataY,'g');
        end

        % Callback function
        function BDisconButtonPushed(app, event)
            app.is_connected = false;
            clear app.s;
            app.ConnLamp.Color = app.RED_COLOR;
            app.ReceivedDataLabel.Text = 'Disconnected';
            drawnow;
        end

        % Button pushed function: BCon
        function BConButtonPushed(app, event)
            % on connect open serial port, start timer and change lamp color to green
            try
                app.s = serialport(app.PortList.Value, 115200);
                configureTerminator(app.s, "CR/LF");
                configureCallback(app.s, "terminator", @app.boardCommIT);
            catch
                warning('Problem using serial.');
            end
            app.is_connected = true;
            app.ReceivedDataLabel.Text = 'Connected';
            app.Refresh.Enable = false;
            app.BCon.Enable = false;
            drawnow;
            app.ConnLamp.Color = app.GREEN_COLOR;
        end

        % Button pushed function: Refresh
        function RefreshPushed(app, event)
            app.PortList.Items = serialportlist("available");
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            app.BDisconButtonPushed();
            delete(app);
        end

        % Button pushed function: Help
        function HelpButtonPushed(app, event)
            open pointage_laser.mlx;
        end

        % Button pushed function: SendingCoeffPButton
        function SendingCoeffPButtonPushed(app, event)
            app.k_pX = app.KpXEditField.Value;
            app.k_pY = app.KpYEditField.Value;
            app.k_iX = app.KiXEditField.Value;
            app.k_iY = app.KiYEditField.Value;
            app.k_dX = app.KdXEditField.Value;
            app.k_dY = app.KdYEditField.Value;
            if app.FeenHzEditField.Value >= 1 & app.FeenHzEditField.Value <= 10000
                app.k_Fe = app.FeenHzEditField.Value;
                if app.is_connected
                    if strcmp(app.TabGroup.SelectedTab.Title,'PID')
                        if app.DerivCheckBox.Value 
                            app.sendData('D');
                        elseif app.IntegCheckBox.Value
                            app.sendData('I');
                        elseif app.GainCheckBox.Value
                            app.sendData('P');
                        else
                            app.sendData('K');
                        end
                    end
                    drawnow;
                end
            else
                msgbox({'Sampling frequency is not in the good range.';'It must be between 1 Hz to 10 kHz.';'Nothing changed on the system.'},'Error','error','non-modal');
            end
            

        end

        % Value changed function: IntegCheckBox
        function IntegCheckBoxValueChanged(app, event)
            value = app.IntegCheckBox.Value;
            if value
                app.GainCheckBox.Value = true;
                app.KpXEditField.Enable = true;
                app.KpYEditField.Enable = true;
                app.KiXEditField.Enable = true;
                app.KiYEditField.Enable = true;
            else
                app.KiXEditField.Enable = false;
                app.KiYEditField.Enable = false;
                app.DerivCheckBox.Value = false;
                app.KdXEditField.Enable = false;
                app.KdYEditField.Enable = false;
            end
        end

        % Value changed function: DerivCheckBox
        function DerivCheckBoxValueChanged(app, event)
            value = app.DerivCheckBox.Value;
            if value
                app.GainCheckBox.Value = true;
                app.IntegCheckBox.Value = true;
                app.KpXEditField.Enable = true;
                app.KpYEditField.Enable = true;
                app.KiXEditField.Enable = true;
                app.KiYEditField.Enable = true;
                app.KdXEditField.Enable = true;
                app.KdYEditField.Enable = true;
            else
                app.KdXEditField.Enable = false;
                app.KdYEditField.Enable = false;
            end            
        end

        % Value changed function: GainCheckBox
        function GainCheckBoxValueChanged(app, event)
            value = app.GainCheckBox.Value;
            if value 
                app.KpXEditField.Enable = true;
                app.KpYEditField.Enable = true;
            else
                app.IntegCheckBox.Value = false;
                app.DerivCheckBox.Value = false;
                app.KpXEditField.Enable = false;
                app.KpYEditField.Enable = false;  
                app.KiXEditField.Enable = false;
                app.KiYEditField.Enable = false; 
                app.KdXEditField.Enable = false;
                app.KdYEditField.Enable = false;                 
            end
        end

        % Button pushed function: StepResponseAcquisitionButton
        function StepResponseAcquisitionButtonPushed(app, event)
            if app.FeenHzEditField.Value >= 1 & app.FeenHzEditField.Value <= 10000
                app.k_Fe = app.FeenHzEditField.Value;
                if app.is_connected
                    if strcmp(app.TabGroup.SelectedTab.Title,'OpenLoop')
                        app.dataS = zeros(1,256);
                        app.dataX = zeros(1,256);
                        app.dataY = zeros(1,256);
                        app.sendData('S');
                    end
                end
            else
                msgbox({'Sampling frequency is not in the good range.';'It must be between 1 Hz to 10 kHz.';'Nothing changed on the system.'},'Error','error','non-modal');
            end
        end

        % Button pushed function: Button
        function ButtonPushed(app, event)
            app.YmotorSlider.Value=0;
            app.XmotorSlider.Value=0;
            app.g_Ux = app.XmotorSlider.Value;
            app.g_Uy = app.YmotorSlider.Value;
            if app.is_connected
                if strcmp(app.TabGroup.SelectedTab.Title,'Motor')
                    plot(app.UIAxes3, app.g_Ux, app.g_Uy,'b+', 'MarkerSize',10, 'LineWidth',3);     
                    drawnow;
                    app.sendData('M');
                end
            end
        end

        % Value changed function: XmotorSlider
        function XmotorSliderValueChanging(app, event)
            app.g_Ux = app.XmotorSlider.Value;
            app.g_Uy = app.YmotorSlider.Value;
            if app.is_connected
                if strcmp(app.TabGroup.SelectedTab.Title,'Motor')
                    plot(app.UIAxes3, app.g_Ux, app.g_Uy,'b+', 'MarkerSize',10, 'LineWidth',3);     
                    drawnow;
                    app.sendData('M');
                end
            end
        end

        % Value changed function: YmotorSlider
        function YmotorSliderValueChanging(app, event)
            app.g_Ux = app.XmotorSlider.Value;
            app.g_Uy = app.YmotorSlider.Value;
            if app.is_connected
                if strcmp(app.TabGroup.SelectedTab.Title,'Motor')
                    plot(app.UIAxes3, app.g_Ux, app.g_Uy,'b+', 'MarkerSize',10, 'LineWidth',3);    
                    drawnow;
                    app.sendData('M');
                end
            end
        end

        % Button pushed function: StartAlignButton
        function StartAlignButtonPushed(app, event)
            if app.is_connected
                if strcmp(app.TabGroup.SelectedTab.Title,'Alignment')
                    app.sendData('A');
                end
            end
        end

        % Button pushed function: StopAlignButton
        function StopAlignButtonPushed(app, event)
            if app.is_connected
                if strcmp(app.TabGroup.SelectedTab.Title,'Alignment')
                    writeline(app.s, 'M_0.5_0.5_!');
                end
            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.AutoResizeChildren = 'off';
            app.UIFigure.Position = [100 100 770 501];
            app.UIFigure.Name = 'Pilotage Laser';
            app.UIFigure.Resize = 'off';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create Panel
            app.Panel = uipanel(app.UIFigure);
            app.Panel.AutoResizeChildren = 'off';
            app.Panel.Position = [1 5 770 497];

            % Create GridLayout
            app.GridLayout = uigridlayout(app.Panel);
            app.GridLayout.ColumnWidth = {'2x', '1x'};
            app.GridLayout.RowHeight = {'5x'};

            % Create GridLayout6
            app.GridLayout6 = uigridlayout(app.GridLayout);
            app.GridLayout6.ColumnWidth = {'1x'};
            app.GridLayout6.RowHeight = {'1x', '10x'};
            app.GridLayout6.ColumnSpacing = 0;
            app.GridLayout6.Padding = [0 0 0 0];
            app.GridLayout6.Layout.Row = 1;
            app.GridLayout6.Layout.Column = 2;

            % Create GridLayout7
            app.GridLayout7 = uigridlayout(app.GridLayout6);
            app.GridLayout7.ColumnWidth = {'1x', '6x', '3x'};
            app.GridLayout7.RowHeight = {'1x'};
            app.GridLayout7.RowSpacing = 20;
            app.GridLayout7.Padding = [10 0 10 0];
            app.GridLayout7.Layout.Row = 1;
            app.GridLayout7.Layout.Column = 1;

            % Create ConnLamp
            app.ConnLamp = uilamp(app.GridLayout7);
            app.ConnLamp.Layout.Row = 1;
            app.ConnLamp.Layout.Column = 1;
            app.ConnLamp.Color = [1 0 0];

            % Create ConnectionLabel
            app.ConnectionLabel = uilabel(app.GridLayout7);
            app.ConnectionLabel.Layout.Row = 1;
            app.ConnectionLabel.Layout.Column = 2;
            app.ConnectionLabel.Text = 'Connection';

            % Create Help
            app.Help = uibutton(app.GridLayout7, 'push');
            app.Help.ButtonPushedFcn = createCallbackFcn(app, @HelpButtonPushed, true);
            app.Help.Layout.Row = 1;
            app.Help.Layout.Column = 3;
            app.Help.Text = 'Help';

            % Create GridLayout8
            app.GridLayout8 = uigridlayout(app.GridLayout6);
            app.GridLayout8.ColumnWidth = {'1x'};
            app.GridLayout8.RowHeight = {'2x', '1x', '1x', '1x', '2x'};
            app.GridLayout8.ColumnSpacing = 0;
            app.GridLayout8.RowSpacing = 20;
            app.GridLayout8.Padding = [10 0 10 0];
            app.GridLayout8.Layout.Row = 2;
            app.GridLayout8.Layout.Column = 1;

            % Create NucleoBoardPanel
            app.NucleoBoardPanel = uipanel(app.GridLayout8);
            app.NucleoBoardPanel.AutoResizeChildren = 'off';
            app.NucleoBoardPanel.Title = 'Nucleo Board';
            app.NucleoBoardPanel.Layout.Row = 1;
            app.NucleoBoardPanel.Layout.Column = 1;

            % Create GridLayout11
            app.GridLayout11 = uigridlayout(app.NucleoBoardPanel);
            app.GridLayout11.ColumnWidth = {'1x', '3x'};
            app.GridLayout11.Padding = [5 5 5 5];

            % Create Refresh
            app.Refresh = uibutton(app.GridLayout11, 'push');
            app.Refresh.ButtonPushedFcn = createCallbackFcn(app, @RefreshPushed, true);
            app.Refresh.Layout.Row = 2;
            app.Refresh.Layout.Column = 2;
            app.Refresh.Text = 'Refresh';

            % Create PortLabel
            app.PortLabel = uilabel(app.GridLayout11);
            app.PortLabel.HorizontalAlignment = 'right';
            app.PortLabel.Layout.Row = 1;
            app.PortLabel.Layout.Column = 1;
            app.PortLabel.Text = 'Port';

            % Create PortList
            app.PortList = uidropdown(app.GridLayout11);
            app.PortList.Items = {'COM?'};
            app.PortList.Editable = 'on';
            app.PortList.BackgroundColor = [1 1 1];
            app.PortList.Layout.Row = 1;
            app.PortList.Layout.Column = 2;
            app.PortList.Value = 'COM?';

            % Create GridLayout20
            app.GridLayout20 = uigridlayout(app.GridLayout8);
            app.GridLayout20.RowHeight = {'1x'};
            app.GridLayout20.Layout.Row = 4;
            app.GridLayout20.Layout.Column = 1;

            % Create GridLayout21
            app.GridLayout21 = uigridlayout(app.GridLayout8);
            app.GridLayout21.Layout.Row = 5;
            app.GridLayout21.Layout.Column = 1;

            % Create FeenHzEditFieldLabel
            app.FeenHzEditFieldLabel = uilabel(app.GridLayout21);
            app.FeenHzEditFieldLabel.HorizontalAlignment = 'right';
            app.FeenHzEditFieldLabel.Layout.Row = 2;
            app.FeenHzEditFieldLabel.Layout.Column = 1;
            app.FeenHzEditFieldLabel.Text = 'Fe (en Hz)';

            % Create FeenHzEditField
            app.FeenHzEditField = uieditfield(app.GridLayout21, 'numeric');
            app.FeenHzEditField.Layout.Row = 2;
            app.FeenHzEditField.Layout.Column = 2;

            % Create Frquencedchantillonnageentre1Hzet10kHzLabel
            app.Frquencedchantillonnageentre1Hzet10kHzLabel = uilabel(app.GridLayout21);
            app.Frquencedchantillonnageentre1Hzet10kHzLabel.HorizontalAlignment = 'center';
            app.Frquencedchantillonnageentre1Hzet10kHzLabel.FontWeight = 'bold';
            app.Frquencedchantillonnageentre1Hzet10kHzLabel.Layout.Row = 1;
            app.Frquencedchantillonnageentre1Hzet10kHzLabel.Layout.Column = [1 2];
            app.Frquencedchantillonnageentre1Hzet10kHzLabel.Text = {'Fréquence d''échantillonnage'; 'entre 1 Hz et 10 kHz'};

            % Create BCon
            app.BCon = uibutton(app.GridLayout8, 'push');
            app.BCon.ButtonPushedFcn = createCallbackFcn(app, @BConButtonPushed, true);
            app.BCon.Layout.Row = 2;
            app.BCon.Layout.Column = 1;
            app.BCon.Text = 'Connect';

            % Create NumDataLabel
            app.NumDataLabel = uilabel(app.GridLayout8);
            app.NumDataLabel.HorizontalAlignment = 'center';
            app.NumDataLabel.Layout.Row = 3;
            app.NumDataLabel.Layout.Column = 1;
            app.NumDataLabel.Text = 'NumData';

            % Create GridLayout12
            app.GridLayout12 = uigridlayout(app.GridLayout);
            app.GridLayout12.ColumnWidth = {'1x'};
            app.GridLayout12.RowHeight = {'7x', '1x', '1x'};
            app.GridLayout12.Layout.Row = 1;
            app.GridLayout12.Layout.Column = 1;

            % Create TabGroup
            app.TabGroup = uitabgroup(app.GridLayout12);
            app.TabGroup.AutoResizeChildren = 'off';
            app.TabGroup.Layout.Row = 1;
            app.TabGroup.Layout.Column = 1;

            % Create StartTab
            app.StartTab = uitab(app.TabGroup);
            app.StartTab.AutoResizeChildren = 'off';
            app.StartTab.Title = 'Start';

            % Create AlignmentTab
            app.AlignmentTab = uitab(app.TabGroup);
            app.AlignmentTab.Title = 'Alignment';

            % Create StartAlignButton
            app.StartAlignButton = uibutton(app.AlignmentTab, 'push');
            app.StartAlignButton.ButtonPushedFcn = createCallbackFcn(app, @StartAlignButtonPushed, true);
            app.StartAlignButton.Position = [48 20 100 22];
            app.StartAlignButton.Text = 'Start Align';

            % Create StopAlignButton
            app.StopAlignButton = uibutton(app.AlignmentTab, 'push');
            app.StopAlignButton.ButtonPushedFcn = createCallbackFcn(app, @StopAlignButtonPushed, true);
            app.StopAlignButton.Position = [324 20 100 22];
            app.StopAlignButton.Text = 'Stop Align';

            % Create UIAxes2_2
            app.UIAxes2_2 = uiaxes(app.AlignmentTab);
            title(app.UIAxes2_2, 'Detector Output')
            xlabel(app.UIAxes2_2, 'X')
            ylabel(app.UIAxes2_2, 'Y')
            zlabel(app.UIAxes2_2, 'Z')
            app.UIAxes2_2.XLim = [-100 100];
            app.UIAxes2_2.YLim = [-100 100];
            app.UIAxes2_2.XGrid = 'on';
            app.UIAxes2_2.YGrid = 'on';
            app.UIAxes2_2.Position = [172 95 128 110];

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

            % Create XmotorSliderLabel
            app.XmotorSliderLabel = uilabel(app.MotorTab);
            app.XmotorSliderLabel.HorizontalAlignment = 'right';
            app.XmotorSliderLabel.Position = [33 86 48 22];
            app.XmotorSliderLabel.Text = 'X motor';

            % Create XmotorSlider
            app.XmotorSlider = uislider(app.MotorTab);
            app.XmotorSlider.ValueChangedFcn = createCallbackFcn(app, @XmotorSliderValueChanging, true);
            app.XmotorSlider.Position = [102 95 150 3];

            % Create YmotorSliderLabel
            app.YmotorSliderLabel = uilabel(app.MotorTab);
            app.YmotorSliderLabel.HorizontalAlignment = 'right';
            app.YmotorSliderLabel.Position = [-42 139 48 22];
            app.YmotorSliderLabel.Text = 'Y motor';

            % Create YmotorSlider
            app.YmotorSlider = uislider(app.MotorTab);
            app.YmotorSlider.ValueChangedFcn = createCallbackFcn(app, @YmotorSliderValueChanging, true);
            app.YmotorSlider.Position = [27 148 150 3];

            % Create Button
            app.Button = uibutton(app.MotorTab, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [113 39 100 22];
            app.Button.Text = '(0,0)';

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.MotorTab);
            title(app.UIAxes2, 'Detector Output')
            xlabel(app.UIAxes2, 'X')
            ylabel(app.UIAxes2, 'Y')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.XLim = [-100 100];
            app.UIAxes2.YLim = [-100 100];
            app.UIAxes2.XGrid = 'on';
            app.UIAxes2.YGrid = 'on';
            app.UIAxes2.Position = [324 139 120 119];

            % Create UIAxes3
            app.UIAxes3 = uiaxes(app.MotorTab);
            title(app.UIAxes3, 'Motor Position')
            xlabel(app.UIAxes3, 'X')
            ylabel(app.UIAxes3, 'Y')
            zlabel(app.UIAxes3, 'Z')
            app.UIAxes3.XLim = [-100 100];
            app.UIAxes3.YLim = [-100 100];
            app.UIAxes3.XGrid = 'on';
            app.UIAxes3.YGrid = 'on';
            app.UIAxes3.Position = [89 139 132 119];

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

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

            % Create StepResponseAcquisitionButton
            app.StepResponseAcquisitionButton = uibutton(app.GridLayout22, 'push');
            app.StepResponseAcquisitionButton.ButtonPushedFcn = createCallbackFcn(app, @StepResponseAcquisitionButtonPushed, true);
            app.StepResponseAcquisitionButton.Layout.Row = 2;
            app.StepResponseAcquisitionButton.Layout.Column = 1;
            app.StepResponseAcquisitionButton.Text = 'Step Response Acquisition';

            % Create UIAxes
            app.UIAxes = uiaxes(app.GridLayout22);
            title(app.UIAxes, 'Title')
            xlabel(app.UIAxes, 'X')
            ylabel(app.UIAxes, 'Y')
            app.UIAxes.Layout.Row = 1;
            app.UIAxes.Layout.Column = 1;

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

            % Create GridLayout18
            app.GridLayout18 = uigridlayout(app.PIDTab);
            app.GridLayout18.ColumnWidth = {'1x'};
            app.GridLayout18.RowHeight = {'6x', '1x'};

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

            % Create KpXEditFieldLabel
            app.KpXEditFieldLabel = uilabel(app.GridLayout19);
            app.KpXEditFieldLabel.HorizontalAlignment = 'right';
            app.KpXEditFieldLabel.FontSize = 16;
            app.KpXEditFieldLabel.Layout.Row = 2;
            app.KpXEditFieldLabel.Layout.Column = 2;
            app.KpXEditFieldLabel.Text = 'KpX';

            % Create KpXEditField
            app.KpXEditField = uieditfield(app.GridLayout19, 'numeric');
            app.KpXEditField.HorizontalAlignment = 'center';
            app.KpXEditField.FontSize = 16;
            app.KpXEditField.Layout.Row = 2;
            app.KpXEditField.Layout.Column = 3;

            % Create KpYEditFieldLabel
            app.KpYEditFieldLabel = uilabel(app.GridLayout19);
            app.KpYEditFieldLabel.HorizontalAlignment = 'right';
            app.KpYEditFieldLabel.FontSize = 16;
            app.KpYEditFieldLabel.Layout.Row = 2;
            app.KpYEditFieldLabel.Layout.Column = 4;
            app.KpYEditFieldLabel.Text = 'KpY';

            % Create KpYEditField
            app.KpYEditField = uieditfield(app.GridLayout19, 'numeric');
            app.KpYEditField.HorizontalAlignment = 'center';
            app.KpYEditField.FontSize = 16;
            app.KpYEditField.Layout.Row = 2;
            app.KpYEditField.Layout.Column = 5;

            % Create AxeYLabel
            app.AxeYLabel = uilabel(app.GridLayout19);
            app.AxeYLabel.HorizontalAlignment = 'center';
            app.AxeYLabel.FontSize = 18;
            app.AxeYLabel.FontWeight = 'bold';
            app.AxeYLabel.Layout.Row = 1;
            app.AxeYLabel.Layout.Column = 5;
            app.AxeYLabel.Text = 'Axe Y';

            % Create AxeXLabel
            app.AxeXLabel = uilabel(app.GridLayout19);
            app.AxeXLabel.HorizontalAlignment = 'center';
            app.AxeXLabel.FontSize = 18;
            app.AxeXLabel.FontWeight = 'bold';
            app.AxeXLabel.Layout.Row = 1;
            app.AxeXLabel.Layout.Column = 3;
            app.AxeXLabel.Text = 'Axe X';

            % Create KiXEditFieldLabel
            app.KiXEditFieldLabel = uilabel(app.GridLayout19);
            app.KiXEditFieldLabel.HorizontalAlignment = 'right';
            app.KiXEditFieldLabel.FontSize = 16;
            app.KiXEditFieldLabel.Layout.Row = 3;
            app.KiXEditFieldLabel.Layout.Column = 2;
            app.KiXEditFieldLabel.Text = 'KiX';

            % Create KiXEditField
            app.KiXEditField = uieditfield(app.GridLayout19, 'numeric');
            app.KiXEditField.HorizontalAlignment = 'center';
            app.KiXEditField.FontSize = 16;
            app.KiXEditField.Layout.Row = 3;
            app.KiXEditField.Layout.Column = 3;

            % Create KiYEditFieldLabel
            app.KiYEditFieldLabel = uilabel(app.GridLayout19);
            app.KiYEditFieldLabel.HorizontalAlignment = 'right';
            app.KiYEditFieldLabel.FontSize = 16;
            app.KiYEditFieldLabel.Layout.Row = 3;
            app.KiYEditFieldLabel.Layout.Column = 4;
            app.KiYEditFieldLabel.Text = 'KiY';

            % Create KiYEditField
            app.KiYEditField = uieditfield(app.GridLayout19, 'numeric');
            app.KiYEditField.HorizontalAlignment = 'center';
            app.KiYEditField.FontSize = 16;
            app.KiYEditField.Layout.Row = 3;
            app.KiYEditField.Layout.Column = 5;

            % Create KdXEditFieldLabel
            app.KdXEditFieldLabel = uilabel(app.GridLayout19);
            app.KdXEditFieldLabel.HorizontalAlignment = 'right';
            app.KdXEditFieldLabel.FontSize = 16;
            app.KdXEditFieldLabel.Layout.Row = 4;
            app.KdXEditFieldLabel.Layout.Column = 2;
            app.KdXEditFieldLabel.Text = 'KdX';

            % Create KdXEditField
            app.KdXEditField = uieditfield(app.GridLayout19, 'numeric');
            app.KdXEditField.HorizontalAlignment = 'center';
            app.KdXEditField.FontSize = 16;
            app.KdXEditField.Layout.Row = 4;
            app.KdXEditField.Layout.Column = 3;

            % Create KdYEditFieldLabel
            app.KdYEditFieldLabel = uilabel(app.GridLayout19);
            app.KdYEditFieldLabel.HorizontalAlignment = 'right';
            app.KdYEditFieldLabel.FontSize = 16;
            app.KdYEditFieldLabel.Layout.Row = 4;
            app.KdYEditFieldLabel.Layout.Column = 4;
            app.KdYEditFieldLabel.Text = 'KdY';

            % Create KdYEditField
            app.KdYEditField = uieditfield(app.GridLayout19, 'numeric');
            app.KdYEditField.HorizontalAlignment = 'center';
            app.KdYEditField.FontSize = 16;
            app.KdYEditField.Layout.Row = 4;
            app.KdYEditField.Layout.Column = 5;

            % Create GainCheckBox
            app.GainCheckBox = uicheckbox(app.GridLayout19);
            app.GainCheckBox.ValueChangedFcn = createCallbackFcn(app, @GainCheckBoxValueChanged, true);
            app.GainCheckBox.Tag = 'gain';
            app.GainCheckBox.Text = 'Gain';
            app.GainCheckBox.FontSize = 16;
            app.GainCheckBox.FontWeight = 'bold';
            app.GainCheckBox.Layout.Row = 2;
            app.GainCheckBox.Layout.Column = 1;

            % Create IntegCheckBox
            app.IntegCheckBox = uicheckbox(app.GridLayout19);
            app.IntegCheckBox.ValueChangedFcn = createCallbackFcn(app, @IntegCheckBoxValueChanged, true);
            app.IntegCheckBox.Tag = 'gain';
            app.IntegCheckBox.Text = {'Integ.'; ''};
            app.IntegCheckBox.FontSize = 16;
            app.IntegCheckBox.FontWeight = 'bold';
            app.IntegCheckBox.Layout.Row = 3;
            app.IntegCheckBox.Layout.Column = 1;

            % Create DerivCheckBox
            app.DerivCheckBox = uicheckbox(app.GridLayout19);
            app.DerivCheckBox.ValueChangedFcn = createCallbackFcn(app, @DerivCheckBoxValueChanged, true);
            app.DerivCheckBox.Tag = 'gain';
            app.DerivCheckBox.Text = 'Deriv.';
            app.DerivCheckBox.FontSize = 16;
            app.DerivCheckBox.FontWeight = 'bold';
            app.DerivCheckBox.Layout.Row = 4;
            app.DerivCheckBox.Layout.Column = 1;

            % Create FormeidaledePIDLabel
            app.FormeidaledePIDLabel = uilabel(app.GridLayout19);
            app.FormeidaledePIDLabel.HorizontalAlignment = 'center';
            app.FormeidaledePIDLabel.FontSize = 14;
            app.FormeidaledePIDLabel.FontWeight = 'bold';
            app.FormeidaledePIDLabel.FontAngle = 'italic';
            app.FormeidaledePIDLabel.FontColor = [0.651 0.651 0.651];
            app.FormeidaledePIDLabel.Layout.Row = 5;
            app.FormeidaledePIDLabel.Layout.Column = [2 4];
            app.FormeidaledePIDLabel.Text = 'Forme idéale de PID';

            % Create SendingCoeffPButton
            app.SendingCoeffPButton = uibutton(app.GridLayout18, 'push');
            app.SendingCoeffPButton.ButtonPushedFcn = createCallbackFcn(app, @SendingCoeffPButtonPushed, true);
            app.SendingCoeffPButton.FontSize = 15;
            app.SendingCoeffPButton.Layout.Row = 2;
            app.SendingCoeffPButton.Layout.Column = 1;
            app.SendingCoeffPButton.Text = 'Sending Coeff P';

            % Create ReceivedDataLabel
            app.ReceivedDataLabel = uilabel(app.GridLayout12);
            app.ReceivedDataLabel.HorizontalAlignment = 'center';
            app.ReceivedDataLabel.FontSize = 14;
            app.ReceivedDataLabel.FontColor = [1 0 1];
            app.ReceivedDataLabel.Layout.Row = 3;
            app.ReceivedDataLabel.Layout.Column = 1;
            app.ReceivedDataLabel.Text = 'Received Data';

            % Create SendedDataLabel
            app.SendedDataLabel = uilabel(app.GridLayout12);
            app.SendedDataLabel.HorizontalAlignment = 'center';
            app.SendedDataLabel.FontSize = 14;
            app.SendedDataLabel.FontColor = [1 0 0];
            app.SendedDataLabel.Layout.Row = 2;
            app.SendedDataLabel.Layout.Column = 1;
            app.SendedDataLabel.Text = 'Sended 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_1

            % 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