Skip to content

[Feature] Implement awaitable ADC reading #2265

@laurensvalk

Description

@laurensvalk

The method pbdrv_adc_update_soon is a bit of a hack to get a value sooner than the typical 10 ms sample time. It mainly exists to support the NXT color sensor (on EV3). It does 4 sequential ADC measurements to measure light intensity while the LED is red/green/blue/none, and we would rather not have it take 40 ms per color measurement, or be just out of sync and get bad results. The current implementation in lib/pbio/src/port_dcm_ev3.c is this:

pbio_error_t pbio_port_dcm_await_new_nxt_analog_sample(pbio_port_dcm_t *dcm, pbio_os_timer_t *timer, const pbdrv_ioport_pins_t *pins, uint32_t *value) {
    pbio_os_state_t *state = &dcm->child;

    PBIO_OS_ASYNC_BEGIN(state);

    // Wait for LED to settle.
    PBIO_OS_AWAIT_MS(state, timer, 1);

    // Request a new ADC sample.  <---- would be nice if we could "pbdrv_adc_await_channel" instead of "soon" + "time"
    pbdrv_adc_update_soon();
    PBIO_OS_AWAIT_MS(state, timer, 4);

    // Get the value.
    uint8_t pin = dcm->category == DCM_CATEGORY_NXT_COLOR ? 6 : 1;
    *value = pbio_port_dcm_get_mv(pins, pin);  // this calls pbdrv_adc_get_ch and just scales it

    PBIO_OS_ASYNC_END(PBIO_SUCCESS);
}

Instead of doing it that way, perhaps we could have a complimentary function to pbdrv_adc_get_ch called something like pbdrv_adc_await_ch that will handle this at the pbdrv/adc_ev3 level and return the value when it is ready. Any ADC settling time (however long that is?) could be handled there as well, so the application code can just await it instead of hard coding this 4 ms wait.

Other ideas for doing this better would be welcome too.

Side note: would it make sense to scale ADC measurements to mV at the driver level instead of having the application level do it?

Metadata

Metadata

Assignees

Labels

platform: EV3Issues related to LEGO MINDSTORMS EV3software: pybricks-micropythonIssues with Pybricks MicroPython firmware (or EV3 runtime)topic: sensorsIssues involving sensors

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions