Skip to content

Commit 88d8d73

Browse files
committed
tutorials/wireless/hub-to-hub: update for v3.3.0b5
1 parent 58d66ac commit 88d8d73

File tree

3 files changed

+56
-98
lines changed

3 files changed

+56
-98
lines changed

tutorials/wireless/hub-to-hub/broadcast/index.md

Lines changed: 36 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Hub to hub communication is still a work in progress. This page explains how
1717
you can install an experimental release on the hub to try it out. This
1818
functionality may change in the future and some things may not work.
1919

20+
Last changed on 2023-05-16. Be sure to use [Pybricks Beta] and the most recent
21+
firmware.
22+
23+
[Pybricks Beta]: https://beta.pybricks.com
24+
2025
# Data broadcasting
2126

2227
When a Bluetooth device is on but not yet connected to anything, it typically
@@ -34,106 +39,55 @@ This means that any number of nearby hubs can receive the broadcasted message
3439
without having to set up a connection. This can be quite convenient, but you
3540
can only broadcast small amounts of information at once, as explained below.
3641

37-
**Topics**
42+
**Channels**
3843

39-
All broadcasting data is labeled with a _topic_. This helps you tell data
40-
apart when multiple hubs are broadcasting at the same time. For example, one
41-
hub might be broadcasting tilt sensor data with the topic ``"tilt"`` while
42-
another hub broadcasts measurements with the topic ``"distance"``.
44+
In Pybricks, all broadcasting data is assigned a *channel*. This helps you tell
45+
data apart when multiple hubs are broadcasting at the same time. For example,
46+
one hub might be broadcasting tilt sensor data on channel 1 while another hub
47+
broadcasts measurements on channel 2.
4348

44-
To prepare the hub to send and receive data with these topics, you initialize
45-
the broadcast class as follows:
49+
To prepare the hub to send and receive data with these channels, you initialize
50+
the hub class as follows:
4651

4752
```python
48-
# Import the experimental Broadcast feature.
49-
from pybricks.experimental import Broadcast
53+
# Prepare the hub for sending.
54+
hub = ThisHub(broadcast_channel=1)
55+
```
5056

51-
# Prepare the hub for sending and/or receiving these topics.
52-
radio = Broadcast(topics=["tilt", "distance"])
57+
```python
58+
# Prepare the hub for receiving.
59+
hub = ThisHub(observe_channels=[1, 2])
5360
```
5461

55-
**Sending and receiving data tuples**
62+
**Sending and receiving data**
5663

57-
You can send data as a tuple of values:
64+
You can send data like this:
5865

5966
```python
60-
# Get some example data as a tuple.
61-
example_data = (123, 456)
67+
roll, pitch = sensor.tilt()
6268

6369
# Send it out to anyone listening.
64-
radio.send("tilt", example_data)
70+
hub.ble.broadcast(roll, pitch)
6571
```
6672

6773
On another hub, you can receive it as follows:
6874

6975
```python
7076
# Try to read previously received tilt data.
71-
data = radio.receive("tilt")
77+
data = hub.ble.observe(1)
7278

7379
# Check if there was any data yet:
74-
if data:
80+
if data is not None:
7581
# There was, so let's print it.
7682
pitch, roll = data
7783
print(pitch, roll)
78-
79-
```
80-
81-
When sending data tuples like these, your values will be automatically encoded
82-
into a format suitable for broadcasting.
83-
84-
The following data tuples are allowed:
85-
86-
```python
87-
# You can send up to 8 small values in the range +/- 32767.
88-
# Each value counts as 2 bytes.
89-
data = (-1, 2, 3000, 4, 5, 6, -32767, 32767)
90-
91-
# You can send up to 5 big values. Each value counts as 4 bytes.
92-
data = (50000, -50000, 123456, 78910, 43210)
93-
94-
# You can send up to 5 floating point values. Each counts as 4 bytes.
95-
data = (1.234, 3.1428)
96-
97-
# You can send up to 8 strings. Each character counts as 1 byte.
98-
data = ("Hello", "World")
99-
100-
# You can combine up to eight of the above types.
101-
# The total size must be 20 bytes or less.
102-
data = (123, -50000, 3.1428, "Hi!")
103-
10484
```
10585

106-
**Sending and receiving raw data**
107-
108-
If you prefer to encode data yourself, you can send and receive bytes directly:
86+
When sending data, your values will be automatically encoded into a format
87+
suitable for broadcasting. You can send integers (`1`, `-5`, ...), floats
88+
(`1.0` `-5.3`, ...), booleans (`True`, `False`), strings (`"example"`), bytes
89+
(`b"\x00\x02"`) and `None`.
10990

