Is encoder count signed or unsigned?

Questions about using encoders with the Roboclaw product line
Post Reply
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Is encoder count signed or unsigned?

Post by Basicmicro Support »

Both ReadEncM# commands return a uint32_t(based on the roboclaw.h and roboclaw.cpp files I just looked at sothis is a typo in the user manual.

Because of some idiosyncracies with Arduino(and GCC) the ReadEncM# and SpeedAccelDeccelPosition commands use unsigned variables. However you can store those values in signed 32bit variables just fine and get the correct +- ranges.

A signed 32bit variable is used with the SetEnc commands for a similar reason but I dont remember the exact reason it couldnt use an unsigned value.

In general if you use signed variables for all your commands you will be fine.
rwschumann
Posts: 8
Joined: Wed Jun 20, 2018 7:39 am
Re: Is encoder count signed or unsigned?

Post by rwschumann »

Note this issue also exists as it relates to the position based commands.

If the encoder is really a signed value, which I would suggest is the correct approach, then it needs to be used consistently including with position.

thanks,
Rob
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Is encoder count signed or unsigned?

Post by Basicmicro Support »

As I explained in my earlier post youi can use the position value as a signed value or not. Your choice. The signed/unsigned arguments to our library functions have to do with the language they are written in so the language doesn tconvert the value inappropriately.

The only limitation is you must use a 32bit variable or smaller. If you used a 64bit variable to read the position value it would not convert to a signed vallue properly. However any other signed variable type will convert the position value to the properly signed value.

For example if your encoder is at 0 and you move backwards by 1 it will turn into 0xFFFFFFFF. This can be read as -1(2s compliment signed) or a rollover to 4billion+(unsigned). Same applies if you read the position value into a 16bit variable. 0 going backwards by 1 = 0xFFFF which is -1(signed) or 65535(unsigned).

Post Reply