forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrick_control.m
93 lines (65 loc) · 1.94 KB
/
brick_control.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function [tout,xout]=brick_control()
global A B K
IC=[]; %initial conditions
%use matlab 'ss' function to define the brick system using standard
%(the global variables are set up for standard x_dot=Ax+Bu notation)
%use matlab 'lqr' to find gain matrix (global variables set up for gain matrix to
%be K)
%simulate system
dt=.01;
T=20;
[tout,xout]=ode45(@control_dyn,[0:.01:T],IC);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%any additional code you need (e.g., plotting, etc)
%please place here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plotdt=.1;
for i=1:plotdt/dt:T/dt
draw((i-1)*dt,xout(i,:));
pause(.001)
end
end
%dynamics function
function xdot=control_dyn(t,x)
global A B
u=lqr_control(x);
%u=minTime_control(x); %this controller may take a bit longer to simulate. that's fine
xdot=A*x+B*u;
end
%implement the LQR controller in this function
function u=lqr_control(x)
global K
end
%implement the minimum time controller in this function
function u=minTime_control(x)
end
% ==============================================================
% This is the draw function.
% ==============================================================
function draw(t,x)
persistent hFig blockx blocky;
if (isempty(hFig))
hFig = figure(25);
set(hFig,'DoubleBuffer','on');
blockx = [-1, -1, 1, 1, -1];
blocky = [0, 0.5, 0.5, 0, 0];
end
figure(hFig);
clf;
% draw the mass
brickcolor=[.75 .6 .5];
fill(blockx+repmat(x(1),1,5),blocky,brickcolor);
hold on
faintline=[.6 .8 .65]*1.1;
plot(min(blockx)+[0 0],[-5 5],'k:','Color',faintline);
plot(max(blockx)+[0 0],[-5 5],'k:','Color',faintline);
% draw the ground
line([-5, 5], [0, 0],'Color',[.3 .5 1],'LineWidth',1);
axis([-5 5 -1 2]);
%grid on
axis equal;
title(['t = ', num2str(t)]);
drawnow;
end