Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder position is not read/set correctly

Hi everyone,

I am currently  running into a problem I cant seam to fix.

 

To measure a linear position I use an encoder task. The task itselfe works well and counts the position as planned.

 

Still I am running into a major issue: For a new measurement I need to clear the buffer of the encoder task, because I dont want to get any old data. As just reading all the data out of the buffer (_encoderTask.Stream.ReadAllAvailableSamples = true; + _encoderTaskReader.ReadMultiSampleInt32(-1); )d oesnt do the job, there are still samples in the buffer.

 

So i followed this guide and tried to delete the task and create a new one for the new measurement. Now the problem is: if I read the current position from the encoder task, delete the task and create a new task with its startingposition to be the same as the last position of the old task, the positions dont match. I make an example to make this clear:

 

The task was running. Now I read the current position:

 

 

// Clear Buffer
_encoderTaskReader.ReadMultiSampleInt32(-1);

// We need to start the clock task to get at least one sample
_clockTask.Start();

UInt32 pos = _encoderTaskReader.ReadSingleSampleUInt32();

_clockTask.Stop();

// Clear Buffer
_encoderTaskReader.ReadMultiSampleInt32(-1);

 

I need to start the clock task for a very short amount of time, so a new sample is generated and I can read the current position.

 

Now that I have the current position (in Ticks) I reinit the encoder task:

 

_encoderTask.CIChannels.CreateLinearEncoderChannel(DeviceName + "/ctr0", "Encoder 1", CIEncoderDecodingType.X4, false, 0, CIEncoderZIndexPhase.AHighBHigh, 1, (double)pos/ 4, CILinearEncoderUnits.Ticks);

 

Funny thing here is: the read position gives you the position in ticks, but you need to devide this number by 4 to set the starting position, cause he doesn't accept the position in ticks but in "samples".

 

If i now read the position again:

 

// Clear Buffer
_encoderTaskReader.ReadMultiSampleInt32(-1);

// We need to start the clock task to get at least one sample
_clockTask.Start();

UInt32 pos2 = _encoderTaskReader.ReadSingleSampleUInt32();

_clockTask.Stop();

// Clear Buffer
_encoderTaskReader.ReadMultiSampleInt32(-1);

 

 

pos and pos2 are not the same. The axis was not moving while this happens so there where no ticks generated (checked it with an osciliscope). The position is always 1-5 ticks smaller than the original one. As I generate new tasks every 5 seconds and have alot of measurements (several hundred), this errors sums up really quickly and ruins my measurements.

 

 

How do i fix this problem? Is there any way to reset the buffer of the task in another way? How do I initialize the task so the psotion doesn't differ?

 

Best,

nano

0 Kudos
Message 1 of 3
(5,536 Views)

Hello everyone,

 

this problem is still relevant and not solved.

 

Is there no one who can help me solve this? If so: where can I find someone able to determin the correct way to do this?

 

Best,
Nano

0 Kudos
Message 2 of 3
(5,454 Views)

Hello,

 

I would try to cast the variable pos in this way:

((double)pos)/4.0

 

only 4 would cast the variable back to an integer and you will loose data.

 

kind regards

Richard

0 Kudos
Message 3 of 3
(5,343 Views)