-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Standardize arm_and_takeoff #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Before making an all-encompassing function do accomplish this task, let's analyze its components and some hypothetical subcommands:
"""
Arm vehicle and fly to aTargetAltitude.
"""
print "Basic pre-arm checks"
# Don't let the user try to fly while autopilot is booting
if vehicle.mode.name == "INITIALISING":
print "Waiting for vehicle to initialise"
time.sleep(1)
while vehicle.gps_0.fix_type < 2:
print "Waiting for GPS...:", vehicle.gps_0.fix_type
time.sleep(1)
print "Arming motors"
# Copter should arm in GUIDED mode
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
vehicle.flush()
while not vehicle.armed and not api.exit:
print " Waiting for arming..."
time.sleep(1)
print "Taking off!"
vehicle.commands.takeoff(aTargetAltitude) # Take off to target altitude
vehicle.flush()
# Wait until the vehicle reaches a safe height before processing the goto (otherwise the command
# after Vehicle.commands.takeoff will execute immediately).
while not api.exit:
print " Altitude: ", vehicle.location.alt
if vehicle.location.alt>=aTargetAltitude*0.95: #Just below target, in case of undershoot.
print "Reached target altitude"
break;
time.sleep(1) How do we feel about this level of granularity? I feel we should start by refactoring out small components, then arrive at either a consistent set of commands or one larger |
@tcr3dr The split looks logical. At the end it makes sense to me that there just be one big fat function. My concerns with the method as it is (for your consideration):
|
Just a thought: why not have the methods be asynchronous but return something like a Future which can either be ignored or waited on. First you would get a future like so: takeoff_future = vehicle.takeoff(100) Then later on, if you want to wait until the previous command succeeds (or fails): takeoff_future.wait()
if takeoff_future:
print "Drone has reached target altitude"
else:
print "Takeoff failed with error {}".format(takeoff_future.error) Here I decided to have the truthiness of the future be indicative of an error, but you could use a property like Since you're not always waiting for a value, but sometimes for a condition to be met, you could set a callback to check for success: takeoff_future.callback = lambda: vehicle.location.alt >= desired_altitude * .95 Though, callbacks can be a bit unsightly in Python, so another idea is to have each function take in a future as an optional (keyword) parameter so that you can create a future specific to the function and fill in properties prior to using it. future = Future(takeoff) # pass in the function you want a future for and it will preload some defaults
future.variance = .05 # this field is unique to a takeoff future
vehicle.takeoff(100, future=future)
future.wait()
if not future:
print future.error |
@atomictom I like the idea - very C#. We need an approach that can be used broadly across the whole API to handle asynchronous behaviour in a common way. My preference is something like this. |
From @hamishwillee:
An example of this code: https://github.com/diydrones/dronekit-python/blob/9130f1021b81e7be88f5d548c0ddcfcbf4231e10/examples/guided_set_speed_yaw/guided_set_speed_yaw.py#L16
The text was updated successfully, but these errors were encountered: