encoder_unwind_delta_tick_count(encoder_index, delta_tick_count)

Returns the delta_tick_count. Unwinds in case encoder wraps around the range. If no wrapping has happened the given delta_tick_count is returned without any modification.

Consider the following situation: You are using an encoder with a UINT16 range, meaning the tick count is always in the [0; 65536[ range. When the encoder is ticking, it may cross either end of the range, which causes the tick count to wrap around to the other end. During your program, the current tick count is assigned to a variable (start:=encoder_get_tick_count(...)). Later, the tick count is assigned to another variable (current:=encoder_get_tick_count(...)). To calculate the distance the conveyor has traveled between the two sample points, the two tick counts are subtracted from each other.

For example, the first sample point is near the end of the range (e.g., start:=65530). When the conveyor arrives at the second point, the encoder may have crossed the end of its range, wrapped around, and reached a value near the beginning of the range (e.g., current:=864). Subtracting the two samples to calculate the motion of the conveyor is  not robust, and may result in an incorrect result

(delta=current-start=-64666).

Conveyor tracking applications rely on these kinds of encoder calculations. Unless special care is taken to compensate the encoder wrapping around, the application will not be robust and may produce weird behaviors (e.g., singularities or exceeded speed limits) which are difficult to explain and to reproduce.

This heuristic function checks that a given delta_tick_count value is reasonable. If the encoder wrapped around the end of the range, it compensates (i.e., unwinds) and returns the adjusted result. If a delta_tick_count is larger than half the range of the encoder, wrapping is assumed and is compensated. As a consequence, this function only works when the range of the encoder is explicitly known, and therefore the designated encoder must be enabled. If not, this function will always return nil.

Parameters

encoder_index: Index of the encoder to query. Must be either 0 or 1.

delta_tick_count: The delta (difference between two) tick count to unwind (float)

Return Value

The unwound delta_tick_count (float)