|
| 1 | +""" |
| 2 | +simple_goto.py: GUIDED mode "simple goto" example (Copter Only) |
| 3 | +
|
| 4 | +The example demonstrates how to arm and takeoff in Copter and how to navigate to |
| 5 | +points using Vehicle.commands.goto. |
| 6 | +
|
| 7 | +Full documentation is provided at http://python.dronekit.io/examples/simple_goto.html |
| 8 | +""" |
| 9 | + |
| 10 | +import time |
| 11 | +from droneapi.lib import VehicleMode, Location |
| 12 | +from pymavlink import mavutil |
| 13 | +from testlib import assert_equals |
| 14 | + |
| 15 | +def test_goto(local_connect): |
| 16 | + api = local_connect() |
| 17 | + vehicle = api.get_vehicles()[0] |
| 18 | + |
| 19 | + def arm_and_takeoff(aTargetAltitude): |
| 20 | + """ |
| 21 | + Arms vehicle and fly to aTargetAltitude. |
| 22 | + """ |
| 23 | + |
| 24 | + print "Basic pre-arm checks" |
| 25 | + # Don't let the user try to fly autopilot is booting |
| 26 | + if vehicle.mode.name == "INITIALISING": |
| 27 | + print "Waiting for vehicle to initialise" |
| 28 | + time.sleep(1) |
| 29 | + while vehicle.gps_0.fix_type < 2: |
| 30 | + print "Waiting for GPS...:", vehicle.gps_0.fix_type |
| 31 | + time.sleep(1) |
| 32 | + |
| 33 | + print "Arming motors" |
| 34 | + # Copter should arm in GUIDED mode |
| 35 | + vehicle.mode = VehicleMode("GUIDED") |
| 36 | + vehicle.flush() |
| 37 | + |
| 38 | + i = 60 |
| 39 | + while not api.exit and vehicle.mode.name != 'GUIDED' and i > 0: |
| 40 | + print " Waiting for guided %s seconds..." % (i,) |
| 41 | + time.sleep(1) |
| 42 | + i = i - 1 |
| 43 | + |
| 44 | + vehicle.armed = True |
| 45 | + vehicle.flush() |
| 46 | + |
| 47 | + i = 60 |
| 48 | + while not api.exit and not vehicle.armed and vehicle.mode.name == 'GUIDED' and i > 0: |
| 49 | + print " Waiting for arming %s seconds..." % (i,) |
| 50 | + time.sleep(1) |
| 51 | + i = i - 1 |
| 52 | + |
| 53 | + # Failure will result in arming but immediately landing |
| 54 | + assert vehicle.armed |
| 55 | + assert_equals(vehicle.mode.name, 'GUIDED') |
| 56 | + |
| 57 | + print "Taking off!" |
| 58 | + vehicle.commands.takeoff(aTargetAltitude) # Take off to target altitude |
| 59 | + vehicle.flush() |
| 60 | + |
| 61 | + # Wait until the vehicle reaches a safe height before |
| 62 | + # processing the goto (otherwise the command after |
| 63 | + # Vehicle.commands.takeoff will execute immediately). |
| 64 | + while not api.exit: |
| 65 | + print " Altitude: ", vehicle.location.alt |
| 66 | + # Test for altitude just below target, in case of undershoot. |
| 67 | + if vehicle.location.alt >= aTargetAltitude * 0.95: |
| 68 | + print "Reached target altitude" |
| 69 | + break; |
| 70 | + |
| 71 | + assert_equals(vehicle.mode.name, 'GUIDED') |
| 72 | + time.sleep(1) |
| 73 | + |
| 74 | + arm_and_takeoff(10) |
| 75 | + |
| 76 | + print "Going to first point..." |
| 77 | + point1 = Location(-35.361354, 149.165218, 20, is_relative=True) |
| 78 | + vehicle.commands.goto(point1) |
| 79 | + vehicle.flush() |
| 80 | + |
| 81 | + # sleep so we can see the change in map |
| 82 | + time.sleep(3) |
| 83 | + |
| 84 | + print "Going to second point..." |
| 85 | + point2 = Location(-35.363244, 149.168801, 20, is_relative=True) |
| 86 | + vehicle.commands.goto(point2) |
| 87 | + vehicle.flush() |
| 88 | + |
| 89 | + # sleep so we can see the change in map |
| 90 | + time.sleep(3) |
| 91 | + |
| 92 | + print "Returning to Launch" |
| 93 | + vehicle.mode = VehicleMode("RTL") |
| 94 | + vehicle.flush() |
0 commit comments