### Gitlab Runner Update from cstd-aux-files/master:Merge branch...

Gitlab Runner Update from cstd-aux-files/master:Merge branch '1-name-files-according-to-new-cstd-script-3' into 'master'
parent 858668fb
 % Exercise 2.4 :: Bass-Gura pole placement % Exercise 3.4 :: Bass-Gura pole placement % % TUHH :: Institut for Control Systems :: Control Systems 2 % Last update: 13.11.2008 % Last update: 19.10.2021 A = [1 -2; 3 -4]; b = [3; 1]; ... ... @@ -19,4 +19,4 @@ f = -p/(C*Ta); Acl = A+b*f; figure Scl = ss(Acl,b,eye(2),0); step(Scl); \ No newline at end of file step(Scl);
 % Exercise 2.7 :: Inverted Pendulum % Exercise 3.7 :: Inverted Pendulum % % TUHH :: Institut for Control Systems :: Control Systems 2 % Last update: 13.10.2013 % Last update: 19.10.2021 %definition of useful vectors for simulation clear all ... ... @@ -55,4 +55,4 @@ for i=1:length(t), u(i) = F*X(i,:)'; end figure(1);plot(t,u);grid;ylabel('control input'); xlabel('time(s)') figure(2);plot(t,X(:,1:2)');grid;xlabel('time(s)');ylabel('angle(radius)') \ No newline at end of file figure(2);plot(t,X(:,1:2)');grid;xlabel('time(s)');ylabel('angle(radius)')
 % exercise 3.2@ control design for an inverted pendulum with friction % exercise 4.2@ control design for an inverted pendulum with friction %definition of useful vectors for simulation clear all ... ...
 close all; load('MinSegPlant\Plant_Parameters.mat'); load('MinSegPlant\Plant_Parameters_VAR.mat'); %Simulation of the Self Balancing Robot for Stabilization % Design of three feedback controllers: % - LQR Design %State-Space Model sys = ss(A, B, C, D); %Check Controllability and Observability disp('Number of controllable states:'); rank(ctrb(sys)) %or rank(ctrb(A,B)) disp('Number of observable states:'); rank(obsv(sys)) %or rank(obsv(A,C)) %% Linear-Quadratic-Regulator Design % ************** LQR ************** %Tuning Q and R R = 0.1; Q = blkdiag(1000,1000,10,1); %LQR Gain K_LQR = -lqr(A,B,Q,R); %continuous %Closed loop state space model sys_cl = ss(A+B*K_LQR, B, C, D); %continuous %% LQR Simulation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% open('Task_2_Simulation_LQR'); %set this values either to 0 or 1 runLinear = 1; runNonlinear = 1; %set initial condition x0 = [0;5*pi/180;0; 0]; x0 = [0;9*pi/180;0; 0]; %Simulation Time set_param('Task_2_Simulation_LQR', 'StopTime', '2') % reference sine input for position sinewave= 1; if (sinewave) x0 = [0; 0; 0; 0]; set_param('Task_2_Simulation_LQR', 'StopTime', '30') end sim('Task_2_Simulation_LQR.slx'); while(~exist('simout_LQR_linear'))% ~ not operator pause(1); end; %linear Model t_LIN = simout_LQR_linear.Time; y_LIN = simout_LQR_linear.Data(:,1:4); u_LIN = simout_LQR_linear.Data(:,5); r_LIN = simout_LQR_linear.Data(:,6); %nonlinear Model t_NL = simout_LQR_nonlinear.Time; y_NL = simout_LQR_nonlinear.Data(:,1:4); u_NL = simout_LQR_nonlinear.Data(:,5); r_NL = simout_LQR_linear.Data(:,6); %plot the states figure; for k = 1:2 subplot(3,1,k); hold on; grid on; if(sinewave == 1 && k==1) plot(t_LIN, r_LIN, 'g', 'LineWidth', 2.0, 'DisplayName', 'linear'); end; if(runLinear == 1) plot(t_LIN, y_LIN(:, k), 'r', 'LineWidth', 2.0, 'DisplayName', 'linear'); end; if(runNonlinear == 1) plot(t_NL, y_NL(:, k), 'b', 'LineWidth', 2.0, 'DisplayName', 'nonlinear'); end; switch k case 1 ylabel('$s(t)$', 'interpreter', 'latex'); case 2 ylabel('$\alpha(t)$', 'interpreter', 'latex'); case 3 ylabel('$\dot{s}(t)$', 'interpreter', 'latex'); case 4 ylabel('$\dot{\alpha}(t)$', 'interpreter', 'latex'); end; legend('-DynamicLegend', 'Location', 'Northeast'); end; %plot the input voltage subplot(3,1,3); if(runLinear == 1) plot(t_LIN, u_LIN, 'r', 'LineWidth', 2.0, 'DisplayName', 'linear'); end; hold on; if(runNonlinear == 1) plot(t_NL, u_NL, 'b', 'LineWidth', 2.0, 'DisplayName', 'nonlinear'); end; legend('-DynamicLegend', 'Location', 'best'); ylabel('$u(t)$', 'interpreter', 'latex'); xlabel('Time [s]', 'interpreter', 'latex') grid on; if (sinewave) figure; if(runLinear == 1) plot(t_LIN, r_LIN, 'b', 'LineWidth', 2.0, 'DisplayName', 'reference'); hold on; plot(t_LIN, simout_LQR_linear.Data(:,1), 'r', 'LineWidth', 2.0, 'DisplayName', 'Linear state'); end; legend('-DynamicLegend', 'Location','Northeast'); ylabel('$s(t)$', 'interpreter', 'latex') title('Sine wave tracking', 'interpreter', 'latex'); grid figure; if(runNonlinear == 1) plot(t_NL, r_NL, 'b', 'LineWidth', 1.5, 'DisplayName', 'reference'); hold on; plot(t_NL, simout_LQR_nonlinear.Data(:,1), 'r', 'LineWidth', 1.5, 'DisplayName', 'Nonlinear state'); end; legend('-DynamicLegend', 'Location', 'Northeast'); title('Sine wave tracking', 'interpreter', 'latex'); ylabel('$s(t)$', 'interpreter', 'latex'); grid end; %% Error calculation error=0; for i=1:1:length(r_LIN) error=error+(r_LIN(i,1)- y_LIN(i, 1))^2; end error=error/length(r_LIN) \ No newline at end of file
 close all; load('MinisegPlant\Plant_Parameters.mat'); load('MinisegPlant\Plant_Parameters_VAR.mat'); %Simulation of the Self Balancing Robot for Stabilization % Design of three feedback controllers: % - LQR Design %State-Space Model sys = ss(A, B, C, D); %Check Controllability and Observability disp('Number of controllable states:'); rank(ctrb(sys)) %or rank(ctrb(A,B)) disp('Number of observable states:'); rank(obsv(sys)) %or rank(obsv(A,C)) %% Linear-Quadratic-Regulator Design % ************** LQR ************** %Tuning Q and R R = 0.1; Q = blkdiag(1000,1000,10,1); %LQR Gain K_LQR = -lqr(A,B,Q,R); %continuous %Closed loop state space model sys_cl = ss(A+B*K_LQR, B, C, D); %continuous %% LQR Simulation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% open('Task_2_Simulation_LQR'); %set this values either to 0 or 1 runLinear = 1; runNonlinear = 1; %set initial condition x0 = [0;5*pi/180;0; 0]; x0 = [0;9*pi/180;0; 0]; %Simulation Time set_param('Task_2_Simulation_LQR', 'StopTime', '2') % reference sine input for position sinewave= 1; if (sinewave) x0 = [0; 0; 0; 0]; set_param('Task_2_Simulation_LQR', 'StopTime', '30') end sim('Task_2_Simulation_LQR.slx'); while(~exist('simout_LQR_linear'))% ~ not operator pause(1); end; %linear Model t_LIN = simout_LQR_linear.Time; y_LIN = simout_LQR_linear.Data(:,1:4); u_LIN = simout_LQR_linear.Data(:,5); r_LIN = simout_LQR_linear.Data(:,6); %nonlinear Model t_NL = simout_LQR_nonlinear.Time; y_NL = simout_LQR_nonlinear.Data(:,1:4); u_NL = simout_LQR_nonlinear.Data(:,5); r_NL = simout_LQR_linear.Data(:,6); %plot the states figure; for k = 1:2 subplot(3,1,k); hold on; grid on; if(sinewave == 1 && k==1) plot(t_LIN, r_LIN, 'g', 'LineWidth', 2.0, 'DisplayName', 'linear'); end; if(runLinear == 1) plot(t_LIN, y_LIN(:, k), 'r', 'LineWidth', 2.0, 'DisplayName', 'linear'); end; if(runNonlinear == 1) plot(t_NL, y_NL(:, k), 'b', 'LineWidth', 2.0, 'DisplayName', 'nonlinear'); end; switch k case 1 ylabel('$s(t)$', 'interpreter', 'latex'); case 2 ylabel('$\alpha(t)$', 'interpreter', 'latex'); case 3 ylabel('$\dot{s}(t)$', 'interpreter', 'latex'); case 4 ylabel('$\dot{\alpha}(t)$', 'interpreter', 'latex'); end; legend('-DynamicLegend', 'Location', 'Northeast'); end; %plot the input voltage subplot(3,1,3); if(runLinear == 1) plot(t_LIN, u_LIN, 'r', 'LineWidth', 2.0, 'DisplayName', 'linear'); end; hold on; if(runNonlinear == 1) plot(t_NL, u_NL, 'b', 'LineWidth', 2.0, 'DisplayName', 'nonlinear'); end; legend('-DynamicLegend', 'Location', 'best'); ylabel('$u(t)$', 'interpreter', 'latex'); xlabel('Time [s]', 'interpreter', 'latex') grid on; if (sinewave) figure; if(runLinear == 1) plot(t_LIN, r_LIN, 'b', 'LineWidth', 2.0, 'DisplayName', 'reference'); hold on; plot(t_LIN, simout_LQR_linear.Data(:,1), 'r', 'LineWidth', 2.0, 'DisplayName', 'Linear state'); end; legend('-DynamicLegend', 'Location','Northeast'); ylabel('$s(t)$', 'interpreter', 'latex') title('Sine wave tracking', 'interpreter', 'latex'); grid figure; if(runNonlinear == 1) plot(t_NL, r_NL, 'b', 'LineWidth', 1.5, 'DisplayName', 'reference'); hold on; plot(t_NL, simout_LQR_nonlinear.Data(:,1), 'r', 'LineWidth', 1.5, 'DisplayName', 'Nonlinear state'); end; legend('-DynamicLegend', 'Location', 'Northeast'); title('Sine wave tracking', 'interpreter', 'latex'); ylabel('$s(t)$', 'interpreter', 'latex'); grid end; %% Comparison with experiment close all; for i=1:length(expOut.signals.values) if ( expOut.signals.values(i,6)~=0) break; end end ExperimentArray=expOut.signals.values(i:end,1); ExperimentTime=expOut.time(i:end,1)-expOut.time(i); figure; plot(t_NL, r_NL, 'b', 'LineWidth', 1.5, 'DisplayName', 'reference'); hold on; plot(t_NL, simout_LQR_nonlinear.Data(:,1), 'r', 'LineWidth', 1.5, 'DisplayName', 'Nonlinear state'); hold on; plot(ExperimentTime,ExperimentArray, 'k', 'LineWidth', 1.5, 'DisplayName', 'experiment'); legend('-DynamicLegend', 'Location', 'best'); ylabel('$s(t)$', 'interpreter', 'latex') title('Sine wave tracking', 'interpreter', 'latex'); grid