-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_drone_tracker.py
executable file
·244 lines (199 loc) · 9.21 KB
/
led_drone_tracker.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
#!/usr/bin/env python
"""
object_tracker.py - Version 1.1 2013-12-20
Rotate the robot left or right to follow a target published on the /roi topic.
Created for the Pi Robot Project: http://www.pirobot.org
Copyright (c) 2012 Patrick Goebel. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/licenses/gpl.html
"""
import time
import rospy
from sensor_msgs.msg import RegionOfInterest, CameraInfo
from geometry_msgs.msg import Twist
import thread
class ObjectTracker():
def __init__(self):
rospy.init_node("object_tracker")
self.pre_time = 0.0
self.now_time = 0.0
self.time = 0.0
self.delaytime = 0.0
self.locktime = 0.0
# Set the shutdown function (stop the robot)
rospy.on_shutdown(self.shutdown)
# How often should we update the robot's motion?
self.rate = rospy.get_param("~rate", 10)
r = rospy.Rate(self.rate)
# The maximum rotation speed in radians per second
self.max_rotation_speed = rospy.get_param("~max_rotation_speed", 2.0)
self.max_rotation_speed_r = rospy.get_param("~max_rotation_speed_r", 2.0)
self.max_vertical_speed = rospy.get_param("~max_vertical_speed", 2.0)
self.max_dis_speed = rospy.get_param("~max_dis_speed", 2.0)
# The minimum rotation speed in radians per second
self.min_rotation_speed = rospy.get_param("~min_rotation_speed", 0.5)
self.min_rotation_speed_r = rospy.get_param("~min_rotation_speed_r", 0.5)
self.min_vertical_speed = rospy.get_param("~min_vertical_speed", 0.5)
self.min_dis_speed = rospy.get_param("~min_dis_speed", 0.5)
# Sensitivity to target displacements. Setting this too high
# can lead to oscillations of the robot.
self.gain = rospy.get_param("~gain", 2.0)
self.gain_y = rospy.get_param("~gain_y", 2.0)
self.gain_z = rospy.get_param("~gain_z", 2.0)
# The x threshold (% of image width) indicates how far off-center
# the ROI needs to be in the x-direction before we react
self.x_threshold = rospy.get_param("~x_threshold", 0.1)
self.y_threshold = rospy.get_param("~y_threshold", 0.1)
self.z_threshold = rospy.get_param("~z_threshold", 0.1)
# Publisher to control the robot's movement
self.cmd_vel_pub = rospy.Publisher('cmd_vel', Twist, queue_size=5)
# Intialize the movement command
self.move_cmd = Twist()
# Get a lock for updating the self.move_cmd values
self.lock = thread.allocate_lock()
# We will get the image width and height from the camera_info topic
self.image_width = 0
self.image_height = 0
# Set flag to indicate when the ROI stops updating
self.target_visible = False
# Wait for the camera_info topic to become available
rospy.loginfo("Waiting for camera_info topic...")
rospy.wait_for_message('camera_info', CameraInfo)
# Subscribe the camera_info topic to get the image width and height
rospy.Subscriber('camera_info', CameraInfo, self.get_camera_info, queue_size=1)
# Wait until we actually have the camera data
while self.image_width == 0 or self.image_height == 0:
rospy.sleep(1)
# Subscribe to the ROI topic and set the callback to update the robot's motion
rospy.Subscriber('roi', RegionOfInterest, self.set_cmd_vel, queue_size=1)
# Wait until we have an ROI to follow
rospy.loginfo("Waiting for messages on /roi...")
rospy.wait_for_message('roi', RegionOfInterest)
rospy.loginfo("ROI messages detected. Starting tracker...")
# Begin the tracking loop
while not rospy.is_shutdown():
# Acquire a lock while we're setting the robot speeds
self.lock.acquire()
try:
# If the target is not visible, stop the robot
if not self.target_visible:
self.move_cmd = Twist()
else:
# Reset the flag to False by default
self.target_visible = False
# Send the Twist command to the robot
self.cmd_vel_pub.publish(self.move_cmd)
finally:
# Release the lock
self.lock.release()
# Sleep for 1/self.rate seconds
r.sleep()
def set_cmd_vel(self, msg):
# Acquire a lock while we're setting the robot speeds
self.lock.acquire()
try:
# If the ROI has a width or height of 0, we have lost the target
if msg.width == 0 or msg.height == 0:
self.target_visible = False
return
# If the ROI stops updating this next statement will not happen
self.target_visible = True
# Compute the displacement of the ROI from the center of the image
target_offset_x = msg.x_offset + msg.width / 2 - self.image_width / 2
target_offset_y = msg.y_offset + msg.height / 2 - self.image_height / 2
target_offset_z = msg.width - 60
try:
percent_offset_x = float(target_offset_x) / (float(self.image_width) / 2.0)
percent_offset_y = float(target_offset_y) / (float(self.image_height) / 2.0)
percent_offset_z = float(target_offset_z) / (float(100))
except:
percent_offset_x = 0
percent_offset_y = 0
percent_offset_z = 0
### x
# Rotate the robot only if the displacement of the target exceeds the threshold
if abs(percent_offset_x) > self.x_threshold:
# Set the rotation speed proportional to the displacement of the target
try:
speed = self.gain * percent_offset_x
if speed < 0:
direction = -1 #'''# fix motor problem #'''#1.5
else:
direction = 1.5 #1
self.move_cmd.linear.y = -direction * max(self.min_rotation_speed,
min(self.max_rotation_speed, abs(speed)))
except:
self.move_cmd = Twist()
else:
# Otherwise stop the robot
#self.move_cmd = Twist()
self.move_cmd.linear.y = 0
#rospy.loginfo(self.move_cmd.angular.z)
### y
if abs(percent_offset_y) > self.y_threshold:
# Set the rotation speed proportional to the displacement of the target
try:
speed = self.gain_y * percent_offset_y
if speed < 0:
direction = -1
else:
direction = 1
self.move_cmd.linear.z = -direction * max(self.min_vertical_speed,
min(self.max_vertical_speed, abs(speed)))
except:
self.move_cmd = Twist()
else:
# Otherwise stop the robot
self.move_cmd.linear.z = 0
#rospy.loginfo(self.move_cmd.linear.z)
### z
if abs(percent_offset_z) > self.z_threshold:
# Set the rotation speed proportional to the displacement of the target
try:
speed = self.gain_z * percent_offset_z
if speed < 0:
direction = -1
else:
direction = 1
'''self.move_cmd.linear.x = -direction * max(self.min_dis_speed,
min(self.max_dis_speed, abs(speed)))'''
except:
self.move_cmd = Twist()
else:
# Otherwise stop the robot
self.move_cmd.linear.x = 0
#rospy.loginfo(self.move_cmd.linear.x)
###
finally:
self.now_time = time.time()
self.time = self.now_time - self.pre_time
if self.time >= 1.0:
self.move_cmd.angular.z = 0
self.move_cmd.linear.z = 0
self.move_cmd.linear.x = 0
rospy.loginfo("stop!")
if self.time >= 1.3:
rospy.loginfo("release!")
self.pre_time = self.now_time
# Release the lock
self.lock.release()
def get_camera_info(self, msg):
self.image_width = msg.width
self.image_height = msg.height
def shutdown(self):
rospy.loginfo("Stopping the robot...")
self.cmd_vel_pub.publish(Twist())
rospy.sleep(1)
if __name__ == '__main__':
try:
ObjectTracker()
rospy.spin()
except rospy.ROSInterruptException:
rospy.loginfo("Object tracking node terminated.")