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

1.3k views Asked by At

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)

3

There are 3 answers

0
squilter On BEST ANSWER
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.

0
epinal On

The best way to know when the mode really changes it is having an 'observer' (attribute listener). You can handle events in the 'vehicle' setting a callback. So just add an observer to 'mode' attribute, this way you know when the mode is actually changed. Something like this:

class Solo(Vehicle):
"""
Solo class that inherit from dronekit.Vehicle
"""

def __init__(self, *args):
    super(Solo, self).__init__(*args)      

    # Observers
    self.add_attribute_listener('mode', self.mode_callback)   

def mode_callback(self, *args):
    # Do whatever you need when the mode changed here
    Printer.message("MODE changed to %s" % self.mode.name)
0
Poojita Sandrakumar On

it has to do with mavlink not recognizing a rc in so try to input rc 3 1500 in mavlink once it shows the mode in stabilize from sitl. Then it will work you have a rc failsafe present, which would go away if you input the values.