-
Notifications
You must be signed in to change notification settings - Fork 0
/
locomotion.py
254 lines (200 loc) · 5.81 KB
/
locomotion.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
from image import *
import rospy
from geometry_msgs.msg import Twist
from kobuki_msgs.msg import BumperEvent
from std_msgs.msg import Float64
##Global Variables Declaration
g=0
y=0
z=0
q=0
bump = False
bumpRight = False
leftTurn= False
uturnflag=False
image=1
cmd_vel = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10)
file=open("datalogging.txt",'a+')
##Function block for obstacle identification
def processBump(data):
print("bump")
global bump
bump = False
if data.state==BumperEvent.PRESSED and bumpRight:
uturn(325)
elif data.state==BumperEvent.PRESSED and not bumpRight:
print("stuck")
bump = True
else:
bump = False
###############################################
#####Moves to opposite side of wall when endpoint reached
def uturn(throw):
global move_cmd
global uturnflag
uturnflag=True
move_cmd= Twist()
degree=throw
global image
global file
t=0
while t<10:
move_cmd.linear.x = -0.4
move_cmd.angular.z =0
file.write("linear=-0.4, angular=0\n")
cmd_vel.publish(move_cmd)
r = rospy.Rate(20)
t+=1
r.sleep()
rospy.loginfo(t)
camera = TakePhoto()
img_title = rospy.get_param('~image_title', 'obstacle_%s.jpg'%image)
image+=image
camera.take_picture(img_title)
t=0
while degree>0:
move_cmd.linear.x = 0
move_cmd.angular.z =-0.2
file.write("linear=0, angular=-0.2\n")
rospy.loginfo(degree)
left_move_little()
def left_move_little():
global leftTurn
global uturnflag
global file
if not leftTurn:
leftTurn = True
s=0
t=0
while t<50:
move_cmd.linear.x = 0.3
move_cmd.angular.z =0
file.write("linear=0.3, angular=0\n")
cmd_vel.publish(move_cmd)
r = rospy.Rate(20)
t+=1
r.sleep()
rospy.loginfo(t)
t=0
while t<150:
move_cmd.linear.x = 0
move_cmd.angular.z =-0.2
file.write("linear=0, angular=-0.2\n")
cmd_vel.publish(move_cmd)
r = rospy.Rate(20)
t+=1
r.sleep()
t=0
uturnflag=False
leftTurn = False
#########################################################
## Avoids the obstacles when encountered
def turn(throw):
print("exe")
global q,g
global move_cmd
move_cmd= Twist()
degree=throw
g=throw
global image
global file
while q<10:
move_cmd.linear.x = -0.4
move_cmd.angular.z =0
file.write("linear=-0.4, angular=0\n")
cmd_vel.publish(move_cmd)
r = rospy.Rate(20)
q+=1
r.sleep()
rospy.loginfo(q)
camera = TakePhoto()
img_title = rospy.get_param('~image_title', 'obstacle_%s.jpg'%image)
image+=image
camera.take_picture(img_title)
q=0
while degree>0:
move_cmd.linear.x = 0
move_cmd.angular.z =-0.2
file.write("linear=0, angular=-0.2\n")
cmd_vel.publish(move_cmd)
r = rospy.Rate(20)
degree-=1
r.sleep()
rospy.loginfo(degree)
move_little()
def move_little():
global y
global move_cmd
global bumpRight
global file
move_cmd= Twist()
if not bumpRight:
while y<25 and not uturnflag:
bumpRight = True
move_cmd.linear.x = 0.3
move_cmd.angular.z =0
file.write("linear=0.3, angular=0\n")
cmd_vel.publish(move_cmd)
r = rospy.Rate(20)
y+=1
r.sleep()
rospy.loginfo(y)
y=0
bumpRight = False
turn_back()
def turn_back():
global g
global move_cmd
move_cmd= Twist()
global file
if not bumpRight:
while g>0 and not uturnflag:
move_cmd.linear.x = 0
move_cmd.angular.z =0.2
file.write("linear=0.2, angular=0.2\n")
cmd_vel.publish(move_cmd)
r = rospy.Rate(20)
g-=1
r.sleep()
rospy.loginfo(g)
# Shutdown function
def shutdown():
# stop turtlebot
rospy.loginfo("Stop TurtleBot")
# a default Twist has linear.x of 0 and angular.z of 0. So it'll stop TurtleBot
file.close()
cmd_vel.publish(Twist())
# Main Function - Turtlebot moves
def listener():
rospy.init_node('laserscan', anonymous=True)
print("listen")
global cmd_vel
global file
sub=rospy.Subscriber('mobile_base/events/bumper', BumperEvent, processBump)
# spin() simply keeps python from exiting until this node is stopped
rospy.on_shutdown(shutdown)
print("run")
move_cmd= Twist()
move_cmd.linear.x = 0.3
move_cmd.angular.z =0
while not rospy.is_shutdown():
if not uturnflag:
global bump
global bumpRight
if bumpRight:
bumpRight=False
if bump==True:
print("turn")
turn(150)
print("running")
file.write("linear=0.3, angular=0\n")
cmd_vel.publish(move_cmd)
r = rospy.Rate(20)
r.sleep()
if __name__ == '__main__':
try:
print("start")
listener()
except Exception as e:
print(e)
rospy.loginfo("GoForward node terminated.")