Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Improve device detection and UART device process #1140

Closed
laurensvalk opened this issue Jul 4, 2023 · 3 comments
Closed

[Feature] Improve device detection and UART device process #1140

laurensvalk opened this issue Jul 4, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: sensors Issues involving sensors

Comments

@laurensvalk
Copy link
Member

laurensvalk commented Jul 4, 2023

Is your feature request related to a problem? Please describe.
The device connection manager (DCM) and LEGO UART Devices (pup_uart) currently run as parallel processes that have to exchange some data to inform the other when to start and stop doing things.

It has been that way historically but there is probably room for improvement in future releases.

Describe the solution you'd like

Instead of two parallel processes, maybe we need just one process (which pushes N "processes" along). In pseudo-code, perhaps it could look like this for each sensor.

while True:
    await passive_device_detection_until_uart_detected()
    success = await pup_uart_do_sync_phase()
    if not success:
        continue
    await gather ( pup_uart_receive_data_until_no_data() , pup_uart_send_data_and_ack() , race = True)
  • Explicit synchronization or messaging is not needed this way.
  • the pup_uart code is entirely independent and can be used on ev3 too, etc.
@laurensvalk laurensvalk added enhancement New feature or request topic: sensors Issues involving sensors software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) labels Jul 4, 2023
@laurensvalk
Copy link
Member Author

This is looking easier than expected, so I'll give this a try for review.

@laurensvalk laurensvalk self-assigned this Jul 10, 2023
@laurensvalk
Copy link
Member Author

This is started in https://github.com/pybricks/pybricks-micropython/commits/uart-process but needs some further cleanups.

Instead of synchronization signals, we now have:

static PT_THREAD(pbdrv_legodev_pup_thread(ext_dev_t * dev)) {

    PT_BEGIN(&dev->pt);

    while (true) {

        // Initially assume nothing is connected.
        dev->connected_type_id = PBDRV_LEGODEV_TYPE_ID_NONE;
        dev->dcm.dev_id_match_count = 0;

        // Run the device detection manager until a UART device is detected.
        etimer_set(&dev->timer, 2);
        PT_WAIT_UNTIL(&dev->pt, ({
            if (etimer_expired(&dev->timer)) {
                etimer_reset(&dev->timer);
                (void)PT_SCHEDULE(poll_dcm(dev));
                debug_state_change(dev);
                dev->prev_type_id = dev->connected_type_id;
            }
            dev->connected_type_id == PBDRV_LEGODEV_TYPE_ID_LPF2_UNKNOWN_UART;
        }));

        // UART device detected, so run that protocol until it disconnects.
        legodev_pup_enable_uart(dev->pins);
        PT_SPAWN(&dev->pt, &dev->uart_dev_pt, pbdrv_legodev_pup_uart_thread(&dev->uart_dev_pt, dev->uart_dev));
    }
    PT_END(&dev->pt);
}```

I have intentionally avoided changes to the `dcm` and `uartdev` code in this commit to make the change easier to process.

@laurensvalk
Copy link
Member Author

This is completed and merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: sensors Issues involving sensors
Projects
None yet
Development

No branches or pull requests

1 participant