Calculating the Delta Degrees Turned From A Gyro

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

Reply to
mark
Loading thread data ...

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.

Reply to
Rich Webb

Rich, Many thanks! Works a treat!

Mark

Rich Webb wrote:

Reply to
mark

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.