Shallow depth warning and avoidance using lua

I was looking through the Lua examples and noticed this, would this work for boat depth?, im not sure how rover handles altitude information. if it can then it should be able to work as shallow water avoidance without much modification by just setting loiter or hold if the water gets too shallow along with sending a warning back to the ground station.

Ok that didnโ€™t work. What should I use instead altitude.

AP_Terrain is not available on Rover. So I dont see how that script will work for you without adding AP_Terrain to rover.

Someone could add AP_Terrain rover like sub has then if you had a rangefinder that script could work for you likely.

1 Like

is there a way of getting depth data in a lua script? it should just be a matter of swapping it in.

this looks like it works
local depth_under_boat = rangefinder:distance_cm_orient(25)

I have made this lua script to display a warning if the depth is under 2m but its not running correctly, if someone could take a look at it I would be grateful.Iโ€™m just copy and pasting, Iโ€™m pretty useless at code lol.

ideally, I would like it to display a warning along with the current depth.
depth.lua (369 Bytes)

Did you get any further with this ?
Weโ€™re doing mapping and water quality sampling - its going to get shallow !!
Ideally - if depth < 1m then STOP or Skip to next waypoint or somethingโ€ฆ

Unfortunately not, Iโ€™m not a software developer so my abilities to make this work are limited. all the parts are there.

to switch the boat to loiter whenever it gets shallow it needs to do something like :

get_mode() - Returns current vehicle mode by mode_number
if mode = auto and rangefinder:distance_cm_orient(25) <1m then
set_mode(loiter) - Attempts to change vehicle mode to loiter
gcs:send_text(0, โ€œSHALLOW WATER HALTโ€ (depth))

@rishabsingh3003 @rmackay9 any assistance would be appreciated.

This is where I am at, I canโ€™t get it to run there is an error on line 52,

essentially itโ€™s just a modification of the terrain warning script but modified to work on the downward facing rangefinder. The idea is that if its in auto mode, and it gets shallow it will switch to loiter and send an alert to the ground station, essentially it restricts the minimum water depth it will work in auto mode. I havenโ€™t had a chance to test my boat in the water, so it hasnโ€™t really been a priority for me.

depth avoid.lua (1.6 KB)

https://raw.githubusercontent.com/ArduPilot/ardupilot/master/libraries/AP_Scripting/examples/terrain_warning.lua

At a glance, the following lines 52/53 will probably get the script running:

  if (water_depth < water_min_depth) and (vehicle:get_mode() == 10) then    -- if mode is in auto 
	vehicle:set_mode(5) --set mode to loiter

Iโ€™ve never polled a rangefinder peripheral using onboard Lua, so Iโ€™m not much more help than this.

1 Like

thatโ€™s running now thank you.

depth avoid.lua (1.6 KB)