forked from KoG-8/Turtlebot_RealRiceThief
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_tunnel2.py
417 lines (300 loc) · 12.1 KB
/
in_tunnel2.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python
import rospy
import numpy
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from tester.msg import raw_sensor_8_10
import sys, select, termios, tty
import copy
from math import atan
from math import asin
import math
global Flag
global check
global flag
global flag2
global count
global num
global dot
global past
global pose
global copy_pose
global past_number
global bool_way
bool_way = [0,0]
num=0
count = 0
dot = [0, 0]
past = [[0,0]]
pose = [0, 0]
copy_pose = [0,0]
past_number = [0]
flag =0
flag2 =0
Flag = 0
check = 0
########################################################################################################################################
#### Publish the process values to '/cmd_vel' node ###################################################################################
########################################################################################################################################
pub = rospy.Publisher('/cmd_vel', Twist, queue_size = 5)
rospy.init_node('vel', anonymous=True)
rate = rospy.Rate(10)
twist = Twist()
########################################################################################################################################
########################################################################################################################################
#### Exit node #######################################################################################################################
########################################################################################################################################
def myhook(): # If Ctrl + c occured, the node ends by myhook function and turtlebot's velocity returns to 0.
twist = Twist()
twist.linear.x=0
twist.angular.z=0
pub.publish(twist)
########################################################################################################################################
########################################################################################################################################
#### main #######################################################################################################################
########################################################################################################################################
def callback1(data):
global Flag
global flag2
global past
global count
global pose
global copy_pose
global past_number
global check
global bool_way
rospy.on_shutdown(myhook)
Lidar = data.data
Lidar2 = data.sharp
Lidar3 = data.front # Get the processed values from sensor node
Far = data.Far
Flag = data.Flag
wall = 0.16 # minimum value of distance from wall turtlebot have to aviod
scan_wall2(Lidar2)
remind_past() # Consider present position is visited place or not
if count == 1:
while check < 10000: # If It placed on visited place, turn way back
print('visited place')
destination1(Flag)
check = check+1
check=0
count = 0
if scan_wall(Lidar3) == 1: # If there is wall in front of turtlebot or block way, turn back
rospy.loginfo('strange wall')
while check <= 100:
destination5(3)
check = check+1
check=0
rospy.loginfo('End')
else:
if bool_way[0] == 1: # If there is wall beside the turtlebot
if Lidar[8] <= wall+0.03 and Lidar[8] > 0: # If turtlebot is in uncertain position with blocked wall
store_pose()
remind_past()
if Lidar[9] > Lidar[10]: # Compare the left and right side of distance and turn to long distance
while check<= 100:
destination5(1)
rospy.loginfo('turn_Left')
check = check+1
check = 0
else:
while check <= 100:
destination5(3)
rospy.loginfo('turn_Right')
check = check+1
check = 0
else:
while check <= 50:
destination4(bool_way[1])
check = check+1
check = 0
bool_way[0] = 0 # Refresh the values
bool_way[1] = 0
else:
# If there is no blocks around turtlebot, It goes to exit way using callback4 function
flag2=0 # Refresh the flag number to store the coordinate again
callback4()
rospy.loginfo('check')
print(past)
print(past_number)
print(count)
rospy.loginfo(scan_wall(Lidar3))
rospy.loginfo('######################################################################')
#### main ends ######################################################################################################################
########################################################################################################################################
#### Functions #######################################################################################################################
########################################################################################################################################
def scan_wall(data):
counting = 0
for i in range(0, len(data)):
if data[i] < 0.2 and data[i] > 0: # Find the distances which are less 0.2m and counts the number of data
counting = counting+1
print(counting)
if counting >= 6: # If the count number of values over 6, turtlebot consider that there is wall
return 1 # in front of It and returns 1
else:
return 0
def scan_wall2(data):
global bool_way
temp = 0
number = 0
for i in range(0, len(data)): # Accept the front Lidar values and excepting zero value and select the init value
if data[i] != 0:
temp = data[i]
break
for j in range(i, len(data)):
if data[j] < temp and data[j] !=0: # Compare the values and get the smallest value
temp = data[j]
number = j
#rospy.loginfo([temp, number])
if temp <= 0.20 and number <= 3:
bool_way[0] = 1 # the smallest value means the wall's postion
bool_way[1] = 3 # Return the direction the turtlebot have to go to avoid the wall
#rospy.loginfo('Right')
elif temp <= 0.23 and number > 3:
bool_way[0] = 1
bool_way[1] = 1
#rospy.loginfo('Left')
def store_pose():
global past
global flag2
global pose
global count
global copy_pose
global past_number
global num
if count == 0:
if flag2==0:
copy_pose = copy.copy(pose) # Store the place coordinate which turtlebot must avoid
past.append(copy_pose) # ex) entrance of tunnel
past_number.append(0)
num = num+1
flag2 = 1
def remind_past():
global past
global pose
global count
global past_number
for i in range(0, len(past)):
X = abs(pose[0]-past[i][0]) # Subtract the Stored coordinate and the present coordinate
Y = abs(pose[1]-past[i][1])
if X > 0.1 and Y > 0.1:
#rospy.loginfo('saved') # After the coordinate stored, turtlebot have to get out the stored position
past_number[i] = 1 # so coordinate subtractin over 0.1m makes the past_number 1
if X < 0.1 and Y < 0.1:
#rospy.loginfo('First step') # When turtlebot come back to past coordinate make count 1
if past_number[i] == 1: # Count 1 make the turtlebot turns for optional time
rospy.loginfo([X, Y])
past_number[i] = 0
count = 1
########################################################################################################################################
#### Turtlebot's linear, angular velocity funcation ###################################################################################
########################################################################################################################################
def destination1(data1):
if data1==1:
twist.linear.x = 0.0
twist.angular.z = 0.6
pub.publish(twist)
elif data1==3:
twist.linear.x = 0.0
twist.angular.z = -0.6 #Right (-) Left (+)
pub.publish(twist)
def destination4(data1):
if data1==1:
twist.linear.x = 0.04
twist.angular.z = 0.5
pub.publish(twist)
elif data1==3:
twist.linear.x = 0.04
twist.angular.z = -0.5 #Right (-) Left (+)
pub.publish(twist)
def destination5(data1):
if data1==1:
twist.linear.x = -0.01
twist.angular.z = 0.6
pub.publish(twist)
elif data1==3:
twist.linear.x = -0.01
twist.angular.z = -0.6 #Right (-) Left (+)
pub.publish(twist)
########################################################################################################################################
########################################################################################################################################
#### Converting Global coordinate to Local coordinate #################################################################################
########################################################################################################################################
def callback3(data):
global dot
global flag
global pose
global past
global copy_pose
global init_ori
global init_deg
global angle_goal
global angle_twisted
global ori
global local_pose
global global_pose
local_pose = [0,0]
global_pose = [0,0]
if flag ==0:
dot[0] = data.pose.pose.position.x # Using flag to save the initial coordinate which turtlebot starts just one time
dot[1] = data.pose.pose.position.y
past[0] = dot
copy_pose = copy.copy(dot) # Copy_pose save the coordinate in past_postition to avoid to heading to entrance
flag = 1
pose[0] = data.pose.pose.position.x # Updating the coordinate
pose[1] = data.pose.pose.position.y
if flag==1:
init_deg = math.degrees(math.pi/2-2*asin(data.pose.pose.orientation.z)) # Use flag to save the initial degree from global axis
if init_deg < 0:
init_deg = 360 + init_deg # Convert the degree to 0 ~ 360
init_ori = math.radians(init_deg)
flag=2
global_pose[0] = pose[0] - dot[0] # Parallel translate make initial coordinate to [0,0]
global_pose[1] = pose[1] - dot[1]
ori = math.degrees(math.pi/2-2*asin(data.pose.pose.orientation.z))
if ori < 0:
ori = 360 + ori # Present degree while turtlebot is moving and Convert the degree to 0 ~ 360
ori = ori - init_deg
if ori < 0:
ori = 360 + ori
# Using initial degree from global axis to make the rotation matrix
matrix_angle = [[math.cos(init_ori), -math.sin(init_ori)],[math.sin(init_ori), math.cos(init_ori)]]
# Multiply the rotation matrix to present global coordinate and make local coordinate
local_pose[0] = matrix_angle[0][0]*global_pose[0] + matrix_angle[0][1]*global_pose[1]
local_pose[1] = matrix_angle[1][0]*global_pose[0] + matrix_angle[1][1]*global_pose[1]
# Calculate the degree turtlebot have to turn and head depend on exit's direction
angle_goal = math.degrees(atan((2-local_pose[1])/(2+local_pose[0])))
########################################################################################################################################
########################################################################################################################################
#### Making turtlebot turn for heading to exit coordinate #################################################################################
########################################################################################################################################
def callback4():
rospy.on_shutdown(myhook)
speed = 0.09
#print([local_pose, init_ori, angle_goal, angle_twisted])
if ori < 270 + angle_goal and ori > 90 + angle_goal: # Catch the turtlebot's present heading and judge which why to turn
if abs(ori - 270 - angle_goal) < 6: # If Present degree and desire degree's substraction is lower than 6
twist.linear.x = speed
twist.angular.z = -0.0
pub.publish(twist)
else:
twist.linear.x = speed
twist.angular.z = -0.3
pub.publish(twist)
else:
if abs(ori - 270 - angle_goal) < 6:
twist.linear.x = speed
twist.angular.z = -0.0
pub.publish(twist)
else:
twist.linear.x = speed
twist.angular.z = 0.3
pub.publish(twist)
def listen():
rospy.Subscriber('/odom', Odometry, callback3)
rospy.Subscriber('/raw_sensor', raw_sensor_8_10, callback1)
rospy.spin()
if __name__ == '__main__':
#rospy.Subscriber('sonar_dist_pub')
listen()