Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Can't sync index pulse of encoder

I am trying to program a encoder (2000 counts/rev) in c#.  The problem I'm having is that the index pulse doesn't happen every 2000 counts. It happens every 2000 +- 5 counts. I suspect it is something wrong with how I am sampling.

I have a PCI-6071e card running nidaq 7.4. Channel A of the encoder is tied to ctr0, channel B to dio 6, and the index to ctr1. Here is my code:

enTask.CIChannels.CreateCountEdgesChannel ("/dev1/ctr0", "",
    CICountEdgesActiveEdge.Rising,
    8388608,
    CICountEdgesCountDirection.ExternallyControlled);
ctrReader = new CounterReader(enTask.Stream);

indexTask = new Task("Index");
indexTask.CIChannels.CreateCountEdgesChannel ("/dev1/ctr1", "",
    CICountEdgesActiveEdge.Rising,
    0,
    CICountEdgesCountDirection.Up);
indexReader = new CounterReader(indexTask.Stream);

enTask.Start();
indexTask.Start();
   
int c=0;
int i=0;
int prevI=0;

while(true) {
    c = ctrReader.ReadSingleSampleInt32();
    i = indexReader.ReadSingleSampleInt32();

    if( i!=prevI){
        Console.WriteLine(c);
    }
                   
    prevI=i;
}

Thanks,

Gerry
0 Kudos
Message 1 of 11
(5,624 Views)
Hi Gerry,

Sounds like you are running into some trouble when you are doing an encoder measurement. 

There is an excellent example of exactly that, which should have installed when you installed the drivers.  You can find that example in this folder path:
C:\Program Files\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Counter\Measure Position\Angular Position-Buff-Cont

Here is the basic meat of that program:
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));

    DAQmxErrChk (DAQmxCreateCIAngEncoderChan(taskHandle,"Dev1/ctr0","",DAQmx_Val_X4,0,0.0,DAQmx_Val_AHighBHigh,DAQmx_Val_Degrees,24,0.0,""));

    DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"/Dev1/PFI9",1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));

    /*********************************************/
    /*/ DAQmx Start Code
    /*********************************************/
    DAQmxErrChk (DAQmxStartTask(taskHandle));

    printf("Continuously reading.  Press any key to interrupt\n");
    while( !_kbhit() ) {
        /*********************************************/
        /*/ DAQmx Read Code
        /*********************************************/
        DAQmxErrChk (DAQmxReadCounterF64(taskHandle,1000,10.0,data,1000,&read,0));

        printf("Acquired %d samples\n",read);
    }
    _getch();
Regards,
0 Kudos
Message 2 of 11
(5,606 Views)
Thank you for the example. Unfortunately, my card (PCI-6071e) doesn't support angular encoder channels. This is the output from the example.

DAQmx Error: Selected physical channel does not support the measurement type req
uired by the virtual channel you are creating.

Does nidaq 7.5 fix this, or is this just not supported by the card? Is it possible to get the index pulse to accurately pulse every 2000 counts as I want with the card I have currently? It seems to me that angular encoder channels is just implementing in the driver what I have programmed myself...

Thanks,

Gerry
0 Kudos
Message 3 of 11
(5,596 Views)
Is there a possibilty that the the pulses are too fast for the counter? What's the maximum frequency that the counter can count? Also, does choice of langauge (C#, C++, C) make a difference, especially since c# is interpreted bytecode?

Thanks

gerry
0 Kudos
Message 4 of 11
(5,587 Views)

Gerry,

Try taking a look at this KnowledgeBase for more information on how to get your Quadrature Encoder working.
Using Quadrature Encoders with E Series DAQ Boards

DAQ-STC based counters (E-Series boards) 
Supports quadrature encoders, but not Z-indexing. The up/down pin is DIO6 for counter 0 and DIO7 for counter 1. Connect channel A to the source pin, channel B to the up/down pin, and the gating pulse to the gate. For information on using quadrature encoders with E-Series boards, refer to the Using Quadrature Encoders with E Series DAQ Boards document

There should not be a problem with using the PCI-6071E as opposed to any other device.  Also there shouldn't be any problems associated with using C# versus any other language as all of the calls are to the same set of dlls.

