Repository navigation
Description
Describe the bug
3rd party motor does work under pybricks while work under official firmware.
To reproduce
Steps to reproduce the behavior:
m1 = Motor(Port.E)
m1.run_time(500, 2000)- connect to the hub
- plug in the motor to "E" port
- start the above code
Tested under Pybricks Code v3.1.2 with firmware v4.1, but reportedly comes up with older firmware as well.
Expected behavior
Motor will run for 2 seconds.
Screenshots
Nothing happens, motor starts initialization but never finishes, hangs.
Notes
My friend asked me to check out and debug her motors - @tertscha, here you are.
I managed to debug it and also provide a fix that works*.
Though this is definitely a decision to take this approach or simply ignore it.
Dear authors - please assess and decide.
*Below summary was created using antigravity. The code works.
====
Key Takeaways
- The Core Obstacle: The 3rd-party motor microcontroller replays official SPIKE Large Motor (Type 49) handshake info on power-up, but ignores LUMP_CMD_SELECT mode changes and streams Mode 0 packets continuously at 115200 baud.
- Infinite Wait: Pybricks kept retrying mode changes indefinitely without a retry cap or fallback. Capping retries at 2 (600 ms) and accepting Mode 0 resolves this.
- Payload Alignment: Mode 0 has an 8-byte format where Byte 0 is speed (int8), Bytes 1–4 are relative angle (int32), and Bytes 5–6 are absolute angle (int16). Reading angle from bin_data + 1 eliminated the 256x gain corruption and derivative noise that caused the wiggling.
====
3rd-Party Powered Up UART Motor Compatibility in Pybricks
1. Executive Summary
Unofficial/clone LEGO Powered Up smart motors (such as 3rd-party SPIKE-compatible motors) operate correctly on official LEGO firmware, but fail under Pybricks. When connected to a PrimeHub or other Powered Up hub, attempts to initialize Motor(Port) or PUPDevice(Port) previously hung in an infinite loop or threw ETIMEDOUT / ENODEV. When driven, the motor violently oscillated ("wiggled") instead of rotating smoothly.
Investigation revealed five distinct root causes spanning the LUMP sync protocol, mode negotiation, sensor classification, and payload byte decoding. With the minimal set of changes described below, the motor connects reliably, reports both relative and absolute shaft angles, and executes full closed-loop PID position and speed control (run_angle, run_target, dc, etc.).
2. Root Cause Analysis
Issue 1: Premature Baud Rate Fallback to 2400 Baud
- Behavior: In
pbio_port_lump_sync_thread(), if the hub did not receive aLUMP_SYS_ACKwithin 10 ms at 115200 baud, it unconditionally dropped the UART baud rate to 2400 baud (EV3_UART_SPEED_MIN). - Cause: EV3 UART sensors start at 2400 baud and negotiate up to 115200 baud. However, Powered Up (LPF2) devices run exclusively at 115200 baud. Dropping the baud rate on a Powered Up device desynchronized the UART receiver permanently.
Issue 2: Mode Switch Rejection & Infinite Retry Loop
- Behavior:
pbio_port_lump_is_ready()returnedPBIO_ERROR_AGAINcontinuously until timeout (ETIMEDOUT). - Cause:
- During handshake, the motor advertised Type ID 49 (
LEGO_DEVICE_TYPE_ID_SPIKE_L_MOTOR) and 6 modes, with capability flags indicating absolute position support in Mode 4. - Pybricks requested default mode 4 (
LEGO_DEVICE_MODE_PUP_ABS_MOTOR__CALIB). - The clone motor microcontroller does not implement the
LUMP_CMD_SELECTcommand; it ignores mode change requests and streams Mode 0 packets continuously. - In
pbio_port_lump_is_ready(),lump_dev->mode != lump_dev->mode_switch.desired_modewas checked. Every 500 ms, Pybricks retriedpbio_port_lump_request_mode(), which resetlump_dev->mode_switch.time. This formed an infinite retry loop with no retry limit or fallback. - Furthermore,
memcpy(lump_dev->bin_data, ...)was gated behindmode == lump_dev->mode_switch.desired_mode, so Mode 0 data was never copied intobin_data.
- During handshake, the motor advertised Type ID 49 (
Issue 3: Device Classification Restricted to Type 38 in Mode 0
- Behavior: In
lib/pbio/src/port_lump/sensors.c,pbio_port_lump_is_relative_motor()checked:return (lump_dev->type_id == LEGO_DEVICE_TYPE_ID_INTERACTIVE_MOTOR) && (lump_dev->mode == LEGO_DEVICE_MODE_PUP_REL_MOTOR__POS || lump_dev->mode == 0);
- Cause: Only the Boost Interactive Motor (Type ID 38) was recognized as a relative motor when in Mode 0. Spike motors (Type ID 49) operating in Mode 0 were rejected by
pbio_port_lump_assert_type_id()withPBIO_ERROR_NO_DEV.
Issue 4: Payload Offset Mismatch (Speed vs. Angle)
-
Behavior: When
Motor(Port)was commanded withrun_angle(500, 360), the motor buzzed and violently wiggled rather than rotating. -
Cause:
In Mode 0, this motor transmits an 8-byte payload:-
Byte 0: Instantaneous speed (signed 8-bit int, e.g.
$+5$ or$-5$ deg/s). - Bytes 1–4: Relative angle (signed 32-bit int, little-endian, 1° resolution).
- Bytes 5–6: Absolute angle (signed 16-bit int, little-endian, 0–360° range).
- Byte 7: Padding / status (0).
Pybricks read the 32-bit position starting from
bin_data + 0. This treated Byte 0 (speed) as the LSB of angle and shifted the true angle into the upper bytes ($\times 256$ ). This injected large derivative noise spikes upon motion and amplified the PID position error gain by 256x, causing severe closed-loop instability. -
Byte 0: Instantaneous speed (signed 8-bit int, e.g.
Issue 5: Instant ENODEV During Port Mode Transitions
- Behavior: Running
Motor(Port)shortly after another mode or at boot caused an immediate[Errno 19] ENODEV. - Cause: In
pb_type_motor.candpb_type_device.c, the wait loop only retried onPBIO_ERROR_AGAIN. While the DCM connection manager is probing port pins or before the sync thread begins,pbio_port_lump_is_ready()returnedPBIO_ERROR_NO_DEV. The constructor aborted immediately instead of waiting for the connection window.
3. Discovered Mode 0 Packet Specification
Byte: [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ]
Field: [Speed] [ Relative Angle ] [ Absolute Angle ] [Pad]
Type: int8_t int32_t (little-endian) int16_t (LE) uint8_t
Units: deg/s degrees degrees (0–360) 0
4. Proposed Minimal Fix
1. lib/pbio/src/port_lump/lump_dev.h
Add a retry counter to track mode switch attempts:
typedef struct {
uint8_t desired_mode;
+ uint8_t retries;
bool requested;
uint32_t time;
} pbio_port_lump_mode_switch_t;2. lib/pbio/src/port_lump/protocol.c
- Prevent baud fallback on Powered Up hubs: Guard
EV3_UART_SPEED_MINwith#if PBIO_CONFIG_PORT_LUMP_EV3. - Cap mode switch retries & fallback to active mode:
Inpbio_port_lump_is_ready():if (lump_dev->mode != lump_dev->mode_switch.desired_mode) { if (time - lump_dev->mode_switch.time > 300) { if (lump_dev->mode_switch.retries >= 2) { // Device does not respond to LUMP_CMD_SELECT; accept active streaming mode. lump_dev->mode_switch.desired_mode = lump_dev->mode; lump_dev->mode_switch.requested = false; if (lump_dev->data_rec && lump_dev->rx_msg[0]) { uint8_t msg_size = ev3_uart_get_msg_size(lump_dev->rx_msg[0]); if (msg_size > 2) { memcpy(lump_dev->bin_data, lump_dev->rx_msg + 1, msg_size - 2); pbio_port_lump_device_handle_data(lump_dev); } } return PBIO_SUCCESS; } lump_dev->mode_switch.retries++; lump_dev->mode_switch.time = time; lump_dev->mode_switch.requested = true; pbio_os_request_poll(); } return PBIO_ERROR_AGAIN; }
3. lib/pbio/src/port_lump/sensors.c
- Recognize any motor in Mode 0 as relative motor:
bool pbio_port_lump_is_relative_motor(pbio_port_lump_dev_t *lump_dev) { if (lump_dev->mode == 0 && (lump_dev->capabilities & (LUMP_MODE_FLAGS0_MOTOR | LUMP_MODE_FLAGS0_MOTOR_REL_POS | LUMP_MODE_FLAGS0_MOTOR_ABS_POS))) { return true; } return (lump_dev->type_id == LEGO_DEVICE_TYPE_ID_INTERACTIVE_MOTOR) && (lump_dev->mode == LEGO_DEVICE_MODE_PUP_REL_MOTOR__POS || lump_dev->mode == 0); }
- Allow absolute angle access in Mode 0:
bool pbio_port_lump_is_absolute_motor(pbio_port_lump_dev_t *lump_dev) { if (lump_dev->mode == 0 && (lump_dev->capabilities & LUMP_MODE_FLAGS0_MOTOR_ABS_POS)) { return true; } return (lump_dev->capabilities & LUMP_MODE_FLAGS0_MOTOR_ABS_POS) && (lump_dev->mode == LEGO_DEVICE_MODE_PUP_ABS_MOTOR__CALIB); }
- Read relative angle at
bin_data + 1:
Inpbio_port_lump_device_handle_data():if (pbio_port_lump_is_relative_motor(lump_dev)) { int32_t degrees; if (lump_dev->type_id == LEGO_DEVICE_TYPE_ID_INTERACTIVE_MOTOR) { degrees = (int32_t)pbio_get_uint32_le(lump_dev->bin_data); } else { degrees = (int32_t)pbio_get_uint32_le(lump_dev->bin_data + 1); } lump_dev->device.angle.millidegrees = (degrees % 360) * 1000; lump_dev->device.angle.rotations = degrees / 360; }
- Read absolute angle at
bin_data + 5:
Inpbio_port_lump_get_angle():if (get_abs_angle) { if (!pbio_port_lump_is_absolute_motor(lump_dev)) { return PBIO_ERROR_NOT_SUPPORTED; } angle->rotations = 0; if (lump_dev->mode == 0) { int32_t abs_deg = (int16_t)pbio_get_uint16_le(lump_dev->bin_data + 5); angle->millidegrees = abs_deg * 1000; } else { angle->millidegrees = lump_dev->device.angle.millidegrees; } if (angle->millidegrees >= 180000) { angle->millidegrees -= 360000; } return PBIO_SUCCESS; }
4. pybricks/common/pb_type_motor.c & pb_type_device.c
Allow the constructor loop to retry during the 2-second connection window when err == PBIO_ERROR_NO_DEV:
-while ((err = pbio_port_get_servo(port, &type_id, &self->srv)) == PBIO_ERROR_AGAIN) {
+while ((err = pbio_port_get_servo(port, &type_id, &self->srv)) == PBIO_ERROR_AGAIN || err == PBIO_ERROR_NO_DEV) {
if (mp_hal_ticks_ms() - start_time > 2000) {
- pb_assert(PBIO_ERROR_TIMEDOUT);
+ pb_assert(err);
}
- mp_hal_delay_ms(1);
+ mp_hal_delay_ms(50);
}5. Verification Results
All tests performed on LEGO Spike Prime Hub (STM32F413 / STM32H563):
| Test Case | Script | Result |
|---|---|---|
| Direct DC control | m.dc(30) |
Motor turns smoothly at constant voltage. |
| Angle readout | m.angle() |
Reports shaft angle in degrees with 1° resolution. |
| Absolute zeroing | m.reset_angle() |
Reads absolute marker from bin_data + 5 and zeroes shaft properly. |
| Position tracking | m.run_angle(500, 360) |
Runs smoothly, stops at target: Initial: -106°, Final: 253° ( |
| Repeatability | Multiple run_angle calls |
Consistent |
| PUPDevice access | PUPDevice(Port.E) |
Instantiates and reports device type ID 49 and mode information. |
Activity
laurensvalk commentedon Oct 30, 2026
Could we not make issues with AI, please?
We enjoy working with humans, but computers talk enough as it is ;)
afarago commentedon Oct 30, 2026
I have written the issue by my own hands, (100% human).
The context and analysis I performed with the help of AI, hence I added it for full context to make it transparent.
laurensvalk commentedon Nov 6, 2026
Thank you @afarago, I appreciate it.
We used to be reluctant about it, but I think we need to purchase some of these after all now that the official product is indeed retired. I don't know how different these off brand ones are, or if many come from the same manufacturer.
See also #2305 and #2395
Olivier34340 commentedon Nov 11, 2026
Hello, I have 4 "lego mindstorms robot inventor hubs", 12 lego medium angulars motors and 12 Chinese large angulars motors. As the "ROBO INVENTOR" application is no longer available, and "sike" is very limited: I am using PYBRICK. Very good application, but surprise farewell to my 12 "angular large motor". I’ve done a lot of research (even in the Pybrics code!!!!!), but I’m not a programmer. I eagerly await the progress of this project. Meci