Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Best way to measure period between pulses? Counters and FPGA (http://www.chiefdelphi.com/forums/showthread.php?t=112193)

jhersh 28-01-2013 17:25

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by Ken Streeter (Post 1223437)
Below is the definition for the ShooterWheelTask, of which the "run" method will be executed every 20 milliseconds. This code computed the wheel RPM by dividing the number of counts observed since the last invocation by the elapsed time since the last invocation, with appropriate unit conversions.

This is "method 1" above. This is what he claims not to want.

Quote:

Originally Posted by Ken Streeter (Post 1223437)
Time was measured using the FPGATimestamp, which has a resolution of approximately 6.5usec.

This is inaccurate. The FPGATimestamp itself has a resolution of 1 us. The digital I/O that it timestamps (in the case of digital interrupts or encoder pulse measurements, etc.) is 6.525 us.

Cheers,
-Joe

jhersh 28-01-2013 17:50

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by Mr. Lim (Post 1223234)
Code:

public double getPeriod() {
        double period;
        if (m_counter.readTimerOutput_Stalled()) {
            return Double.POSITIVE_INFINITY;
        } else {
            period = (double) m_counter.readTimerOutput_Period() / (double) m_counter.readTimerOutput_Count();
        }
        return period / 1.0e6;
    }

Something isn't jiving here. In the above code, why would you take the period and divide by the count? If the FPGA is measuring the period directly, should we just be taking the period directly? Will count ever be anything other than 1?

The variables are not terribly verbose... the "count" is the number of samples in the sliding window average. Count will always be 1 if you specify not to average (NumberOfSamplesToAverage = 1).

The FPGA does not like to divide without using up a bunch of gates, where and the RT CPU with an FPU has no problem doing this. This is an optimization where all the addition and synchronization is done in hardware where it needs to be done, but the divide operation to get the actual average period is pushed to the driver.

If you want "Method 1", you have to do it the way Ken Streeter described. If you want "Method 2", you must use the FPGA.

Cheers,
-Joe

Ken Streeter 28-01-2013 17:53

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by jhersh (Post 1223475)
This is inaccurate. The FPGATimestamp itself has a resolution of 1 us. The digital I/O that it timestamps (in the case of digital interrupts or encoder pulse measurements, etc.) is 6.525 us.

Thanks for the correction, Joe! I'll edit my post above to fix it!

jhersh 28-01-2013 17:58

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by Mr. Lim (Post 1223219)
We had a Counter set up exactly as you describe above. The periods we were getting had a very pronounced stepwise response. As a result, our RPM readings changed in increments of about 375 RPM.

There is a finite resolution on the timing of the pulses. That period is 6.525 us per edge. In your system, does that correspond to 375 RPM? You may want to turn on averaging to improve the resolution (but increase the latency).

Quote:

Originally Posted by Mr. Lim (Post 1223219)
We did the math, and these increments corresponded with 1 count / 20ms, which led us to believe the reads were still being done via method 1.

Sounds like a coincidence, maybe? Where are you getting 20ms as the refresh period? Just because that's the Driver Station's packet rate?

Quote:

Originally Posted by Mr. Lim (Post 1223219)
Is it possible that the other modes (other than semi-period) use the FPGA to count pulses over a 20ms period instead of measuring the time between pulses directly?

No. The FPGA only implements method 2.

jhersh 28-01-2013 18:02

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by Joe Ross (Post 1223291)
The FPGA doesn't return floating point values, so the period isn't in units you can use directly. Presumably, the Period is actually the period in clock cycles, and the count is the number of clock cycles per second. Dividing those two gives you a period in seconds.

Close. The "period" is in microseconds. The "count" is the number of samples accumulated by the averaging engine. The code also divides by 1.0e6 before returning to give the required "seconds" unit.

Ken Streeter 28-01-2013 18:28

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by jhersh (Post 1223475)
This is "method 1" above. This is what he claims not to want.

I had missed that in the original post. However, now I understand why the original poster doesn't want to use "Method 1." Given the statement of seeing RPM values with a resolution of 375 RPM and a hypothesized 20ms sampling period, their system must be using a wheel that has only 8 counts per revolution. In order to get greater RPM measurement resolution while keeping their 8 CPR sensor, they would need to have a longer sampling interval (longer than 20ms).

Similarly, with a 250 CPR sensor such as 1519 used in 2012, our computed RPM values are discretized at units of 12RPM. Measuring wheel speed to the nearest multiple of 12RPM was fine for our wheel shooter last year. Measuring to the nearest 375RPM might be troublesome.

DrakusDarkus 28-01-2013 20:29

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by Ken Streeter (Post 1223437)
As described in another post, hidden in the LabVIEW programming forum (even though we were doing this in Java), Team 1519 had success last year using the Encoder object at 1X sampling to count pulses from a USDigital E7P encoder with a 250CPR optical wheel spinning at around 4000RPM. However, we performed our RPM computation in a different manner than using the built-in functions. We found our approach to give excellent accuracy. This same strategy should work just fine for a Counter object.

First off, we set up a separate task in Java to periodically compute the RPM of our shooter wheels. (This task also will control the motor for the wheels, too.) Below is a code excerpt showing how to set this up:

Code:



    private Encoder wheelEncoder = new Encoder(RobotMap.SHOOTER_WHEEL_ENCODER_A, RobotMap.SHOOTER_WHEEL_ENCODER_B, false, CounterBase.EncodingType.k1X);
    private static final long WHEEL_SPEED_PERIOD = 20; // milliseconds

    timer = new java.util.Timer();
    timer.scheduleAtFixedRate(new ShooterWheelTask(), 10000, WHEEL_SPEED_PERIOD);

Below is the definition for the ShooterWheelTask, of which the "run" method will be executed every 20 milliseconds. This code computed the wheel RPM by dividing the number of counts observed since the last invocation by the elapsed time since the last invocation, with appropriate unit conversions. Time was measured using the FPGATimestamp, which has a resolution of approximately 6.5usec 1usec. (Edit: updated resolution per jhersh posting, below.) We then compared the measured RPM against the desired RPM, and used a "Bang - Bang" controller to set the wheel speed. This simplistic approach worked incredibly well. We plan to use it again this year.

We can't take the credit for the "Bang - Bang" controller application -- lengthy CD discussions on this last year led to Ether's white paper on the subject.

Below is a snippet of our "ShooterWheelTask" code. Various declarations of variables are not in this code snippet, but I think you'll be able to figure out what they are from the context.

PS: See you at GSR, Mr. Lim!

Code:

    private class ShooterWheelTask extends java.util.TimerTask {
        public void run() {
            double power;
           
            double now = Timer.getFPGATimestamp();
            int count = wheelEncoder.get();

            wheelEncoder.reset();
            // NOTE:  60 seconds per minute;  250 counts per rotation
            actualWheelSpeed = (60.0 / 250.0) * count / (now - prevWheelTime);
            prevWheelTime = now;
           
            if (actualWheelSpeed >= wheelSpeed) {
                power = 0;
            } else {
                power = 1;
            }
           
            if (automatic) {
                setWheelJag(power);
            }
            else {
                setWheelJag(0);
            }
        }
    }


Thanks, we will try utilizing this in some way because we are having similar problems

Ether 28-01-2013 20:52

Re: Best way to measure period between pulses? Counters and FPGA
 
1 Attachment(s)
Quote:

Originally Posted by jhersh (Post 1223492)
the "count" is the number of samples in the sliding window average.

Joe,

How is this implemented under the hood in FPGA? Is there a ring buffer in the FPGA which the FPGA populates with the 6.525us_resolution_timestamp for each rising edge it detects (assuming it's not in semi-period mode), and then when requested retrieves the elapsed time between the N+1 most recent samples in the ring (which the cRIO CPU then divides by N)?

Quote:

Count will always be 1 if you specify not to average (NumberOfSamplesToAverage = 1).
How large can "NumberOfSamplesToAverage" be? I searched the 2012 C++ and the 2013 Java WPILib code but couldn't find that search string.


EDIT:

@all:

I attached a small Excel app that computes the RPM jitter caused by the 6.525us timing resolution of the edge detections. There will be additional jitter due to manufacturing tolerances in the physical locations of the edges in the sensor, but I've not included those here.

Note how large the jitter is with a 360 PPR sensor at 5000 RPM with averaging set to 1. If you set the averaging to 120 (1/3 revolution) you can reduce the jitter dramatically, at the cost of some phase lag in the signal.

If you make a 1 PPR sensor with averaging set to 1 you'll be able to get an updated reading every 12ms at 5000 RPM with very low jitter.



jhersh 28-01-2013 22:41

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by Ether (Post 1223581)
How is this implemented under the hood in FPGA? Is there a ring buffer in the FPGA which the FPGA populates with the 6.525us_resolution_timestamp for each rising edge it detects (assuming it's not in semi-period mode), and then when requested retrieves the elapsed time between the N+1 most recent samples in the ring (which the cRIO CPU then divides by N)?

There is a buffer that will hold up to 127 samples for each counter / encoder. It has write pointer that does form a ring buffer. The sum of the samples between the write pointer - average_size (usually) and write pointer are summed every 0.1 us * average_size (yeah I know... overkill) and made available to the driver (i.e. not on demand) along with the number of samples available. When a counter is reset, the write pointer is moved to mark all samples unwritten (so the "count" returned would be 0).

The I/O is 6.525 us from sample to sample. The timer used has a 1 us resolution. So the actual resolution is 6..7 us. However, the error is not cumulative across the average. Regardless of the number of samples averaged, the error is less than 7.525 us.

Quote:

Originally Posted by Ether (Post 1223581)
How large can "NumberOfSamplesToAverage" be? I searched the 2012 C++ and the 2013 Java WPILib code but couldn't find that search string.

The FPGA interface function is called writeTimerConfig_AverageSize(). Looks like we never exposed that to the WPILib API layer in C++ (and therefore Java). It's exposed in LabVIEW.

Cheers,
-Joe

apalrd 29-01-2013 00:52

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by jhersh (Post 1223657)
The FPGA interface function is called writeTimerConfig_AverageSize(). Looks like we never exposed that to the WPILib API layer in C++ (and therefore Java). It's exposed in LabVIEW.

And I was sitting in this discussion wondering how nobody knows that you can configure the hardware averaging. It's a register in the FPGA's counter/encoder timer config register, exposed by the Set Timer functions, and is very useful.

Our 2012 robot (with a 4-line handmade aluminum and gaffers tape disc) averaged 12 samples (three whole rotations) to get a perfect signal with ~2.5rpm minimum step. If I set the graph to autosize I could see the controller oscillate between exactly one step above or below the target speed, when steady state.

Ether 29-01-2013 01:10

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by apalrd (Post 1223723)
It's a register in the FPGA's counter/encoder timer config register, exposed by the Set Timer functions

What source code file would that be in? I cannot find it.

It's not in the 2012 C++ WPILib source rev3111 timer.cpp



jhersh 29-01-2013 01:13

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by Ether (Post 1223727)
What source code file would that be in? I cannot find it.

It's not in the 2012 C++ WPILib source rev3111 timer.cpp



Encoder.cpp and Counter.cpp

Ether 29-01-2013 01:18

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by jhersh (Post 1223729)
Encoder.cpp and Counter.cpp

OK, so we're talking about modifying and re-compiling the WPILib source, not simply passing a parameter?



jhersh 29-01-2013 01:23

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by Ether (Post 1223732)
OK, so we're talking about modifying and re-compiling the WPILib source, not simply passing a parameter?

Quote:

Originally Posted by jhersh (Post 1223657)
The FPGA interface function is called writeTimerConfig_AverageSize(). Looks like we never exposed that to the WPILib API layer in C++ (and therefore Java). It's exposed in LabVIEW.

That is what I was saying. It is not in the WPILib API layer in C++ and Java. That means if you care to use the hardware feature, you need to move down a layer.

Cheers,
-Joe

Ether 29-01-2013 01:42

Re: Best way to measure period between pulses? Counters and FPGA
 
Quote:

Originally Posted by jhersh (Post 1223733)
That is what I was saying. It is not in the WPILib API layer in C++ and Java. That means if you care to use the hardware feature, you need to move down a layer.

Got it.




All times are GMT -5. The time now is 21:45.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi