How to estimate proper heading angle

I obtain the heading or yaw angle from a source which I am not allowed to modify, but when the angle goes more than +180 or -180, it interchanges either from 180 to -180 or the other way around. Any suggestion on how to overcome this drastic angle changes will be appreciated.

Here is the plot of angle changes over time,

Update after @Chuc comment, Here is what I feel like desired behaviour

• +180 and -180 are both valid heading angles. What problem are you having? If it's a large delta between measured angles at subsequent timesteps, then you may want to add a conditional check to any calculation you're doing. E.g. 179 -> -179 is a difference of 2, not 358. – doggie_breath Oct 27 '20 at 13:16
• Welcome to Robotics, GPrathap. It would help if you could explain how this is negatively affecting you and what you would like the alternative to be. If, for example, you want angles to be 0 to 360 instead of -180 to +180 then you'll still wind up with a jump when the angle wraps. If there's a particular (inverse) trig function that's giving you trouble we might be able to recommend an alternative, etc., but as it stands I think the best anyone can say is that you'll always wind up with a jump discontinuity. – Chuck Oct 27 '20 at 13:33
• @Chuck thank you for your reply, I send this yaw angle to control a quadcopter; when angle goes from -180 to 180 quadrotor just rotate. I want to avoid this. How actually approach this? – GPrathap Oct 27 '20 at 20:00
• @GPrathap - What is the problem you are having with using a -180 to +180 reference? What is the alternative you would like to use? You could easily add 360 to any negative values, but then you have a 0-360 heading instead of a -180 to +180 heading; there's still a discontinuity. Are you using a reference that is also +/- 180? It's hard to give you meaningful help without knowing why specifically the +/- 180 feedback is giving you trouble. – Chuck Oct 27 '20 at 20:24
• @Chunk thank you again. I updated assuming that's what I feel like desired yaw angle – GPrathap Oct 27 '20 at 20:45

if I am able to get 0 to 2pi, would be enough as well.

The following code will do it:

modifiedHeading = SensorOutput();