Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculating Duty Cycle, VB6, DAQmx, PCI6624

I currently have a 5000 Pulse per revolution encoder that I need to calculate the duty cycle of all the pulses.
 
I am doing this in Visual Basic 6.0 and I am using DAQmx.  The counter card is the PCI 6624.
 
Currently my problem is this..I think that I am filling the array with the necessary data, but I am having a hard time retrieving it.  When I do retrieve some data, it seem like it is repetetive information.  Here is my code and any suggestions that can help is greatly appreciated.
 
Dim data(100000) As Double
Dim samples As Integer
Dim NSamples As Integer
Dim sampsPerChanRead As Long
Dim A_Gate_Counter As String
Dim A_SignalSource As String
A_Gate_Counter = "Dev1/ctr1"
A_SignalSource = "/Dev1/PFI34"
samples = 5000  'value of encoder ppr
NSamples = (samples * 2)
 
DAQmxErrChk DAQmxCreateTask("", ADutyTaskHandle)
DAQmxErrChk DAQmxCreateCISemiPeriodChan(ADutyTaskHandle, A_Gate_Counter, "", 0.0000001, 0.000008, DAQmx_Val_TimeUnits3_Seconds, "")
DAQmxErrChk DAQmxCfgSampClkTiming(ADutyTaskHandle, "20MHzTimebase", 10000, DAQmx_Val_Rising, DAQmx_Val_AcquisitionType_FiniteSamps, 10000)
DAQmxErrChk DAQmxCfgImplicitTiming(ADutyTaskHandle, DAQmx_Val_AcquisitionType_FiniteSamps, NSamples)
DAQmxErrChk DAQmxSetCISemiPeriodTerm(ADutyTaskHandle, "", A_SignalSource)
DAQmxErrChk DAQmxSetArmStartTrigType(ADutyTaskHandle, DAQmx_Val_TriggerType4_DigEdge)
DAQmxErrChk DAQmxSetDigEdgeArmStartTrigSrc(ADutyTaskHandle, A_SignalSource)
DAQmxErrChk DAQmxSetDigEdgeArmStartTrigEdge(ADutyTaskHandle, edge)
DAQmxErrChk DAQmxSetCISemiPeriodStartingEdge(ADutyTaskHandle, "", DAQmx_Val_Edge1_Falling)
DAQmxErrChk DAQmxStartTask(ADutyTaskHandle)
 
DAQmxErrChk DAQmxReadCounterF64(ADutyTaskHandle, NSamples, 10#, data(1), NSamples, sampsPerChanRead, ByVal 0&)
DAQmxStopTask ADutyTaskHandle
 
 
For i = 1 To samples  'this is the value of encoder PPR
     Cycle(i) = data(2 * i - 1) + data(2 * i)
     If Cycle(i) >= CycleMax Then CycleMax = Cycle(i)
          If Cycle(i) <= CycleMin Then
               CycleMin = Cycle(i)
               TotalCycle = TotalCycle + Cycle(i)
               CycleAve = TotalCycle / i
          End If
     High(i) = data(2 * i - 1)
     Duty(i) = (High(i) / Cycle(i) * 100)
     CycleCum = CycleCum + Cycle(i) * 80000000
Next i
 
For i = 1 To samples    'this is the value of encoder PPR
     If Duty(i) >= DutyMax Then DutyMax = Duty(i)
     If Duty(i) <= DutyMin Then DutyMin = Duty(i)
          TotalDuty = TotalDuty + Duty(i)
          DutyAve = TotalDuty / i
Next i
 
DutyMax = (DutyMax * 3.6)  'converts the duty cycle percentage value into a degree value
DutyMin = (DutyMin * 3.6)
DutyAve = (DutyAve * 3.6)
   
 
 
 
 
Anybody please help me with this.  I can even change the code.  I am not stuck with this code so if you have any recommendations, I'll take them.
 
Thanks
0 Kudos
Message 1 of 5
(8,654 Views)
Hello Big Guy,

I've looked over your code and nothing seems glaringly wrong with it.  I have a few questions that might give me a better idea of what you're doing/ 

What data values are you expecting for the duty cycles?

How are you rotating the encoder? 

How fast is it rotating?

Regards?
Regards,
John Bongaarts
0 Kudos
Message 2 of 5
(8,647 Views)
Thanks for the quick response John.
 
The data values are in percentage and on a perfect encoder should produce the obvious 50% duty cycle.  Since this is a 5000 count encoder there are 5000 high and 5000 low pulses.  Of course based on tolerences in the encoder each of the values will vary to some degree.  This is why the duty variable is declared as a double so all these variances can be seen.  There are situations where the encoder will produce what we call jitter and the signals could produce measured values from 30% to 70% or even worse.
 
The encoder is coupled to a servo motor and is spinning at 3600 RPM's which produces a frequency of 300kHz.
 
Hope this helps....
 
Thank you very much.
Steve
0 Kudos
Message 3 of 5
(8,644 Views)
Hi Big Guy,

I was also curious, how have you concluded that the data is not valid?

Regards,
Regards,
John Bongaarts
0 Kudos
Message 4 of 5
(8,635 Views)
I have been testing encoders for the past 11 years and I have been using traditional DAQ for the majority of that time.  I am somewhat a newby with DAQmx.  The values do fluctuate considerably.  When I measure the values in traditional DAQ for a 5000 PPR encoder I get 10,000 unique values.  Values could be something like 49.123431, 50.000392, 49.123453......etc.
 
I also am using a TEK scope with the duty cycle measurement enabled.  It is showing values that are fluctuating too much for these values to be valid.
 
Currently, the values that I am receiving only expose 4-5 unique values which are duplicated multiple times throughout the revolution of the encoder.  Another reason that I know that these values aren't valid is because of how identical they are to each other.  I am exposing up to eight levels of decimal representation and they are all the same. 
 
This is why I am thinking that the same values are stored in the counter and then transfered to the array.
 
Thanks again for your help and I am looking forward to your guidance in resolving this issue with me.
 
Steve
0 Kudos
Message 5 of 5
(8,632 Views)