Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple Brickpi3 example #50

Merged
merged 4 commits into from
Mar 20, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions platform/brickpi3-motor-and-sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

"""
The Brickpi3 doesn't support auto-detecting motors and sensors. To use devices
connected to the LEGO ports, you must specify what type of device it is.
"""

from time import sleep
from ev3dev2 import list_devices
from ev3dev2.port import LegoPort
from ev3dev2.motor import OUTPUT_A, LargeMotor, SpeedPercent
dlech marked this conversation as resolved.
Show resolved Hide resolved
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor.lego import UltrasonicSensor

p1 = LegoPort(INPUT_1)
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/brickpi3.html#brickpi3-in-port-modes
p1.mode = 'ev3-uart'
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensors.html#supported-sensors
p1.set_device = 'lego-ev3-us'

pA = LegoPort(OUTPUT_A)
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/brickpi3.html#brickpi3-out-port-modes
pA.mode = 'tacho-motor'
# http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/motors.html#supported-motors
pA.set_device = 'lego-ev3-l-motor'
dlech marked this conversation as resolved.
Show resolved Hide resolved

# allow for some time to load the new drivers
sleep(0.5)

s = UltrasonicSensor(INPUT_1)
m = LargeMotor(OUTPUT_A)

print("Running motor...")

while True:
dist = s.distance_centimeters
if dist < 50:
m.on(SpeedPercent(30))
else:
m.on(SpeedPercent(-30))
dlech marked this conversation as resolved.
Show resolved Hide resolved