Calculating the Delta Degrees Turned From A Gyro

Jan 14, 2006 2 Replies

Hi, I'm having trouble working out a neat equation which will determine the change in degrees from a gyro who's output is 0 to 359.



The problem I'm having is that I need to cater for when say the output goes from 0 to 359 or 359 to 0 and ensuring this is only calculated as a



1 deg shift, not 358. I've got an alg to do this but other issues such as ensuring this the following reading are taken say:


2, 358, 359, 1



then the overall degs shift is 1 deg.



Anyone got any code, or idea where I can look to achieve this.



Many thanks



Mark


DeltaBearing = NewBearing - OldBearing

if DeltaBearing > 180 then DeltaBearing = DeltaBearing - 360 else if DeltaBearing < -180 then DeltaBearing = DeltaBearing + 360

or, especially if you're calculating in relative bearing (0 deg = dead ahead) rather than calculating with respect to, say, geographic north then instead of using the range 0 to 360 try using the range -180 to

180. This keeps "0" degrees as dead ahead but behaves "correctly" for offsets to the left. There's still a discontinuity at 180, of course, but if you're moving forwards and most of the objects of interest are also forwards then you can generally get by with doing

if ObjectSensedBearing > 180 then ObjectSensedBearing = ObjectSensedBearing - 360

one time and then getting on with business.

Rich, Many thanks! Works a treat!

Mark

Rich Webb wrote:

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required