-
Notifications
You must be signed in to change notification settings - Fork 636
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
Incorrect sampling in AnalogSensor.h #2625
Comments
ref. #2625 incorrect average calculation in the loop, sum and divide for the result spend time in tick() instead of waiting in either pre() or value()
Apparently, c22b369 changed a lot of neighbouring sensors. Commit above does something different; instead of waiting for the sample(s) immediately, value is cached to satisfy the delayed reading timing and is sampled in tick() instead of pre() / value() |
But the problem I see isn't about the reading timing, but rather the way it calculates the average; in previous version clearly does it well: |
Commit above i.e. commit a1ffa9f, average is calculated as sum divided by sample total. Weights can be added, if necessary I also meant to point out time spent in yield() compared to time it can spend in main task. espurna::time::critical::delay(delay); // cpu spin, nothing happens
yield(); // task yield to scheduler, wait sdk, resume here |
ah, sorry, I hadn't seen your last commit... You are reading the sensor in tick(). Btw, I built a version of my code with your modifications; I'll test it as soon as I can. |
Sensor::tick() is called every Arduino loop(), so it can be treated similarly. The general idea was caching value for value(). Actually reading this cached value has to fit somewhere between sensor module readings which call pre() and value() |
Device
Lolin
Version
1.18
Bug description
Hi. I suppose the idea of the samples variable is to achieve some kind of average between several measurements as the final result.
espurna/code/espurna/sensors/AnalogSensor.h
Lines 122 to 138 in 0976bdb
But if I'm not mistaken, that code is simply returning the last value read from analogRead.
I think that a way to approximate the average (without fear of generating overflows) can be
Surely there are other ways to approximate the average, that's the one that occurred to me. I think it is correct, or at least it does not generate crazy results.
Steps to reproduce
No response
Build tools used
No response
Any relevant log output (when available)
No response
Decoded stack trace (when available)
No response
The text was updated successfully, but these errors were encountered: