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

WIP/RFQ: Option to average ADC samples #3190

Closed
wants to merge 1 commit into from

Conversation

mluessi
Copy link
Contributor

@mluessi mluessi commented May 8, 2018

I have a current sensor that gave very noisy reading when used with an airplane ESC. After some testing, I found that the current drawn by the ESC is very irregular, i.e., every 125us it draws a lot of current when the FETs are switching (8kHz switching frequency). This is reflected in the current sensor output.

The battery task currently updates at 50Hz, so it can randomly sample the current sensor output at a peak or valley, which leads to the fluctuating readings.

I see 2 possible ways to fix this:

  1. Add a hardware low-pass filter to the current sensor output. Which isn't ideal, as it requires hardware changes

  2. Average ADC samples over a longer time interval. This is implemented here.

On a typical F4 target with an APB2 clock of 84MHz, the ADC is running at 10.5MHz and a sample is obtained every 503 clock cycles (488 + 15). So for a single channel, the actual sampling rate is 20.9kHz, for 2 channels it is 10.4kHz and for 3 it is 7kHz.

With the previous implementation, we just use the last obtained ADC sample. So if we have 2 channels and e.g. update the battery status at 50Hz, there are over 200 samples obtained between each update that we don't use.

This PR adds the option to use a longer circular buffer that is filled by the DMA and averaged when the ADC is being read.

Other than using a bit more memory and averaging the samples when the ADC output is read, this doesn't add much additional processing (the DMA controller does most of the work "for free").

For the current sensor I have, this completely solves the problem when a 20 sample average is used. I tested this on the BrainFPV RADIX FC, which currently isn't a part of iNAV but it probably makes sense to use this on other F4 and F7 targets as well.

@digitalentity digitalentity added this to the 2.0 milestone May 9, 2018
@digitalentity
Copy link
Member

Nice! I like it. Averaging will certainly make readings more stable. Especially noticeable with analog pitot sensor which suffers a lot from noise on the wire (or less than perfect power supply).

@digitalentity
Copy link
Member

Regarding RADIX, you're more than welcome to assist with #2951

@shellixyz
Copy link
Collaborator

Good idea. I also noticed the voltage and current measurements are always noisy. I was going to do something like that. Would help with the sag compensated battery voltage I'm working on and #2863 too.

@shellixyz
Copy link
Collaborator

Tested. I duplicated the changes to the F4 ADC driver to the F3 ADC driver and tested on Omnibus with 20 samples averaging. Working well.

@digitalentity
Copy link
Member

@shellixyz can you create an F3 PR on top of this one?

@shellixyz
Copy link
Collaborator

@digitalentity Support for STM32F30X added in #3268

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.

None yet

3 participants