Skip to content

Commit d707b76

Browse files
authored
Update MissionRotation.lua
- for each loaded mission it also shows the number of events it contains - cosmetic
1 parent 7c8f1f5 commit d707b76

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

libraries/AP_Scripting/applets/MissionRotation.lua

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
-- Reset the mission0.txt if AUX is high for more than 3 seconds
44
-- Prevents mission change if the vehicle is in AUTO mode
55
-- Prevents the script from loading if the vehicle is in AUTO
6+
-- For each loaded mission it also shows the number of events it contains
67

78
local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7}
89
local rc_switch = rc:find_channel_for_option(24)
910

1011
if not rc_switch then
11-
gcs:send_text(MAV_SEVERITY.ERROR, "Mission Reset switch not assigned")
12+
gcs:send_text(MAV_SEVERITY.ERROR, "Mission Reset switch not right assigned")
1213
return
1314
end
1415

16+
local vehicle_type
1517
local vehicle_fw_type = FWVersion:type()
1618

17-
local vehicle_type
18-
if vehicle_fw_type == 1 then
19-
vehicle_type = "Rover"
20-
elseif vehicle_fw_type == 2 then
21-
vehicle_type = "Copter"
22-
elseif vehicle_fw_type == 3 then
19+
if vehicle_fw_type == 3 then
2320
vehicle_type = "Plane"
21+
elseif vehicle_fw_type == 2 then
22+
vehicle_type = "Rover"
2423
elseif vehicle_fw_type == 4 then
25-
vehicle_type = "Tracker"
24+
vehicle_type = "Copter"
2625
elseif vehicle_fw_type == 5 then
2726
vehicle_type = "Submarine"
27+
elseif vehicle_fw_type == 10 then
28+
vehicle_type = "Tracker"
2829
else
2930
gcs:send_text(MAV_SEVERITY.ERROR, "Unrecognized vehicle type!")
3031
return
@@ -41,8 +42,18 @@ elseif vehicle_type == "Rover" then
4142
mode_auto = 10
4243
elseif vehicle_type == "Submarine" then
4344
mode_auto = 10
45+
elseif vehicle_type == "Helicopter" then
46+
mode_auto = 3
4447
elseif vehicle_type == "Tracker" then
4548
mode_auto = 10
49+
elseif vehicle_type == "Sailboat" then
50+
mode_auto = 3
51+
elseif vehicle_type == "Car" then
52+
mode_auto = 10
53+
elseif vehicle_type == "Boat" then
54+
mode_auto = 3
55+
elseif vehicle_type == "VTOL" then
56+
mode_auto = 10
4657
else
4758
return
4859
end
@@ -74,39 +85,45 @@ local function read_mission(file_name)
7485

7586
local item = mavlink_mission_item_int_t()
7687
local index = 0
88+
local last_event = nil
7789

7890
while true do
91+
local line = file:read('l')
92+
if not line then break end
93+
if string.match(line, '^%s*$') == nil then
94+
last_event = line
95+
end
7996
local data = {}
80-
for i = 1, 12 do
81-
data[i] = file:read('n')
82-
if data[i] == nil then
83-
if i == 1 then
84-
file:close()
85-
return true
86-
else
87-
mission:clear()
88-
error('Failed to read file: premature end of data')
89-
end
97+
for number in string.gmatch(line, "[^%s]+") do
98+
table.insert(data, tonumber(number))
99+
end
100+
if #data >= 12 then
101+
item:seq(data[1])
102+
item:frame(data[3])
103+
item:command(data[4])
104+
item:param1(data[5])
105+
item:param2(data[6])
106+
item:param3(data[7])
107+
item:param4(data[8])
108+
item:x(data[9] * 10^7)
109+
item:y(data[10] * 10^7)
110+
item:z(data[11])
111+
if not mission:set_item(index, item) then
112+
mission:clear()
113+
error(string.format('Failed to set mission item %i', index))
90114
end
115+
index = index + 1
91116
end
117+
end
118+
file:close()
92119

93-
item:seq(data[1])
94-
item:frame(data[3])
95-
item:command(data[4])
96-
item:param1(data[5])
97-
item:param2(data[6])
98-
item:param3(data[7])
99-
item:param4(data[8])
100-
item:x(data[9] * 10^7)
101-
item:y(data[10] * 10^7)
102-
item:z(data[11])
103-
104-
if not mission:set_item(index, item) then
105-
mission:clear()
106-
error(string.format('Failed to set mission item %i', index))
107-
end
108-
index = index + 1
120+
if last_event then
121+
local first_number = tonumber(string.match(last_event, "^%d+"))
122+
gcs:send_text(MAV_SEVERITY.WARNING, "Loaded " .. file_name .. " with " .. tostring(first_number) .. " events")
123+
else
124+
gcs:send_text(MAV_SEVERITY.WARNING, "Loaded " .. file_name .. " with 0 events")
109125
end
126+
return true
110127
end
111128

112129
if not read_mission("mission0.txt") then
@@ -128,7 +145,6 @@ local function load_next_mission()
128145
if file then
129146
file:close()
130147
if read_mission(file_name) then
131-
gcs:send_text(MAV_SEVERITY.WARNING, "Loaded mission: " .. file_name)
132148
return
133149
end
134150
end
@@ -163,5 +179,4 @@ function update()
163179
end
164180

165181
gcs:send_text(MAV_SEVERITY.NOTICE, "Mission Rotation loaded")
166-
gcs:send_text(MAV_SEVERITY.NOTICE, "Loaded default mission: mission0.txt")
167182
return update, 1000

0 commit comments

Comments
 (0)