22
33from mission_wrapper import waypointWrap
44from quad_commander import *
5+ from geo_math import len_diff_coord
56
6- from rospy import init_node , is_shutdown , spin , sleep , logerr , loginfo
7+ from rospy import init_node , is_shutdown , spin , sleep , logerr , loginfo , logwarn
78from rospy import Subscriber , Publisher
89from small_atc_msgs .msg import *
910from sensor_msgs .msg import NavSatFix
10- from std_msgs .msg import Bool , UInt32
11+ from std_msgs .msg import Bool , UInt32 , Int64
12+ from mavros_msgs .msg import WaypointList
1113from threading import Thread
12- from Queue import Queue
14+ from Queue import Queue , Empty
1315
1416def drop_all (queue ):
1517 while not queue .empty ():
@@ -19,36 +21,77 @@ def take_current(queue):
1921 drop_all (queue )
2022 return queue .get (True , None )
2123
22- def quad_controller (route_queue , position_queue , arming_queue ):
23- flight_done_pub = Publisher ('remove' , UInt32 , queue_size = 1 )
24+ def find_waypoint_id (route , target_pos ):
25+ for i in range (0 , len (route .route )):
26+ rt = route .route [i ]
27+ if rt .latitude == target_pos [0 ] and rt .longitude == target_pos [1 ]:
28+ return i
29+
30+ def find_cur_waypoint_id (route ):
31+ for i in range (0 , len (route )):
32+ if route [i ].is_current :
33+ return i
34+
35+ def drop_cargo ():
36+ logwarn ("DROP!!!" )
37+
38+ def quad_controller (route_queue , position_queue , arming_queue ,
39+ trgt_lat_queue , trgt_lon_queue , waypoints_queue ):
40+ flight_done_pub = Publisher ('remove' , UInt32 , queue_size = 1 )
2441 while not is_shutdown ():
2542 # Take a route
2643 route = route_queue .get (True , None )
2744 loginfo ('Received route with id #' + str (route .id )+ '.' )
2845 if not route .valid :
2946 logerr ('Route invalid!' )
3047 return
48+ target_pos = None
49+ need_waypoint_id = None
50+ if trgt_lat_queue .empty () or trgt_lon_queue .empty ():
51+ logwarn ('No target!' )
52+ else :
53+ target_pos = (trgt_lat_queue .get ().data / 1e6 ,
54+ trgt_lon_queue .get ().data / 1e6 )
55+ # Determine the point where you want to dump the load
56+ need_waypoint_id = find_waypoint_id (route , target_pos )
57+ loginfo ('need_waypoint_id: ' + str (need_waypoint_id ))
58+
3159 # Push a mission into flight controller
32- push_mission (waypointWrap (target .route ))
60+ push_mission (waypointWrap (route .route ))
3361 loginfo ('Mission loaded to flight controller.' )
3462 # Set manual mode
3563 set_mode ('ACRO' )
36- # Enable motors
64+ # Enable motors
3765 arming ()
3866 # Set autopilot mode
3967 set_mode ('AUTO' )
4068 loginfo ('Flight!' )
4169
4270 # Wainting for arming
43- # TODO: More elegant case for mission finish check
4471 sleep (5 )
4572 drop_all (arming_queue )
73+
74+ # If you do not have a goal to clear cargo,
75+ # we believe that already dropped
76+ droped = (need_waypoint_id == None )
77+ while arming_queue .get ().data and not droped :
78+ waypoints = None
79+ # To not accumulate elements in arming_queue
80+ try :
81+ waypoints = waypoints_queue .get_nowait ().waypoints
82+ except Empty :
83+ continue
84+ current_waypoint_id = find_cur_waypoint_id (waypoints )
85+ if current_waypoint_id > need_waypoint_id :
86+ droped = True
87+ drop_cargo ()
88+
89+ # TODO: More elegant case for mission finish check
4690 while arming_queue .get ().data :
4791 pass
4892
4993 loginfo ('Mission #' + str (route .id )+ ' complete!' )
5094 flight_done_pub .publish (route .id )
51-
5295
5396def main ():
5497 ''' The main routine '''
@@ -57,20 +100,28 @@ def main():
57100 route_queue = Queue ()
58101 position_queue = Queue ()
59102 arming_queue = Queue ()
103+ trgt_lat_queue = Queue ()
104+ trgt_lon_queue = Queue ()
105+ waypoints_queue = Queue ()
60106 # Create controller thread
61107 controller = Thread (target = quad_controller ,
62- args = (target_queue , position_queue , arming_queue ))
63-
108+ args = (route_queue , position_queue , arming_queue ,
109+ trgt_lat_queue , trgt_lon_queue , waypoints_queue ))
110+
64111 # Route message handler
65112 def quad_route (msg ):
66113 if not controller .isAlive ():
67114 controller .start ()
68115 route_queue .put (msg )
69-
70- Subscriber ('mavros/global_position/global' , NavSatFix , position_queue .put )
116+
117+ Subscriber ('mavros/global_position/global' , NavSatFix , position_queue .put )
71118 Subscriber ('armed' , Bool , arming_queue .put )
119+ # TODO: combine next two topic in one
120+ Subscriber ('drop_target_lat' , Int64 , trgt_lat_queue .put )
121+ Subscriber ('drop_target_lon' , Int64 , trgt_lon_queue .put )
122+ Subscriber ('mavros/mission/waypoints' , WaypointList , waypoints_queue .put )
72123 Subscriber ('route' , RouteResponse , quad_route )
73124 spin ()
74-
125+
75126if __name__ == '__main__' :
76127 main ()
0 commit comments