opendroneid / opendroneid-core-c

Open Drone ID Core C Library
Apache License 2.0
170 stars 64 forks source link

Rounding issue in encodeDirection() #79

Closed prateekn9 closed 11 months ago

prateekn9 commented 11 months ago

It seems there is a rounding issue in encodeDirection() function. When the function receives the value from 359.5 to 359.9 it rounds the Direction in degree value to 360 which in turn create an issue by producing the wrong value 180 but the encode value should be between 0-179.

static uint8_t encodeDirection(float Direction, uint8_t *EWDirection) { unsigned int direction_int = (unsigned int) roundf(Direction);

Example: Input Direction in degree : 359.5 unsigned int direction_int = (unsigned int) roundf(Direction); // direction_int =360 if (direction_int < 180) { *EWDirection = 0; } else { *EWDirection = 1; // EWDirection = 1 direction_int -= 180; // direction_int = 180 } return (uint8_t) intRangeMax(direction_int, 0, UINT8_MAX); // return 180 (wrong output)

friissoren commented 11 months ago

Thanks for spotting this. I added a PR to fix this. If @gabrielcox could please double check.