Regards,

 
0 Kudos
Message 5 of 11
(5,582 Views)
I've read the article and followed its directions, and I still can't get it to work. Interestingly enough, on one computer, the program consistantly reports greater than 2000 pulses/rev, while on another computer, the program reports less then 2000. I'm sure the encoder works because I've hooked it up to a counter unit, where it functioned properly. So the problem is either with the card or the code. I've run all the self tests and diagnostics on the card, and those are all fine. Am I missing something in the code? Do I have to worry about timebases or gating...? I'm very new to all this, and it is frustrating that such a simple problem is taking so long to solve.

Thanks again,

Gerry
0 Kudos
Message 6 of 11
(5,551 Views)

Hi Gerry,

Sorry about the confusion on this one, but I found out what is happening.  Turns out that there were some differences between the E-Series and M-Series devices that I was unaware of until a fellow engineer pointed it out to me.

They helped me find the following KB that describes exactly the behavior that you are seeing and why it occurs.  Looks like the real culprit is the board you are using uses the digital lines for direction rather than a PFI line.  Unfortunately it is a hardware limitation and if you need faster rates than about 1000 Hz, then you will need a different board. 

More details are below:

Title:
Incorrect Count When Using a Quadrature Encoder on an E-Series Board

Problem:
I am trying to obtain the count of a quadrature encoder using an E-Series board. I have made the connections correctly, but the count is incorrect. What is the problem?

Solution :
This problem is most likely due to the data transfer rate limitation of the DIO of the E-Series Board. When connecting a quadrature encoder to an E-Series board, you wire channel A output of the encoder to the PFI8 (SOURCE) and channel B output of the encoder to DIO6 (UP_DOWN) input. Since the data transfer method of the DIO channels of the E-Series boards are programmed I/O (the slowest data transfer method), your data transfer rate will be limited by the software timing.

Depending on the system, the maximum data transfer rate for a programmed I/O is about 1KHz.

If you require a data transfer rate of greater than 1KHz or experience inaccurate readings from your quadrature encoder using an E-Series board, you will need to choose another option.

Here are two options:
  1. 6602 Counter/Timer: Ideal for counter timer applications. 8 Counter/Timers, 3 DMA channels. Simple to use with quadrature encoders. Refer to page 3-19 of the 6602 for detail on use with quadrature encoders (type X1, X2, X4)
  2. M Series 2 Counter/Timers, 32 hardware clocked DIO lines up to 1MHz, 6 DMA channels.

Major Differences Between E Series and M Series DAQ Devices
Using Quadrature Encoders with E Series DAQ Boards

Regards,

 
0 Kudos
Message 7 of 11
(5,532 Views)
Thanks for all your help.

Gerry
0 Kudos
Message 8 of 11
(5,530 Views)
Am I correct to say that since the PCI-6601 has 4 counters, I can hook up 4 encoders, each with channels A, B, and I? (This is in contrast to e-series cards, where each encoder took up 2 counters (one for channel A, the other for I)).
 
Also, the m-series cards that have 32 analog inputs are referring to single-ended inputs, right? Equivalently, I'd have 16 differential inputs.
 
Also, could I be pointed to documents that explain RTSI, triggers, and timebases?
 
Thanks,
 
Gerry
0 Kudos
Message 9 of 11
(5,506 Views)

Hi Gerry,

You can connect 4 encoders with A, B, and Z inputs to the PCI-6601.  Each counter has an auxiliary connection in addition to gate, source, and out that is used.  The best place to look for this type of information for our counter/timer products is in the DAQmx Help.  Under a search for "encoder" I found a pinout that shows where to connect the encoder inputs. 

Yes, on an M Series, that is referring to single ended inputs so you would have 16 differential inputs.

The best place to learn about these concepts is in the different help files that install with the driver.  If you go to Start >> Programs >> National Instruments >> NI-DAQ you will find a number of help files that will help you with these concepts.  I do not see any general tutorials for these concepts on the website since they are normally more specific to a certain hardware setup or situation.  You can also see the help files for the specific hardware you are using. 

If you have any specific questions about these ideas, please let us know.

Thanks,

Laura

 
0 Kudos
Message 10 of 11
(5,483 Views)