-
Notifications
You must be signed in to change notification settings - Fork 4
/
FindDirectionality.py
149 lines (117 loc) · 4.88 KB
/
FindDirectionality.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
the_code.py
A code which will solve a scenario where a resource is kept at a particular angle,
and the bot has to go and fetch it.
THIS CODE COMES WITH ABSOLUTELY NO WARRANTY. DO WHATEVER YOU WANT TO DO WITH IT,
AS LONG AS YOU HAVE GOT BACKUP.
"""
from math import ceil
class Direction(object):
"""
Constant values for direction of movement.
"""
FORWARD = 'FORWARD'
BACKWARD = 'BACKWARD'
LEFT = 'LEFT'
RIGHT = 'RIGHT'
FORWARD_LEFT = 'FORWARD_LEFT' # 135 deg
FORWARD_RIGHT = 'FORWARD_RIGHT' # 45 deg
BACKWARD_LEFT = 'BACKWARD_LEFT' # 225 deg
BACKWARD_RIGHT = 'BACKWARD_RIGHT' # 315 deg
command = {FORWARD:'f',BACKWARD:'b',LEFT:'left',RIGHT:'right',BACKWARD_LEFT:'lb',BACKWARD_RIGHT:'rb',FORWARD_LEFT:'lfd',FORWARD_RIGHT:'rfd'}
class Orientation(object):
SPOT_RIGHT = 'SPOT_RIGHT'
SPOT_LEFT = 'SPOT_LEFT'
ARC_RIGHT = 'ARC_RIGHT'
ARC_LEFT = 'ARC_LEFT'
command = {SPOT_RIGHT:'c', SPOT_LEFT:'a', ARC_RIGHT:'r', ARC_LEFT :'l'}
class MovementFunctions():
@staticmethod
def get_sector(angle_of_resource):
"""
Calculate the sector in which resource is lying,
Assuming that there are 8 sectors in circle, each with 45 degree separation.
:param angle_of_resource: Angle in degrees where the resource is lying.
:return: sector(int)
"""
# To make sure angle_of_resource is within (0, 360)
angle_of_resource %= 360
sector = ceil(angle_of_resource / 45.0)
return sector
@staticmethod
def get_orientation(angle_of_resource):
"""
Calculate angle to turn and where to turn(clockwise or anti-clockwise).
Returns a tuple of angle in degrees and Turn direction(clock or anti clock)
:param angle_of_resource:
:return: (Angle, orientation)
"""
sector = MovementFunctions.get_sector(angle_of_resource)
# Calculate whether to turn clockwise or anti clock wise.
min_angle_of_sector = (sector - 1) * 45
max_angle_of_sector = sector * 45
# print 'min max', min_angle_of_sector, max_angle_of_sector
mid_angle = (max_angle_of_sector + min_angle_of_sector) / float(2)
if angle_of_resource < mid_angle:
orientation = Orientation.SPOT_LEFT
degree_to_turn = angle_of_resource - min_angle_of_sector
else:
orientation = Orientation.SPOT_RIGHT
degree_to_turn = max_angle_of_sector - angle_of_resource
# print 'orientation', degree_to_turn
return degree_to_turn, orientation
@staticmethod
def get_direction(angle_of_resource):
"""
Determine in which direction the bot has to move in order to grab the resource
:param angle_of_resource: angle at which the resource is located(in degrees)
:return: tuple(angle, orientation, direction)
"""
sector = MovementFunctions.get_sector(angle_of_resource)
(angle, orientation) = MovementFunctions.get_orientation(angle_of_resource)
if sector == 1:
if orientation == Orientation.SPOT_LEFT:
direction = Direction.FORWARD_RIGHT
else:
direction = Direction.RIGHT
elif sector == 2:
if orientation == Orientation.SPOT_LEFT:
direction = Direction.FORWARD
else:
direction = Direction.FORWARD_RIGHT
elif sector == 3:
if orientation == Orientation.SPOT_LEFT:
direction = Direction.FORWARD_LEFT
else:
direction = Direction.FORWARD
elif sector == 4:
if orientation == Orientation.SPOT_LEFT:
direction = Direction.LEFT
else:
direction = Direction.FORWARD_LEFT
elif sector == 5:
if orientation == Orientation.SPOT_LEFT:
direction = Direction.BACKWARD_LEFT
else:
direction = Direction.LEFT
elif sector == 6:
if orientation == Orientation.SPOT_LEFT:
direction = Direction.BACKWARD
else:
direction = Direction.BACKWARD_LEFT
elif sector == 7:
if orientation == Orientation.SPOT_LEFT:
direction = Direction.BACKWARD_RIGHT
else:
direction = Direction.BACKWARD
else :
if orientation == Orientation.SPOT_LEFT:
direction = Direction.RIGHT
else:
direction = Direction.BACKWARD_RIGHT
msg = "Resource is at {resource} degree. Turn the bot by {angle} degree {motion}, and move {direction}"
msg = msg.format(resource = angle_of_resource, angle=angle, motion=orientation, direction=direction)
print(msg)
return angle, orientation, direction
if __name__ == '__main__':
print MovementFunctions.get_direction(-230)