-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple_Pulley.m
202 lines (185 loc) · 7.84 KB
/
Simple_Pulley.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
1;
pkg load control
##**************************************************************************
##* OCTAVE PROGRAMMING (e-Yantra 2019-20)
##* ====================================
##* This software is intended to teach Octave Programming and Mathematical
##* Modeling concepts
##* Theme: Biped Patrol
##* Filename: Simple_Pulley.m
##* Version: 1.0.0
##* Date: November 3, 2019
##*
##* Team ID :
##* Team Leader Name:
##* Team Member Name
##*
##* Author: e-Yantra Project, Department of Computer Science
##* and Engineering, Indian Institute of Technology Bombay.
##*
##* Software released under Creative Commons CC BY-NC-SA
##*
##* For legal information refer to:
##* http://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
##*
##*
##* This software is made available on an “AS IS WHERE IS BASIS”.
##* Licensee/end user indemnifies and will keep e-Yantra indemnified from
##* any and all claim(s) that emanate from the use of the Software or
##* breach of the terms of this agreement.
##*
##* e-Yantra - An MHRD project under National Mission on Education using
##* ICT(NMEICT)
##*
##**************************************************************************
## Function : draw_pulley()
## ----------------------------------------------------
## Input: y - State Vector. In case of simple pulley system, the state variables
## are position x of mass m1 wrt pulley (along vertical) and the
## velocity x_dot of mass m1 wrt pulley (along vertical)
##
## Purpose: Takes the state vector as input. It draws the simple pulley system in
## a 2D plot.
function draw_pulley(y)
pd = 0.4; ## Pulley Diameter
p_y = 0.5; ## Pulley position wrt y
L = 1; ## Length of string
ml = 0.2; ## Mass Length
mb = 0.1; ## Mass Breadth
x1 = y(1);
x2 = L-y(1);
hold on;
clf;
axis equal;
rectangle('Position',[0-(pd/2),p_y-(pd/2),pd,pd],'Curvature',1,'FaceColor',[1 0 0]);
rectangle('Position',[-(pd/2)-(ml/2),p_y-x1-(mb/2),ml,mb],'Curvature',0.1,'FaceColor',[0 0 1]);
rectangle('Position',[(pd/2)-(ml/2),p_y-x2-(mb/2),ml,mb],'Curvature',0.1,'FaceColor',[0 1 0]);
line ([0-(pd/2) 0-(pd/2)], [p_y p_y-x1], "linestyle", "-", "color", "k");
line ([(pd/2) (pd/2)], [p_y p_y-x2], "linestyle", "-", "color", "k");
line ([0 0], [1 p_y], "linestyle", "-", "color", "k");
xlim([-1 1])
ylim([-1 1])
drawnow
hold off
endfunction
## Function : pulley_dynamics()
## ----------------------------------------------------
## Input: y - State Vector. In case of simple pulley system, the state variables
## are position x of mass m1 wrt pulley (along vertical) and the
## velocity x_dot of mass m1 wrt pulley (along vertical)
## m1 - Mass of 1st block
## m2 - Mass of 2nd block
## g - Acceleration due to gravity
## r - radius of pulley
## u - Input to the system
##
## Output: dy - Derivative of State Vector. In case of simple pulley system they
## will be the velocity x_dot and acceleration x_dot_dot
## Purpose: Calculates the value of the vector dy according to the equations which
## govern this system.
function dy = pulley_dynamics(y, m1, m2, g, r, u)
dy(1,1) = y(2);
dy(2,1) = (u/r+g*(m1-m2))/(m1+m2);
endfunction
## Function : sim_pulley()
## ----------------------------------------------------
## Input: m1 - Mass of 1st block
## m2 - Mass of 2nd block
## g - Acceleration due to gravity
## r - radius of pulley
## y0 - Initial Condition
## Output: t - Timestep
## y - Solution array
##
## Purpose: This function demonstrates the behavior of simple pulley without
## any external input (u)
## This integrates the system of differential equation from t0 = 0 to
## tf = 10 with initial condition y0
function [t,y] = sim_pulley(m1, m2, g, r, y0)
tspan = 0:0.1:10; ## Initialise time step
u = 0; ## No Input
[t,y] = ode45(@(t,y)pulley_dynamics(y, m1, m2, g, r, u),tspan,y0);
endfunction
## Function : pulley_AB_matrix()
## ----------------------------------------------------
## Input: m1 - Mass of 1st block
## m2 - Mass of 2nd block
## g - Acceleration due to gravity
## r - radius of pulley
##
## Output: A - A matrix of system
## B - B matrix of system
##
## Purpose: Declare the A and B matrices in this function.
function [A,B] = pulley_AB_matrix(m1, m2, g, r)
A = [0 1;0 0];
B = [0;1/(r*(m1+m2))];
endfunction
## Function : pole_place_pulley()
## ----------------------------------------------------
## Input: m1 - Mass of 1st block
## m2 - Mass of 2nd block
## g - Acceleration due to gravity
## r - radius of pulley
## y_setpoint - Reference Point
## y0 - Initial Condition
##
## Output: t - Timestep
## y - Solution array
##
## Purpose: This function demonstrates the behavior of simple pulley with
## external input using the pole_placement controller
## This integrates the system of differential equation from t0 = 0 to
## tf = 10 with initial condition y0 and input u = -Kx where K is
## calculated using Pole Placement Technique.
function [t,y] = pole_place_pulley(m1, m2, g, r, y_setpoint, y0)
[A,B] = pulley_AB_matrix(m1, m2, g, r); ## Initialize A and B matrix
eigs = [-9;-9]; ## Initialise desired eigenvalues
K = place(A,B,eigs); ## Calculate K matrix for desired eigenvalues
tspan = 0:0.1:10; ## Initialise time step
[t,y] = ode45(@(t,y)pulley_dynamics(y, m1, m2, g, r, -K*(y-y_setpoint)),tspan,y0);
endfunction
## Function : lqr_pulley()
## ----------------------------------------------------
## Input: m1 - Mass of 1st block
## m2 - Mass of 2nd block
## g - Acceleration due to gravity
## r - radius of pulley
## y_setpoint - Reference Point
## y0 - Initial Condition
##
## Output: t - Timestep
## y - Solution array
##
## Purpose: This function demonstrates the behavior of simple pulley with
## external input using the LQR controller
## This integrates the system of differential equation from t0 = 0 to
## tf = 10 with initial condition y0 and input u = -Kx where K is
## calculated using LQR
function [t,y] = lqr_pulley(m1, m2, g, r, y_setpoint, y0)
[A,B] = pulley_AB_matrix(m1, m2, g, r); ## Initialize A and B matrix
Q = diag([9000,100]); ## Initialise desired eigenvalues
R = 1;
K = lqr(A,B,Q,R); ## Calculate K matrix for desired eigenvalues
tspan = 0:0.1:10; ## Initialise time step
[t,y] = ode45(@(t,y)pulley_dynamics(y, m1, m2, g, r, -K*(y-y_setpoint)),tspan,y0);
endfunction
## Function : simple_pulley_main()
## ----------------------------------------------------
## Purpose: Used for testing out the various controllers by calling their
## respective functions. Constant parameters like masses m1 and m2,
## gravity g and radius of pulley are defined here.
function simple_pulley_main()
m1 = 7.5;
m2 = 7.51;
g = 9.8;
r = 0.2;
y0 = [0.5 ; 0]; ## Initial condtion
y_setpoint = [0.4; 0]; ## Set Point
## [t,y] = sim_pulley(m1, m2, g, r, y0);
## [t,y] = pole_place_pulley(m1, m2, g, r, y_setpoint, y0);
[t,y] = lqr_pulley(m1, m2, g, r, y_setpoint, y0);
for k = 1:length(t)
draw_pulley(y(k, :));
endfor
endfunction