Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
NXTurtle is an extension to the Turtle graphics for Tk module that comes with Python. Therefore programming NXTurtle is very similar to programming the standard turtle module. So, first you might want to read the introduction to the turtle graphics module.
Have you read it? Fine, so you know how to use common functions, like
There are some differences though:
Moving and turning commands are also sent to the connected LEGO brick.
A LEGO brick can't jump, so
turtle.reset(), etc. are automatically converted to turns and moves.
The turtle is probably not moving as precisely as we would hope, so don't expect perfect squares ;-)
These functions are not supported:
turtle.circle()is not yet implemented.
- Other drawing functions like
.tilt()are not available
- Still working on the
But there is also some additional functionality: (Most of this just exposes functions that the NXT-Python brick driver provides.)
turtle.play_tone(freq, ms)plays a tone for the specified duration.
- Plus everything that NXT-Python provides, using
turtle.brick., for example
First, make sure that you have read the installation instructions and build instructions. Sample turtle drawing a square (sort of)
from nxturtle import NXTurtle import math ### Create the turtle and connect to LEGO NXT brick turtle = NXTurtle(connect=True) ### Calibrate # All turtles are not created equal, so we have to define how many motor turns # it takes to move it by one unit, or turn it by one degree. # Here we do this by a combination of calculation and empirical information ;-) # # We assume this preconditions: # - The left wheel is expected to be connected to motor B. # - The right wheel is at motor C. # - Motor A is used to control the pen. # Distance between left and right wheel in [cm] (measured from middle of treads) AXIS_LENGTH = 12.5 # Wheel diameter in [cm] WHEEL_DIAMETER = 4.4 # Now we can calculate the number of wheel turns it takes, to move the turtle # by one unit (i.e. one centimeter) tachoPerUnit = 360.0 / (WHEEL_DIAMETER * math.pi) correction = 1.00 turtle.set_tacho_units_per_unit(correction * tachoPerUnit) # It should also be possible to calculate the number of wheel turns it takes, # to turn the turtle by one degree tachoPerDegree = AXIS_LENGTH / WHEEL_DIAMETER # ... observation shows that Archimedes was wrong, or our turtle is not # perfect. Either way this correction factor will improve the results: correction = 0.90 turtle.set_tacho_units_per_degree(correction * tachoPerDegree) # This function is passed to our turtle. It will be called whenever the pen # should be raised or put down. def pen_handler(turtle, on): power = 50 tacho_units = 150 if on: power *= -1 turtle.penMotor.turn(power, tacho_units) turtle.set_pen_handler(pen_handler) ### Go an try it # get set... turtle.pendown() # draw a square with 25 cm edge length turtle.fd(25) turtle.lt(90) turtle.fd(25) turtle.lt(90) turtle.fd(25) turtle.lt(90) turtle.fd(25) # walk to the center turtle.lt(135) turtle.fd(18) # wait for 5 seconds turtle.wait(5000) ### Close connection (will also raise the pen) turtle.disconnect()
This is how it looks like in action
Using brick commands
Here we are using some functions that separate NXTurtle from a plain Logo turtle ;-)
from nxturtle import NXTurtle turtle = NXTurtle(connect=True) [...] Calibrate and set pen handler as in the sample above ### Access name and other info turtle.set_name("Yertle") print "Hello, my name is '%s'" % turtle.get_name() print "Info: " print turtle.get_brick_info() ### Sounds # Play 'a' note for half a second turtle.play_tone(440, 500) print "I know these sounds:" for name in turtle.find_files("*.rso"): print "sound", name # Play a sound file ('rso' extension is added by default) turtle.play_sound_file("Goodmorning") # starting a sound will stop the previous, so we wait a second turtle.wait(1000) turtle.play_sound_file("Alarm.rso") ### Programs print "I know these programs:" for name in turtle.find_files("*.rxe"): print "program", name turtle.start_program("Test.rxe") ### Close connection (will also raise the pen) turtle.disconnect()
A very simple example. Please refer to NXT-Python for details.
from nxturtle import NXTurtle turtle = NXTurtle(connect=True) [...] Calibrate and set pen handler as in the sample above # Attach sensor turtle.eyes = Ultrasonic(turtle.brick, PORT_4) # Read sensor data print "I can see for %s miles..." % turtle.eyes.get_distance() ### Close connection (will also raise the pen) turtle.disconnect()