Debian Codegraph @

examples/tvtk/visual/gyro.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Author: Raashid Baig <[email protected]>
4 # License: BSD Style.
5
6 # Gyroscope hanging from a spring
7 from math import atan, cos, sin, pi
8
9 from tvtk.tools.visual import vector, MVector, Box, Helix, Frame, \
10 Cylinder, curve, color, iterate, show
11
12
13 top = vector(0,1.,0) # where top of spring is held
14 ks = 100. # spring stiffness
15 Lspring = 1. # unstretched length of spring
16 Rspring = 0.03 # radius of spring
17 Dspring = 0.03 # thickness of spring wire
18 Lshaft = 1. # length of gyroscope shaft
19 Rshaft = 0.03 # radius of gyroscope shaft
20 M = 1. # mass of gyroscope (massless shaft)
21 Rrotor = 0.4 # radius of gyroscope rotor
22 Drotor = 0.1 # thickness of gyroscope rotor
23 Dsquare = 1.4*Drotor # thickness of square that turns with rotor
24 I = 0.5*M*Rrotor**2. # moment of inertia of gyroscope
25 omega = 40.0 # angular velocity of rotor along axis
26 g = 9.8
27 Fgrav = MVector(0,-M*g,0)
28 precession = M*g*(Lshaft/2.)/(I*abs(omega)) # exact precession angular velocity
29 phi = atan(precession**2*(Lshaft/2.)/g) # approximate angle of spring to vertical
30 s = M*g/(ks*cos(phi)) # approximate stretch of spring
31 # Refine estimate of angle of spring to vertical:
32 phi = 1./(((I*abs(omega))/(M*Lshaft/2.))**2/(g*Lshaft/2.)-(Lspring+s)/(Lshaft/2.))
33 # Refine again:
34 s = M*g/(ks*cos(phi))
35 phi = 1./( ((I*abs(omega))/(M*Lshaft/2.))**2/(g*Lshaft/2.)-(Lspring+s)/(Lshaft/2.) )
36 # Momentum required for completely smooth precession:
37 pprecess = vector(0,0,M*precession*(Lshaft/2.+(Lspring+s)*sin(phi)))
38 if omega < 0:
39 pprecess = -pprecess
40
41 support = Box(pos = top+MVector(0,0.01,0), size = (0.2,0.02,0.2), color = (0,1,0))
42 spring = Helix(pos = top, axis = vector(-0.161579, -0.98686, 0), radius = Rspring, color = (1,0.7,0.2))
43
44 gyro1 = Frame(pos = top+spring.axis) # gyro.pos at end of spring
45 gyro1.axis = MVector(1,0,0)
46
47 shaft = Cylinder(pos = gyro1.pos, axis = Lshaft*gyro1.axis, radius =
48 Rshaft, color = (0.85,0.85,0.85), length = 1.0)
49
50 rotor = Cylinder(pos = 0.5*gyro1.axis*(Lshaft-Drotor), axis =
51 gyro1.axis*Drotor, radius = Rrotor, color = (0.5,0.5,0.5), length = 0.1)
52
53 stripe1 = curve(color = color.green,
54 points = [rotor.pos+1.03*rotor.axis+vector(0,Rrotor,0),
55 rotor.pos+1.03*rotor.axis-vector(0,Rrotor,0)])
56 stripe2 = curve(color = color.green,
57 points = [rotor.pos-0.03*rotor.axis+vector(0,Rrotor,0),
58 rotor.pos-0.03*rotor.axis-vector(0,Rrotor,0)])
59
60 gyro = Frame(stripe1, stripe2) # gyro.pos at end of spring
61 gyro.pos = top+spring.axis # gyro.pos at end of spring
62 gyro.axis = vector(1,0,0)
63
64 gyro.rotate(axis=(0,1,0), angle = 180.0, origin = gyro.pos)
65
66 cm = gyro.pos+0.5*Lshaft*gyro.axis # center of mass of shaft
67 Lrot = I*omega*gyro.axis
68 p = pprecess
69 dt = 0.01
70 t = 0.
71
72 c = 0
73
74 def anim():
75 global Lrot, p, cm, t
76 Fspring = -ks*vector.norm(spring.axis)*(vector.mag(spring.axis)-Lspring)
77 # Calculate torque about center of mass:
78 torque = vector.cross(-0.5*Lshaft*gyro.axis,Fspring)
79 Lrot = Lrot+torque*dt
80 p = p+(Fgrav+Fspring)*dt
81 cm = cm+(p/M)*dt
82
83 # Update positions of shaft, rotor, spring, stripes
84 if omega > 0:
85 gyro.axis = vector.norm(Lrot)
86 else:
87 gyro.axis = -vector.norm(Lrot)
88
89 gyro.pos = cm-0.5*Lshaft*gyro.axis # shaft rotated, adjust connection to spring
90 spring.axis = gyro.pos - top
91 spring.length = vector.mag(spring.axis)
92 gyro.rotate(180*omega*dt/(4.0*pi), gyro.axis, gyro.pos) # spin easier to see if slower than actual omega
93 shaft.pos = gyro.pos
94 shaft.axis = Lshaft*gyro.axis
95 rotor.pos = gyro.pos + 0.5*gyro.axis*(Lshaft-Drotor)
96 rotor.axis = gyro.axis*Drotor
97
98 t = t+dt
99
100 a = iterate(40, anim)
101 a.edit_traits()
102 show()