Shageenth Sandrakumar Shageenth Sandrakumar - 1 year ago 61
Python Question

Drone-kit Python would not let me change modes in the simulator while in air

Whenever I try changing modes in the air, drone-kit python script continues to leave the copter in GUIDED mode. I essential want my python script to allow my drone to fly through a certain location and switch its mode to LOITER in the air and stay in the air for a certain period of time.
Here is a small piece of my script:

print "Going towards location"

vehicle.mode = VehicleMode("LOITER")
print vehicle.mode

Every single time I run my script, it outputs the vehicle modes as GUIDED not LOITER.
I'm not understanding why not.

Here is a defintion of goto python function

def goto(dNorth, dEast, gotoFunction=vehicle.simple_goto):
targetLocation=get_location_metres(currentLocation, dNorth, dEast)
targetDistance=get_distance_metres(currentLocation, targetLocation)

while ("GUIDED") and (get_distance_metres(vehicle.home_location,vehicle.location.global_frame)<radius) and (vehicle.location.global_relative_frame.alt<alt_limit):
#Stop action if we are no longer in guided mode or outside radius.
remainingDistance=get_distance_metres(vehicle.location.global_frame, targetLocation)
print "Distance to target: ", remainingDistance
if remainingDistance<=targetDistance*0.1: #Just below target, in case of undershoot.
print "Reached target"

I understand that simple_goto cannot run if the copter is not in GUIDED mode. But after it reaches its destination, the function tells it to break and I'm assuming it no longer runs in simple_goto anymore. If anybody can help me with an explanation of why this is happening because I do not understand what is wrong with my code.

(Whole code can be posted on request)

Answer Source
vehicle.mode = VehicleMode("LOITER")
print vehicle.mode

This part isn't going to work because it takes a bit for the vehicle to change modes and then confirm the mode change.