annotate python/bouncing_cube.py @ 174:3eb06f5fb987

Added memory alloc for locals
author Windel Bouwman
date Fri, 19 Apr 2013 19:22:52 +0200
parents 4a37d6992bd3
children ef683881c64e
rev   line source
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
1 from PyQt4.QtGui import *
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
2 from PyQt4.QtCore import *
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
3 from PyQt4.QtOpenGL import QGLWidget
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
4 from OpenGL.GL import *
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
5 from OpenGL.GLU import gluPerspective
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
6 import sys
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
7 from random import random
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
8 from math import pi, cos, sin, fabs, sqrt
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
9 from numpy import mat, array, ones, zeros, eye
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
10 from numpy.linalg import norm
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
11 import numpy as np
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
12 import time
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
13 import scipy.integrate
53
6dd7d6a1737c Added in and output port icons
windel
parents: 44
diff changeset
14 #import pyopencl
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
15
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
16 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
17 Test script that lets a dice bounce.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
18 Converted from 20-sim equations into python code.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
19
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
20 20-sim website:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
21 http://www.20sim.com
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
22
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
23 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
24 def drawCube(w):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
25 glBegin(GL_QUADS) # Start Drawing The Cube
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
26 glColor3f(0.0,1.0,0.0) # Set The Color To Blue
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
27 glVertex3f( w, w,-w) # Top Right Of The Quad (Top)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
28 glVertex3f(-w, w,-w) # Top Left Of The Quad (Top)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
29 glVertex3f(-w, w, w) # Bottom Left Of The Quad (Top)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
30 glVertex3f( w, w, w) # Bottom Right Of The Quad (Top)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
31
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
32 glColor3f(1.0,0.5,0.0) # Set The Color To Orange
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
33 glVertex3f( w,-w, w) # Top Right Of The Quad (Bottom)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
34 glVertex3f(-w,-w, w) # Top Left Of The Quad (Bottom)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
35 glVertex3f(-w,-w,-w) # Bottom Left Of The Quad (Bottom)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
36 glVertex3f( w,-w,-w) # Bottom Right Of The Quad (Bottom)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
37
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
38 glColor3f(1.0,0.0,0.0) # Set The Color To Red
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
39 glVertex3f( w, w, w) # Top Right Of The Quad (Front)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
40 glVertex3f(-w, w, w) # Top Left Of The Quad (Front)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
41 glVertex3f(-w,-w, w) # Bottom Left Of The Quad (Front)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
42 glVertex3f( w,-w, w) # Bottom Right Of The Quad (Front)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
43
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
44 glColor3f(1.0,1.0,0.0) # Set The Color To Yellow
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
45 glVertex3f( w,-w,-w) # Bottom Left Of The Quad (Back)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
46 glVertex3f(-w,-w,-w) # Bottom Right Of The Quad (Back)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
47 glVertex3f(-w, w,-w) # Top Right Of The Quad (Back)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
48 glVertex3f( w, w,-w) # Top Left Of The Quad (Back)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
49
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
50 glColor3f(0.0,0.0,1.0) # Set The Color To Blue
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
51 glVertex3f(-w, w, w) # Top Right Of The Quad (Left)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
52 glVertex3f(-w, w,-w) # Top Left Of The Quad (Left)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
53 glVertex3f(-w,-w,-w) # Bottom Left Of The Quad (Left)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
54 glVertex3f(-w,-w, w) # Bottom Right Of The Quad (Left)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
55
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
56 glColor3f(1.0,0.0,1.0) # Set The Color To Violet
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
57 glVertex3f( w, w,-w) # Top Right Of The Quad (Right)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
58 glVertex3f( w, w, w) # Top Left Of The Quad (Right)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
59 glVertex3f( w,-w, w) # Bottom Left Of The Quad (Right)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
60 glVertex3f( w,-w,-w) # Bottom Right Of The Quad (Right)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
61 glEnd() # Done Drawing The Quad
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
62
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
63 def drawFloor(w, h):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
64 glBegin(GL_QUADS) # Start Drawing The Cube
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
65
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
66 glColor3f(1.0,0.5,0.0) # Set The Color To Orange
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
67 glVertex3f( w,-w,h)# Top Right Of The Quad (Bottom)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
68 glVertex3f(-w,-w,h)# Top Left Of The Quad (Bottom)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
69 glVertex3f(-w,w,h)# Bottom Left Of The Quad (Bottom)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
70 glVertex3f( w,w,h)# Bottom Right Of The Quad (Bottom)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
71 glEnd() # Done Drawing The Quad
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
72
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
73 def drawAxis():
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
74 glLineWidth(0.5)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
75 glBegin(GL_LINES)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
76 glColor3f(1.0, 0.0, 0.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
77 glVertex3f(0,0,0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
78 glVertex3f(1,0,0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
79 glColor3f(0.0, 1.0, 0.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
80 glVertex3f(0,0,0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
81 glVertex3f(0,1,0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
82 glColor3f(0.0, 0.0, 1.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
83 glVertex3f(0,0,0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
84 glVertex3f(0,0,1)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
85 glEnd()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
86
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
87
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
88 def cross(A, B):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
89 a = A.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
90 b = B.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
91 return mat(np.cross(a, b)).T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
92
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
93 def skew(X):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
94 Y = mat(zeros( (3, 3) ))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
95 a,b,c = X.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
96 Y[0,1] = -c
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
97 Y[0,2] = b
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
98 Y[1,0] = c
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
99 Y[1,2] = -a
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
100 Y[2,0] = -b
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
101 Y[2,1] = a
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
102 return Y
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
103
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
104 def adjoint(T):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
105 W = T[0:3, 0]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
106 V = T[3:6, 0]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
107 a = mat(zeros( (6,6) ) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
108 a[0:3, 0:3] = skew(W)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
109 a[3:6, 0:3] = skew(V)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
110 a[3:6, 3:6] = skew(W)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
111 return a
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
112
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
113 def Adjoint(H):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
114 R = H[0:3, 0:3]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
115 P = H[0:3, 3]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
116 a = mat(zeros( (6,6) ) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
117 a[0:3, 0:3] = R
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
118 a[3:6, 3:6] = R
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
119 a[3:6, 0:3] = skew(P) * R
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
120 return a
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
121
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
122 def quatToR(q):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
123 x, y, z, w = q.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
124 r = mat(eye(3))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
125 r[0,0] = 1 - (2*y**2+2*z**2)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
126 r[0,1] = 2*x*y+2*z*w
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
127 r[0,2] = 2*x*z - 2*y*w
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
128 r[1,0] = 2*x*y-2*z*w
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
129 r[1,1] = 1 - (2*x**2 + 2*z**2)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
130 r[1,2] = 2*y*z + 2*x*w
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
131 r[2,0] = 2*x*z+2*y*w
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
132 r[2,1] = 2*y*z - 2*x*w
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
133 r[2,2] = 1 - (2*x**2+2*y**2)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
134 return r
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
135
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
136 def rotateAbout(axis, angle):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
137 ax, ay, az = (axis/norm(axis)).A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
138 qx = ax*sin(angle/2.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
139 qy = ay*sin(angle/2.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
140 qz = az*sin(angle/2.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
141 qw = cos(angle/2.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
142 q = mat(array([qx,qy,qz,qw])).T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
143 return q
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
144
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
145 def normalizeQuaternion(quat):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
146 x,y,z,w = quat.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
147 magnitude = sqrt(x*x + y*y + z*z + w*w)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
148 x = x / magnitude
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
149 y = y / magnitude
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
150 z = z / magnitude
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
151 w = w / magnitude
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
152 quat[0, 0] = x
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
153 quat[1, 0] = y
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
154 quat[2, 0] = z
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
155 quat[3, 0] = w
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
156 return quat
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
157
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
158 def VTo4x4(V):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
159 v1, v2, v3 = V.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
160 return mat(array( \
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
161 [[0.0, -v3, v2, -v1], \
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
162 [ v3, 0.0, -v1, -v2], \
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
163 [-v2, v1, 0.0, -v3], \
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
164 [v1, v2, v3, 0.0] ]))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
165
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
166 def homogeneous(R,p):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
167 H = mat(eye(4))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
168 H[0:3, 0:3] = R
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
169 H[0:3, 3] = p
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
170 return H
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
171
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
172 def rateOfChange(states, thetime, parameters):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
173 quat = states[0:4, 0] # Orientation (4)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
174 pos = states[4:7, 0] # Position (3)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
175 P = states[7:13, 0] # Momentum (6)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
176 massI, gravity = parameters
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
177 # Rigid body parts:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
178 # Forward Kinematic chain:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
179 H = homogeneous(quatToR(quat), pos) # Forward kinematics
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
180
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
181 AdjX2 = mat(eye(6)) # The connectionpoint in the real world
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
182 adjAdjX2_1 = adjoint(AdjX2[0:6,0])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
183 adjAdjX2_2 = adjoint(AdjX2[0:6,1])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
184 adjAdjX2_3 = adjoint(AdjX2[0:6,2])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
185 adjAdjX2_4 = adjoint(AdjX2[0:6,3])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
186 adjAdjX2_5 = adjoint(AdjX2[0:6,4])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
187 adjAdjX2_6 = adjoint(AdjX2[0:6,5])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
188 AdjInv2 = Adjoint(H.I)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
189 M2 = AdjInv2.T * (massI * AdjInv2) # Transfor mass to base frame
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
190 MassMatrix = M2
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
191
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
192 wrenchGrav2 = mat( zeros((1,6)) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
193 wrenchGrav2[0, 0:3] = -cross(gravity, pos).T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
194 wrenchGrav2[0, 3:6] = gravity.T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
195
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
196 Bk = mat( zeros( (6,6) ))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
197 Bk[0:3, 0:3] = skew(P[0:3, 0])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
198 Bk[3:6, 0:3] = skew(P[3:6, 0])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
199 Bk[0:3, 3:6] = skew(P[3:6, 0])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
200
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
201 # TODO: do this a cholesky:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
202 v = np.linalg.solve(MassMatrix, P) # Matrix inverse like thingy !
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
203
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
204 T2_00 = v # Calculate the relative twist!
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
205 TM2 = T2_00.T * M2
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
206 twistExternal = T2_00 # Twist van het blokje
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
207 TMSum2 = TM2
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
208
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
209 PDotBodies = mat( zeros( (6,1)) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
210 PDotBodies[0,0] = TMSum2 * (adjAdjX2_1 * T2_00)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
211 PDotBodies[1,0] = TMSum2 * (adjAdjX2_2 * T2_00)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
212 PDotBodies[2,0] = TMSum2 * (adjAdjX2_3 * T2_00)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
213 PDotBodies[3,0] = TMSum2 * (adjAdjX2_4 * T2_00)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
214 PDotBodies[4,0] = TMSum2 * (adjAdjX2_5 * T2_00)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
215 PDotBodies[5,0] = TMSum2 * (adjAdjX2_6 * T2_00)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
216
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
217 PDot = -PDotBodies - Bk * v
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
218 PDot += wrenchGrav2.T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
219
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
220 ##### Contact wrench part:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
221 HB_W = H # Is H-matrix van het blokje
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
222 WrenchB = mat(zeros( (1,6) ))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
223 for px in [-0.5, 0.5]:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
224 for py in [-0.5, 0.5]:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
225 for pz in [-0.5, 0.5]:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
226 HB1_B = homogeneous(mat(eye(3)), mat([px,py,pz]).T)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
227 HB1_W = HB_W * HB1_B
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
228 HW1_W = homogeneous(mat(eye(3)), HB1_W[0:3,3])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
229 HW_W1 = HW1_W.I
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
230 HB_W1 = HW_W1 * HB_W
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
231
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
232 AdjHB_W1 = Adjoint(HB_W1)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
233 TB_W1_W1 = AdjHB_W1 * twistExternal
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
234 z = HB1_W[2, 3]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
235 vx, vy, vz = TB_W1_W1[3:6, 0].A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
236 if z < 0:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
237 # Contact forces:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
238 Fx = -50.0*vx
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
239 Fy = -50.0*vy
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
240 Fz = -z*50000.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
241 else:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
242 Fx = 0.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
243 Fy = 0.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
244 Fz = 0.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
245 # TODO: reflect impulse
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
246 WrenchW1 = mat([0,0,0,0,0,Fz])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
247 # Transform it back:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
248 WrenchB += (AdjHB_W1.T * WrenchW1.T).T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
249 ##### End of contact wrench
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
250
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
251 PDot += (WrenchB * AdjInv2).T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
252
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
253 # Position and orientation rates:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
254 QOmega = VTo4x4(v[0:3, 0])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
255 quatDot = 0.5 * QOmega * quat
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
256 vel = v[3:6, 0]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
257 posDot = skew(v[0:3]) * pos + vel
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
258 # The rate vector:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
259 rates = mat(zeros( (13,1) ))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
260 rates[0:4, 0] = quatDot
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
261 rates[4:7, 0] = posDot
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
262 rates[7:13, 0] = PDot
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
263 return rates
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
264
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
265 def fWrapper(y, t, parameters):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
266 y = mat(y).T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
267 dy = rateOfChange(y, t, parameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
268 return dy.T.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
269
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
270 def SCIPY(endtime, dt, state, parameters):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
271 times = np.arange(0.0, endtime, dt)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
272 y0 = state.T.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
273 res = scipy.integrate.odeint(fWrapper, y0, times, args=(parameters,))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
274 states = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
275 res = res.T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
276 r,c = res.shape
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
277 for ci in range(0,c):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
278 states.append(mat(res[:,ci]).T)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
279 return states
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
280
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
281 def RK4(endtime, dt, state, parameters):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
282 t = 0.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
283 states = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
284 while t < endtime:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
285 newstate = mat (zeros( state.shape )) # Create a new object
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
286
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
287 #### Runge Kutta integration:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
288 k1 = rateOfChange(state, t, parameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
289 k2 = rateOfChange(state + 0.5*dt*k1, t, parameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
290 k3 = rateOfChange(state + 0.5*dt*k1, t, parameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
291 k4 = rateOfChange(state + dt*k3, t, parameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
292 newstate = state + (dt/6.0)*(k1+2*k2+2*k3+k4)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
293
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
294 # Normalize quat:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
295 newstate[0:4, 0] = normalizeQuaternion(newstate[0:4, 0])
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
296 states.append(newstate)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
297
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
298 state = newstate
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
299
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
300 t += dt
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
301 print state[6,0], t, ' ', (t/endtime)*100.0, '%'
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
302 return states
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
303
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
304
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
305 def simulate(endtime, dt):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
306 PInitial = mat( zeros((6,1)) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
307 posInitial = mat(array([-1.2, -1.3, 2.8])).T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
308 quatInitial = rotateAbout(mat(array([0.2, 1.0, 0.4])).T, 0.5)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
309 # Parameters:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
310 gravity = mat( array([0,0,-9.81]) ).T
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
311 massI = mat(eye(6)) * 1.01 # Mass matrix
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
312 parameters = (massI, gravity)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
313
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
314 # The state vector!
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
315 state = mat(zeros( (13,1) ))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
316 state[0:4, 0] = quatInitial
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
317 state[4:7, 0] = posInitial
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
318 state[7:13, 0] = PInitial
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
319
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
320 return SCIPY(endtime, dt, state, parameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
321
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
322 class W(QGLWidget):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
323 time = 0.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
324 index = 0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
325 def __init__(self, states, dt, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
326 super(W, self).__init__(parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
327 self.firstRun = True
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
328 self.savePNGS = False
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
329 self.dt = dt
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
330 self.resize(500,500)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
331 self.states = states
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
332 self.UP()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
333 t = QTimer(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
334 t.timeout.connect(self.UP)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
335 t.start(self.dt*1000.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
336
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
337 def UP(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
338 self.time += self.dt
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
339 if self.states:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
340 state = self.states[self.index]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
341 self.Dicequat = state[0:4, 0]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
342 self.Dicepos = state[4:7, 0]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
343 self.index += 1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
344 if self.index == len(self.states):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
345 self.index = 0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
346 self.firstRun = False
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
347 # Paint:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
348 self.update()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
349 if self.firstRun:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
350 # Create png images for the movie:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
351 if self.savePNGS:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
352 pm = self.renderPixmap()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
353 pm.save('image'+str(self.index)+'.png')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
354
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
355 def initializeGL(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
356 glClearColor(0.0, 0.5, 0.0, 1.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
357 glEnable(GL_DEPTH_TEST)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
358 glDepthFunc(GL_LESS)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
359 glShadeModel(GL_SMOOTH)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
360 def resizeGL(self,w,h):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
361 glViewport(0, 0, w, h)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
362 glMatrixMode(GL_PROJECTION)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
363 glLoadIdentity()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
364 gluPerspective(45.0, float(w)/float(h), 0.1, 100.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
365 glMatrixMode(GL_MODELVIEW)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
366 def paintGL(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
367 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear buffers
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
368 glLoadIdentity() # Reset The View
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
369
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
370 glLoadIdentity()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
371 glTranslatef(0.0,-2.0,-10.0) # Move Left And Into The Screen
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
372 glRotatef(-90.0, 1.0, 0.0, 0.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
373 drawFloor(2.0, 0.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
374 drawAxis()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
375 self.renderText(1.0, 0.0, 0.0, 'X')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
376 self.renderText(0.0, 1.0, 0.0, 'Y')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
377 self.renderText(0.0, 0.0, 1.0, 'Z')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
378
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
379 self.renderText(0.0,0.0,1.2,str(self.time))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
380
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
381 x,y,z = self.Dicepos.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
382 R = quatToR(self.Dicequat)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
383
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
384 glTranslatef(x, y, z)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
385 # Trick to rotate the openGL matrix:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
386 r = R.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
387 rotR = (r[0], r[3], r[6], 0.0,
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
388 r[1], r[4], r[7], 0.0,
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
389 r[2], r[5], r[8], 0.0,
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
390 0.0, 0.0, 0.0, 1.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
391 glMultMatrixd(rotR)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
392
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
393 drawCube(0.6)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
394
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
395 et = 20.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
396 dt = 0.04
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
397 print 'starting integration... endtime =', et, ' stepsize =', dt
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
398 t0 = time.time()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
399 states = simulate(et, dt)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
400 t1 = time.time()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
401 print 'That was heavy, it took me ', t1-t0, ' seconds!'
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
402 app = QApplication(sys.argv)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
403 w = W(states, dt)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
404 w.show()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
405 sys.exit(app.exec_())
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
406