1- =========================
1+ ======================
22Example: Flight Replay
3- =========================
3+ ======================
44
5- This example creates and runs a waypoint mission using position information in a TLOG file.
5+ This example creates and runs a waypoint mission using position information from a TLOG file.
66
7- The log used in this example contains around 2700 points. We reduce this to a smaller number
8- (that is able to fit on the autopilot) by taking 100 points that are evenly spread across the range.
9- After 60 seconds the mission is artificially ended by setting the mode to RTL (return to launch).
7+ The log used in this example contains around 2700 points. This is too many points to upload
8+ to the autopilot (and to usefully display). Instead we only add points that are more than
9+ 3 metres away from the previously kept point, and only store 99 points in total.
10+ After 60 seconds the mission is ended by setting the mode to RTL (return to launch).
1011
1112.. figure :: flight_replay_example.png
1213 :width: 50%
1314
14- Mission generated from log
15+ 99 point mission generated from log
16+
1517
1618.. note ::
1719
18- A more detailed example might determine the best points for the mission
19- by mapping the path to lines and spline curves.
20+ The method used to reduce the number of points is fairly effective, but we
21+ could do better by grouping some of the waypoints, and mapping others using
22+ spline waypoints. This might be a
23+ `fun research project <https://github.com/dronekit/dronekit-python/issues/561 >`_!
24+
2025
2126
2227Running the example
@@ -45,22 +50,23 @@ In summary, after cloning the repository:
4550 .. code :: bash
4651
4752 Generating waypoints from tlog...
48- Generated 96 waypoints from tlog
53+ Generated 100 waypoints from tlog
4954 Starting copter simulator (SITL)
5055 SITL already Downloaded.
51- Connecting to vehicle on: tcp:127.0.0.1:5760
56+ Connecting to vehicle on: tcp:127.0.0.1:5
5257 >>> APM:Copter V3.3 (d6053245)
5358 >>> Frame: QUAD
5459 >>> Calibrating barometer
5560 >>> Initialising APM...
5661 >>> barometer calibration complete
5762 >>> GROUND START
58- Uploading 96 waypoints to vehicle...
63+ Uploading 100 waypoints to vehicle...
5964 Arm and Takeoff
6065 Waiting for vehicle to initialise...
6166 >>> flight plan received
6267 Waiting for arming...
63- ...
68+ Waiting for arming...
69+ Waiting for arming...
6470 Waiting for arming...
6571 >>> ARMING MOTORS
6672 >>> GROUND START
@@ -69,53 +75,35 @@ In summary, after cloning the repository:
6975 Waiting for arming...
7076 >>> ARMING MOTORS
7177 Taking off!
78+ Altitude: 0.000000 < 28.500000
7279 Altitude: 0.010000 < 28.500000
73- Altitude: 0.020000 < 28.500000
7480 ...
75- Altitude: 26.150000 < 28.500000
76- Altitude: 28.170000 < 28.500000
81+ Altitude: 26.350000 < 28.500000
82+ Altitude: 28.320000 < 28.500000
7783 Reached target altitude of ~ 30.000000
7884 Starting mission
79- Distance to waypoint (1): 6.31671220061
80- Distance to waypoint (1): 5.54023406731
85+ Distance to waypoint (1): 3.02389745499
8186 >>> Reached Command # 1
82- Distance to waypoint (2): 4.05805003875
83- ...
84- Distance to waypoint (2): 4.66600036548
87+ Distance to waypoint (2): 5.57718471895
88+ Distance to waypoint (2): 4.1504263025
8589 >>> Reached Command # 2
86- Distance to waypoint (3): 1.49371523482
87- Distance to waypoint (3): 0.13542601646
88- Distance to waypoint (3): 0.708432959397
90+ Distance to waypoint (3): 0.872847106279
91+ Distance to waypoint (3): 1.88967925144
92+ Distance to waypoint (3): 2.16157704522
8993 >>> Reached Command # 3
90- Distance to waypoint (4): 3.29161427437
91- Distance to waypoint (4): 3.63454331996
92- Distance to waypoint (4): 2.89070828637
93- >>> Reached Command # 4
94- Distance to waypoint (5): 0.955857968149
95- >>> Reached Command # 5
96- >>> Reached Command # 6
97- >>> Reached Command # 7
98- ...
99- >>> Reached Command # 42
100- Distance to waypoint (42): 7.6983209285
101- ...
102- Distance to waypoint (43): 6.05247510021
103- >>> Reached Command # 43
104- Distance to waypoint (43): 4.80180763387
105- >>> Reached Command # 44
106- Distance to waypoint (44): 3.89880557617
94+ Distance to waypoint (4): 4.91867197924
10795 ...
108- Distance to waypoint (45): 11.0865559753
109- >>> Reached Command # 45
110- Distance to waypoint (46): 9.45419808223
11196 ...
112- Distance to waypoint (46): 13.2676499949
97+ Distance to waypoint (35): 4.37309981133
98+ >>> Reached Command # 35
99+ Distance to waypoint (36): 5.61829417257
100+ >>> Reached Command # 36
113101 Return to launch
114102 Close vehicle object
115103 Completed...
116104
117- .. tip ::
118105
106+ .. tip ::
119107 It is more interesting to watch the example run on a map than the console. The topic :ref: `viewing_uav_on_map `
120108 explains how to set up *Mission Planner * to view a vehicle running on the simulator (SITL).
121109
@@ -132,22 +120,19 @@ In summary, after cloning the repository:
132120 How it works
133121============
134122
135- The example parses the **flight.tlog ** file for position information. It then selects about 100
136- points that are evenly spread across the log and uploads them as a mission.
137-
138- For safety reasons, the altitude for the waypoints is set to 30 meters (irrespective of the recorded height).
139-
140123Getting the points
141124------------------
142125
143- The following simple function parses the tlog for points and then
144- returns 100 evenly points from the collected set.
126+ The example parses the **flight.tlog ** file for position information. First we read all the points.
127+ We then keep the first 99 points that are at least 3 metres separated from the preceding kept point.
128+
129+ For safety reasons, the altitude for the waypoints is set to 30 meters (irrespective of the recorded height).
145130
146131.. code :: python
147132
148133 def position_messages_from_tlog (filename ):
149134 """
150- Given telemetry log, get a series of waypoints approximating the previous flight
135+ Given telemetry log, get a series of wpts approximating the previous flight
151136 """
152137 # Pull out just the global position msgs
153138 messages = []
@@ -164,22 +149,36 @@ returns 100 evenly points from the collected set.
164149 continue
165150 messages.append(m)
166151
167- # Shrink the # of pts to be lower than the max # of wpts allowed by vehicle
152+ # Shrink the number of points for readability and to stay within autopilot memory limits.
153+ # For coding simplicity we:
154+ # - only keep points that are with 3 metres of the previous kept point.
155+ # - only keep the first 100 points that meet the above criteria.
168156 num_points = len (messages)
169- max_points = 99
170- if num_points > max_points:
171- step = int (math.ceil((float (num_points) / max_points)))
172- shorter = [messages[i] for i in xrange (0 , num_points, step)]
173- messages = shorter
174- return messages
157+ keep_point_distance= 3 # metres
158+ kept_messages = []
159+ kept_messages.append(messages[0 ]) # Keep the first message
160+ pt1num= 0
161+ pt2num= 1
162+ while True :
163+ # Keep the last point. Only record 99 points.
164+ if pt2num== num_points- 1 or len (kept_messages)== 99 :
165+ kept_messages.append(messages[pt2num])
166+ break
167+ pt1 = LocationGlobalRelative(messages[pt1num].lat/ 1.0e7 ,messages[pt1num].lon/ 1.0e7 ,0 )
168+ pt2 = LocationGlobalRelative(messages[pt2num].lat/ 1.0e7 ,messages[pt2num].lon/ 1.0e7 ,0 )
169+ distance_between_points = get_distance_metres(pt1,pt2)
170+ if distance_between_points > keep_point_distance:
171+ kept_messages.append(messages[pt2num])
172+ pt1num= pt2num
173+ pt2num= pt2num+ 1
174+
175+ return kept_messages
175176
176177
177178
178179 Setting the new waypoints
179180-------------------------
180181
181- If necessary, the example then reduces the number of messages retrieved into a set that can fit on the vehicle (in this case 100 waypoints).
182-
183182The following code shows how the vehicle writes the received messages as commands (this part of the code is very similar to that
184183shown in :ref: `example_mission_basic `):
185184
0 commit comments