-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBUG1_controller.py
186 lines (154 loc) · 7.72 KB
/
BUG1_controller.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# important points:
# use <robot_position> to get current position of robot in <x,y,theta> format.
# use <robot_omega> to get current values for the wheels in <w1,w2,w3> format.
import numpy as np
from initialization import *
import math
def is_on_M_line(x_current, y_current, x0, y0, threshold=0.05):
goal_point_X = 1.3
goal_point_Y = 6.15
if x0 - goal_point_X != 0:
dist = abs(y_current - ((x_current - x0) * (y0 - goal_point_Y) / (x0 - goal_point_X) + y0))
return dist < 1
else:
dist = abs(x_current - goal_point_X)
return dist < threshold
def align_to_M(heading, theta, turn_left=False):
threshold = 5
ts = 3 # turning speed
if turn_left:
ts *= -1 # turn left
if abs(heading - theta%360) < threshold:
return True
else:
update_motor_speed(input_omega=[ts, ts, ts])
return False
def calculate_euclidean_distance(x1, y1, x2, y2):
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
def calculate_theta(x1, y1, x0, y0):
if x1 == x0:
return 180
else:
return (math.degrees(math.atan((y1-y0) / (x1-x0))) % 360)-90
if __name__ == "__main__":
TIME_STEP = 32
robot = init_robot(time_step=TIME_STEP)
init_robot_state(in_pos=[0,0,0],in_omega=[0,0,0])
prev = ""
global goal_postition
goal_postition = 1.3, 6.15
start_position = 1.3, -9.74
state = 'start'
robot_speed = 8
forward_left_speeds = [-1*robot_speed, -1*robot_speed, 2*robot_speed]
far_from_wall_counter = 0
close_to_wall_counter = 0
hit_point = [] # x, y
closest_point = None # x, y
x0, y0 = start_position
is_finding_closest_point = False
while robot.step(TIME_STEP) != -1:
gps_values,compass_val,sonar_value,encoder_value,ir_value = read_sensors_values()
front_ir_values = ir_value[0], ir_value[3]
right_ir_values = ir_value[2], ir_value[5]
left_ir_values = ir_value[1], ir_value[4]
left_sonar = sonar_value[2]
right_sonar = sonar_value[0]
front_sonar = sonar_value[1]
update_robot_state()
# DEFINE STATE MACHINE HERE!
if state == 'start':
distance_threshold = 0.0001
if(is_on_M_line(gps_values[0], gps_values[1], x0, y0)):
prev = state
state = 'align_robot_heading'
elif state == 'align_robot_heading':
print(goal_postition[0], goal_postition[1], x0, y0)
theta = calculate_theta(goal_postition[0], goal_postition[1], x0, y0)
print('THETA:', theta, 'HEADING:', get_bearing_in_degrees(compass_val))
is_aligned = align_to_M(get_bearing_in_degrees(compass_val), theta=theta)
if is_aligned:
prev = state
state = 'move_to_goal'
elif state == 'move_to_goal':
update_motor_speed(input_omega=[-1*robot_speed, robot_speed, 0])
difference = abs(front_ir_values[1] - front_ir_values[0])
if (front_ir_values[0] + front_ir_values[1]) / 2 < 1000:
prev = state
state = 'wall_following'
hit_point.append([gps_values[0], gps_values[1]])
closest_point = [gps_values[0], gps_values[1]]
update_motor_speed(input_omega=[-1*robot_speed, -1*robot_speed, 2*robot_speed])
elif(calculate_euclidean_distance(gps_values[0], gps_values[1], goal_postition[0], goal_postition[1])< 0.5):
state = 'end'
elif state == 'wall_following':
if not is_finding_closest_point and calculate_euclidean_distance(gps_values[0], gps_values[1], goal_postition[0], goal_postition[1]) < calculate_euclidean_distance(closest_point[0], closest_point[1], goal_postition[0], goal_postition[1]):
closest_point = gps_values[0], gps_values[1]
# check if it must turn
difference = front_ir_values[1] - front_ir_values[0]
if calculate_euclidean_distance(gps_values[0], gps_values[1], hit_point[-1][0], hit_point[-1][1]) < distance_threshold:
is_finding_closest_point = True
if difference > 100:
update_motor_speed(input_omega=[robot_speed, robot_speed, 0])
elif difference < -100:
update_motor_speed(input_omega=[-1*robot_speed, -1*robot_speed, 0])
elif front_sonar > 0.8:
far_from_wall_counter += 1
if far_from_wall_counter == 10:
prev = state
state = 'go_close'
far_from_wall_counter = 0
else:
update_motor_speed(input_omega=[-1*robot_speed, -1*robot_speed, 2*robot_speed])
forward_left_speeds = [-1*robot_speed, -1*robot_speed, 2*robot_speed]
elif 100 < difference < 200:
forward_left_speeds[0] -= 1
elif -200 < difference < -100:
forward_left_speeds[1] -= 1
else:
update_motor_speed(input_omega=[-1*robot_speed, -1*robot_speed, 2*robot_speed])
forward_left_speeds = [-1*robot_speed, -1*robot_speed, 2*robot_speed]
if left_sonar < 120 or left_ir_values[0] < 300:
prev = state
state = 'turn'
theta_0 = get_bearing_in_degrees(compass_val)
if front_sonar < 0.3:
close_to_wall_counter += 1
if close_to_wall_counter >= 20:
prev = state
state = 'go_reverse'
if is_finding_closest_point:
x0, y0 = closest_point
dist = calculate_euclidean_distance(gps_values[0], gps_values[1], x0, y0)
if calculate_euclidean_distance(gps_values[0], gps_values[1], x0, y0) < 0.2:
if(is_on_M_line(gps_values[0], gps_values[1], x0, y0)):
state = 'align_robot_heading'
hit_point = []
closest_point = []
is_finding_closest_point = False
elif state == 'go_reverse':
update_motor_speed(input_omega=[robot_speed, -1*robot_speed, 0])
if front_sonar > 0.8:
prev = state
state = 'wall_following'
close_to_wall_counter = 0
elif state == 'go_close':
update_motor_speed(input_omega=[-1*robot_speed, robot_speed, 0])
difference = abs(front_ir_values[1] - front_ir_values[0])
if (front_ir_values[0] + front_ir_values[1]) / 2 < 1000 or front_sonar < 1:
prev = state
state = 'wall_following'
elif state == 'turn':
distance_threshold = 1
is_aligned = align_to_M(get_bearing_in_degrees(compass_val), theta=theta_0+90, turn_left=True)
if is_aligned:
prev = state
state = 'wall_following'
elif state == 'end':
is_aligned = align_to_M(get_bearing_in_degrees(compass_val), theta=90)
if is_aligned:
update_motor_speed(input_omega=[0, 0, 0]) #end
elif(calculate_euclidean_distance(gps_values[0], gps_values[1], goal_postition[0], goal_postition[1])< 0.5):
state = 'end'
update_motor_speed(input_omega=[0, 0, 0])
pass