From a4a3523e16fb517716b4ef228d559c091f1a4c35 Mon Sep 17 00:00:00 2001 From: Johannes Ringler Date: Tue, 2 Jan 2024 20:13:18 +0100 Subject: [PATCH] Fix error with new pybricks firmware due to reliance on DCMotor (#174) --- CHANGELOG.md | 1 + ble-server/hub_programs/layout_controller.py | 12 +++++++++--- ble-server/hub_programs/smart_train.py | 7 +++++-- brickrail-gui/brickrail-layouts/ble_test.brl | 4 ++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d375c8c..a97f194 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Fixed +- Fix pybricks error with motors with rotation encoders in pybricks 3.3-stable (https://github.com/Novakasa/brickrail/issues/174). - Don't allow adding train to occupied block. This previously created invalid state. - Fixed Switches and Crossing motors invalidated port when renaming a hub (https://github.com/Novakasa/brickrail/issues/163) diff --git a/ble-server/hub_programs/layout_controller.py b/ble-server/hub_programs/layout_controller.py index ae7bb2d..080731a 100644 --- a/ble-server/hub_programs/layout_controller.py +++ b/ble-server/hub_programs/layout_controller.py @@ -3,7 +3,7 @@ from uselect import poll from usys import stdin -from pybricks.pupdevices import DCMotor +from pybricks.pupdevices import DCMotor, Motor from pybricks.parameters import Port from pybricks.tools import wait, StopWatch @@ -51,7 +51,10 @@ def get_port(index): class Crossing: def __init__(self, port): - self. motor = DCMotor(get_port(port)) + try: + self.motor = DCMotor(get_port(port)) + except OSError: + self.motor = Motor(get_port(port)) self.position = _CROSSING_POS_UP self.port = port self.stopwatch = StopWatch() @@ -82,7 +85,10 @@ def execute(self, data): class Switch: def __init__(self, port, pulse_duration = 600): - self.motor = DCMotor(get_port(port)) + try: + self.motor = DCMotor(get_port(port)) + except OSError: + self.motor = Motor(get_port(port)) self.position = _SWITCH_POS_NONE self.port = port self.pulse_duration = pulse_duration diff --git a/ble-server/hub_programs/smart_train.py b/ble-server/hub_programs/smart_train.py index 053eac5..a03a51d 100644 --- a/ble-server/hub_programs/smart_train.py +++ b/ble-server/hub_programs/smart_train.py @@ -1,7 +1,7 @@ from micropython import const from ustruct import pack, pack_into -from pybricks.pupdevices import ColorDistanceSensor, DCMotor +from pybricks.pupdevices import ColorDistanceSensor, DCMotor, Motor from pybricks.parameters import Port from io_hub_unfrozen import IOHub, VERSION @@ -112,7 +112,10 @@ class TrainMotor: def __init__(self): self.speed = 0 self.target_speed = 0 - self.motor = DCMotor(Port.A) + try: + self.motor = DCMotor(Port.A) + except OSError: + self.motor = Motor(Port.A) self.direction = 1 def flip_direction(self): diff --git a/brickrail-gui/brickrail-layouts/ble_test.brl b/brickrail-gui/brickrail-layouts/ble_test.brl index b3a90a0..8d4b0d5 100644 --- a/brickrail-gui/brickrail-layouts/ble_test.brl +++ b/brickrail-gui/brickrail-layouts/ble_test.brl @@ -3,7 +3,7 @@ "devices": { "trains": [ { - "name": "city-blue", + "name": "city_hub", "storage": { "0": 3500, "1": 40, @@ -881,7 +881,7 @@ "random_targets": true, "block_id": "block2", "blockindex": 0, - "ble_train": "city-blue" + "ble_train": "city_hub" } ] }