| 1 |
|
| 2 |
|
| 3 |
"""A simple example demonstrating the creation of actors and animating |
| 4 |
the in a scene using visual modeule.""" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from math import pi, cos, sin |
| 9 |
|
| 10 |
from tvtk.tools.visual import vector, box, cylinder, frame, curve, \ |
| 11 |
iterate, show |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
Lshaft = 1. |
| 20 |
Rshaft = 0.03 |
| 21 |
M = 1. |
| 22 |
Rrotor = 0.4 |
| 23 |
Drotor = 0.1 |
| 24 |
Dsquare = 1.4*Drotor |
| 25 |
I = 0.5*M*Rrotor**2. |
| 26 |
hpedestal = Lshaft |
| 27 |
wpedestal = 0.1 |
| 28 |
tbase = 0.05 |
| 29 |
wbase = 3.*wpedestal |
| 30 |
g = 9.8 |
| 31 |
Fgrav = vector(0,-M*g,0) |
| 32 |
top = vector(0,0,0) |
| 33 |
|
| 34 |
theta = pi/3. |
| 35 |
thetadot = 0 |
| 36 |
alpha = 0 |
| 37 |
alphadot = 15 |
| 38 |
phi = -pi/2. |
| 39 |
phidot = 0 |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
pedestal = box(pos=top-vector(0,hpedestal/2.,0), |
| 44 |
height=hpedestal, length=wpedestal, width=wpedestal, |
| 45 |
color=(0.4,0.4,0.5)) |
| 46 |
base = box(pos=top-vector(0,hpedestal+tbase/2.,0), |
| 47 |
height=tbase, length=wbase, width=wbase, |
| 48 |
color=pedestal.color) |
| 49 |
|
| 50 |
shaft = cylinder(axis=(Lshaft,0,0), length = Lshaft, |
| 51 |
radius=Rshaft, color=(0,1,0)) |
| 52 |
rotor = cylinder(pos=(Lshaft/2 - Drotor/2, 0, 0), |
| 53 |
axis=(Drotor, 0, 0), length = Drotor, |
| 54 |
radius=Rrotor, color=(1,0,0)) |
| 55 |
|
| 56 |
gyro = frame(shaft, rotor) |
| 57 |
gyro.axis = (sin(theta)*sin(phi),cos(theta),sin(theta)*cos(phi)) |
| 58 |
|
| 59 |
trail = curve(radius=Rshaft/8., color=(1,1,0)) |
| 60 |
|
| 61 |
r = Lshaft/2. |
| 62 |
dt = 0.0001 |
| 63 |
t = 0. |
| 64 |
Nsteps = 20 |
| 65 |
|
| 66 |
def anim(): |
| 67 |
global theta, phidot, alphadot, M, g, r, thetadot, phi, alpha, t |
| 68 |
for step in range(Nsteps): |
| 69 |
|
| 70 |
atheta = (phidot**2*sin(theta)*cos(theta) |
| 71 |
-2.*(alphadot+phidot*cos(theta))*phidot*sin(theta) |
| 72 |
+2.*M*g*r*sin(theta)/I) |
| 73 |
aphi = 2.*thetadot*(alphadot-phidot*cos(theta))/sin(theta) |
| 74 |
aalpha = phidot*thetadot*sin(theta)-aphi*cos(theta) |
| 75 |
|
| 76 |
thetadot = thetadot+atheta*dt |
| 77 |
phidot = phidot+aphi*dt |
| 78 |
alphadot = alphadot+aalpha*dt |
| 79 |
|
| 80 |
theta = theta+thetadot*dt |
| 81 |
phi = phi+phidot*dt |
| 82 |
alpha = alpha+alphadot*dt |
| 83 |
|
| 84 |
gyro.axis = vector(sin(theta)*sin(phi),cos(theta),sin(theta)*cos(phi)) |
| 85 |
|
| 86 |
gyro.rotate(axis = gyro.axis, angle=alphadot*dt*Nsteps, origin = gyro.pos) |
| 87 |
trail.append(gyro.pos + gyro.axis * Lshaft) |
| 88 |
t = t+dt*Nsteps |
| 89 |
|
| 90 |
a = iterate(40, anim) |
| 91 |
a.edit_traits() |
| 92 |
show() |