You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge pull request #559 from dronekit/flight-replay-localfile
Rejig flight replay example to use a telemetry log
peter.barker
5:08 PM All looks good to me. You OK if I merge that one?
hamishwillee
5:08 PM Yes please!
On the command prompt you should see (something like):
49
49
50
-
The ``--connect`` parameter above connects to SITL on udp port 127.0.0.1:14550, while
51
-
``--mission_id`` specifies we're replaying mission 101. Both of these are the default
52
-
values for the parameters, and may be omitted.
53
-
50
+
.. code:: bash
51
+
52
+
Generating waypoints from tlog...
53
+
Generated 100 waypoints from tlog
54
+
Starting copter simulator (SITL)
55
+
SITL already Downloaded.
56
+
Connecting to vehicle on: tcp:127.0.0.1:5
57
+
>>> APM:Copter V3.3 (d6053245)
58
+
>>> Frame: QUAD
59
+
>>> Calibrating barometer
60
+
>>> Initialising APM...
61
+
>>> barometer calibration complete
62
+
>>> GROUND START
63
+
Uploading 100 waypoints to vehicle...
64
+
Arm and Takeoff
65
+
Waiting for vehicle to initialise...
66
+
>>> flight plan received
67
+
Waiting for arming...
68
+
Waiting for arming...
69
+
Waiting for arming...
70
+
Waiting for arming...
71
+
>>> ARMING MOTORS
72
+
>>> GROUND START
73
+
Waiting for arming...
74
+
>>> Initialising APM...
75
+
Waiting for arming...
76
+
>>> ARMING MOTORS
77
+
Taking off!
78
+
Altitude: 0.000000 < 28.500000
79
+
Altitude: 0.010000 < 28.500000
80
+
...
81
+
Altitude: 26.350000 < 28.500000
82
+
Altitude: 28.320000 < 28.500000
83
+
Reached target altitude of ~30.000000
84
+
Starting mission
85
+
Distance to waypoint (1): 3.02389745499
86
+
>>> Reached Command #1
87
+
Distance to waypoint (2): 5.57718471895
88
+
Distance to waypoint (2): 4.1504263025
89
+
>>> Reached Command #2
90
+
Distance to waypoint (3): 0.872847106279
91
+
Distance to waypoint (3): 1.88967925144
92
+
Distance to waypoint (3): 2.16157704522
93
+
>>> Reached Command #3
94
+
Distance to waypoint (4): 4.91867197924
95
+
...
96
+
...
97
+
Distance to waypoint (35): 4.37309981133
98
+
>>> Reached Command #35
99
+
Distance to waypoint (36): 5.61829417257
100
+
>>> Reached Command #36
101
+
Return to launch
102
+
Close vehicle object
103
+
Completed...
104
+
105
+
106
+
.. tip::
107
+
It is more interesting to watch the example run on a map than the console. The topic :ref:`viewing_uav_on_map`
108
+
explains how to set up *Mission Planner* to view a vehicle running on the simulator (SITL).
54
109
55
-
.. tip::
56
-
57
-
It is more interesting to watch the example above on a map than the console. The topic :ref:`viewing_uav_on_map`
58
-
explains how to set up *Mission Planner* to view a vehicle running on the simulator (SITL).
110
+
#. You can run the example against a specific connection (simulated or otherwise) by passing the :ref:`connection string <get_started_connect_string>` for your vehicle in the ``--connect`` parameter.
59
111
60
-
On the command prompt you should see (something like):
112
+
For example, to connect to SITL running on UDP port 14550 on your local computer:
61
113
62
-
.. code-block:: bash
63
-
64
-
Connecting to vehicle on: 127.0.0.1:14550
65
-
>>> APM:Copter V3.3 (d6053245)
66
-
>>> Frame: QUAD
67
-
JSON downloaded...
68
-
Generating 95 waypoints from replay...
69
-
Close vehicle object
70
-
Completed...
114
+
.. code-block:: bash
71
115
116
+
python simple_goto.py --connect 127.0.0.1:14550
117
+
72
118
119
+
73
120
How it works
74
121
============
75
122
76
-
The example requests the web server for representative points from the flight, parses the JSON response
77
-
and uses that data to generate 100 waypoints. These are then sent to the vehicle.
78
-
79
-
80
123
Getting the points
81
124
------------------
82
125
83
-
The following simple function asks for the droneshare flight data:
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.
84
128
85
-
.. code:: python
129
+
For safety reasons, the altitude for the waypoints is set to 30 meters (irrespective of the recorded height).
86
130
87
-
defdownload_messages(mission_id, max_freq=1.0):
88
-
"""Download a public mission from droneshare (as JSON)"""
89
-
f = urllib.urlopen("%s/api/v1/mission/%s/messages.json?max_freq=%s&api_key=%s"% (api_server, mission_id, max_freq, api_key))
90
-
j = json.load(f, object_hook=_decode_dict)
91
-
f.close()
92
-
return j
131
+
.. code:: python
93
132
94
-
Some comments:
133
+
defposition_messages_from_tlog(filename):
134
+
"""
135
+
Given telemetry log, get a series of wpts approximating the previous flight
136
+
"""
137
+
# Pull out just the global position msgs
138
+
messages = []
139
+
mlog = mavutil.mavlink_connection(filename)
140
+
whileTrue:
141
+
try:
142
+
m = mlog.recv_match(type=['GLOBAL_POSITION_INT'])
143
+
if m isNone:
144
+
break
145
+
exceptException:
146
+
break
147
+
# ignore we get where there is no fix:
148
+
if m.lat ==0:
149
+
continue
150
+
messages.append(m)
151
+
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.
156
+
num_points =len(messages)
157
+
keep_point_distance=3#metres
158
+
kept_messages = []
159
+
kept_messages.append(messages[0]) #Keep the first message
* ``max_freq`` is used to throttle the messages found in the raw flight data to a lower message rate
97
-
* ``_decode_dict`` is a utility function found on stack overflow which extracts usable strings from unicode encoded JSON (see `flight_replay.py <https://github.com/hamishwillee/dronekit-python/blob/master/examples/flight_replay/flight_replay.py>`_ for its implementation).
98
177
99
178
100
179
Setting the new waypoints
101
180
-------------------------
102
181
103
-
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).
104
-
105
182
The following code shows how the vehicle writes the received messages as commands (this part of the code is very similar to that
0 commit comments