Skip to content

Commit a886d16

Browse files
committed
AP_Scripting: add Plane autoland applet
1 parent 601d9ef commit a886d16

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
--[[ Upon Arming , creates a four item mission consisting of: NAV_TAKEOFF, DO_LAND_START,Final Approach WP opposite bearing from HOME of heading used during takeoff, at TKOFF_ALT or SCR_USER3 above home, SCR_USER2 or 2X TKOFF_DIST, and a LAND waypoint at HOME and stops until next disarm/boot. SCR_USER1 is used to enable or disable it.
2+
--]]
3+
4+
--local variablees
5+
6+
SCR_USER_EN = Parameter("SCR_USER1") -- enable this script
7+
local FIN_WP_DIST = param:get("SCR_USER2") -- distance of final wp from HOME
8+
local FIN_WP_ALT = param:get("SCR_USER3") --- altitude of final wp from HOME
9+
10+
11+
FRAME_GLOBAL = 3
12+
NAV_WAYPOINT = 16
13+
NAV_TAKEOFF = 22
14+
NAV_LAND = 21
15+
DO_LAND_START = 189
16+
17+
TAKEOFF_PITCH = 15
18+
19+
local function wrap_360(angle)
20+
local res = math.fmod(angle, 360.0)
21+
if res < 0 then
22+
res = res + 360.0
23+
end
24+
return res
25+
end
26+
27+
local function wrap_180(angle)
28+
local res = wrap_360(angle)
29+
if res > 180 then
30+
res = res - 360
31+
end
32+
return res
33+
end
34+
35+
function create_final_approach_WP(i,bearing,dist,alt) --index,degs,m,m
36+
local item = mavlink_mission_item_int_t()
37+
local loc = ahrs:get_home()
38+
loc:offset_bearing(bearing,dist) ---degs and meters
39+
40+
item:seq(i)
41+
item:frame(FRAME_GLOBAL)
42+
item:command(NAV_WAYPOINT)
43+
item:param1(0)
44+
item:param2(0)
45+
item:param3(0)
46+
item:param4(0)
47+
item:x(loc:lat())
48+
item:y(loc:lng())
49+
item:z(alt)
50+
return item
51+
end
52+
53+
function create_takeoff_WP(alt)
54+
local item = mavlink_mission_item_int_t()
55+
local loc = ahrs:get_home()
56+
57+
item:seq(1)
58+
item:frame(FRAME_GLOBAL)
59+
item:command(NAV_TAKEOFF)
60+
item:param1(TAKEOFF_PITCH)
61+
item:param2(0)
62+
item:param3(0)
63+
item:param4(0)
64+
item:x(loc:lat())
65+
item:y(loc:lng())
66+
item:z(alt)
67+
return item
68+
end
69+
70+
function create_land_WP()
71+
local item = mavlink_mission_item_int_t()
72+
local loc = ahrs:get_home()
73+
74+
item:seq(4)
75+
item:frame(FRAME_GLOBAL)
76+
item:command(NAV_LAND)
77+
item:param1(15)
78+
item:param2(0)
79+
item:param3(0)
80+
item:param4(0)
81+
item:x(loc:lat())
82+
item:y(loc:lng())
83+
item:z(0)
84+
return item
85+
end
86+
87+
function create_do_land_start_WP()
88+
local item = mavlink_mission_item_int_t()
89+
90+
item:seq(2)
91+
item:frame(FRAME_GLOBAL)
92+
item:command(DO_LAND_START)
93+
item:param1(0)
94+
item:param2(0)
95+
item:param3(0)
96+
item:param4(0)
97+
item:x(0)
98+
item:y(0)
99+
item:z(0)
100+
return item
101+
end
102+
103+
function update()
104+
if not arming:is_armed() then --if disarmed, wait until armed
105+
mission_loaded = false
106+
return update,1000
107+
end
108+
109+
if not mission_loaded then --if first time after arm and enabled is valid then create mission
110+
local home = ahrs:get_home()
111+
local location = ahrs:get_location()
112+
if location and home and location:alt() > (home:alt() + param:get("TKOFF_LVL_ALT")*100) then
113+
local yaw = ahrs:get_yaw()
114+
mission:set_item(2,create_do_land_start_WP())
115+
mission:set_item(3,create_final_approach_WP(3,wrap_180(math.deg(yaw)+180),FIN_WP_DIST,FIN_WP_ALT))
116+
mission:set_item(4,create_land_WP())
117+
mission_loaded = true
118+
end
119+
end
120+
return update, 1000
121+
end
122+
123+
gcs:send_text(5,"Loaded TKOFF/RTL/Land Mission'.lua")
124+
if SCR_USER_EN:get() == 1 then
125+
if FIN_WP_DIST == 0 or FIN_WP_ALT ==0 then
126+
gcs:send_text(0, string.format("Must set SCR_USER1 and 2 values!"))
127+
return
128+
end
129+
mission:clear()
130+
local item = mavlink_mission_item_int_t()
131+
item:command(NAV_WAYPOINT)
132+
mission:set_item(0,item)
133+
mission:set_item(1,create_takeoff_WP(param:get("TKOFF_ALT")))
134+
return update, 1000
135+
else
136+
gcs:send_text(0, string.format("Script disabled by SCR_USER1"))
137+
return
138+
end
139+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This script is intended to allow easy, unpre-planned operation at any location with the protection of a do-land-start autoland sequence for failsafes that accounts for takeoff direction (ie wind direction). Final approach objects must be considered before you launch.
2+
3+
If enabled by SCR_USER1 =1, setups up an autotakeoff waypoint as first waypoint and upon Arming , adds mission items consisting of: DO_LAND_START,Final Approach WP opposite bearing from HOME of heading used during takeoff, to SCR_USER3 above home, and at SCR_USER2 distancee, and a LAND waypoint at HOME and stops until next disarm/boot. Warnings are given if the SCR_USER values are not set.
4+
5+
In use you just arm and switch to AUTO, and then switch to other flight modes after takeoff is completed to fly around.....relatively assured that a failsafe (assuming defaults for FS) will result in an autolanding in the correct direction. You can also just switch back to AUTO or RTL to do an autoland.
6+
7+
Caveats: be sure you have tested and setup autoland and SCR_USER1/2 parameters. Setting SCR_USER2 and 3 for a good glide on a final approach is required (be aware of possible obstructions when using). RTL_AUTOLAND =2 is recommended also.

0 commit comments

Comments
 (0)