OpenMarine

Full Version: Averaging of angles done incorrectly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I looked at heading_filter function as example.
It applies low pass on angle value. Angle value is a circular parameter. I think it’s invalid way to average
 angles.
Correct way of averaging angles is low pass on sin and cos, the normalization and reverting back to angle with atan2.

It seems everywhere in the code averaging of angles is not right.

Average between 170 and -170 degrees is 180. It’s not 0 or
whatever weighted currently coded in low pass filters. 

Average between 1 and 359 is 0. It’s not 180

This will be possibly the reason people can’t get heading right even
with working IMUs. 

I think it a serious bug. 

I’ve reported it here:  https://github.com/pypilot/pypilot/issues/258

Thanks,
Fair Winds!
it does resolve the headings so that the first one is +- 180 of the second one before. I dont think this is a bug.

I tried all your cases it works as expected. However using vectors requires more trig to be computed.
Actually no issue there. The original code correctly used short path.
My bad and apologies for waisted time.
Its ok I have done this myself. I'm glad you are looking into details trying to find issues.
Gee, brilliant. Spent in some other project hours of coding trying to do it without trigonometric functions, then succumbed to those functions and subsequently running into quadrant ambiguity until I learned about atan2. Now it turns out it is as simple as this. Simplicity is engineer's perfection. Any time learning is certainly not wasted!
(2025-07-09, 09:30 PM)ironman Wrote: [ -> ] Any time learning is certainly not wasted!

Big Grin

Multi tasking too these days. 
With google set on the phone to use Gemini, making coffee this morning -  "OK google, write a radio documentary about atan2"

Couple of minutes of a nice lady talking. 
Ah, the unsung hero of the digital world, works in all 4 quadrants of the cartesian plane.  Cool
Didn't know Gemini would work this way. This accelerates learning even more!
(2025-07-10, 07:48 PM)ironman Wrote: [ -> ]Didn't know Gemini would work this way. This accelerates learning even more!

I use it quite a lot, something to listen to washing the dishes Smile 
"You are a geoligist and radio presented and sailor. Desribe the rock formations and geological history on a sailing trip down the french, spanish and portuguese coasts to Gibraltar."

Works on the laptop as well, at the bottom of the response click the 3 dots "More" there's a listen button.

" Good evening, fellow adventurers of the sea and land! This is your resident geologist, radio presenter, and sometime sailor, here to take you on a truly epic voyage down the Atlantic coasts of France, Spain, and Portugal, culminating at the grand Rock of Gibraltar. Prepare to be amazed by the ancient stories etched in the very rocks beneath our keel....."  Cool