forked from tuuzdu/lua_pioneer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathTest.lua
160 lines (122 loc) · 2.94 KB
/
pathTest.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
local function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
Ev = {ALTITUDE_REACHED = 61, POINT_REACHED = 21, COPTER_LANDED = 23}
local Path = {}
function Path.new()
local obj = { point = { [0] = {} }, state = 0 }
Path.__index = Path
return setmetatable( obj, Path )
end
function Path:addWaypoint( _x, _y, _z )
local point = {x = _x * 1000, y = _y * 1000, z = _z * 1000, waypoint = true}
table.insert( self.point, point )
end
function Path:addTakeoff()
table.insert( self.point, { takeoff = true } )
end
function Path:addLanding()
table.insert( self.point, { landing = true } )
end
function Path:addFuncForPoint( _func, point_index )
if self.point[point_index] then
self.point[point_index].func = _func
end
end
function Path:addFunc( _func )
self.point[#self.point].func = _func
end
function Path:Start()
--[[if self.point[1].takeoff then
ap.push(Ev.MCE_PREFLIGHT)
sleep(1)
ap.push(Ev.MCE_TAKEOFF)
end]]--
self.state = 7 -- should be replaced into the block above!!!
end
function Path:eventHandler( e )
local change_state = false
local obj_state = self.point[self.state]
if e == Ev.ALTITUDE_REACHED and obj_state.takeoff then
change_state = true
print("TAKEOFF")
elseif e == Ev.POINT_REACHED and obj_state.waypoint then
change_state = true
print("WAYPOINT")
elseif e == Ev.POINT_REACHED and obj_state.landing then
change_state = true
print("LANDING")
elseif e == Ev.COPTER_LANDED and ( obj_state.landing or obj_state.takeoff ) then
change_state = true
print("LANDED")
end
if change_state then
if obj_state.func then
obj_state.func()
end
if self.state < #self.point then
self.state = self.state + 1
else
self.state = 0
end
if obj_state.waypoint then
--ap.goToLocalPoint( {x = self.point[self.state].x, y = self.point[self.state].y, z = self.point[self.state].z} )
elseif obj_state.takeoff then
--ap.push(Ev.MCE_PREFLIGHT)
--sleep(1)
--ap.push(Ev.MCE_TAKEOFF)
elseif obj_state.landing then
--ap.push(Ev.MCE_LANDING)
end
end
end
function callback( event )
pn:eventHandler(event)
end
function loop()
end
-- ###### ^ Module above ^ ######
--[[
local action = {
["ACTION_1"] = function()
-- Function block
end
}
p:addFunc(action["ACTION_1"])
]]--
function func_1 ()
local f = "Test function"
print(f)
end
--p = Path.new()
pn = Path.new()
pn:addTakeoff()
pn:addWaypoint(1, 2, 1.2)
pn:addWaypoint(2, 2, 1.2)
pn:addFunc(
function()
local f = "Test function 2"
print(f)
end
)
pn:addWaypoint(2, 1, 1.2)
pn:addLanding()
pn:addTakeoff()
pn:addWaypoint(2, 2, 1.2)
pn:addLanding()
pn:addFuncForPoint(func_1, 8)
pn:Start()
pn:eventHandler(Ev.POINT_REACHED)
print("State:" .. pn.state)
print(dump(pn))
--print(p.point[2].x)
--print(pn.point[2].takeoff)