2017-05-03 122 views
2

我有一个机器人,分别在前部和后部安装红色led和绿色led。我想计算机器人的头部方向,因为指向greenLEd - redLed矢量的方向是哪个方向。计算2点之间的反时针角度

我该如何编码,以使下图中标记为1和2的点具有相同的角度,即45度逆时针,而点3应该在225度。

enter image description here

我用下面的脚本,但它给了我错误的结果:

def headDirectionAngle(redLEDCoords, greenLEDCoords, referenceVector): 
    greenRedLEDVector = np.array(greenLEDCoords) - np.array(redLEDCoords) 
    angle = np.math.atan2(np.linalg.det([referenceVector,greenRedLEDVector]),np.dot(referenceVector,greenRedLEDVector)) 
    return np.degrees(angle) 
referenceVector = np.array([0,240]) 

我应该如何进行?谢谢您的帮助。

+0

这个矢量的目标是什么? –

回答

1

回到基础知识,没有numpy

atan2已经为您提供了一个逆时针的角度,但-180和180之间可以添加360和计算模360获得0和360之间的角度:

from math import atan2, degrees 

def anti_clockwise(x,y): 
    alpha = degrees(atan2(y,x)) 
    return (alpha + 360) % 360 

print(anti_clockwise(480, 480)) 
# 45.0 
print(anti_clockwise(-480, -480)) 
# 225.0 

x应该只是在差绿色和红色LED之间的X坐标。 y也一样。

+0

atan2给你的角度与水平轴。有什么办法可以更新它来计算矢量的角度? – deathracer

+0

@deathracer:您可以计算两个向量的水平轴的角度,并计算差异。 –