Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

RPM from Encoder

Hello,

 

I am trying to get RPM from a quadrature encoder, and I am seeing some error, and drift in my data.  I was hoping someone could point me in the right direction.  I am using VB.NEt 2010 with MS 2012, the code from CountDigEvents, has been used as my example. 

 

In my global module I have the following declarations...

 

   
 'Encoder
    Friend EncoderInput As Task
    Friend EncoderReader As NationalInstruments.DAQmx.CounterReader
    Friend EncoderChannel As String = "CDOT/ctr0"
    Friend CounterbitRate As Long = 32
    Friend CountsperRev As Long = 3000
    Friend watch As Stopwatch

 I then use the following code to start the reading:

            'Encoder Related
            If EncoderInput Is Nothing Then
                EncoderInput = New Task
            End If
            If EncoderInput.CIChannels.Count = 0 Then
                             EncoderInput.CIChannels.CreateCountEdgesChannel(EncoderChannel, "Encoder On", CICountEdgesActiveEdge.Falling, 0, CICountEdgesCountDirection.Up)
                EncoderInput.Control(TaskAction.Verify)
            End If
            If EncoderReader Is Nothing Then
                EncoderReader = New CounterReader(EncoderInput.Stream)
            End If
            EncoderInput.Start()

 This all seems to work fine.  I then use a timer with a stopwatch for accurate time measurements.  I seem to get "close" readings (+/- 10-15%), but nowhere near what I would expect, and the values seem to drift.  I am comparing these values to a NIST calibrated laser tachometer that I know is right.

            Dim count As Integer
            Dim time As Double
            Dim diff As Integer
            'We will use this to Count the number of pulses that have occured
            'We will actually determine the time programmatically, since the timer isn't perfectly accurate
            'We will use stopwatch for this
            'We need to check if this is the first read, if so we need to enable the timer
            If watch.IsRunning = False Then
                'We need to start the stopwatch
                watch.Start()
                'Set the prevcount to 0
                PrevCount = 0
                'Set the speed data to 0
                DataGrid(1, 1) = 0
                DataGrid(2, 1) = 0
                gaugeRollSpeed.Value = 0
                PrevTime = 0
            Else
                Dim newtime As Double
                'Now we need to read the counter value from the DAQ device
                count = EncoderReader.ReadSingleSampleDouble

                'Now we must get the elapsed time in milliseconds
                newtime = watch.ElapsedMilliseconds
                Debug.Print("Count = " & count & "Time = " & newtime)
                time = newtime - PrevTime
                'Now we must restart the stopwatch
                PrevTime = newtime
                'Since this is a 32 bit counter the counter will read up to the bit's, then reset to 0
                'We need to take that into consideration
                diff = count - PrevCount
                If diff < 0 Then
                    diff = diff + ((2 ^ CounterbitRate) - 1)
                End If
                PrevCount = count
                'Now we can actually calculate the rpm during this time frame
                'Get the total revs
                Dim totalrevs As Double
                totalrevs = diff / CountsperRev
                'Now we need to convert the time to minutes
                Dim minutes As Double
                minutes = (time / 1000) / 60
                'Now we can calculate the rpm
                RPM = totalrevs / minutes

 Any ideas or thoughts on how I should proceed?

 

 

Thanks,

0 Kudos
Message 1 of 4
(6,117 Views)

Hi Matt0316,

 

I have a few questions to help me understand the issue you're seeing.

 

 

1. Could you link me to where you found the example you started from? It would help if I can see what you've built off of.

2. What DAQ device are you working with? Could you tell me the model?

3. You say you're seeing an error. Is there an actual error message, or are you referring to the fact that your values aren't what you expect?

4. What sort of values are you expecting to see? You say there's a 10-15% error, but what is the actual range of that error? What is the range of values you're expecting to see?

 

This should help us narrow down what might be causing the drift you're seeing. Thanks!

 

Emily C
Applications Engineer
National Instruments
0 Kudos
Message 2 of 4
(6,104 Views)

Hi Emily,

 

1) The example in terms of getting the counter data is from the examples installed with Measurement Studio.  "C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DotNET4.0\Counter\Count Digital Events\CountDigEvents\VB" is the specific example I used.  As far as the actual code to calculate RPM, I coded that, but it is pretty simple.  How many pulses have occured in how much time...

2) NI USB-6211

3)No actual Error, just unexpected values

4) I can set my motor to turn at a constant speed, again this is verified.  So say I have it set at 800RPM, and I measure it using a calibrated tachometer at 800RPM, my program outputs maybe 680 - 710.  then for a breif time it will go up to 950, then back down to 680-710.

 

 

I hope this helps, and we can find the source of the error.

 

Thanks,

 

--Matt

0 Kudos
Message 3 of 4
(6,078 Views)

Hi Matt,

 

Could you please try testing in Test Panels? Open MAX, select your USB-6211 device, and click Test Panels along the top. In the Counter I/O tab, set the mode to Edge Counting, and set the Edge Source you're using. If you know your RPM, and count for a set amount of time, can you verify that this gives you the count you're expecting? This could help us determine if the hardware is working as expected.

 

Thanks!

Emily C
Applications Engineer
National Instruments
0 Kudos
Message 4 of 4
(6,065 Views)