Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Quadrature decoding


rlwoell Sep 27, 2010 08:18 PM

Has anyone used a 9071 on a CR9000 or DIO on other loggers to do quadrature decoding? I have position sensors with phased pulse outputs on A and B channels. A direction channel is not available. I know there are chips out there that can do the decoding and give a channel of pulses and another channel of direction but right now that is not an option for me. Besides, I should be able to do it directly.

Any example code would be appreciated.


Sam Sep 29, 2010 09:17 PM

Rlwoell,

First, officially we support shaft encoders through the use of QD1, Incremental Shaft Encoder Interface.
http://www.campbellsci.com/qd1

However, I also have hoped to do this - directly monitor shaft encoder without an interface. I think this should be possible using ReadIO. Additionally, I think it could potentially be done efficiently using ReadIO in combination with WaitDigTrig. I have only devoted a few moments to this, and there is still a lot of ground to cover. So far, I have not successfully combined WaitDigTrig and ReadIO, but I have had good success scanning the control ports at 100 Hz and reading slowly changing signals.

-----------------------------------------------
THIS CODE IS NOT INTENDED FOR PRODUCTION IMPLEMENTATION.
THIS CODE IS FOR DEMONSTRATION PURPOSES ONLY.

Public PTemp, batt_volt
Const APort = 1
Const BPort = 2
Public CurCode
Public LastCode
Public Direction
Public AccumRaw
Public AccumRpt

Sub QuadDecode
If CurCode = 2 AND LastCode = 0 Then
Direction = 1
AccumRaw = AccumRaw + 0.25
AccumRpt = Floor (AccumRaw)
ElseIf CurCode = 3 AND LastCode = 2 Then
Direction = 1
AccumRaw = AccumRaw + 0.25
AccumRpt = Floor (AccumRaw)
ElseIf CurCode = 1 AND LastCode = 3 Then
Direction = 1
AccumRaw = AccumRaw + 0.25
AccumRpt = Floor (AccumRaw)
ElseIf CurCode = 0 AND LastCode = 1 Then
Direction = 1
AccumRaw = AccumRaw + 0.25
AccumRpt = Floor (AccumRaw)

ElseIf CurCode = 3 AND LastCode = 1 Then
Direction = 0
AccumRaw = AccumRaw - 0.25
AccumRpt = Ceiling (AccumRaw)
ElseIf CurCode = 2 AND LastCode = 3 Then
Direction = 0
AccumRaw = AccumRaw - 0.25
AccumRpt = Ceiling (AccumRaw)
ElseIf CurCode = 0 AND LastCode = 2 Then
Direction = 0
AccumRaw = AccumRaw - 0.25
AccumRpt = Ceiling (AccumRaw)
ElseIf CurCode = 1 AND LastCode = 0 Then
Direction = 0
AccumRaw = AccumRaw - 0.25
AccumRpt = Ceiling (AccumRaw)
EndIf

LastCode = CurCode

EndSub


BeginProg
Scan (10,mSec,0,0)
ReadIO (CurCode,&B11)
Call (QuadDecode)
NextScan

'SlowSequence
'Do While TRUE
'WaitDigTrig (APort,0)
'ReadIO (CurCode,&B11)
'Call (QuadDecode)
'Loop
'EndSequence
'
'SlowSequence
'Do While TRUE
'WaitDigTrig (BPort,0)
'ReadIO (CurCode,&B11)
'Call (QuadDecode)
'Loop
'EndSequence
'
'SlowSequence
'Do While TRUE
'WaitDigTrig (APort + 2,1)
'ReadIO (CurCode,&B11)
'Call (QuadDecode)
'Loop
'EndSequence
'
'SlowSequence
'Do While TRUE
'WaitDigTrig (BPort + 2,1)
'ReadIO (CurCode,&B11)
'Call (QuadDecode)
'Loop
'EndSequence

EndProg

Log in or register to post/reply in the forum.