-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathangle_to_goal.m
50 lines (44 loc) · 1.39 KB
/
angle_to_goal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function angle = angle_to_goal(current_position_x, current_position_y, goal_position_x, goal_position_y)
y_d = current_position_y - goal_position_y;
x_d = goal_position_x - current_position_x;
if (x_d == 0)
if (y_d >= 0)
angle_east = pi/2;
angle_north = 0;
angle_west = -pi/2;
angle_south = pi;
else
angle_east = -pi/2;
angle_north = pi;
angle_west = pi/2;
angle_south = 0;
end
else
angle = atan(y_d/x_d);
if (y_d >= 0 && x_d > 0)
angle_east = angle;
angle_north = -(pi/2 - angle);
angle_west = -(pi - angle);
angle_south = pi/2 + angle;
elseif (y_d >= 0 && x_d < 0)
angle_east = pi + angle;
angle_north = pi/2 + angle;
angle_west = angle;
angle_south = -(pi/2 - angle);
elseif (y_d <= 0 && x_d < 0)
angle_east = -(pi - angle);
angle_north = pi/2 + angle;
angle_west = angle;
angle_south = -(pi/2 - angle);
elseif (y_d <= 0 && x_d > 0)
angle_east = angle;
angle_north = -(pi/2 - angle);
angle_west = pi + angle;
angle_south = pi/2 + angle;
end
end
angle_east = abs(180/pi*angle_east);
angle_north = abs(180/pi*angle_north);
angle_west = abs(180/pi*angle_west);
angle_south = abs(180/pi*angle_south);
angle = [angle_east, angle_north, angle_west, angle_south];