Update mission planner GUI in LUA script

for context I’m writing a program in LUA script to monitor the distance read from a forward facing range finder on a copter-plane.

I am writing my code in LUA script
I am using mission planner for the GUI
I am using the pixhawk cube

The objective is that if the range finder detects a distance less than some minimum safe distance it will change from auto flight mode to Qloiter flight mode, (from what I’ve been told this will basically stop the plane). I wrote a quick script to do this and to read the changing flight modes. In the first image below you can see the flight mode values for all 6 flight modes as well as the range finder value (835). In the second image you can see that the distance it’s reading has passed the designated threshold, that being 100cm, and it’s set all the flight mode values to 19, which is QLoiter. But you can also see in that same image that the flight mode that the GUI is displaying is still reading RTL not Qloiter, even though all flight modes have been set to Qloiter.

It should be noted that the GUI does change when I toggle the flight mode switchs on my radio controller. See below image

In this image we can see that the GUI is now accuratly reflecting the flight mode values for the flight modes.

My objective would be to force the GUI to update based on the newly updated parameters, so when I set a parameter in LUA I would like that to be reflected in the mission planner GUI.

My concern is that it not indicating in the GUI may imply that the system responsible for governing the flight mode in the pixhawk cube that I’m using is not recognizing the parameter change unless there’s some force update signal that I need to provide.

Code I’ve written

This code performs a sensor check and changes the flight mode parameters accordingly

local address = 102
local com = 0x00
local safeAlt = 100

local i2c_bus = i2c:get_device(0,0)
i2c_bus:set_retries(10)


function update()

  i2c_bus:set_address(address)

  returnTable = i2c_bus:read_registers(0, 2)
  distance = returnTable[1]*256 + returnTable[2]

  gcs:send_text(0, "Distance: ****" .. distance)
  if(distance < 100) then
    param:set('FLTMODE1', 19)
    param:set('FLTMODE2', 19)
    param:set('FLTMODE3', 19)
    param:set('FLTMODE4', 19)
    param:set('FLTMODE5', 19)
    param:set('FLTMODE6', 19)


  else
    param:set('FLTMODE1', 19)
    param:set('FLTMODE2', 18)
    param:set('FLTMODE3', 10)
    param:set('FLTMODE4', 6)
    param:set('FLTMODE5', 8)
    param:set('FLTMODE6', 10)
    
  end
   
  return update, 100
end

return update() 

This code just printed the flight mode values to the console

function update() -- this is the loop which periodically runs

  gcs:send_text(0, "Current Flight mode1: *****" .. param:get('FLTMODE1'))
  gcs:send_text(0, "Current Flight mode2: *****" .. param:get('FLTMODE2'))
  gcs:send_text(0, "Current Flight mode3: *****" .. param:get('FLTMODE3'))
  gcs:send_text(0, "Current Flight mode4: *****" .. param:get('FLTMODE4'))
  gcs:send_text(0, "Current Flight mode5: *****" .. param:get('FLTMODE5'))
  gcs:send_text(0, "Current Flight mode6: *****" .. param:get('FLTMODE6'))

  return update, 1000
end

return update()

Thank you for any answers.

Overwriting every flight mode parameter is a strange way of doing vehicle:set_mode(19) …which would probably solve all of your problems.

1 Like

This example may prove useful:

Thank you for your response

I tried vehicle:set_mode and I do agree it’s better but the only this is that when I break out of the condition I want some way to go back to what it was before the condition set.

So if it was in rtl or Qhover mode before, then the condition is set and it goes to QLoiter, I would like it to go back to rtl or Qhover what ever it was before.

The example is geared toward those who are interested in crafting failsafe logic based on GPS fix states, but it contains similar logic as to what you’re attempting here (conditionally changing modes).

Before changing modes in your script, save the current mode using vehicle:get_mode() so that it can be resumed later.

1 Like