Position counter cannot deal with initial negative rotation

3 views (last 30 days)
Hi everyone
I'm using an optical encoder to keep track of how much my stepper motor has rotated, however I am encountering a problem which I think has a simple solution but I haven't been able to figure it out or find anything online.
My quadrature encoder is connected to a NI 6229-USB DAQ unit and by using the code below, I am able to read data from the encoder.
----------------------------------------------------------------------------------------
s = daq.createSession('ni');
ch = s.addCounterInputChannel ('Dev3', 'ctr0', 'Position');
ch.EncoderType = 'X4'
ch =
Data acquisition counter input position channel 'ctr0' on device 'Dev3':
EncoderType: X4
ZResetEnable: 0
ZResetValue: 0
ZResetCondition: BothHigh
TerminalA: 'PFI8'
TerminalB: 'PFI10'
TerminalZ: 'PFI9'
Name: ''
ID: 'ctr0'
Device: [1x1 daq.ni.DeviceInfo]
MeasurementType: 'Position'
----------------------------------------------------------------------------------------
When I tell my motor to turn in its positive direction (in this case clockwise), the encoder works fine and I get the right angle by using:
(s.inputSingleScan/(4*1024))*360
where "s.inputSingleScan" gives me the count. The encoder also works if I then tell the motor to in its negative direction, for example if I initialise the session and tell the motor to go to 10degrees and then 5degrees, I get the right value. However, if I initialise the code and I tell the motor to go in the negative direction first, I get nonsensical values i.e. if I tell it to go from 0 to -5 degrees I get an angle of 4x10^6... Any ideas why this is happening?

Accepted Answer

Jovan
Jovan on 9 Dec 2013
Found a previous post with the same problem:
The issue is to do with the formatting of the numbers from the DAQ unit, hence they need to be converted. I have found this to work for me:
indx = double(typecast(uint32(s.inputSingleScan),'int32'));
encoder_angle = (indx/4096)*360;

More Answers (0)

Categories

Find more on Counter and Timer Input and Output in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!