Skip to content

Commit a45ebcb

Browse files
committed
Merge pull request #4 from simonpavlov/master
Thank you, Simon!
2 parents 0c0a8d4 + 8edaee3 commit a45ebcb

File tree

12 files changed

+186
-31
lines changed

12 files changed

+186
-31
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
*.pyc
2+
.ycm_extra_conf.py
3+
.*.swp
4+
.*.swo

drone_employee/doc/delivery.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Drop API
2+
In order to relieve the load you simply publish the `latitude` and `longitude` of the point of delivery to the example as [here](https://github.com/simonpavlov/contracts/blob/master/drone_carrier.sol).
3+
To perform additional actions may be modified `drop_cargo()` in [quad_controller.py](https://github.com/DroneEmployee/drone_employee_ros/blob/master/drone_employee/scripts/quad_controller.py).
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<launch>
33
<node name="state" pkg="drone_employee" type="state_publisher.py" />
4-
<node name="controller" pkg="drone_employee" type="quad_controller.py" />
4+
<node name="controller" pkg="drone_employee" type="quad_controller.py" output="screen"/>
55
</launch>

drone_employee/scripts/quad_controller.py

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
from mission_wrapper import waypointWrap
44
from 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
78
from rospy import Subscriber, Publisher
89
from small_atc_msgs.msg import *
910
from 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
1113
from threading import Thread
12-
from Queue import Queue
14+
from Queue import Queue, Empty
1315

1416
def 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

5396
def 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+
75126
if __name__ == '__main__':
76127
main()

small_atc/include/small_atc/ObstacleProvider.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class ObstacleProviderImpl : public ObstacleProvider
5656
}
5757

5858
void publishAll() {
59-
for (auto k = obstacles.begin(); k != obstacles.end(); ++k) {
60-
int id = k->first;
61-
const Obstacle *map = k->second;
59+
for (auto k : obstacles) {
60+
int id = k.first;
61+
const Obstacle *map = k.second;
6262
publishers[id].publish(map->getOctomapMsg());
6363
}
6464
}

small_atc/launch/demo.launch

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<launch>
33
<include ns="small_atc" file="$(find small_atc)/launch/atc.launch"/>
4-
<include file="$(find small_atc_dron)/launch/apm_sitl.launch">
4+
<include file="$(find drone_employee)/launch/apm_sitl.launch">
55
<arg name="ident" value="1" />
66
</include>
7-
<include file="$(find small_atc_dron)/launch/apm_sitl.launch">
7+
<include file="$(find drone_employee)/launch/apm_sitl.launch">
88
<arg name="ident" value="2" />
99
</include>
10-
<include file="$(find small_atc_dron)/launch/apm_sitl.launch">
10+
<include file="$(find drone_employee)/launch/apm_sitl.launch">
1111
<arg name="ident" value="3" />
1212
</include>
1313
</launch>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<launch>
3+
<include ns="small_atc" file="$(find small_atc)/launch/atc.launch"/>
4+
<include file="$(find drone_employee)/launch/apm_sitl.launch">
5+
<arg name="ident" value="1" />
6+
</include>
7+
</launch>

small_atc/scripts/geo_convert.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/python2
2+
3+
from geo_helper import satFix2Point, point2SatFix
4+
from geometry_msgs.msg import Point
5+
from small_atc_msgs.msg import SatFix
6+
from sys import argv
7+
8+
DESCRIPTION = '''DESCRIPTION:\n\tThe script for simple convert from SatFix to Point and from Point to SatFix'''
9+
USAGE = '''USAGE:\n\t{0} -p x y z\n\t{0} -s lat lon alt'''.format(argv[0])
10+
11+
def main():
12+
13+
def wrong_usage():
14+
print(USAGE)
15+
exit(1)
16+
17+
def convert_init(cls, members_name, init_mass):
18+
res = cls()
19+
for i in zip(members_name, map(float, init_mass)):
20+
res.__setattr__(*i)
21+
return res
22+
23+
if argv[1] == '-h':
24+
print DESCRIPTION
25+
print USAGE
26+
exit(0)
27+
28+
if len(argv) != 5:
29+
wrong_usage()
30+
31+
if argv[1] == '-p':
32+
print 'convert to SatFix'
33+
try:
34+
point = convert_init(Point, ['x', 'y', 'z'], argv[2:])
35+
except ValueError:
36+
wrong_usage()
37+
print point2SatFix(point)
38+
39+
elif argv[1] == '-s':
40+
print 'convert to Point'
41+
try:
42+
satfix = convert_init(SatFix, ['latitude', 'longitude', 'altitude'], argv[2:])
43+
except ValueError:
44+
wrong_usage()
45+
print satFix2Point(satfix)
46+
47+
else:
48+
wrong_usage()
49+
50+
if __name__ == '__main__':
51+
main()

small_atc/scripts/geo_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Origin(object):
1515
origin = Origin()
1616
origin.latitude = 37.874815
1717
origin.longitude = -122.616569
18-
origin.altitude = -1000
18+
origin.altitude = -1139
1919
origin.offset = (1060, 1930)
2020
#
2121
###

small_atc/scripts/geo_math.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import math
2+
3+
def len_diff_coord(lat1, lon1, lat2, lon2):
4+
R = 6378.137 # Radius of earth in KM
5+
dLat = (lat2 - lat1) * math.pi / 180
6+
dLon = (lon2 - lon1) * math.pi / 180;
7+
a = (math.sin(dLat/2) * math.sin(dLat/2) +
8+
math.cos(lat1 * math.pi / 180) * math.cos(lat2 * math.pi / 180) *
9+
math.sin(dLon/2) * math.sin(dLon/2))
10+
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
11+
d = R * c
12+
return d * 1000 # meters

0 commit comments

Comments
 (0)