Lessons learned on the encoder driver #422
elsevers
started this conversation in
Show and tell
Replies: 1 comment 1 reply
-
I suspect that this might be related to |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Pitfall
In a couple of our codebases, we discovered that encoder users are finding it confusing to change the number of pulses per revolution. When a user encounters an encoder that has a different number of bits than the default value in
encoder.h
, there is a tendency to directly edit this line:AMDC-Firmware/sdk/app_cpu1/common/drv/encoder.h
Line 10 in d348fd4
and then not commit the code into the user's repo because this file resides in the
amdc-firmware
repo, which is typically a submodule of a user repo.This workflow is of course undesired because then the change isn't tracked and the user repo no longer has all information needed to build/use the code.
Solution 1: another pitfall
The next most common approach taken is to instead redefine
ENCODER_PULSES_PER_REV_BITS
elsewhere in the user's repo. Depending on where this is redefined (i.e., if it overrides the#define
inencoder.h
before the init function inencoder.c
is employed), this approach may work.However, it is never a good practice to be redefining macros and doing so leads to confusing code. So this workflow is also undesired.
Preferred Solution
The correct way to handle this situation is to instead make use of the
encoder.c
driver functions to set the encoder pulses per revolution within the user's code:AMDC-Firmware/sdk/app_cpu1/common/drv/encoder.h
Lines 15 to 16 in d348fd4
For example, introduce a new define
And in the user task init:
And then when reading the encoder (i.e., in the task callback function):
Additional Info
encoder_get_steps() and
encoder_get_position`encoder_get_steps()
is used to return a count of the number of "pulses" counted by the encoder since power on. One direction of rotation causes this to increment. The other direction, decrement. This function returns a 32 bit counter that does not wrap around based on the number of bits the user specifies of the encoder.encoder_get_position()
is used to return the position of the shaft. The position value is determined by a counter (again counting "pulses") that wraps around based on the number of bits that the user specifies of the encoder.encoder_set_pulses_per_rev_bits()
andencoder_set_pulses_per_rev_bits
are used by the user to specify the number of bits / pulses per revolution.Question
Since$2\pi$ ?
encoder_get_position()
is wrapped by the number of pulses per revolution, why does our code usually still mod the converted radian value byThat is, why is the last line necessary here?
Beta Was this translation helpful? Give feedback.
All reactions