Skip to content

Commit 6d8cc73

Browse files
committed
Merge pull request #551 from dronekit/hgw_dk_sitl_example_follow_me
Update follow_me example to use dk-sitl by default
2 parents 7800c40 + d436bf3 commit 6d8cc73

File tree

2 files changed

+67
-45
lines changed

2 files changed

+67
-45
lines changed

docs/examples/follow_me.rst

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,54 +51,58 @@ Once you've done that:
5151
cd dronekit-python/examples/follow_me/
5252
5353
54-
#. Start the example, passing the :ref:`connection string <get_started_connect_string>` you wish to use in the ``--connect`` parameter:
54+
#. You can run the example against a simulator (DroneKit-SITL) by specifying the Python script without any arguments.
55+
The example will download SITL binaries (if needed), start the simulator, and then connect to it:
5556

5657
.. code-block:: bash
5758
58-
python follow_me.py --connect 127.0.0.1:14550
59-
59+
python follow_me.py
6060
61-
.. note::
61+
On the command prompt you should see (something like):
6262

63-
The examples uses the ``--connect`` parameter to pass the :ref:`connection string <get_started_connect_string>` into the script.
64-
The command above is used to connect to :ref:`SITL <sitl_setup>` running on the local Linux machine via UDP port 14550.
63+
.. code:: bash
64+
65+
Starting copter simulator (SITL)
66+
SITL already Downloaded.
67+
Connecting to vehicle on: tcp:127.0.0.1:5760
68+
>>> APM:Copter V3.4-dev (e0810c2e)
69+
>>> Frame: QUAD
70+
Link timeout, no heartbeat in last 5 seconds
71+
Basic pre-arm checks
72+
Waiting for GPS...: None
73+
...
74+
Waiting for GPS...: None
75+
Taking off!
76+
Altitude: 0.019999999553
77+
...
78+
Altitude: 4.76000022888
79+
Reached target altitude
80+
Going to: Location:lat=50.616468333,lon=7.131903333,alt=30,is_relative=True
81+
...
82+
Going to: Location:lat=50.616468333,lon=7.131903333,alt=30,is_relative=True
83+
Going to: Location:lat=50.616468333,lon=7.131903333,alt=30,is_relative=True
84+
User has changed flight modes - aborting follow-me
85+
Close vehicle object
86+
Completed
87+
88+
.. note::
6589

90+
The terminal output above was created using simulated GPS data
91+
(which is why the same target location is returned every time).
92+
93+
To stop follow-me you can change the vehicle mode or do Ctrl+C
94+
(on a real flight you can just change the mode switch on your
95+
RC transmitter).
96+
6697

67-
On the terminal you should see (something like):
98+
#. 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.
6899

100+
For example, to connect to SITL running on UDP port 14550 on your local computer:
69101

70-
.. code-block:: bash
71-
72-
Connecting to vehicle on: 127.0.0.1:14550
73-
>>> APM:Copter V3.4-dev (e0810c2e)
74-
>>> Frame: QUAD
75-
Link timeout, no heartbeat in last 5 seconds
76-
Basic pre-arm checks
77-
Waiting for GPS...: None
78-
...
79-
Waiting for GPS...: None
80-
Taking off!
81-
Altitude: 0.019999999553
82-
...
83-
Altitude: 4.76000022888
84-
Reached target altitude
85-
Going to: Location:lat=50.616468333,lon=7.131903333,alt=30,is_relative=True
86-
...
87-
Going to: Location:lat=50.616468333,lon=7.131903333,alt=30,is_relative=True
88-
Going to: Location:lat=50.616468333,lon=7.131903333,alt=30,is_relative=True
89-
User has changed flight modes - aborting follow-me
90-
Close vehicle object
91-
Completed
92-
93-
94-
To stop follow-me you can change the vehicle mode (on a real flight you can just change the mode switch on your RC transmitter).
95-
96-
.. note::
102+
.. code-block:: bash
97103
98-
The terminal output above was created using simulated GPS data
99-
(which is why the same target location is returned every time). It was exited by changing
100-
the mode from GUIDED to STABILIZE using MAVProxy.
101-
104+
python follow_me.py --connect 127.0.0.1:14550
105+
102106
103107
104108
How does it work?

examples/follow_me/follow_me.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,29 @@
1818

1919
#Set up option parsing to get connection string
2020
import argparse
21-
parser = argparse.ArgumentParser(description='Tracks GPS position of your computer (Linux only). Connects to SITL on local PC by default.')
22-
parser.add_argument('--connect', default='127.0.0.1:14550',
23-
help="vehicle connection target. Default '127.0.0.1:14550'")
21+
parser = argparse.ArgumentParser(description='Tracks GPS position of your computer (Linux only).')
22+
parser.add_argument('--connect',
23+
help="vehicle connection target string. If not specified, SITL automatically started and used.")
2424
args = parser.parse_args()
2525

26+
connection_string = args.connect
27+
sitl = None
28+
29+
30+
#Start SITL if no connection string specified
31+
if not args.connect:
32+
print "Starting copter simulator (SITL)"
33+
from dronekit_sitl import SITL
34+
sitl = SITL()
35+
sitl.download('copter', '3.3', verbose=True)
36+
sitl_args = ['-I0', '--model', 'quad', '--home=-35.363261,149.165230,584,353']
37+
sitl.launch(sitl_args, await_ready=True, restart=True)
38+
connection_string = 'tcp:127.0.0.1:5760'
39+
2640

2741
# Connect to the Vehicle
28-
print 'Connecting to vehicle on: %s' % args.connect
29-
vehicle = connect(args.connect, wait_ready=True)
42+
print 'Connecting to vehicle on: %s' % connection_string
43+
vehicle = connect(connection_string, wait_ready=True)
3044

3145

3246

@@ -44,8 +58,8 @@ def arm_and_takeoff(aTargetAltitude):
4458

4559
print "Arming motors"
4660
# Copter should arm in GUIDED mode
47-
vehicle.mode = VehicleMode("GUIDED")
48-
vehicle.armed = True
61+
vehicle.mode = VehicleMode("GUIDED")
62+
vehicle.armed = True
4963

5064
while not vehicle.armed:
5165
print " Waiting for arming..."
@@ -102,4 +116,8 @@ def arm_and_takeoff(aTargetAltitude):
102116
print "Close vehicle object"
103117
vehicle.close()
104118

119+
# Shut down simulator if it was started.
120+
if sitl is not None:
121+
sitl.stop()
122+
105123
print("Completed")

0 commit comments

Comments
 (0)