Thursday, March 05, 2009

Orbital motion in Python and TurtleArt

Orbital motion according to Newton's law of gravity using the programmable block in the experimental fork of TurtleArt for the OLPC. (Now included in Turtle Art from V44)

Intended to demonstrate two things,
a) that programmable simulations are good ways for kids to learn physics and maths
b) that the programmable block provides a way for kids to move from simple drag and drop programming to more complicated text based programming

As at V18 of experimental TurtleArt, Rainbow is preventing the loading of the file tamyblock.py from the journal. See previous post. The following hack gets round the problem.

Open Terminal,
go to /home/olpc/Activities/TurtleArtPortfolio.activity
Save your edited tamyblock.py in the Journal immediately before typing
copy-from-journal tamyblock.py
mv tamyblock..py tamyblock.py
(as at v19 this is not required, neither are the 2 imports)





# Orbital motion with inverse square law
# Tony Forster, March 2009
#
# For each iteration, acceleration is calculated
# as inversely proportional to orbital distance.
# The horizontal and vertical components of the
# acceleration are added to the horizontal and
# vertical components of the velocity.
# The horizontal and vertical velocity components are
# added to the x and y coordinates.
# A line is drawn from the old to the new coordinates.
# The turtle is moved.

from taturtle import *
from math import *
def myblock(lc,x): # x is not used
hspeed=lc.heap.pop(-1) # pop hor speed from the heap
vspeed=lc.heap.pop(-1) # pop ver speed from the heap
point_distance2=lc.tw.turtle.xcor*lc.tw.turtle.xcor+
lc.tw.turtle.ycor*lc.tw.turtle.ycor
point_distance=sqrt(point_distance2)
hspeed -= lc.tw.turtle.xcor*800/(point_distance2*point_distance)
vspeed -= lc.tw.turtle.ycor*800/(point_distance2*point_distance)
ox=lc.tw.turtle.xcor #old x coordinate
oy=lc.tw.turtle.ycor #old y coordinate
nx=ox + hspeed #new x coordinate
ny=oy + vspeed #new y coordinate
setlayer(lc.tw.turtle.spr,630) #make turtle visible
setxy(lc.tw.turtle, nx, ny) #jump to new coord.
draw_line(lc.tw.turtle,ox,oy,nx,ny)
lc.heap.append(vspeed) #push vert speed onto heap
lc.heap.append(hspeed) #push hor speed onto heap
return


What was difficult:
Any syntax error in tamyblock.py will prevent TurtleArt from loading.
The Rainbow problem makes it more complicated, also copy-from-journal is unreliable and poorly documented, I obviously do not understand the syntax.
The error log shows where your error was in the source listing but if you haven't reloaded TurtleArt, you can be running one version of the code and seeing errors in a different version.
The TurtleArt wait block is necessary to see the turtle but it slows execution too much, it could be shortened, i used a hack setlayer setlayer(lc.tw.turtle.spr,630) but don't fully understand.

Labels: , ,

6 Comments:

Blogger Unknown said...

The following modification to the wait block in talogo.py would allow it to take values <1. That means that the wait block could be placed in program loops to maintain turtle visibility but with values <1, the execution would be faster

def prim_wait(lc,time):
setlayer(lc.tw.turtle.spr,630)
endtime = millis()+ time*100
while millis()< endtime:
yield True
setlayer(lc.tw.turtle.spr,100)
ireturn(lc); yield True

Thursday, March 05, 2009 6:21:00 PM  
Blogger Gregor said...

You can do this with Python's own turtle module. For instance like this:

from turtle import *

color("orange")
dot(10)
color("blue")
shape("turtle")
speed(0)
pu(); goto(200,0); pd()

G = 800
v = Vec2D(0, 1)
t = 0
while t < 1000:
____goto(pos() + v)
____setheading(towards(0,0))
____r = distance(0,0)
____acc = (-G/r**3)*pos()
____v = v + acc
____t = t + 1

Regards, Gregor

Thursday, March 26, 2009 3:59:00 AM  
Blogger Edward said...

I did something similar in Tutsim on the old Apple II in 1982.

We are discussing this and other uses of Turtle Art in physics, math, and Computer Science on the edu-sig@python.org and fourthgrademath@lists.sugarlabs.org mailing lists.

Gregor Lingl just sent three different TA programs for orbits to edu-sig, including the one he posted here.

See http://wiki.sugarlabs.org/go/Creating_textbooks for the Earth Treasury Digital Textbook plan and current partners.

Thursday, March 26, 2009 6:22:00 AM  
Blogger Unknown said...

Thanks Gregor and Edward. Gregor's 3 code samples are at http://mail.python.org/pipermail/edu-sig/2009-March/009179.html

I presume that they do not require TurtleArt, just Python.

Gregor says turtle is: "Python's own turtle module (since 3.6)"

I suspect that Sugar (0.82.1 767) is based on an earlier Python. I pasted into Pippy and got Import Error: no module named Turtle

Thursday, March 26, 2009 9:59:00 AM  
Blogger Unknown said...

There is no Python 3.6, build 767 uses Python 2.5.1

Tried running Gregor's code in Idle on the PC (python 2.5.2)
File "C:/Python25/test", line 5, in module
shape("turtle")
NameError: name 'shape' is not defined

Thursday, March 26, 2009 10:47:00 AM  
Blogger Edward said...

Gregor meant to recommend the turtle.py module in Python 2.6

"This has a new Python module
It runs also with Python 2.5
You can download it separately from here:"

http://svn.python.org/view/python/trunk/Lib/lib-tk/turtle.py?view=log

I installed it and verified that it runs with Python 2.5, and all of Gregor's scripts then run fine.

Thursday, March 26, 2009 2:44:00 PM  

Post a Comment

<< Home