pendulum.tcl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # pendulum.tcl --
  2. #
  3. # This demonstration illustrates how Tcl/Tk can be used to construct
  4. # simulations of physical systems.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .pendulum
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Pendulum Animation Demonstration"
  13. wm iconname $w "pendulum"
  14. positionWindow $w
  15. label $w.msg -font $font -wraplength 4i -justify left -text "This demonstration shows how Tcl/Tk can be used to carry out animations that are linked to simulations of physical systems. In the left canvas is a graphical representation of the physical system itself, a simple pendulum, and in the right canvas is a graph of the phase space of the system, which is a plot of the angle (relative to the vertical) against the angular velocity. The pendulum bob may be repositioned by clicking and dragging anywhere on the left canvas."
  16. pack $w.msg
  17. ## See Code / Dismiss buttons
  18. set btns [addSeeDismiss $w.buttons $w]
  19. pack $btns -side bottom -fill x
  20. # Create some structural widgets
  21. pack [panedwindow $w.p] -fill both -expand 1
  22. $w.p add [labelframe $w.p.l1 -text "Pendulum Simulation"]
  23. $w.p add [labelframe $w.p.l2 -text "Phase Space"]
  24. # Create the canvas containing the graphical representation of the
  25. # simulated system.
  26. canvas $w.c -width 320 -height 200 -background white -bd 2 -relief sunken
  27. $w.c create text 5 5 -anchor nw -text "Click to Adjust Bob Start Position"
  28. # Coordinates of these items don't matter; they will be set properly below
  29. $w.c create line 0 25 320 25 -tags plate -fill grey50 -width 2
  30. $w.c create oval 155 20 165 30 -tags pivot -fill grey50 -outline {}
  31. $w.c create line 1 1 1 1 -tags rod -fill black -width 3
  32. $w.c create oval 1 1 2 2 -tags bob -fill yellow -outline black
  33. pack $w.c -in $w.p.l1 -fill both -expand true
  34. # Create the canvas containing the phase space graph; this consists of
  35. # a line that gets gradually paler as it ages, which is an extremely
  36. # effective visual trick.
  37. canvas $w.k -width 320 -height 200 -background white -bd 2 -relief sunken
  38. $w.k create line 160 200 160 0 -fill grey75 -arrow last -tags y_axis
  39. $w.k create line 0 100 320 100 -fill grey75 -arrow last -tags x_axis
  40. for {set i 90} {$i>=0} {incr i -10} {
  41. # Coordinates of these items don't matter; they will be set properly below
  42. $w.k create line 0 0 1 1 -smooth true -tags graph$i -fill grey$i
  43. }
  44. $w.k create text 0 0 -anchor ne -text "\u03b8" -tags label_theta
  45. $w.k create text 0 0 -anchor ne -text "\u03b4\u03b8" -tags label_dtheta
  46. pack $w.k -in $w.p.l2 -fill both -expand true
  47. # Initialize some variables
  48. set points {}
  49. set Theta 45.0
  50. set dTheta 0.0
  51. set pi 3.1415926535897933
  52. set length 150
  53. set home 160
  54. # This procedure makes the pendulum appear at the correct place on the
  55. # canvas. If the additional arguments "at $x $y" are passed (the 'at'
  56. # is really just syntactic sugar) instead of computing the position of
  57. # the pendulum from the length of the pendulum rod and its angle, the
  58. # length and angle are computed in reverse from the given location
  59. # (which is taken to be the centre of the pendulum bob.)
  60. proc showPendulum {canvas {at {}} {x {}} {y {}}} {
  61. global Theta dTheta pi length home
  62. if {$at eq "at" && ($x!=$home || $y!=25)} {
  63. set dTheta 0.0
  64. set x2 [expr {$x - $home}]
  65. set y2 [expr {$y - 25}]
  66. set length [expr {hypot($x2, $y2)}]
  67. set Theta [expr {atan2($x2, $y2) * 180/$pi}]
  68. } else {
  69. set angle [expr {$Theta * $pi/180}]
  70. set x [expr {$home + $length*sin($angle)}]
  71. set y [expr {25 + $length*cos($angle)}]
  72. }
  73. $canvas coords rod $home 25 $x $y
  74. $canvas coords bob \
  75. [expr {$x-15}] [expr {$y-15}] [expr {$x+15}] [expr {$y+15}]
  76. }
  77. showPendulum $w.c
  78. # Update the phase-space graph according to the current angle and the
  79. # rate at which the angle is changing (the first derivative with
  80. # respect to time.)
  81. proc showPhase {canvas} {
  82. global Theta dTheta points psw psh
  83. lappend points [expr {$Theta+$psw}] [expr {-20*$dTheta+$psh}]
  84. if {[llength $points] > 100} {
  85. set points [lrange $points end-99 end]
  86. }
  87. for {set i 0} {$i<100} {incr i 10} {
  88. set list [lrange $points end-[expr {$i-1}] end-[expr {$i-12}]]
  89. if {[llength $list] >= 4} {
  90. $canvas coords graph$i $list
  91. }
  92. }
  93. }
  94. # Set up some bindings on the canvases. Note that when the user
  95. # clicks we stop the animation until they release the mouse
  96. # button. Also note that both canvases are sensitive to <Configure>
  97. # events, which allows them to find out when they have been resized by
  98. # the user.
  99. bind $w.c <Destroy> {
  100. after cancel $animationCallbacks(pendulum)
  101. unset animationCallbacks(pendulum)
  102. }
  103. bind $w.c <Button-1> {
  104. after cancel $animationCallbacks(pendulum)
  105. showPendulum %W at %x %y
  106. }
  107. bind $w.c <B1-Motion> {
  108. showPendulum %W at %x %y
  109. }
  110. bind $w.c <ButtonRelease-1> {
  111. showPendulum %W at %x %y
  112. set animationCallbacks(pendulum) [after 15 repeat [winfo toplevel %W]]
  113. }
  114. bind $w.c <Configure> {
  115. %W coords plate 0 25 %w 25
  116. set home [expr {%w/2}]
  117. %W coords pivot [expr {$home-5}] 20 [expr {$home+5}] 30
  118. }
  119. bind $w.k <Configure> {
  120. set psh [expr {%h/2}]
  121. set psw [expr {%w/2}]
  122. %W coords x_axis 2 $psh [expr {%w-2}] $psh
  123. %W coords y_axis $psw [expr {%h-2}] $psw 2
  124. %W coords label_dtheta [expr {$psw-4}] 6
  125. %W coords label_theta [expr {%w-6}] [expr {$psh+4}]
  126. }
  127. # This procedure is the "business" part of the simulation that does
  128. # simple numerical integration of the formula for a simple rotational
  129. # pendulum.
  130. proc recomputeAngle {} {
  131. global Theta dTheta pi length
  132. set scaling [expr {3000.0/$length/$length}]
  133. # To estimate the integration accurately, we really need to
  134. # compute the end-point of our time-step. But to do *that*, we
  135. # need to estimate the integration accurately! So we try this
  136. # technique, which is inaccurate, but better than doing it in a
  137. # single step. What we really want is bound up in the
  138. # differential equation:
  139. # .. - sin theta
  140. # theta + theta = -----------
  141. # length
  142. # But my math skills are not good enough to solve this!
  143. # first estimate
  144. set firstDDTheta [expr {-sin($Theta * $pi/180)*$scaling}]
  145. set midDTheta [expr {$dTheta + $firstDDTheta}]
  146. set midTheta [expr {$Theta + ($dTheta + $midDTheta)/2}]
  147. # second estimate
  148. set midDDTheta [expr {-sin($midTheta * $pi/180)*$scaling}]
  149. set midDTheta [expr {$dTheta + ($firstDDTheta + $midDDTheta)/2}]
  150. set midTheta [expr {$Theta + ($dTheta + $midDTheta)/2}]
  151. # Now we do a double-estimate approach for getting the final value
  152. # first estimate
  153. set midDDTheta [expr {-sin($midTheta * $pi/180)*$scaling}]
  154. set lastDTheta [expr {$midDTheta + $midDDTheta}]
  155. set lastTheta [expr {$midTheta + ($midDTheta + $lastDTheta)/2}]
  156. # second estimate
  157. set lastDDTheta [expr {-sin($lastTheta * $pi/180)*$scaling}]
  158. set lastDTheta [expr {$midDTheta + ($midDDTheta + $lastDDTheta)/2}]
  159. set lastTheta [expr {$midTheta + ($midDTheta + $lastDTheta)/2}]
  160. # Now put the values back in our globals
  161. set dTheta $lastDTheta
  162. set Theta $lastTheta
  163. }
  164. # This method ties together the simulation engine and the graphical
  165. # display code that visualizes it.
  166. proc repeat w {
  167. global animationCallbacks
  168. # Simulate
  169. recomputeAngle
  170. # Update the display
  171. showPendulum $w.c
  172. showPhase $w.k
  173. # Reschedule ourselves
  174. set animationCallbacks(pendulum) [after 15 [list repeat $w]]
  175. }
  176. # Start the simulation after a short pause
  177. set animationCallbacks(pendulum) [after 500 [list repeat $w]]