encoder9
Software counting of quadrature encoder signals
Usage
loadrt encoder [num_chan=num | names=name1[,name2...]]
Overview
encoder is used to measure position by counting the pulses generated by a quadrature encoder. As a software-based implementation it is much less expensive than hardware, but has a limited maximum count rate. The limit is in the range of 10 kHz to 50 kHz, depending on the computer speed and other factors. If better performance is needed, a hardware encoder counter is a better choice. Some hardware-based systems can count at MHz rates.
encoder supports a maximum of eight channels. The number of channels actually loaded is set by the num_chan argument when the module is loaded. Alternatively, specify names= and unique names separated by commas.
The num_chan= and names= specifiers are mutually exclusive. If neither num_chan= nor names= are specified, or if num_chan=0 is specified, the default value is three.
encoder has a one-phase, unidirectional mode called counter. In this mode, the phase-B input is ignored; the counts increase on each rising edge of phase-A. This mode may be useful for counting a unidirectional spindle with a single input line, though the noise-resistant characteristics of quadrature are lost.
If used in counter-mode it is also possible to enable the missing-teeth index mode, where a gap in the pulse train of one or more teeth is used as in index marker. This system is used extensively for automotive crank position sensors.
Functions
encoder.update-counters(no floating-point)-
Does the actual counting, by sampling the encoder signals and decoding the quadrature waveforms. Must be called as frequently as possible, preferably twice as fast as the maximum desired count rate. Operates on all channels at once.
encoder.capture-position(uses floating point)-
Captures the raw counts from
update-countersand performs scaling and other necessary conversion, handles counter rollover, etc. Can (and should) be called less frequently thanupdate-counters. Operates on all channels at once.
Naming
The names for pins and parameters are prefixed as:
encoder.N. for N=0,1,...,num-1 when using num_chan=num
nameN. for nameN=name1,name2,... when using names=name1,name2,...
The encoder.N. format is shown in the following descriptions.
Pins
encoder.N.counter-modebit i/o-
Enables counter mode. When true, the counter counts each rising edge of the phase-A input, ignoring the value on phase-B. This is useful for counting the output of a single channel (non-quadrature) sensor. When false (the default), it counts in quadrature mode.
encoder.N.countss32 out-
Position in encoder counts.
encoder.N.index-enablebit i/o-
When true,
countsandpositionare reset to zero on the next rising edge ofPhase-Z. At the same time,index-enableis reset to zero to indicate that the rising edge has occurred.
encoder.N.min-speed-estimatefloat in (default: 1.0)-
Determine the minimum speed at which
velocitywill be estimated as nonzero andpostition-interpolatedwill be interpolated. The units ofmin-speed-estimateare the same as the units ofvelocity. Setting this parameter too low will cause it to take a long time forvelocityto go to 0 after encoder pulses have stopped arriving.
encoder.N.phase-Abit in-
Quadrature input for encoder channel N.
encoder.N.phase-Bbit in-
Quadrature input.
encoder.N.phase-Zbit in-
Index pulse input.
encoder.N.positionfloat out-
Position in scaled units (see
position-scale)
encoder.N.position-interpolatedfloat out-
Position in scaled units, interpolated between encoder counts. Only valid when velocity is approximately constant and above
min-speed-estimate. Do not use for position control.
encoder.N.position-scalefloat i/o-
Scale factor, in counts per length unit. For example, if
position-scaleis 500, then 1000 counts of the encoder will be reported as a position of 2.0 units.
encoder.N.missing-teeths32 in-
The number of teeth missing from the index gap. For example a 60 tooth gear with two teeth shortened to form an index so that there are 58 pulses per revolution would use a position-scale of 60 and a missing-teeth of 2.
encoder.N.rawcountss32 out-
The raw count, as determined by
update-counters. This value is updated more frequently thancountsandposition. It is also unaffected byresetor the index pulse.
encoder.N.resetbit in-
When true,
countsandpositionare reset to zero immediately.
encoder.N.velocityfloat out-
Velocity in scaled units per second.
encoderuses an algorithm that greatly reduces quantization noise as compared to simply differentiating thepositionoutput. When the magnitude of the true velocity is below min-speed-estimate, the velocity output is 0.
encoder.N.velocity-rpmfloat out-
Velocity in scaled units per minute. Simply
encoder.N.velocityscaled by a factor of 60 for convenience.
encoder.N.x4-modebit i/o-
Enables times-4 mode. When true (the default), the counter counts each edge of the quadrature waveform (four counts per full cycle). When false, it only counts once per full cycle. In
counter-mode, this parameter is ignored.
encoder.N.latch-inputbit in
encoder.N.latch-fallingbit in (default: TRUE)
encoder.N.latch-risingbit in (default: TRUE)
encoder.N.counts-latcheds32 out
encoder.N.position-latchedfloat out-
Update
counts-latchedandposition-latchedon the rising and/or falling edges oflatch-inputas indicated bylatch-risingandlatch-falling.
encoder.N.counter-modebit rw-
Enables counter mode. When true, the counter counts each rising edge of the phase-A input, ignoring the value on phase-B. This is useful for counting the output of a single channel (non-quadrature) sensor. When false (the default), it counts in quadrature mode.
encoder.N.capture-position.tmaxs32 rw Maximum number of CPU cycles it took to execute this function.
Parameters
The encoder component has no HAL Parameters.
Releases
Unchanged in 2 releases since Debian 12 (Bookworm).