Skip to content

Two's complement for burst motion values  #3

@camchaney

Description

@camchaney

Hello, I am just having trouble converting the dx and dy values from readBurst to usable signed integers and wondering if I could get some assistance on that. I was using the code from mrjohnk to do two's convolution on the DX_L and DY_L registers readings, but it doesn't seem to work on the raw dx and dy values from your code. It seems your code is returning a 16 bit number instead of an 8 bit..? Not sure on how to translate that. I just want to be able to use decimal integer values for motion tracking in a robotics project. Thanks!
Here is the code that I have been using:

int convTwosComp(int b){
  //Convert from 2's complement
  if(b & 0x80){                     // 0x80 (hex) = 10000000 (bin) = 128 (dec)
    b = -1 * ((b ^ 0xff) + 1);      // 0xff (hex) = 11111111 (bin) = 255 (dec)
  }
  return b;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions