aboutsummaryrefslogtreecommitdiff
path: root/AT91SAM7S256/armdebug/nxt-python-fantom/examples
diff options
context:
space:
mode:
Diffstat (limited to 'AT91SAM7S256/armdebug/nxt-python-fantom/examples')
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/examples/alpharex.py151
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/examples/cnc.py53
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/examples/latency.py23
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/examples/mary.py45
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/examples/message_test.py15
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/examples/spin.py14
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/examples/test_sensors.py11
7 files changed, 312 insertions, 0 deletions
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/examples/alpharex.py b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/alpharex.py
new file mode 100644
index 0000000..13c5979
--- /dev/null
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/alpharex.py
@@ -0,0 +1,151 @@
+r'''Alpha Rex API
+
+ A high-level, object-oriented programming interface to Lego MINDSTORMS
+ NXT's "Alpha Rex" model (see [1] for assembling instructions), along with a
+ small collection of functions demonstrating obvious usage scenarios.
+
+ 1. http://www.active-robots.com/products/mindstorms4schools/building-instructions.shtml
+'''
+
+from time import sleep
+
+from nxt.brick import Brick
+from nxt.locator import find_one_brick
+from nxt.motor import Motor, PORT_A, PORT_B, PORT_C
+from nxt.sensor import Light, Sound, Touch, Ultrasonic
+from nxt.sensor import PORT_1, PORT_2, PORT_3, PORT_4
+
+
+FORTH = 100
+BACK = -100
+
+
+class AlphaRex(object):
+ r'''A high-level controller for the Alpha Rex model.
+
+ This class implements methods for the most obvious actions performable
+ by Alpha Rex, such as walk, wave its arms, and retrieve sensor samples.
+ Additionally, it also allows direct access to the robot's components
+ through public attributes.
+ '''
+ def __init__(self, brick='NXT'):
+ r'''Creates a new Alpha Rex controller.
+
+ brick
+ Either an nxt.brick.Brick object, or an NXT brick's name as a
+ string. If omitted, a Brick named 'NXT' is looked up.
+ '''
+ if isinstance(brick, basestring):
+ brick = find_one_brick(name=brick)
+
+ self.brick = brick
+ self.arms = Motor(brick, PORT_A)
+ self.legs = [Motor(brick, PORT_B), Motor(brick, PORT_C)]
+
+ self.touch = Touch(brick, PORT_1)
+ self.sound = Sound(brick, PORT_2)
+ self.light = Light(brick, PORT_3)
+ self.ultrasonic = Ultrasonic(brick, PORT_4)
+
+ def echolocate(self):
+ r'''Reads the Ultrasonic sensor's output.
+ '''
+ return self.ultrasonic.get_sample()
+
+ def feel(self):
+ r'''Reads the Touch sensor's output.
+ '''
+ return self.touch.get_sample()
+
+ def hear(self):
+ r'''Reads the Sound sensor's output.
+ '''
+ return self.sound.get_sample()
+
+ def say(self, line, times=1):
+ r'''Plays a sound file named (line + '.rso'), which is expected to be
+ stored in the brick. The file is played (times) times.
+
+ line
+ The name of a sound file stored in the brick.
+
+ times
+ How many times the sound file will be played before this method
+ returns.
+ '''
+ for i in range(0, times):
+ self.brick.play_sound_file(False, line + '.rso')
+ sleep(1)
+
+ def see(self):
+ r'''Reads the Light sensor's output.
+ '''
+ return self.light.get_sample()
+
+ def walk(self, secs, power=FORTH):
+ r'''Simultaneously activates the leg motors, causing Alpha Rex to walk.
+
+ secs
+ How long the motors will rotate.
+
+ power
+ The strength effected by the motors. Positive values will cause
+ Alpha Rex to walk forward, while negative values will cause it
+ to walk backwards. If you are unsure about how much force to
+ apply, the special values FORTH and BACK provide reasonable
+ defaults. If omitted, FORTH is used.
+ '''
+ for motor in self.legs:
+ motor.run(power=power)
+
+ sleep(secs)
+
+ for motor in self.legs:
+ motor.idle()
+
+ def wave(self, secs, power=100):
+ r'''Make Alpha Rex move its arms.
+
+ secs
+ How long the arms' motor will rotate.
+
+ power
+ The strength effected by the motor. If omitted, (100) is used.
+ '''
+ self.arms.run(power=power)
+ sleep(secs)
+ self.arms.idle()
+
+
+def wave_and_talk():
+ r'''Connects to a nearby Alpha Rex, then commands it to wave its arms and
+ play the sound file 'Object.rso'.
+ '''
+ robot = AlphaRex()
+ robot.wave(1)
+ robot.say('Object')
+
+
+def walk_forth_and_back():
+ r'''Connects to a nearby Alpha Rex, then commands it to walk forward and
+ then backwards.
+ '''
+ robot = AlphaRex()
+ robot.walk(10, FORTH)
+ robot.walk(10, BACK)
+
+
+def walk_to_object():
+ r'''Connects to a nearby Alpha Rex, then commands it to walk until it
+ approaches an obstacle, then stop and say 'Object' three times.
+ '''
+ robot = AlphaRex()
+ while robot.echolocate() > 10:
+ robot.walk(1, FORTH)
+
+ robot.say('Object', 3)
+
+
+if __name__ == '__main__':
+ walk_to_object()
+
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/examples/cnc.py b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/cnc.py
new file mode 100644
index 0000000..456cf92
--- /dev/null
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/cnc.py
@@ -0,0 +1,53 @@
+#Script to control a NXT 2-axis CNC "Pancake maker"
+#Illustrates controlling more than one motor at the same time without trying to
+#sync them. Uses the thread module.
+#Written 2/3/11 by Marcus Wanner
+#
+#For more info and warnings see:
+#http://groups.google.com/group/nxt-python/browse_thread/thread/f6ef0865ae768ef
+
+import nxt, thread, time
+b = nxt.find_one_brick(name = 'MyNXT')
+mx = nxt.Motor(b, nxt.PORT_A)
+my = nxt.Motor(b, nxt.PORT_B)
+motors = [mx, my]
+
+def turnmotor(m, power, degrees):
+ m.turn(power, degrees)
+
+#here are the instructions...
+#the first value is the time to start the instruction
+#the second is the axis (0 for x, 1 for y)
+#the third is the power
+#the fourth is the degrees
+#it's probably not a good idea to run simultaneous turn
+#functions on a single motor, so be careful with this
+instructions = (
+ [0, 0, 80, 180],
+ [0, 1, -40, 1080],
+ [1, 0, -80, 180],
+ [2, 0, 80, 180],
+ [3, 1, 100, 360],
+ [3, 0, -100, 360],
+)
+#how long from start until the last instruction is ended
+length = 5
+
+def runinstruction(i):
+ motorid, speed, degrees = i
+ #THIS IS THE IMPORTANT PART!
+ thread.start_new_thread(
+ turnmotor,
+ (motors[motorid], speed, degrees))
+
+#main loop
+seconds = 0
+while 1:
+ print "Tick %d" % seconds
+ for i in instructions:
+ if i[0] == seconds:
+ runinstruction(i[1:])
+ seconds = seconds + 1
+ if seconds >= length:
+ break
+ time.sleep(1)
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/examples/latency.py b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/latency.py
new file mode 100644
index 0000000..b4b1f26
--- /dev/null
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/latency.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+
+import time
+import nxt.locator
+from nxt.sensor import *
+
+b = nxt.locator.find_one_brick()
+
+#Touch sensor latency test
+touch = Touch(b, PORT_1)
+start = time.time()
+for i in range(100):
+ touch.get_sample()
+stop = time.time()
+print 'touch latency: %s ms' % (1000 * (stop - start) / 100.0)
+
+#Ultrasonic sensor latency test
+ultrasonic = Ultrasonic(b, PORT_4)
+start = time.time()
+for i in range(100):
+ ultrasonic.get_sample()
+stop = time.time()
+print 'ultrasonic latency: %s ms' % (1000 * (stop - start) / 100.0)
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/examples/mary.py b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/mary.py
new file mode 100644
index 0000000..907cc23
--- /dev/null
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/mary.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+#
+# Converted from mary.rb found in ruby_nxt package
+# Plays "Mary Had A Little Lamb"
+# Author: Christopher Continanza <christopher.continanza@villanova.edu>
+
+from time import sleep
+import nxt.locator
+
+FREQ_C = 523
+FREQ_D = 587
+FREQ_E = 659
+FREQ_G = 784
+
+b = nxt.locator.find_one_brick()
+
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_C, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_E, 500)
+sleep(0.5)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+sleep(0.5)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_G, 500)
+b.play_tone_and_wait(FREQ_G, 500)
+sleep(0.5)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_C, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_E, 500)
+b.play_tone_and_wait(FREQ_D, 500)
+b.play_tone_and_wait(FREQ_C, 750)
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/examples/message_test.py b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/message_test.py
new file mode 100644
index 0000000..c2fbda7
--- /dev/null
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/message_test.py
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+
+#During this test you need to run any program on the brick
+#which doesn't use the messaging system. Most programs fit
+#this requirement.
+
+import nxt.locator
+
+b = nxt.locator.find_one_brick()
+for box in range(10):
+ b.message_write(box, 'message test %d' % box)
+for box in range(10):
+ local_box, message = b.message_read(box, box, True)
+ print local_box, message
+print 'Test succeeded!'
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/examples/spin.py b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/spin.py
new file mode 100644
index 0000000..fc7cb2c
--- /dev/null
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/spin.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+import nxt.locator
+from nxt.motor import *
+
+def spin_around(b):
+ m_left = Motor(b, PORT_B)
+ m_left.turn(100, 360)
+ m_right = Motor(b, PORT_C)
+ m_right.turn(-100, 360)
+
+b = nxt.locator.find_one_brick()
+spin_around(b)
+
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/examples/test_sensors.py b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/test_sensors.py
new file mode 100644
index 0000000..4c8d249
--- /dev/null
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/examples/test_sensors.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+
+import nxt.locator
+from nxt.sensor import *
+
+b = nxt.locator.find_one_brick()
+
+print 'Touch:', Touch(b, PORT_1).get_sample()
+print 'Sound:', Sound(b, PORT_2).get_sample()
+print 'Light:', Light(b, PORT_3).get_sample()
+print 'Ultrasonic:', Ultrasonic(b, PORT_4).get_sample()