40 from socket
import (AF_INET, SO_BROADCAST, SOCK_DGRAM, SOL_SOCKET, socket,
47 from geometry_msgs.msg
import Pose, Pose2D, Twist
48 from nav_msgs.msg
import Odometry
49 from sensor_msgs.msg
import Image
50 from std_msgs.msg
import Bool, Float64, Int16, String, UInt32
51 from tf.transformations
import euler_from_quaternion, quaternion_from_euler
52 from xiaoqiang_msgs.msg
import Status
55 USER_SOCKET_PORT = 20001
56 USER_SOCKET_REMOTE =
None 57 USER_SERVER_SOCKET =
None 60 PACKAGE_HEADER = [205, 235, 215]
69 STATUS.brightness = 0.0
70 STATUS.image_status =
False 71 STATUS.odom_status =
False 72 STATUS.orb_start_status =
False 73 STATUS.orb_init_status =
False 75 STATUS.orb_scale_status =
False 78 STATUS_LOCK = threading.Lock()
82 SEND_DATA = bytearray([205, 235, 215, 24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
90 SCALE_ORB_THREAD =
None 91 GLOBAL_MOVE_PUB =
None 103 completeData = DATA_CACHE + package_list[0]
104 package_list.remove(package_list[0])
105 package_list =
split_req(completeData) + package_list
107 for count
in range(0, len(package_list)):
108 if len(package_list[count]) != 0
and len(package_list[count]) == package_list[count][0] + 1:
109 res.append(package_list[count][1:])
110 last_one = package_list[-1:][0]
111 if len(last_one) == 0
or len(last_one) != last_one[0] + 1:
112 DATA_CACHE = last_one
119 for count
in range(0, len(req) - 2):
120 if req[count] == PACKAGE_HEADER[0]
and req[count + 1] == PACKAGE_HEADER[1]
and req[count + 2] == PACKAGE_HEADER[2]:
133 res.append(req[start_index: start_index + new_index])
134 start_index = new_index + 3 + start_index
135 res.append(req[start_index:])
142 for count
in range(0, len(cmds)):
143 if len(cmds[count]) > 0:
146 if len(cmds[count]) == 2:
147 global_move_flag = Bool()
148 global_move_flag.data =
True 150 if cmds[count][0] == 0xaa
and cmds[count][1] == 0x44:
151 rospy.loginfo(
"system poweroff")
152 commands.getstatusoutput(
153 '/sbin/shutdown -h now')
155 if cmds[count][0] == ord(
'f'):
156 rospy.loginfo(
"forward")
157 GLOBAL_MOVE_PUB.publish(global_move_flag)
158 SPEED_CMD.linear.x = MAX_VEL*cmds[count][1]/100.0
159 CMD_PUB.publish(SPEED_CMD)
160 elif cmds[count][0] == ord(
'b'):
161 rospy.loginfo(
"back")
162 GLOBAL_MOVE_PUB.publish(global_move_flag)
163 SPEED_CMD.linear.x = -MAX_VEL*cmds[count][1]/100.0
164 CMD_PUB.publish(SPEED_CMD)
165 elif cmds[count][0] == ord(
'c'):
166 rospy.loginfo(
"circleleft")
167 GLOBAL_MOVE_PUB.publish(global_move_flag)
168 SPEED_CMD.angular.z = MAX_THETA*cmds[count][1]/100.0/2.8
169 CMD_PUB.publish(SPEED_CMD)
170 elif cmds[count][0] == ord(
'd'):
171 rospy.loginfo(
"circleright")
172 GLOBAL_MOVE_PUB.publish(global_move_flag)
173 SPEED_CMD.angular.z = -MAX_THETA*cmds[count][1]/100.0/2.8
174 CMD_PUB.publish(SPEED_CMD)
175 elif cmds[count][0] == ord(
's'):
176 rospy.loginfo(
"stop")
177 SPEED_CMD.linear.x = 0
178 SPEED_CMD.angular.z = 0
179 CMD_PUB.publish(SPEED_CMD)
186 global USER_SERVER_SOCKET
189 USER_SERVER_SOCKET = socket(AF_INET, SOCK_DGRAM)
190 USER_SERVER_SOCKET.bind((HOST, USER_SOCKET_PORT))
193 if USER_SERVER_SOCKET !=
None:
194 USER_SERVER_SOCKET.close()
198 return self._stop.isSet()
201 global USER_SOCKET_REMOTE
202 USER_SERVER_SOCKET.settimeout(2)
203 while not self.
stopped()
and not rospy.is_shutdown():
205 data, USER_SOCKET_REMOTE = USER_SERVER_SOCKET.recvfrom(BUFSIZE)
212 dataList.append(ord(c))
218 STATUS_LOCK.acquire()
219 STATUS.power = power.data
220 STATUS_LOCK.release()
224 STATUS_LOCK.acquire()
226 STATUS.image_status =
True 228 STATUS.image_status =
False 229 STATUS_LOCK.release()
234 STATUS_LOCK.acquire()
236 STATUS.odom_status =
True 237 CURRENT_POSE = odom.pose.pose
240 STATUS.odom_status =
False 241 STATUS_LOCK.release()
245 STATUS_LOCK.acquire()
246 if orb_frame !=
None:
247 STATUS.orb_start_status =
True 249 STATUS.orb_start_status =
False 250 STATUS_LOCK.release()
254 STATUS_LOCK.acquire()
255 if camera_pose !=
None:
256 STATUS.orb_init_status =
True 258 STATUS.orb_init_status =
False 259 STATUS_LOCK.release()
263 if not move_enable.data:
265 if not NAV_THREAD.stopped():
270 global CMD_PUB, GLOBAL_MOVE_PUB, MAV_LAST_TIME
271 rospy.init_node(
"broadcast", anonymous=
True)
272 MAV_LAST_TIME = rospy.Time.now()
273 rospy.Subscriber(
"/xiaoqiang_driver/power", Float64, get_power)
274 rospy.Subscriber(
"/usb_cam/image_raw", Image, get_image)
275 rospy.Subscriber(
"/xiaoqiang_driver/odom", Odometry, get_odom)
276 rospy.Subscriber(
"/orb_slam/camera", rospy.msg.AnyMsg, get_orb_tracking_flag)
277 rospy.Subscriber(
"/orb_slam/frame", Image, get_orb_start_status)
278 rospy.Subscriber(
"/xiaoqiang_driver/global_move_flag",
279 Bool, get_global_move_flag)
280 GLOBAL_MOVE_PUB = rospy.Publisher(
"/xiaoqiang_driver/global_move_flag", Bool, queue_size=1)
281 CMD_PUB = rospy.Publisher(
'/xiaoqiang_driver/cmd_vel', Twist, queue_size=0)
284 if __name__ ==
"__main__":
286 rate = rospy.Rate(10)
289 s = socket(AF_INET, SOCK_DGRAM)
291 s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
294 user_server_thread.start()
297 cmd4 =
"aplay /home/xiaoqiang/Desktop/d.wav" 298 while not rospy.is_shutdown():
313 if USER_SOCKET_REMOTE !=
None and USER_SERVER_SOCKET !=
None:
315 SEND_DATA[4:8] = map(ord, struct.pack(
'f', 0.0))
316 SEND_DATA[8:12] = map(ord, struct.pack(
'f', 0.0))
317 SEND_DATA[12:16] = map(ord, struct.pack(
'f', 0.0))
318 SEND_DATA[16:20] = map(ord, struct.pack(
'f', STATUS.power))
319 SEND_DATA[24:28] = map(ord, struct.pack(
'f', 0.0))
320 if STATUS.odom_status:
324 if STATUS.image_status:
328 if STATUS.orb_start_status:
332 if STATUS.orb_init_status:
336 SEND_DATA[20] = statu0 + statu1 + statu2 + statu3
338 USER_SERVER_SOCKET.sendto(bytes(SEND_DATA), USER_SOCKET_REMOTE)
340 print "remote disconnect !\n" 348 s.sendto(data, (
'<broadcast>', MYPORT))
353 STATUS.orb_init_status =
False 354 STATUS.orb_start_status =
False 355 STATUS.image_status =
False 356 STATUS.odom_status =
False 359 user_server_thread.stop()
def find_package_header(req)
def get_global_move_flag(move_enable)
def get_orb_start_status(orb_frame)
def get_orb_tracking_flag(camera_pose)