calculating angles

hey there! i wanna calculate the angles that my robot moves with by giving the positions x,y,z i have the expression to do that but i have troubles programming them, i don't know how to use the math fonction atan2(x,y) and how should i give the position ( degres or radian )

for exemple i have int x = 10; int y = 10; i should had alpha = 45 degre or pi/4 radian how do i code it ?? thanks for the help

You can find the signature for that function and the angle measurement units here. Note that on an 8-bit AVR, 'double' and 'float' are the same type.

atan2 returns the result in radians and you must convert it to degrees by multiplying with 180/Pi. I would create some constants for the conversion between radians and degrees:

const float DEG2RAD = PI / 180.0f;
const float RAD2DEG = 180.0f / PI;

int x = 10, y = 10;
float angle = atan2(y, x) * RAD2DEG;

Serial.print("Angle=");
Serial.println(angle);

You should start to consider your robot as the center of your coordinate system (0,0), and every "heading" as being the (un-normalized) movement vector. This will help you find the answers you may need.

Given the height (y) and base (x) of a right-angled triangle, the function to calculate the angle (in radian) bounded by the hypotenuse and the base is:
radangle.png

float angleRadian = atan2(y, x);

The variables y and x are 'floating point numbers' -- numbers with integer part and fractional part. Hence, the formal declarations of the variables are:

float x = 10.0;     //fractional part is 0
float y = 10.0;
float angleRadian;
angleRadian = atan2(y, x);
float angleDegree = angleRadian*180/3.14159265;

radangle.png

1 Like