Hub to Hub code examples #1244
Replies: 2 comments 1 reply
-
Yes, it should be fixed since beta 3.3.0b6. https://github.com/pybricks/pybricks-micropython/blob/master/CHANGELOG.md#fixed-3 |
Beta Was this translation helpful? Give feedback.
-
|
I've managed to get the following code working (very rough), and it mostly seems to work.
by use of the LED light in different sections of the code, I can see that once broadcasting starts, it never seems to stop, and the recieving hub (Unloader) will blink red/green occasionally, indicating it's picking up some broadcast Code from the "Train" - incomplete - all it does is send a broadcast a command when it detects red from pybricks.hubs import CityHub
from pybricks.pupdevices import Motor, Light, ColorDistanceSensor
from pybricks.parameters import Button, Color, Direction, Port, Stop
from pybricks.tools import wait, StopWatch
trainhub = CityHub(broadcast_channel=1, observe_channels=[2,3])
trainmotor = Motor(Port.A)
trainsensor = ColorDistanceSensor(Port.B)
check_color_interval_ms = 20
def check_for_color(color):
while trainsensor.color() != color:
wait(check_color_interval_ms)
print("Program Started")
while True:
print("Waiting for Red")
trainhub.light.on(Color.RED)
check_for_color(Color.RED)
print("Red detected")
trainhub.light.on(Color.ORANGE)
print("LED Orange - sending Load Command")
trainhub.ble.broadcast(1)
wait(2000)
print("LED Orange - sending UnLoad Command")
trainhub.ble.broadcast(2)
wait(2000)
print("Stop Broadcasting?")
trainhub.ble.broadcast(0)
wait(200) Code from the Unloader - experimenting with recieving 2 commands, although it will really only need a single command and then a simple back/forth motor movement from pybricks.hubs import CityHub
from pybricks.pupdevices import Motor, Light
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.tools import wait, StopWatch
UnloaderHub = CityHub(observe_channels=[1], broadcast_channel=3)
UnloaderMotor = Motor(Port.A)
print("Program Started, Waiting for Command")
while True:
# Receive broadcast from the other hub.
data = UnloaderHub.ble.observe(1)
if data is None:
UnloaderHub.light.on(Color.RED)
else:
UnloaderHub.light.on(Color.GREEN)
command = data[0]
if command is 1:
wait(200)
print("command 1 recieved")
UnloaderHub.light.on(Color.ORANGE)
print("Run Motor forwards")
UnloaderMotor.dc(70)
wait(1500)
print("Stop Motor")
UnloaderMotor.stop()
wait(1500)
data = None
elif command is 2:
wait(200)
print("command 2 recieved")
UnloaderHub.light.on(Color.ORANGE)
print("Run Motor backwards")
UnloaderMotor.dc(-70)
wait(1500)
print("Stop Motor")
UnloaderMotor.stop()
wait(1500)
data = None
wait(200)I am learning a bit.. although it's frustrating when it doesn't seem to work consistantly! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm getting started with a new project, and still very new to pybricks coding...
I'm looking at having a train run up and down a track, (thanks @eggybricks for really nice clear code examples!) and then have a loader, and unloader mechanism at each end (each using a hub/motor) - So at least 3 hubs
while working my way through examples, I found this - is it still true?
https://pybricks.com/projects/tutorials/wireless/hub-to-hub/broadcast/#known-issue-broadcasting-using-the-city-hub-does-not-work
or has it been fixed in the beta of pybricks that I'm using?
Ultimately, I would like the train hub, once it has reached a station, to send a simple signal to a hub, eg: "Load", and then that hub to send a signal back "Done", to allow the train to go.
unlike the examples, I don't need to send constant data (motor angles, etc)..
I have managed to get this working in the powered up app, so hopefully it should be easy to replicate in pybricks.
If anyone has any code examples that do a similar task, I'd love to see them!
thanks!
Rohan.
Beta Was this translation helpful? Give feedback.
All reactions