![]() |
Best way to measure period between pulses? Counters and FPGA
We have a digital optical sensor, and we want to measure the time between between pulses. Once we get the time, we would then calculate the rotational speed of a wheel.
I thought the Counter class did this for us using the FPGA? Using the GearTooth class (which extends Counter), for some reason we're getting very poor resolution from getPeriod(). GearTooth geartooth = new GearTooth(1); rpm = ??? / geartooth.getPeriod(); Our RPM steps up and down in increments of 375 RPM! You might ask how many pulses per revolution we have, but should that even matter? I was under the impression that the resolution should be dependent on the precision of the timing device? Ironically, we did the math, and the resolution of 375 RPM is what we would get if we polled the Counter every 20ms, and calculated the period by dividing 20ms by the number of pulses received. What am I missing here? Do I need to explicitly set up the Counter to use the FPGA / Timers / etc? Or is this all a ruse, and the Counter doesn't measure time between pulses at all, but polls every 20ms instead? Has anyone successfully calculated a wheel speed by measuring time between pulses using the FPGA via the Counter/GearTooth class? |
Re: Best way to measure period between pulses? Counters and FPGA
I need to clarify this post...
I am having problems with how the period is being calculated. There is a BIG difference between these two approaches: 1) Polling the counter every 20ms and counting how many pulses occurred. Period = 20ms / pulses 2) ACTUALLY measuring the time between two pulses using the FPGA. Period = the measured time Approach 2 results in a much higher resolution in our application, and the documentation in WPILib is unclear when 1 or 2 is used with Counter/GearTooth objects. From looking at source more, I'm lead to believe approach 2 ONLY happens if you've specifically setup a 4x Encoder object and you can't do approach 2 at all with a Counter/GearTooth object. Has anyone else seen otherwise? |
Re: Best way to measure period between pulses? Counters and FPGA
Well, to answer my own question...
It seems it is possible to directly measure time between transitions with a Counter object by using SemiPeriodMode. Thanks to Joe Hershberger's post on the FIRST Forums here: http://forums.usfirst.org/showpost.p...8&postcount=12 |
Re: Best way to measure period between pulses? Counters and FPGA
I am currently trying to do the same thing, using a light sensor to create a custom tachometer. As it has been explained to me (correct me if I am wrong) the crio FPGA has an integrater function built in, so even if pulses moving faster than 20ms can be measured. I calculated that on our shooter even with a 1 tick tachometer it will be sending a pulse every 1-2ms, which may be too fast for the AndyMark optical sensors, but I haven't checked too closely. However, it does seem like a good idea for certain high speed feedback.
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
From reading your application description, you probably want to use Up/Down counter mode, and only specify the "up source". When you read the timer output, it will be reporting the speed based on "method 2" that you described above. Cheers -Joe |
Re: Best way to measure period between pulses? Counters and FPGA
So for our shooter last year we used a counter with a light sensor. How we got the correct value is we took the period, then took the reciprocal. We then divided that number by how many pulses you get per rotation, and then multiply that by 60. That gives the RPM.
|
Re: Best way to measure period between pulses? Counters and FPGA
Oooh, we were doing this today! Thank you for potentially solving any questions that we might have come up with!
Also, teams who have done this before, how accurate is finding the RPM using an optical sensor compared to using an encoder? |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
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. 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. Very strange. 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? |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Code:
public double getPeriod() { |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
But the counter functions should work just fine with a single sensor. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
I dont know about in Java or C++, but in labview the library already converts it into period per pulse in seconds. So if you do 1/x on that, you get pulses per period. Then you have pulses per second, and that is easy to convert to rotations per minute with just unit conversions. We used geartooth mode for this purpose and it worked perfectly.
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
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);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 { |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
Quote:
Cheers, -Joe |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
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 |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
Quote:
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
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. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
1 Attachment(s)
Quote:
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:
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. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
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:
Cheers, -Joe |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
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. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
It's not in the 2012 C++ WPILib source rev3111 timer.cpp |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
Quote:
Cheers, -Joe |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
This post clarified pretty much everything we're seeing. We WERE hitting the 6.525us timing resolution per edge. That corresponded (coincidentally) with the resolution of 375 RPM we were seeing, at our desired speed. There'll be a more detailed post, or even a whitepaper once we've sorted everything out, but we've got a really neat solution together involving SetSemiPeriod(true), a mostly white disc with a single small black sector, and an averaging window that holds about 10ms worth of reads at our slowest speed. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
http://www.chiefdelphi.com/forums/sh...3&postcount=29 |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
I'd also appreciate any clarification on what you mean by moving down a layer. Other than switching to LabView, is there some other action that can be taken? I thought the FPGA code was handled by the cRIO imaging tool and no modifications to the FPGA programming are allowed. Is this your understanding as well? I am not sure if your comment implies more is available. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
In general we try to make everything the same. Quote:
The layer that most people write their code is only in the "User Code" layer. The layer below that is the "WPILib" layer. That is the layer that I mean that you need to edit if you want this functionality in that language. You may even be able to call into the FPGA interface layer directly from your user code if you call it at a time that it won't be overwritten by WPILib. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
The things I've found are SPI, DMA, and CAN periodic status. Someone else filed a bug about digital input interrupts only being partially implemented. For SPI, I attached an implementation that works for me in the tracker. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
Quote:
I'll happily work with the key java developers on the above. |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
I figure that if there isn't a bug filed, you can't complain about it not being fixed. |
Re: Best way to measure period between pulses? Counters and FPGA
Hey, kinda a thread hijack, but this seemed like the best place to ask. We're using a photo-eye based encoder on our team (due to issues mounting traditional shaft encoders) and are having issues with GetPeriod() when we drop below 20 rps where it returns infinity about half of the time, and the correct speed the rest of the time. GetPeriod() apparently returns infinity only if the counter is "stalled". Does anyone know what this means, or if there's a way around it? Overriding GetPeriod doesn't work, as m_counter is private, not protected. I'm guessing that the FPGA has some kind of threshold where if it doesn't see an edge it assumes the wheel has stopped. Is there any way of modifying that, maybe giving it a large tolerance?
|
Re: Best way to measure period between pulses? Counters and FPGA
The is a function you can use to set what the FPGA sees as stopped. its the setMinRate function.
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
Quote:
I imagine that we don't want to use the stalled feature at all. Is there a downside to setting the max period to a high value? e.g. 6 seconds period is equivalent to 10 RPM. This would seem safe at any reasonably low speeds. Is there any functionality trade off to setting a high max period? |
Re: Best way to measure period between pulses? Counters and FPGA
Doesn't that mean it wouldn't be able to tell if the wheel is actually stopped?
|
Re: Best way to measure period between pulses? Counters and FPGA
If you look at the code the only reason it has that function is to not have a divide by 0 error. In the get period function, it uses that bool in order to not make it divide by 0.
|
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
So we probably need to decide some happy medium rotational velocity that we want to control and set the period accordingly. -al g |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
Agree that the stall feature is solving the zero divide problem but probably more importantly its dealing with the issue of period growing dramatically as rotational velocity approaches zero. Take the following scenario: you decelerate very rapidly and your last transition happens at a time that is equivalent to 200 RPM. The wheel is stopped but the code continues to report 200 RPM. If the timeout (max period/min rate) is set for 10 RPM this will continue for 6 seconds. Another interesting C++ fact that I never realized before (or once I knew but forgot): For IEEE floats, division of a finite nonzero float by 0 is well-defined and results in +infinity (if the value was >zero) or -infinity (if the value was less than zero). The result of 0/0 is NaN. If you use integers, the behaviour is undefined. |
Re: Best way to measure period between pulses? Counters and FPGA
The IEEE defined behavior is the same for LV, and I presume Java.
Greg McKaskle |
Re: Best way to measure period between pulses? Counters and FPGA
Quote:
|
Re: Best way to measure period between pulses? Counters and FPGA
I agree that PPC is IEEE 754 compliant, but some languages will have their own conventions, and since Java in interpreted, it would be pretty easy and efficient for them to have thrown an exception or taken some other action.
Greg McKaskle |
| 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