-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a new BLE class that is used for connectionless broadcasting/ observing on hubs with built-in Bluetooth Low Energy. Also see pybricks/pybricks-micropython#158.
- Loading branch information
Showing
16 changed files
with
413 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub | ||
from pybricks.hubs import ThisHub | ||
from pybricks.pupdevices import Motor | ||
from pybricks.parameters import Port | ||
from pybricks.tools import wait | ||
|
||
# Initialize the hub. | ||
hub = ThisHub(broadcast_channel=1) | ||
|
||
# Initialize the motors. | ||
left_motor = Motor(Port.A) | ||
right_motor = Motor(Port.B) | ||
|
||
while True: | ||
# Read the motor angles to be sent to the other hub. | ||
left_angle = left_motor.angle() | ||
right_angle = right_motor.angle() | ||
|
||
# Set the broadcast data and start broadcasting if not already doing so. | ||
hub.ble.broadcast(left_angle, right_angle) | ||
|
||
# Broadcasts are only sent every 100 milliseconds, so there is no reason | ||
# to call the broadcast() method more often than that. | ||
wait(100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub | ||
from pybricks.hubs import ThisHub | ||
from pybricks.pupdevices import Motor | ||
from pybricks.parameters import Color, Port | ||
from pybricks.tools import wait | ||
|
||
# Initialize the hub. | ||
hub = ThisHub(observe_channels=[1]) | ||
|
||
# Initialize the motors. | ||
left_motor = Motor(Port.A) | ||
right_motor = Motor(Port.B) | ||
|
||
while True: | ||
# Receive broadcast from the other hub. | ||
|
||
data = hub.ble.observe(1) | ||
|
||
if data is None: | ||
# No data has been received in the last 1 second. | ||
hub.light.on(Color.RED) | ||
else: | ||
# Data was received and is less that one second old. | ||
hub.light.on(Color.GREEN) | ||
|
||
# *data* contains the same values in the same order | ||
# that were passed to hub.ble.broadcast() on the | ||
# other hub. | ||
left_angle = data[0] | ||
right_angle = data[1] | ||
|
||
# Make the motors on this hub mirror the position of the | ||
# motors on the other hub. | ||
left_motor.track_target(left_angle) | ||
right_motor.track_target(right_angle) | ||
|
||
# Broadcasts are only sent every 100 milliseconds, so there is | ||
# no reason to call the observe() method more often than that. | ||
wait(100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.