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.

deathracer
  • 305
  • 1
  • 20

1 Answers1

2

Back to basics, without numpy.

atan2 already gives you an anticlockwise angle, but between -180 and 180. You can add 360 and calculate modulo 360 to get an angle between 0 and 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 should just be the difference in X coordinates between the green and red LEDs. Same goes for y.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124