How do I go on to script a function that can save a position and return to that position if distance between current position and to the saved position exceeds more than x meters?
You could save that position as home and set a circular radius fence that triggers an RTL at the distance you want.
Can be done with lua, take a look at lua examples in the github repo. In particular the following script could be a good starting point.
function update () – periodic function that will be called
local current_pos = ahrs:get_position() – fetch the current position of the vehicle
local home = ahrs:get_home() – fetch the home position of the vehicle
if current_pos and home then – check that both a vehicle location, and home location are available
local distance = current_pos:get_distance(home) – calculate the distance from home in meters
if distance > 1000 then – if more then 1000 meters away
distance = 1000; – clamp the distance to 1000 meters
servo.set_output_pwm(96, 1000 + distance) – set the servo assigned function 96 (scripting3) to a proportional value
return update, 1000 – request “update” to be rerun again 1000 milliseconds (1 second) from now
return update, 1000 – request “update” to be the first time 1000 milliseconds (1 second) after script is loaded
The script changes a servo output based on distance from home. You need to change home position with a position you can store with a flick of the switch and the servo output with a command to fly to the stored position.
flick switch—>store current position
monitor distance current position - stored position
if current position - stored position > your set max distance command to fly to stored position.
The other advice could be quite tricky if the position you want is different from homeposition and a failsafe rtl is fired and your machine goes to that point and never comes back. Probably some advices should be more articulated or not given at all.
thanks for the awesome information.