-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathorb_track.py
executable file
·268 lines (218 loc) · 8.16 KB
/
orb_track.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
#!/usr/bin/env python
# encoding=utf-8
# The MIT License (MIT)
#
# Copyright (c) 2018 Bluewhale Robot
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author: Randoms, Xiefusheng
#
'''
使orb获得初始追踪,发布kinect角度,监听系统状态
'''
import threading
import time
import rospy
from geometry_msgs.msg import Pose, Twist
from nav_msgs.msg import Odometry
from std_msgs.msg import Bool, Int16, Int32
from system_monitor.msg import Status
from galileo_serial_server.msg import GalileoNativeCmds, GalileoStatus
ORB_TRACK_THREAD = None
ORB_INIT_FLAG = False
ORB_START_FLAG = False
STATUS_LOCK = threading.Lock()
VEL_PUB = None
GLOBAL_MOVE_PUB = None
CAMERA_UPDATE_FLAG = False
CAR_UPDATE_FLAG = False
CAMERA_CURRENT_TIME = None
CAR_CURRENT_TIME = None
BAR_FLAG = False
NAV_FLAG_PUB = None
ENABLE_MOVE_FLAG = True
WORKING_FLAG = False
time2_diff_value = 0.0
ORB_INIT_FLAG2 = False
class TrackTask(threading.Thread):
def __init__(self):
super(TrackTask, self).__init__()
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
global VEL_PUB, ORB_TRACK_THREAD
if VEL_PUB == "":
# driver is not ready
ORB_TRACK_THREAD = None
return
rospy.loginfo("start get track")
car_twist = Twist()
# rotate small angels
car_twist.linear.x = 0.0
car_twist.linear.y = 0.0
car_twist.linear.z = 0.0
car_twist.angular.x = 0.0
car_twist.angular.y = 0.0
car_twist.angular.z = 0.3
VEL_PUB.publish(car_twist)
self.wait(3)
car_twist.angular.z = -0.3
VEL_PUB.publish(car_twist)
self.wait(8)
car_twist.angular.z = 0.
VEL_PUB.publish(car_twist)
ORB_TRACK_THREAD = None
rospy.loginfo("stop get track")
def wait(self, wait_time):
sleep_time = 0
while sleep_time < wait_time and not self.stopped():
time.sleep(0.05)
sleep_time += 0.05
def system_status_handler(system_status):
global ORB_INIT_FLAG, STATUS_LOCK, ORB_START_FLAG
with STATUS_LOCK:
ORB_INIT_FLAG = system_status.orbInitStatus
ORB_START_FLAG = system_status.orbStartStatus
def car_odom(odom):
global CAR_CURRENT_TIME, STATUS_LOCK, CAR_UPDATE_FLAG
with STATUS_LOCK:
CAR_CURRENT_TIME = rospy.Time.now()
CAR_UPDATE_FLAG = True
def camera_odom(campose):
global CAMERA_CURRENT_TIME, STATUS_LOCK, CAMERA_UPDATE_FLAG,ORB_INIT_FLAG2
with STATUS_LOCK:
CAMERA_CURRENT_TIME = rospy.Time.now()
CAMERA_UPDATE_FLAG = True
ORB_INIT_FLAG2 = True
def do_security():
global STATUS_LOCK, CAR_UPDATE_FLAG, CAMERA_UPDATE_FLAG
global CAMERA_CURRENT_TIME, CAR_CURRENT_TIME
global GLOBAL_MOVE_PUB, VEL_PUB
global NAV_FLAG_PUB, ENABLE_MOVE_FLAG, BAR_FLAG
global time2_diff_value,ORB_INIT_FLAG2
time_now = rospy.Time.now()
STATUS_LOCK.acquire()
time1_diff = time_now - time_now
if not CAR_UPDATE_FLAG :
time1_diff = time_now - CAR_CURRENT_TIME
if not CAMERA_UPDATE_FLAG:
time2_diff = time_now - CAMERA_CURRENT_TIME
if time2_diff.to_sec()>1.0 :
time2_diff_value = time2_diff_value + time2_diff.to_sec()
CAMERA_CURRENT_TIME = time_now
else:
time2_diff_value = 0.0
CAMERA_UPDATE_FLAG = False
CAR_UPDATE_FLAG = False
# 视觉丢失超时保险
# 里程计丢失超时保险
if ORB_INIT_FLAG2 and ( time2_diff_value > 120 or time1_diff.to_sec() > 120.) and not BAR_FLAG and WORKING_FLAG:
# 发布全局禁止flag
global_move_flag = Bool()
global_move_flag.data = False
GLOBAL_MOVE_PUB.publish(global_move_flag)
car_twist = Twist()
VEL_PUB.publish(car_twist)
ORB_INIT_FLAG2 = False
time2_diff_value = 0.0
CAMERA_CURRENT_TIME = rospy.Time.now()
CAR_CURRENT_TIME = rospy.Time.now()
rospy.logwarn("oups3 %d %d %f %f",BAR_FLAG,WORKING_FLAG,time2_diff.to_sec(),time1_diff.to_sec())
STATUS_LOCK.release()
def deal_car_status(car_status):
global BAR_FLAG, STATUS_LOCK,CAMERA_CURRENT_TIME
with STATUS_LOCK:
status = car_status.data
if status == 2:
BAR_FLAG = True
CAMERA_CURRENT_TIME = rospy.Time.now()
else:
BAR_FLAG = False
def deal_car_status(car_twist_msg):
global BAR_FLAG, STATUS_LOCK,CAMERA_CURRENT_TIME
with STATUS_LOCK:
vx_temp = car_twist_msg.linear.x
theta_temp = car_twist_msg.angular.z
if abs(vx_temp) < 0.05 and abs(theta_temp) <0.05:
BAR_FLAG = True
CAMERA_CURRENT_TIME = rospy.Time.now()
else:
BAR_FLAG = False
#rospy.logwarn("robot stopped")
def deal_galileo_status(galileo_msg):
global WORKING_FLAG, STATUS_LOCK,CAR_CURRENT_TIME,CAMERA_CURRENT_TIME
with STATUS_LOCK:
if galileo_msg.targetStatus == 1:
WORKING_FLAG = True
else:
WORKING_FLAG = False
CAR_CURRENT_TIME = rospy.Time.now()
CAMERA_CURRENT_TIME = rospy.Time.now()
def init():
global ORB_INIT_FLAG, ORB_START_FLAG
global VEL_PUB, ORB_TRACK_THREAD, GLOBAL_MOVE_PUB
global CAMERA_CURRENT_TIME, CAR_CURRENT_TIME
global NAV_FLAG_PUB, ENABLE_MOVE_FLAG
rospy.init_node("orb_track", anonymous=True)
CAMERA_CURRENT_TIME = rospy.Time.now()
CAR_CURRENT_TIME = rospy.Time.now()
ENABLE_MOVE_FLAG = rospy.get_param("~enableMoveFlag", True)
rospy.Subscriber("/system_monitor/report", Status, system_status_handler)
rospy.Subscriber("/ORB_SLAM/Camera", Pose, camera_odom)
rospy.Subscriber("/xqserial_server/Odom", Odometry, car_odom)
#rospy.Subscriber("/xqserial_server/StatusFlag", Int32, deal_car_status)
rospy.Subscriber("/xqserial_server/Twist", Twist, deal_car_status)
rospy.Subscriber("/galileo/status", GalileoStatus, deal_galileo_status)
GLOBAL_MOVE_PUB = rospy.Publisher('/global_move_flag', Bool, queue_size=1)
NAV_FLAG_PUB = rospy.Publisher('/nav_setStop', Bool, queue_size=0)
VEL_PUB = rospy.Publisher('/cmd_vel', Twist, queue_size=5)
# 确保orb 已经启动
while not ORB_START_FLAG and not rospy.is_shutdown():
rospy.logwarn("ORB_SLAM2 not started")
time.sleep(1)
# 确保orb 已经 track
time.sleep(4)
rospy.logwarn("ORB_SLAM2 not track")
globalMoveFlag = Bool()
globalMoveFlag.data = True
GLOBAL_MOVE_PUB.publish(globalMoveFlag)
while not ORB_INIT_FLAG and not rospy.is_shutdown() and ENABLE_MOVE_FLAG:
if ORB_TRACK_THREAD == None:
ORB_TRACK_THREAD = TrackTask()
ORB_TRACK_THREAD.start()
time.sleep(6)
time.sleep(1)
if ORB_TRACK_THREAD != None:
ORB_TRACK_THREAD.stop()
if __name__ == "__main__":
init()
rate = rospy.Rate(10)
tilt_pub = rospy.Publisher('/set_tilt_degree', Int16, queue_size=1)
while not rospy.is_shutdown():
do_security()
tilt_degree = Int16()
tilt_degree.data = -19
tilt_pub.publish(tilt_degree)
rate.sleep()
if ORB_TRACK_THREAD != None:
ORB_TRACK_THREAD.stop()