Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Genral How do I get syncronized encoders to work with a PCI-6602

Solved!
Go to solution

I am new to the DAQmx and need some general help writting VB code that works.

I can get some things to work, but others don't work at all and I don't have enough knowledge of these drivers to get up the learnign curve hump.

 

I wanted to post this as a follow-up to the thread:

http://forums.ni.com/ni/board/message?board.id=40&message.id=5430&query.id=215100#M5430

But I couldn’t figure out how.  

 

Anyway, my problem is that I will eventually have to do much the same thing as the above thread with two encoders in sync.

 

I have a pci-6602 card, and my code snippet is at the bottom  The 6602 doesn't have the analog in to syncronize to like the thread above

  (yes, it is in Visual basic 6)

 

I can’t figure out the DAQmxCfgSampClkTiming.   Why do I want to have a sample clock at all?   (okay, when I have two channels synced I do – but this sample I want to get working and I don’t see why)?

 

Can’t I just get a new counter value with each quadrature input change?   (That’s what the DEV/PFI39 is, the same as encoder channel A )

 

Okay, so hopefully you tell me how to get that to work.

 

Next question:  What can I use for the DAQmxCfgSampClkTiming when I am doing two channels in sync?  Nothing seems to work, and I have tried a bunch of things.

 

Brynn Rogers

DeltaTech Controls

952-403-7400 x431

 

//// code that doesn't really work.   "Dev1/PFI39" is the channel A encoder input, I just want new data whenever the encoder changes (for now)

    ' DAQmx Configure Code

    DAQmxErrChk DAQmxCreateTask("count", TaskHandle)

      

    TaskIsRunning = True

     

    DAQmxErrChk DAQmxCreateCIAngEncoderChan(TaskHandle, "Dev1/ctr0", "", DAQmx_Val_EncoderType2_X4, 0, 0#, DAQmx_Val_EncoderZIndexPhase1_AHighBHigh, DAQmx_Val_AngleUnits2_Ticks, 500, 0#, "")

    DAQmxErrChk DAQmxCfgSampClkTiming(TaskHandle, "Dev1/PFI39", 1, DAQmx_Val_Rising, DAQmx_Val_AcquisitionType_FiniteSamps, samples)

                                                                                                       

    'DAQmx Start Code

    DAQmxErrChk DAQmxStartTask(TaskHandle)

 

    ' DAQmx Read Code

    DAQmxErrChk DAQmxReadCounterF64(TaskHandle, -1, 0.05 * samples, data(0), samples, ReadCount, ByVal 0&)

 

    ' All done! StopTask

    'DAQmxErrChk DAQmxStopTask(TaskHandle)

    'Stop task isn't needed because Clear Task also stops task

    DAQmxErrChk DAQmxClearTask(TaskHandle)

0 Kudos
Message 1 of 3
(4,338 Views)

Hey Brynn,

 

You don't need the analog in to syncronize like the thread above because your 6602 has 8 counters.  The reason we used the analog in with the multifunction DAQ card is because it didn't have enough counters to measure two encoders, since we needed a third counter to create a sample clock for the encoders.  Since we have enough counters with your 6602, we can use two counters for the encoder position and one counter for the time base.  

 

You need the sample clock because you need to be able to control when each scan starts.  Without the sample clock we sample on demand, but with the sample clock we can buffer, even if we set the timing to implicit.  We can definitely get a new counter value with each quadrature input change using a PFI line.

For the sample clock timing, using a PFI line is the best method to connect to the counters.

 

There's a C example on counter angular position located on your machine at C:\Documents and Settings\All Users\Documents\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Counter\Measure Position\Angular Position-Buff-Cont

-John Sullivan
Problem Solver
0 Kudos
Message 2 of 3
(4,314 Views)
Solution
Accepted by topic author Tonkabot

Okay,   Here is the working routine to get syncronous data from two encoders, and it seems to work okay.

   I have this working with my two 2000 CPR encoders, we'll see how it works when I get the 500,000 CPR encoders later.

 

The three big problems that caused it to not work were:  

1)   Theduty cycle argument for the createCOpulsechanfreq task needed to be between 0 and 1.  I had '50.0' before, it works when it is '0.5'

2)   The sample timebase that I used (ctr7) needed to be started before either of my two angle encoder tasks were created

