Skip to content

Commit 6749c99

Browse files
committed
Add examples
1 parent 5406d58 commit 6749c99

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ include docs/Makefile
55
include docs/conf.py
66
include docs/favicon.ico
77
recursive-include docs *.rst
8+
recursive-include examples *.py

examples/motcont.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
"""NXT-Python example to use Linus Atorf's MotorControl.
3+
4+
You need to have MotorControl22 program installed on the brick, you can find it here:
5+
6+
https://github.com/schodet/MotorControl
7+
8+
Build it and install it with nxc.
9+
"""
10+
import time
11+
12+
import nxt.locator
13+
import nxt.motcont
14+
import nxt.motor
15+
16+
with nxt.locator.find() as b:
17+
mc = nxt.motcont.MotCont(b)
18+
19+
def wait():
20+
while not mc.is_ready(nxt.motor.Port.A) or not mc.is_ready(nxt.motor.Port.B):
21+
time.sleep(0.5)
22+
23+
mc.start()
24+
25+
mc.cmd((nxt.motor.Port.A, nxt.motor.Port.B), 50, 360)
26+
wait()
27+
28+
mc.cmd((nxt.motor.Port.A, nxt.motor.Port.B), -50, 360)
29+
wait()
30+
31+
mc.stop()

examples/sensors.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
"""NXT-Python example to use sensors."""
3+
import time
4+
5+
import nxt.locator
6+
import nxt.sensor
7+
import nxt.sensor.generic
8+
9+
with nxt.locator.find() as b:
10+
touch = b.get_sensor(nxt.sensor.Port.S1, nxt.sensor.generic.Touch)
11+
sound = b.get_sensor(nxt.sensor.Port.S2, nxt.sensor.generic.Sound)
12+
light = b.get_sensor(nxt.sensor.Port.S3, nxt.sensor.generic.Light, False)
13+
us = b.get_sensor(nxt.sensor.Port.S4)
14+
sensors = [touch, sound, light, us]
15+
16+
while True:
17+
samples = [s.get_sample() for s in sensors]
18+
print(samples)
19+
time.sleep(0.5)

examples/tutorial/debug.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python3
2+
"""NXT-Python tutorial: increase log level."""
3+
import logging
4+
5+
import nxt.locator
6+
7+
# Increase the log level, must be done before using any NXT-Python function. See logging
8+
# documentation for details.
9+
logging.basicConfig(level=logging.DEBUG)
10+
11+
with nxt.locator.find() as b:
12+
b.play_tone(440, 250)

examples/tutorial/find.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/python3
2+
"""NXT-Python tutorial: find the brick."""
3+
import nxt.locator
4+
5+
# Find a brick.
6+
with nxt.locator.find() as b:
7+
# Once found, print its name.
8+
print("Found brick:", b.get_device_info()[0])
9+
# And play a recognizable note.
10+
b.play_tone(440, 250)

examples/tutorial/motor.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python3
2+
"""NXT-Python tutorial: turn a motor."""
3+
import nxt.locator
4+
import nxt.motor
5+
6+
with nxt.locator.find() as b:
7+
# Get the motor connected to the port A.
8+
mymotor = b.get_motor(nxt.motor.Port.A)
9+
# Full circle in one direction.
10+
mymotor.turn(25, 360)
11+
# Full circle in the opposite direction.
12+
mymotor.turn(-25, 360)

examples/tutorial/sensor_touch.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/python3
2+
"""NXT-Python tutorial: use touch sensor."""
3+
import time
4+
5+
import nxt.locator
6+
import nxt.sensor
7+
import nxt.sensor.generic
8+
9+
with nxt.locator.find() as b:
10+
# Get the sensor connected to port 1, not a digital sensor, must give the sensor
11+
# class.
12+
mysensor = b.get_sensor(nxt.sensor.Port.S1, nxt.sensor.generic.Touch)
13+
# Read the sensor in a loop (until interrupted).
14+
print("Use Ctrl-C to interrupt")
15+
while True:
16+
value = mysensor.get_sample()
17+
print(value)
18+
time.sleep(0.5)

examples/tutorial/sensor_us.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/python3
2+
"""NXT-Python tutorial: use ultra-sonic sensor."""
3+
import time
4+
5+
import nxt.locator
6+
import nxt.sensor
7+
8+
# Need to import generic sensors for auto-detection to work.
9+
import nxt.sensor.generic
10+
11+
with nxt.locator.find() as b:
12+
# Find the sensor connected to port 4.
13+
mysensor = b.get_sensor(nxt.sensor.Port.S4)
14+
# Read the sensor in a loop (until interrupted).
15+
print("Use Ctrl-C to interrupt")
16+
while True:
17+
distance_cm = mysensor.get_sample()
18+
print(distance_cm)
19+
time.sleep(0.5)

0 commit comments

Comments
 (0)