MissionPlanner Python Script cs CurrentState Shows Stale Info

I execute the following code in my MissionPlanner Python script:

MAV.setMode("Guided")
print "Mode " + cs.mode

print "First"
MAV.doCommand(MAVLink.MAV_CMD.DO_SET_ROI, 0, 0, 0, 0, 41.053184,
              -75.511862, 569)
time.sleep(5)
print "Second"
MAV.doCommand(MAVLink.MAV_CMD.DO_SET_ROI, 0, 0, 0, 0, 41.053630,
              -75.515075, 569)
time.sleep(5)
print "Third"
MAV.doCommand(MAVLink.MAV_CMD.DO_SET_ROI, 0, 0, 0, 0, 41.055364,
              -75.513406, 569)
time.sleep(5)
print "Fourth"
MAV.doCommand(MAVLink.MAV_CMD.DO_SET_ROI, 0, 0, 0, 0, 41.057604,
              -75.512762, 569)
time.sleep(5)
MAV.doCommand(MAVLink.MAV_CMD.DO_SET_ROI, 0, 0, 0, 0, 41.059639,
              -75.512434, 569)

Script.ChangeMode("PosHold")
print "Mode " + cs.mode

However, the cs.mode is showing the prior mode.

Windows_10_x64

I’m guessing that the mode change hasn’t actually occurred yet and we have some sort of race condition. What can I while with in order to wait for the mode to actually change. Should I keep checking cs.mode for the desired change to happen, or is there a better way? Perhaps a wait handle of sorts? Thanks!

1 Like

I had the same issue as you, Script.ChangeMode is not working for me. I observed that it does change the “mode” in the sense that the if you were to check your Configuration -> Flight Mode tab, running the code does change the name of the mode. But the PWM does not change correspondingly. I think this is a bug of some sort. Instead, using Script.SendRC(5, your flight mode pwm, True), changes the flight mode AND it locks out control from operator, preventing manual control and only allowing script controls. This is what I really dislike.

To release, you can set pwms to 0, but using Script.ChangeMode does not lock out control instead. I highly suspect that it is due to the PWM listener not being able to listen for 2 different signals at once (1 from your controller, the other from your script). This might also be the reason why the Script.ChangeMode is not working as intended since while the drone is connected to MP, there will also be a RC Controller which constantly pushes a PWM signal to it.

TLDR: Use Script.SendRC(5, PWM Channel, True) instead.

PS: If I’m wrong, please correct me

Script.ChangeMode(...) works just fine; it’s just async. I made a small routine that I use in my script

def changeMode(mode):
    Script.ChangeMode(mode)
    while cs.mode != mode:
        time.sleep(1)
    print "Mode " + cs.mode

Now I’m sure the mode has changed before I proceed. This works just fine.

2 Likes