Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4834

General • Re: Is this a job for PIO?

$
0
0
Am I over complicating this?
I would think so. At 100 MPH you should be getting 320 pulses per second, one every 3 milliseconds. That's pedestrian for an RP2.

I am not sure why using interrupts wouldn't work to keep count of the pulses but I'm not familiar with Arduino coding. Maybe there's an issue with how you have implemented your code ?

The way I have considered doing it is to maintain an array of elapsed time from one reading to the next -

Code:

Pulses   _|___|___|__|____|_____|_____|____|_____|__|_|___|__|___Time      0123456789-123456789-123456789-123456789-123456789-1234Interval      4   4  2    5     6     6    5     6  3 2   4  2
You can add the intervals up, know how many pulses there were in that time period - it's the number of intervals so no need to track separately, and can convert that to pulses per second, then to MPH.

Assuming the intervals here were 10ms periods the above would indicate 12 pulses in 490ms. An average of 40.8ms per pulse. That works out at 24.5 pulses per second, about 7.7 MPH.

Proof of concept in Python / MicroPython - But check the maths !

Code:

intervals = [40, 40, 20, 50, 60, 60, 50, 60, 30, 20, 40, 20]pulses = len(intervals)period = 0for interval in intervals:  period += intervalprint("{} pulses in {} ms".format(pulses, period))pulsesPerSecond = (1000.0 / period) * pulsesprint("{:5.2f} pulses per second".format(pulsesPerSecond))mph = (pulsesPerSecond / 8) * 2.5print("{:5.2f} MPH".format(mph))

Code:

12 pulses in 490 ms24.49 pulses per second7.65 MPH
You can keep track of the array so it never adds up to more than a second to keep that a sensible length, but it should work for any size array; just means the time you've averaged over may not be an exact second, but it can still calculate pulses per second, MPH.

It's also continuous, no need to wait for any time; just copy the array, add it up and use it whenever you want to show it.

This seemed more accurate to me than the chance of missing a pulse just before and just after any period being measured but I've never tried it.

Statistics: Posted by hippy — Sat Aug 17, 2024 5:44 pm



Viewing all articles
Browse latest Browse all 4834

Trending Articles