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 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 in the Journal immediately before typing
(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**
hspeed -=*800/(point_distance2*point_distance)
vspeed -=*800/(point_distance2*point_distance) #old x coordinate #old y coordinate
nx=ox + hspeed #new x coordinate
ny=oy + vspeed #new y coordinate
setlayer(,630) #make turtle visible
setxy(, nx, ny) #jump to new coord.
lc.heap.append(vspeed) #push vert speed onto heap
lc.heap.append(hspeed) #push hor speed onto heap

What was difficult:
Any syntax error in 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(,630) but don't fully understand.

Labels: , ,


Blogger Tony Forster said...

The following modification to the wait block in 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):
endtime = millis()+ time*100
while millis()< endtime:
yield True
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 *

pu(); goto(200,0); pd()

G = 800
v = Vec2D(0, 1)
t = 0
while t < 1000:
____goto(pos() + v)
____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 and mailing lists.

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

See for the Earth Treasury Digital Textbook plan and current partners.

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

Thanks Gregor and Edward. Gregor's 3 code samples are at

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 Tony Forster 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
NameError: name 'shape' is not defined

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

Gregor meant to recommend the 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:"

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