I am working with an acceleration dataset using python 2.7 to find the angle, I am using arctan2 (y, x). My problem is that although my data can rotate outside of pi, the output of arctan2 (y, x) is limited between pi and -pi. This means that whenever I go above pi, I suddenly have a sharp jump in my data and cycle to a negative pi.
I am trying to find an elegant solution to fix this and make my schedule continuous. I performed a simple check where, even if I jump more than 80% of the way, I assume that I went beyond and started adding 2 pi to each subsequent data point. It works, but it feels very awkward. Is there a more elegant way to implement this? Or is this the best I can do?
Thanks ^ _ ^
for index in range(1,(len(x_data))):
new_angle = math.atan2((y_data[index]), (x_data[index]))
if (new_angle - angle[index-1]) > 5:
new_angle = new_angle - 6.28
if (new_angle - angle[index-1]) < -5:
new_angle = new_angle + 6.28
angle.append(new_angle)
Green shows what I want, black shows what I get

source
share