Search code examples
pythondronekit-pythondronekit

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"
    goto(5,3)

    vehicle.mode = VehicleMode("LOITER")
    print vehicle.mode
    time.sleep(70)

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):
        currentLocation=vehicle.location.global_relative_frame
        targetLocation=get_location_metres(currentLocation, dNorth, dEast)
        targetDistance=get_distance_metres(currentLocation, targetLocation)
        gotoFunction(targetLocation)

        while (vehicle.mode.name=="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"
            break
    time.sleep(2)

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)


Solution

  • 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.