hsioEncoderEnable (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

All

Firmware version:

1.52.00


hsioEncoderEnable configures and enables monitoring of an incremental encoder.

If the Z input is provided, it can be used to improve the positioning.

 

The encoder must be using quadrature encoding, where each step consists of four phases:

hsio_Quadrature_Diagram

By examining the signals, it is possible to determine which direction the encoder is being moved and for how many steps, making it possible to determine the position.

 

When the Z-input is asserted while the encoder uses a mode that uses the Z-input, it will be registered and the position will be updated when the next step starts.

 

Note: Digital input 6-8 on the RTCU LX4 can not be used for encoders.

 

 

Input:

a : PTR

Address of the digital input signal to use as A (please see the example below).

 

b : PTR

Address of the digital input signal to use as B (please see the example below).

 

z : PTR

Address of the optional digital input signal to use as Z (please see the example below).

 

id : INT (1..2) default 1

Identifier of the encoder to configure.

 

mode : INT (0..3) default 0

The mode to use for handling the Z signal.

0

-

Z is not handled.

1

-

Reset on every Z.

Every time Z is observed, the position is reset to 0. This is useful if Z is only active once during the entire movement, e.g. a rotary encoder that rotates 360 degrees or less.

2

-

Reset on first Z.

The position will be reset to 0 the next time z is observed. This can e.g. be used in combination with an end-stop to zero the position.

3

-

Calibrate the position on every Z.

This will set the position to the nearest multiple of the number of steps provided, every time Z is asserted.

This will make sure that a few lost steps does not accumulate across multiple rotations of a rotary encoder.

 

steps : INT default 0

The number of steps on a full rotation. Only needed in mode 3.

 

Returns: INT

1

- Success

0

- Function is not supported.

-1

- Unspecified error.

-2

- Invalid signal provided. This can only be used on on-board high speed input signals.

-3

- Invalid mode.

-4

- The mode requires that steps has a valid value.

-5

- Invalid ID.

-6

- The encoder is already in use. Disable it first with hsioEncoderDisable.

 

 

Declaration:

FUNCTION hsioEncoderEnable : INT;
VAR_INPUT
  a     : PTR;
  b     : PTR;
  z     : PTR;
  id    : INT := 1;
  mode  : INT := 0;
  steps : INT := 0;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
VAR_INPUT
 a : BOOL;
 b : BOOL;
 z : BOOL;
END_VAR;
 
PROGRAM test;
BEGIN
  ...
  // Enable encoder
  hsioEncoderEnable(id := 1, a := ADDR(a), b := ADDR(b), z := ADDR(z), mode := 3, steps := 200);
  ...
END;
 
END_PROGRAM;