forked from dov/dov-conrob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pendulum-math.py
58 lines (49 loc) · 1.53 KB
/
pendulum-math.py
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
#!/usr/bin/python
from pylab import *
import control # Load the controls systems library
# This script tries to reproduce in python the equations of for the pendulum
# as can be found on:
#
# http://ctms.engin.umich.edu/CTMS/index.php?example=InvertedPendulum§ion=SystemModeling
#
# The goal is then to try to control a simulated pendulum with the same parameters.
#
# Dov Grobgeld <dov.grobgeld@gmail.com>
# 2013-02-24 Sun
M = 0.5 # kg - mass of cart
m = 0.2 # kg - mass of pendulum
b = 0.1 # N/m/s - coefficient of friction for cart
l = 0.3 # Length to pendulum center of mass
I = 0.006 # kg - m^2
F = 0 # N - Force applied to the cart
x = 0 # cart position
theta = 0 # pendulum angle from vertical down
g = 9.8 # gravitational constant
p = I*(M+m)+M*m*l**2 # denominator for the A and B matrices
A = array([[0, 1, 0, 0],
[0, -(I+m*l**2)*b/p, (m**2*g*l**2)/p, 0],
[0, 0, 0, 1],
[0, -(m*l*b)/p, m*g*l*(M+m)/p, 0]])
B = array([[0],
[(I+m*l**2)/p],
[0],
[m*l/p]])
C = array([[1, 0, 0, 0],
[0, 0, 1, 0]])
D = array([[0],
[0]])
poles,vect = eig(A)
# Print vectors to verify that the system is unstable
print poles
K = control.place(A,B, [-1,-1,-1,-1])
print 'K(place)=',K
Q = C.transpose().dot(C)
Q[1,1]=5000
Q[3,3] = 100
R = 1
K,S,E = control.lqr(A,B,Q,R)
print 'K(lqr)=',K
# New control matrix
Ac = A-B.dot(K)
poles,vect = eig(Ac)
print "Ac poles=", poles