Skip to content

Wait for conversions in continuous example #62

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

Merged
merged 14 commits into from
Jan 5, 2021

Conversation

WizardTim
Copy link
Contributor

Previously polling rate (apparent sample rate) was limited purely by I2C speed, script will now wait a full conversion cycle until it next reads the conversion result.

Requires #61 to be merged first.

With RATE=250 and i2c_baudrate=1000000 (1 MHz)

Old

Took 0.139 s to acquire 1000 samples.

Configured:
    Requested       =   250    sps
    Reported        =   250    sps

Actual:
    Polling Rate    =  7171.07 sps
                       2868.43%
    Repeats         =   966
    Conversion Rate =   243.82 sps   (estimated)

New

Took 4.009 s to acquire 1000 samples.

Configured:
    Requested       =   250    sps
    Reported        =   250    sps

Actual:
    Polling Rate    =   249.41 sps
                         99.76%
    Repeats         =    25
    Conversion Rate =   243.18 sps   (estimated)

Also constrains polling rate at any other valid sample rate, eg. RATE=3300 and i2c_baudrate=1000000 (1 MHz)
New

Took 0.309 s to acquire 1000 samples.

Configured:
    Requested       =  3300    sps
    Reported        =  3300    sps

Actual:
    Polling Rate    =  3234.39 sps
                         98.01%
    Repeats         =    25
    Conversion Rate =  3153.53 sps   (estimated)

Previously polling rate (apparent sample rate) was limited purely by I2C speed, script will now wait a full conversion cycle until it next reads the conversion result.

NOTE: Not implementing a time.sleep() in _read(self, pin) as this would be blocking and would inhibit easy implementation of interrupt driven sampling by the ALERT/RDY pin.
Copy link
Contributor Author

@WizardTim WizardTim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(44e135d) This commit affected local file EOLs only

@WizardTim WizardTim marked this pull request as ready for review December 3, 2020 00:40
- Remove accumulating error introduced by basing next sample time on time.monotonic call
- Skip samples if scheduled time has passed to prevent over polling one conversion result in order to catch up
- Pre-calculate time_next_sample in while loop comparison and sample_interval
@WizardTim
Copy link
Contributor Author

WizardTim commented Dec 15, 2020

This pull request resolves #27, #35, #43.

New

Took 10.003 s to acquire 33000 samples.

Configured:
    Requested       =  3300    sps
    Reported        =  3300    sps

Actual:
    Polling Rate    =  3298.86 sps
                         99.97%
    Skipped         =     2
    Repeats         =   444
    Conversion Rate =  3254.48 sps   (estimated)

Plot of time.monotonic() error in comparison to time_next_sample at ADC read (3,300 sps jitter target is 303 μs)
ads1015 i2c jitter

Bugs: Will often skip first 1 or 2 samples, I suspect this is due to the following line however I don't think it's worth the trouble to change it.

time.sleep(2 / self.data_rate)

@ladyada ladyada requested a review from caternuson December 15, 2020 01:44
- Do first slow channel read(&configure/wait) and discard result before entering timed loop
- Align start time with acquisition timing
- Bug not entirely fixed, will still sometimes skip 1 reading when taking >1,000 samples, probably won't fix this
@WizardTim
Copy link
Contributor Author

New
Fix it skipping 1-2 samples on the first acquisition due to setup time, still skips 1 sample every so often with >1000 samples

Took 0.303 s to acquire 1000 samples.

Configured:
    Requested       =  3300    sps
    Reported        =  3300    sps

Actual:
    Polling Rate    =  3298.52 sps
                         99.96%
    Skipped         =     0
    Repeats         =    27
    Conversion Rate =  3209.46 sps   (estimated)

@caternuson
Copy link
Contributor

Sorry. Just now seeing this.

So this essentially looks like you've implemented something like suggested here?
#35 (comment)

@caternuson caternuson self-assigned this Jan 5, 2021
@WizardTim
Copy link
Contributor Author

WizardTim commented Jan 5, 2021

Sorry. Just now seeing this.

So this essentially looks like you've implemented something like suggested here?
#35 (comment)

Yes pretty much although this implementation is a bit more advanced to make sure the sample rate doesn't drift or over-sample to catch up. It also uses a blocking while loop instead of time.sleep(SOME_DELAY) in order to achieve better timing at the cost of power consumption.

@caternuson
Copy link
Contributor

Yep. Thanks. Looks like you also tested it, so I'm fine with merging. If this was useful for you, hopefully will be for others trying to use fast read.

@caternuson caternuson merged commit 2a0f208 into adafruit:master Jan 5, 2021
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 8, 2021
Updating https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15 to 2.2.5 from 2.2.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_ADS1x15#62 from WizardTim/WizardTim-continuous-fix-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel to 6.0.1 from 6.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel#100 from funkfinger/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE_BroadcastNet to 0.10.2 from 0.10.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE_BroadcastNet#16 from alexwhittemore/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_OAuth2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants