Is encoder count signed or unsigned?

Questions about using encoders with the Roboclaw product line
Posts: 1
Joined: Wed Jun 06, 2018 9:39 am

Is encoder count signed or unsigned?

Postby mitch » Thu Jun 07, 2018 7:21 am

Can you clarify whether the encoder count is signed or unsigned? I see some conflicting information:

1. In RoboClaw.h: SetEncM1(uint8_t address, int32_t val);

2. In RoboClaw.h: uint32_t ReadEncM1(uint8_t address, uint8_t *status=NULL,bool *valid=NULL);

3. In RoboClaw.h: SpeedAccelDeccelPositionM1(uint8_t address, uint32_t accel, uint32_t speed, uint32_t deccel, uint32_t position, uint8_t flag);

4. In RoboClaw Series User Manual p.99: int32_t enc2 = roboclaw.ReadEncM2(address, &status2, &valid2);

5. In IonStudio, position control appears to be signed.

Thank you.

User avatar
Basicmicro Support
Posts: 1130
Joined: Thu Feb 26, 2015 9:45 pm

Re: Is encoder count signed or unsigned?

Postby Basicmicro Support » Wed Jun 13, 2018 10:05 am

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.

Posts: 8
Joined: Wed Jun 20, 2018 7:39 am

Re: Is encoder count signed or unsigned?

Postby rwschumann » Wed Jun 20, 2018 8:21 am

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.


User avatar
Basicmicro Support
Posts: 1130
Joined: Thu Feb 26, 2015 9:45 pm

Re: Is encoder count signed or unsigned?

Postby Basicmicro Support » Thu Jun 21, 2018 11:00 am

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).

Return to “Velocity and Position Control”

Who is online

Users browsing this forum: No registered users and 1 guest