Automatically switch between GPS and optical flow

Yes there are some similar thread but none of them concluded something,
So please help me to automatically switch between GPS and optical flow

That is not supported yet. You will need to add code yourself to get it to run.
That is why there is no thread explaining how to do it.

As @amilcarlucas said. Use this as a reference and rev the code for the sensor you have.
Auto Switch lua

This is supported in Copter-4.2 though which will start beta testing in about a month. If you’re happy to using 4.2.0-DEV (aka “latest”) then this script handles switching between GPS and optical flow.

In a more general sense, switching between GPS and optical flow works in 4.1 but the optical flow variances weren’t available so automatically making the decision was difficult.

I configured Optical Flow Sensor(Matek Optical FLow & Lidar Sensor 3901-L0X) with help of

Then i installed lua script ahrs-source.lua and it started giving below error while arming

Log file is


I think maybe the wrong lua script has been put on the vehicle. It’s not ahrs-source.lua but rather ahrs-source-gps-optflow.lua that should be used.

1 Like

ohhh, thanks I could not find separate help document for gps and optflow, so I followed gps and non gps from those shared links and found “ahrs-source.lua”.
I shall try with “source-gps-optflow.lua”

1 Like

Even after changing script I am not able to Arm, getting error

Complete log


I’m pretty sure you probably haven’t set the EK3_SRCx parameters as specified at the top of the script.

Really sorry for silly mistake, below settings caused that pre arm error.
EK3_SRC2_POSXY = 3 (GPS) Correct value as mentioned in script is 0 (None)
EK3_SRC2_VELXY = 6(ExternalNav) Correct value as mentioned in script is 5 (OpticalFlow)
EK3_SRC2_VELZ = 3 (GPS) Correct value as mentioned in script is 0 (None)

With correcting above values, I could Arm vehicle and fly it in stabilize mode, then i switched to Altitude Hold and then Loiter, as soon as i switched to Loiter it starter climbing so I switched back to Stabilize mode and all motors stopped and it crashed, below is complete log of it

It does a battery failsafe and RTL then it’s trying to climb to the RTL altitude - you had already reduced your throttle to zero since you didnt realise there was a failsafe and RTL, when you changed modes to Stabilise it did exactly what it was told → dropped like a stone.

I am an advocate for a spring-centered throttle for multirotors and set PILOT_THR_BHV,7
Also set up yaapu telemetry with a suitable transmitter and reciever, or a normal telemetry radio and groundstation so you can be aware of these unexpected changes during flight.

I suggest you set BATT_FS_CRT_ACT = 1 instead of 2 since by the time you reach critical battery voltage there is probably no chance of doing an RTL unless you are already over home.
And FS_OPTIONS,8 to continue landing if another failsafe occurs, don’t abort the landing and start over. You didn’t have this issue, but it’s nice to have that set.

1 Like

The dreaded RRTE Mode. Rapid Return To Earth…