You can use the UNTIL command. For example:
do your flight code here
UNTIL (VAR > X) [var = home distance, distance for example 1000m]
FLY_TO_RELATIVE(0,0,100)
Until the distance of 1000m is reached you can do whatever you want, if home distance gets larger then 1km it flies back to your home position. Or something similiar to this. Works with battery voltage as well.
do your flight code here
UNTIL (VAR < X) [var = battery voltage, x = 10]
FLY_TO_RELATIVE(0,0,100)
As long as battery has more then 10V do whatever you want, after that set the FLY_TO_RELATIVE(0,0,100). Then it returns to home if battery voltage is under or same as 10V.
There is an example in the wiki:
1 CLIMB(35m)
2 CIRCLE_RELATIVE(lon: 0m, lat: 0m, radius: 100m, height: 150m)
3 UNTIL(Height(m) > 145)
4 FLY_TO_REL(lon: -150m, lat: 150m, height: 145m)
5 FROM_TO_REL(lon: -150m, lat: 150m, height: 145m)
6 FROM_TO_REL(lon: 150m, lat: 150m, height: 145m)
7 FROM_TO_REL(lon: 150m, lat: -150m, height: 145m)
8 FROM_TO_REL(lon: -150m, lat: -150m, height: 145m)
9 GOTO(5)
Let me explain it for you:
1. the plane will get some altitude until it reaches 35m
2. the plane will come back and flies circles above your head in a 100m radius and the height of 150m
3. it will fly circles and circles until height is 145m
for 4-8 have a look at the picture:
The numbers on the lines are the numbers of the code. This one will fly to the 4 points forever until it crashes because battery is empty