|
1 |
| - |
2 |
| -function drawpendulum(t,x, m1, m2,g,l) |
3 |
| -%% drawpendulum |
4 |
| -% adapted from Patrick Suhms code |
5 |
| -% (source: https://github.com/PatrickSuhm/LqrControlTutorial) |
6 |
| -% ------------------------------------------------------------------------ |
7 |
| -% input: |
8 |
| -% t ... time (,1) |
9 |
| -% x ... state ([s, ds, phi, dphi]) |
10 |
| -% m1 ... mass of the cart |
11 |
| -% m2 ... mass of pole |
12 |
| -% ------------------------------------------------------------------------ |
13 |
| - |
14 |
| -s = x(1,:); % position |
15 |
| -phi = x(3,:); % angle |
16 |
| - |
17 |
| - |
18 |
| -% dimensions of cart and mass |
19 |
| -W = 1*sqrt(m1/5); % cart width |
20 |
| -H = .5*sqrt(m1/5); % cart height |
21 |
| -mr = .3*sqrt(m2); % mass radius |
22 |
| - |
23 |
| -% position mass |
24 |
| -px = s - l*sin(phi); |
25 |
| -py = H/2 + l*cos(phi); |
26 |
| - |
27 |
| -% create new figure and |
28 |
| -figure |
29 |
| -plot([-25 10],[0 0],'w','LineWidth',2) |
30 |
| -hold on |
31 |
| - |
32 |
| -% plot the cart |
33 |
| -h1=rectangle('Position',[s(1)-W/2,0,W,H],'Curvature',.1,'FaceColor',[1 0.1 0.1],'EdgeColor',[1 1 1]); |
34 |
| - |
35 |
| - % plot the pole |
36 |
| -h2=plot([s(1) px(1)],[0 py(1)],'w','LineWidth',2); |
37 |
| -h3=rectangle('Position',[px(1)-mr/2,py(1)-mr/2,mr,mr],'Curvature',1,'FaceColor',[.3 0.3 1],'EdgeColor',[1 1 1]); |
38 |
| -h4=text(0.95, 1.5, ['phi: ', num2str(1)]); |
39 |
| -set(h4,'color','w', 'fontsize', 14); |
40 |
| -xlim([-12 2]); |
41 |
| -ylim([-1 5]); |
42 |
| -set(gca,'Color','k','XColor','w','YColor','w') |
43 |
| -set(gcf,'Color','k') |
44 |
| - |
45 |
| -pause(0.1) |
46 |
| -tic |
47 |
| - |
48 |
| -% animation in a for loop |
49 |
| -for k=1:length(t) |
50 |
| - |
51 |
| - % update pole and cart position |
52 |
| - set(h1, 'position',[s(k)-W/2,0,W,H]); |
53 |
| - set(h2, 'XData',[s(k) px(k)], 'YData', [H/2 py(k)]); |
54 |
| - set(h3, 'position', [px(k)-mr/2,py(k)-mr/2,mr,mr]); |
55 |
| - set(h4, 'string',['time: ', num2str(t(k))]); |
56 |
| - |
57 |
| - drawnow(); |
58 |
| - pause(0.1) |
59 |
| - |
60 |
| - |
61 |
| - % meassure the time and create a fixed time loop |
62 |
| -% t2=toc; |
63 |
| -% while t2 < t(k) |
64 |
| -% t2 = toc; |
65 |
| -% end |
66 |
| -% t3(k) = t2; |
67 |
| - |
68 |
| - end |
0 commit comments