gyroscope1.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Simulation of a gyroscope hanging from a spring"""
  2. # (adapted by M. Musy from Bruce Sherwood, 2009)
  3. from vedo import *
  4. # ############################################################ parameters
  5. dt = 0.005 # time step
  6. ks = 15 # spring stiffness
  7. Lrest = 1 # unstretched length of spring
  8. Ls = 1 # length of gyroscope shaft
  9. M = 1 # mass of gyroscope (massless shaft)
  10. R = 0.4 # radius of gyroscope rotor
  11. omega = 50 # angular velocity of rotor (rad/s, not shown)
  12. gpos = vector(0, 0, 0) # initial position of spring free end
  13. # ############################################################ inits
  14. top = vector(0, 2, 0) # where top of spring is held
  15. precess = vector(0, 0, 0) # initial momentum of center of mass
  16. Fgrav = vector(0, -M * 9.81, 0)
  17. gaxis = vector(0, 0, 1) # initial orientation of gyroscope
  18. gaxis = versor(gaxis)
  19. I = 1 / 2 * M * R ** 2 # moment of inertia of gyroscope
  20. Lrot = I * omega * gaxis # angular momentum
  21. cm = gpos + 0.5 * Ls * gaxis # center of mass of shaft
  22. # ############################################################ the scene
  23. plt = Plotter()
  24. plt += __doc__
  25. shaft = Cylinder([[0, 0, 0], Ls * gaxis], r=0.03).c("dark green")
  26. rotor = Cylinder([(Ls - 0.55) * gaxis, (Ls - 0.45) * gaxis], r=R).c("tomato")
  27. gyro = shaft + rotor # group meshes into a single one of type Assembly
  28. spring = Spring(top, gpos, r1=0.06, thickness=0.01).c("gray")
  29. plt += [gyro, spring] # add it to Plotter.
  30. plt += Box(top, size=(0.2, 0.02, 0.2)).c("gray")
  31. plt += Box(pos=(0, 0.5, 0), size=(2.6, 3, 2.6)).wireframe().c("gray",0.2)
  32. # ############################################################ the physics
  33. def loop_func(event):
  34. global t, Lrot, precess, cm, gpos
  35. t += dt
  36. Fspring = -ks * versor(gpos - top) * (mag(gpos - top) - Lrest)
  37. torque = cross(-1/2 * Ls * versor(Lrot), Fspring) # torque about center of mass
  38. Lrot += torque * dt
  39. precess += (Fgrav + Fspring) * dt # momentum of center of mass
  40. cm += (precess / M) * dt
  41. gpos = cm - 1/2 * Ls * versor(Lrot)
  42. # set orientation along gaxis and rotate it around its axis by omega*t degrees
  43. gyro.reorient([0,0,1], Lrot, rotation=omega*t, rad=True).pos(gpos)
  44. spring = Spring(top, gpos, r1=0.06, thickness=0.01).c("gray")
  45. plt.remove("Spring").add(spring)
  46. plt.render()
  47. t = 0
  48. plt.add_callback("timer", loop_func)
  49. plt.timer_callback("start")
  50. plt.show().close()