2

I have a robot with red led and green led mounted at the front and back respectively. I want to calculate the head direction of the robot as in which direction is the greenLEd - redLed vector pointed to.

How can I code it such that the points marked 1 and 2 in the image below have the same angle i.e. 45degree anti-clockwise whereas point 3 should be at 225degrees.

enter image description here

I used the following script but it giving me wrong results:

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])

How should I proceed? Thanks for the help.

4

1 回答 1

2

回到基础,没有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

于 2017-05-03T14:55:54.470 回答