110-
```python
111-
112-
# Send out up to 23 bytes to anyone listening.
113-
radio.send_bytes("tilt", b"byte data!")
114-
115-
# Read up to 23 bytes.
116-
data = radio.receive_bytes("tilt")
117-
```
118-
119-
# Installing the experimental firmware
120-
121-
To use the broadcasting feature, you have to install a special version of the
122-
Pybricks firmware that includes the ``Broadcast`` class:
123-
124-
1. Download the firmware file for your hub:
125-
- [Technic Hub](./technichub-firmware-build-2178.zip)
126-
- [City Hub](./cityhub-firmware-build-2178.zip)
127-
- [Essential Hub](./essentialhub-firmware-build-2178.zip)
128-
- [Inventor Hub and Prime Hub](./primehub-firmware-build-2178.zip)
129-
2. In [Pybricks Beta](https://beta.pybricks.com/), open the settings menu.
130-
3. Click ``Install Pybricks Firmware``.
131-
4. Instead of selecting your hub, choose ``Advanced`` at the bottom.
132-
5. Follow the instructions to select the firmware file you just downloaded.
133-
6. The installation now proceeds as usual. Make sure to choose a descriptive
134-
hub name. This makes it easy to distinguish them later.
135-
7. Start coding. You can open [Pybricks Beta](https://beta.pybricks.com/) in
136-
multiple different tabs. Use one tab for each hub.
13791

13892
# Running the example programs
13993

@@ -168,13 +122,17 @@ sensor, you can delete the lines that make use of the Ultrasonic Sensor.
168122
{% include_relative vehicle.py %}
169123
```
170124

171-
# Known issues: Slow communication while connected to computer
125+
# Known issue: Slow communication while connected to computer
172126

173127
If the hub is still connected to the computer, the bluetooth chip is quite busy
174128
and data broadcasting may be slow. Especially on the Technic Hub and the City
175129
Hub.
176130

177-
This is something we are still working on. To work around it, just load the
178-
program onto the hub and disconnect from your computer. You can just restart
179-
the program with the hub button.
131+
To work around it, just load the program onto the hub and disconnect from your
132+
computer. You can just restart the program with the hub button.
133+
134+
# Known issue: Broadcasting using the City hub does not work
180135

136+
There is a bug in the Bluetooth chip firmware that prevents broadcasting
137+
from working properly on the City hub. It was working in previous experimental
138+
implementations because we were broadcasting data the "wrong" way.

tutorials/wireless/hub-to-hub/broadcast/remote.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
from pybricks.hubs import ThisHub
12
from pybricks.pupdevices import TiltSensor, ColorLightMatrix
23
from pybricks.parameters import Port, Color
34
from pybricks.tools import wait
4-
from pybricks.experimental import Broadcast
5+
6+
# Initialize the hub for sending and receiving.
7+
hub = ThisHub(broadcast_channel=1, observe_channels=[2])
58

69
# Initialize the devices.
710
lights = ColorLightMatrix(Port.A)
811
sensor = TiltSensor(Port.B)
912

10-
# Initialize broadcast with two topics.
11-
radio = Broadcast(topics=["tilt", "distance"])
12-
1313
while True:
1414
# Read pitch and roll.
1515
pitch, roll = sensor.tilt()
@@ -21,13 +21,13 @@
2121
roll = 0
2222

2323
# Send the data!
24-
radio.send("tilt", (pitch, roll))
24+
hub.ble.broadcast(pitch, roll)
2525

2626
# Check for distance data.
27-
data = radio.receive("distance")
27+
data = hub.ble.observe(2)
2828

2929
# If there was distance data, use it to activate the light.
30-
if data and data < 500:
30+
if data is not None and data < 500:
3131
lights.on(Color.RED)
3232
else:
3333
lights.off()
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1+
from pybricks.hubs import ThisHub
12
from pybricks.pupdevices import Motor, UltrasonicSensor
23
from pybricks.parameters import Port, Direction
34
from pybricks.robotics import DriveBase
45
from pybricks.tools import wait
5-
from pybricks.experimental import Broadcast
66

7-
# Initialize broadcast with two topics.
8-
radio = Broadcast(topics=["tilt", "distance"])
7+
# Initialize the hub for sending and receiving.
8+
hub = ThisHub(broadcast_channel=2, observe_channels=[1])
99

1010
# Initialize the drive base.
1111
left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
1212
right_motor = Motor(Port.B)
13-
drive_base = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=112)
13+
drive_base = DriveBase(
14+
left_motor, right_motor, wheel_diameter=56, axle_track=112
15+
)
1416

1517
# Initialize the distance sensor.
1618
sensor = UltrasonicSensor(Port.C)
1719

1820
while True:
21+
# Receive tilt data.
22+
data = hub.ble.observe(1)
1923

20-
# Receive tilt data
21-
data = radio.receive("tilt")
22-
23-
# If we received it, start driving.
24-
if data:
24+
if data is not None:
25+
# If we received it, start driving.
2526
pitch, roll = data
2627
drive_base.drive(speed=pitch * 8, turn_rate=roll * 3)
2728
else:
28-
print(data)
29+
# If we lost the signal, stop.
2930
drive_base.stop()
3031

31-
3232
# Send the distance data
33-
radio.send("distance", sensor.distance())
33+
hub.ble.broadcast(sensor.distance())
3434

3535
# Wait some time.
36-
wait(50)
36+
wait(10)

0 commit comments

Comments
 (0)