|
| 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(3,create_final_approach_WP(3,wrap_180(math.deg(yaw)+180),FIN_WP_DIST,FIN_WP_ALT)) |
| 115 | + mission:set_item(4,create_land_WP()) |
| 116 | + mission_loaded = true |
| 117 | + end |
| 118 | + end |
| 119 | + return update, 1000 |
| 120 | +end |
| 121 | + |
| 122 | +gcs:send_text(5,"Loaded TKOFF/RTL/Land Mission'.lua") |
| 123 | +if SCR_USER_EN:get() == 1 then |
| 124 | + if FIN_WP_DIST == 0 or FIN_WP_ALT ==0 then |
| 125 | + gcs:send_text(0, string.format("Must set SCR_USER1 and 2 values!")) |
| 126 | + return |
| 127 | + end |
| 128 | + mission:clear() |
| 129 | + local item = mavlink_mission_item_int_t() |
| 130 | + item:command(NAV_WAYPOINT) |
| 131 | + mission:set_item(0,item) |
| 132 | + mission:set_item(1,create_takeoff_WP(param:get("TKOFF_ALT"))) |
| 133 | + mission:set_item(2,create_do_land_start_WP()) |
| 134 | + return update, 1000 |
| 135 | +else |
| 136 | + gcs:send_text(0, string.format("Script disabled by SCR_USER1")) |
| 137 | +return |
| 138 | +end |
| 139 | + |
0 commit comments