I am having a problem with the atan2 function and unwrap.
My Simulink model contains a feedback control system for a vehicle which is required to make turns.
When the required turning angle exceeds +/- pi radians (e.g. 3*pi/2 radians or three quarters of a full revolution in either direction) I am getting constant oscillation between the limits of +/- pi instead of a step.
I tried to unwrap this oscillating data coming out of my atan2 function, so that the output would extend beyond these limits, but it simply unwraps to an extremely large number.
Please advise.
Roger Stafford
Thank you very much for providing me with a better understanding of the problem. I shall go back to the drawing board.
The ANGLE function is discontinuous in the neighbourhood of the negative real axis. This means that any numerical noise in the input X to angle(X) will can cause the values of X to jitter back and forth across the real axis with corresponding discontinuous jumps in angle(X) between pi and -pi.
Bottom line. You must insert code to detect when X gets close, within some tolerance, to the negative real axis and manually round it to a desired value (pi or -pi).
Sorry, you were talking about ATAN2 as opposed to ANGLE. But the same discontinuity is present in ATAN2, so the same remarks apply:
>> tinyError = 1e-20;
>> atan2(0,-1)
ans =
3.1416
>> atan2(0 + tinyError,-1)
ans =
3.1416
>> atan2( 0 - tinyError,-1)
ans =
-3.1416