3)   The "dev/PFI8" string needed to be changed to "/dev/PFI8".   I don't really understand why, but that was what seemed to fix it all.

 

So this question has been answered...      Now I have a different problem while reading 8 PWM values at once with the Semiperiodcounter,  which I will post in a new a different thread.

 

Thanks!

 

''''''''''''''''''   Working code here

Public Sub Aquireposition(samples As Long, data0() As Double, data1() As Double)

Dim BaseTaskHandle    As Long
Dim encoder0TaskHandle    As Long
Dim encoder1TaskHandle    As Long
Dim TaskIsRunning As Boolean
Dim ReadCount As Long
Dim strCounterString As String

   On Error GoTo ErrorHandler
  
    DAQmxErrChk DAQmxCreateTask("base", BaseTaskHandle)
     
    DAQmxErrChk DAQmxCreateCOPulseChanFreq(BaseTaskHandle, "/Dev1/ctr7", "base", DAQmx_Val_FrequencyUnits2_Hz, DAQmx_Val_Level1_Low, 0#, 200#, 0.5)
'Specify continuous timing
    DAQmxErrChk DAQmxCfgImplicitTiming(BaseTaskHandle, DAQmx_Val_AcquisitionType_ContSamps, 200)
     'DAQmx Start Code
    DAQmxErrChk DAQmxStartTask(BaseTaskHandle)
   
' DAQmx Configure Code
    DAQmxErrChk DAQmxCreateTask("encoder", encoder0TaskHandle)
      
    TaskIsRunning = True
     
    DAQmxErrChk DAQmxCreateCIAngEncoderChan(encoder0TaskHandle, "/Dev1/ctr0", "", DAQmx_Val_EncoderType2_X4, 0, 0#, DAQmx_Val_EncoderZIndexPhase1_AHighBHigh, DAQmx_Val_AngleUnits2_Degrees, 500, 0#, "")
    DAQmxErrChk DAQmxCfgSampClkTiming(encoder0TaskHandle, "/Dev1/PFI8", 1, DAQmx_Val_Rising, DAQmx_Val_AcquisitionType_FiniteSamps, samples)
   
    ' second encoder
    DAQmxErrChk DAQmxCreateTask("encoder1", encoder1TaskHandle)
    TaskIsRunning = True
     
    DAQmxErrChk DAQmxCreateCIAngEncoderChan(encoder1TaskHandle, "/Dev1/ctr1", "", DAQmx_Val_EncoderType2_X4, 0, 0#, DAQmx_Val_EncoderZIndexPhase1_AHighBHigh, DAQmx_Val_AngleUnits2_Degrees, 500, 0#, "")
    DAQmxErrChk DAQmxCfgSampClkTiming(encoder1TaskHandle, "/Dev1/PFI8", 1, DAQmx_Val_Rising, DAQmx_Val_AcquisitionType_FiniteSamps, samples)

    'DAQmx Start Code
    DAQmxErrChk DAQmxStartTask(encoder0TaskHandle)
    DAQmxErrChk DAQmxStartTask(encoder1TaskHandle)
 
    ' DAQmx Read Code
    DAQmxErrChk DAQmxReadCounterF64(encoder0TaskHandle, -1, 0.05 * samples, data0(0), samples, ReadCount, ByVal 0&)
    DAQmxErrChk DAQmxReadCounterF64(encoder1TaskHandle, -1, 0.05 * samples, data1(0), samples, ReadCount, ByVal 0&)
 
    ' All done! StopTask
    'DAQmxErrChk DAQmxStopTask(TaskHandle)
    'Stop task isn't needed because Clear Task also stops task
    DAQmxErrChk DAQmxClearTask(encoder0TaskHandle)
    DAQmxErrChk DAQmxClearTask(encoder1TaskHandle)
    DAQmxErrChk DAQmxClearTask(BaseTaskHandle)

    Exit Sub

ErrorHandler:

'   MsgBox "Error: " & Err.Number & " " & Err.Description, , "Error"
'    mlngPWM_ErrorCount(lngChannel) = mlngPWM_ErrorCount(lngChannel) + 1
    'MainForm.PWMErrorCount = "Errors(" & lngChannel & "): " & mlngPWM_ErrorCount(lngChannel)
  
    Resume Next

End Sub

 

0 Kudos
Message 3 of 3
(4,287 Views)