Skip to content

Commit 1d5b651

Browse files
WasabiFandlech
andauthored
Add simple Brickpi3 example (#50)
* Add simple Brickpi3 example * newine * minor updates * Update brickpi3-motor-and-sensor.py `set_device` on output port is not supported Co-authored-by: David Lechner <david@pybricks.com>
1 parent 3fdeb3a commit 1d5b651

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

platform/brickpi3-motor-and-sensor.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
The Brickpi3 doesn't support auto-detecting motors and sensors. To use devices
5+
connected to the input ports, you must specify what type of device it is.
6+
Output ports are pre-configured as NXT Large motors and do not need to be
7+
configured manually.
8+
"""
9+
10+
from time import sleep
11+
from ev3dev2 import list_devices
12+
from ev3dev2.port import LegoPort
13+
from ev3dev2.motor import OUTPUT_A, LargeMotor, SpeedPercent
14+
from ev3dev2.sensor import INPUT_1
15+
from ev3dev2.sensor.lego import UltrasonicSensor
16+
17+
p1 = LegoPort(INPUT_1)
18+
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/brickpi3.html#brickpi3-in-port-modes
19+
p1.mode = 'ev3-uart'
20+
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensors.html#supported-sensors
21+
p1.set_device = 'lego-ev3-us'
22+
23+
# allow for some time to load the new drivers
24+
sleep(0.5)
25+
26+
s = UltrasonicSensor(INPUT_1)
27+
m = LargeMotor(OUTPUT_A)
28+
29+
print("Running motor...")
30+
31+
while True:
32+
dist = s.distance_centimeters
33+
if dist < 50:
34+
m.on(SpeedPercent(30))
35+
else:
36+
m.on(SpeedPercent(-30))
37+
38+
sleep(0.05)

0 commit comments

Comments
 (0)