Você está na página 1de 5

# dSPACE FAQ 235

FAQ 235

## Measure incremental encoder signal at low speed

Keywords
incremental encoder interface; calculate speed; angular velocity; DS1104; DS1103; DS3001; DS3002;
DS1401
Question
When calculating the angular velocity from an encoder signal at low speed the result becomes
imprecise. How can a better measurement result be achieved?
Solution
Image 1 shows the usual implementation when calculating the angular velocity from measured
encoder pulses. The Enc delta position output equals the number of measured encoder pulses within
the last sampling interval. This signal is converted to the corresponding angle and divided by the time
step.
Problems arise in this case if only a few pulses are counted within the sampling interval. Usually the
sampling interval equals the Fixed Step Size of the model.

## Image 1 Usual implementation

To solve such a problem at low speed there are the following approaches.
Method 1 Use wider sampling interval
Instead of executing the Encoder position block in each program step, it can be placed in a subsystem
(Triggered Subsystem, Function-Call Subsystem) that is executed with a greater step size.
When using a Triggered Subsystem it is even possible to implement a variable sampling interval for
the encoder measurement: The output of a configurable loop counter can be used as trigger signal.
This way a small sampling interval can be used for high sensor frequencies (high speed) and a wide
sampling interval can be realized for low sensor frequencies (low speed).
It is only necessary to divide the result by the correct time difference (cf. Image 1).
Method 2 Measure the time difference between encoder pulses
Instead of measuring the number of pulses in a given interval, another measurement principle can be
realized: Using interrupts and RTLib functions the time difference between successive encoder pulses
can be measured. The following images show the implementation with the RTI1104 blockset.

## Measure incremental encoder signal at low speed

FAQ Version: 1 / 2010-12-29
Page 1

## Using the hardware interrupt

Image 2 shows a hardware interrupt block triggering a Function-Call Subsystem in which the time
measurement is implemented. The external hardware input is connected to phase A (phi 0) of the
incremental encoder.
The Task Transition blocks are required to exchange data between the interrupt driven task and the
base rate task of the model.

## Measure the time difference

Image 3 shows the proposed contents of the subsystem for the time measurement.
The magenta-colored System Outputs block has to be taken from the custom code library. Enter
custcode at the MATLAB command prompt to open the library.
Additionally insert the Data Store Memory (DSM) blocks and the DSM Read block.
The input port Angle represents the fixed angle between two sensor pulses
( (2*pi) / (impulses per revolution) ).
This fixed angle is divided by the measured time difference (DSM deltaT).

## Image 3 Contents of Function-Call Subsytem

Measure incremental encoder signal at low speed
FAQ Version: 1 / 2010-12-29
Page 2

## Implementing the custom C-code

It is important to set the RTW storage class of the DSM blocks to ExportedGlobal.
This way global variables with the same name as the Data store are created (Image 4).

## In the System Outputs block the following code must be entered:

// declare local variable
double currentTime;
// calculate difference to previous step
deltaT = currentTime - oldTime;
// save new timestamp
oldTime = currentTime;
With the ts_time_read() function the absolute simulation time is read from the hardware with a
resolution of approx. 80ns (depending on the hardware platform).
The time difference deltaT is written to the corresponding DSM block.

## Measure incremental encoder signal at low speed

FAQ Version: 1 / 2010-12-29
Page 3

## dSPACE FAQ 235

The custom code must completely be entered into the Function Declaration Code field of the System
Outputs block as shown in Image 5. This guarantees that the custom code is executed before any
generated code.

## Detect the direction

The B signal (phi90) of the sensor can be connected to an additional digital input. This is
demonstrated in Image 3. The level of the input indicates the direction of the movement because the
B phase is shifted by 90 against the A phase which is used for triggering.
Recommended model settings
The task driven by the hardware interrupt (sensor phase A) must have the highest priority to achieve
the best resolution. Consider that the activation of a task involves a certain delay and jitter.
The relevant task switching times are documented in the RTI and RTI-MP Implementation Guide
(%dspace_root%\doc\print\ImplementRTI.pdf).

## Measure incremental encoder signal at low speed

FAQ Version: 1 / 2010-12-29
Page 4

## How to Contact dSPACE Support

dSPACE GmbH
Rathenaustr. 26
Germany
++49 5251 1638-941
mailto:support@dspace.de
http://www.dspace.com/support

dSPACE recommends that you use the support request form on the internet to contact
dSPACE support.
It is available under
http://www.dspace.com/goto?supportrequest
Software Updates and Patches
dSPACE strongly recommends that you download and install the most recent patches for your
current dSPACE installation. Visit http://www.dspace.com/goto?patches for software updates
and patches.
FAQ
FAQ documents are available under http://www.dspace.com/goto?faq.

Important Notice
This document contains proprietary information that is protected by copyright. All rights are
reserved. Neither the documentation nor software may be copied, photocopied, reproduced,
translated, or reduced to any electronic medium or machine-readable form, in whole or in part,
without the prior written consent of dSPACE GmbH.