forked from tuuzdu/lua_pioneer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc4d_animation_v3.lua
208 lines (182 loc) · 5.15 KB
/
c4d_animation_v3.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
local Animation = {}
function Animation.new(points_str, colors_str)
local tblUnpack = table.unpack
local strUnpack = string.unpack
local state = {stop = 0, idle = 1, flight = 2, landed = 3}
local function getGlobalTime()
return time() + deltaTime()
end
local Color = {}
Color.colors_str_size = string.packsize(str_format)
Color.colors_str = points_str -- TODO: colors string
Color.first_led = 0
Color.last_led = 28
Color.leds = Ledbar.new(29)
-- кастомное преобразование hsv
-- линейная интерполяция (некоторые зарегистрированные значения сознательно опущены)
-- {pioneer_base_hue, c4d_hue}
Color.hue_correction_table = {
{0, 0},
{1, 15},
{2, 20},
{10, 30},
{15, 40},
{20, 45},
{30, 55},
{40, 70},
{60, 90},
{120, 120},
{140, 155},
{160, 185},
{210, 210},
{240, 240},
{250, 260},
{270, 280},
{290, 295},
{300, 300},
{340, 320},
{355, 335},
{357, 345},
{358, 355},
{360, 360}
}
-- {pioneer_base_saturation, c4d_saturation}
Color.saturation_correction_table = {
{0, 0},
{50, 10},
{70, 20},
{80, 40},
{85, 70},
{100, 100}
}
-- {pioneer_base_value, c4d_value}
Color.value_correction_table = {
{0, 0},
{1, 60},
{3, 80},
{10, 100},
{100, 255}
}
function Color.correct(p, correct_table)
local i = 1
local pmin = 0
local pmax = 0
while i < #correct_table do
pmin = correct_table[i][2]
pmax = correct_table[i + 1][2]
if (p >= pmin and p <= pmax) then
break
end
i = i + 1
end
local pnewmin = correct_table[i][1]
local pnewmax = correct_table[i+1][1]
local pnew = (pnewmin + (pnewmax - pnewmin)*(p - pmin)/(pmax - pmin))
return pnew
end
function Color.correctHue(h)
-- корректируем hue в соответствии с таблицей
return Color.correct(h, Color.hue_correction_table)
end
function Color.correctSaturation(s)
-- корректируем saturation в соответствии с таблицей
return Color.correct(s, Color.saturation_correction_table)
end
function Color.correctValue(v)
-- корректируем value в соответствии с таблицей
return Color.correct(v, Color.value_correction_table)
end
function Color.getColor(index)
local t, _, _, _, h, s, v, _ = strUnpack(str_format, Color.colors_str, 1 + (index - 1) * Color.colors_str_size)
h = Color.correctHue(h)
s = Color.correctSaturation(s)
v = Color.correctValue(v)
local r, g, b = fromHSV(h, s, v)
t = t / 1000
return t, r, g, b
end
function Color.setMatrix(r, g, b)
for i = Color.first_led, Color.last_led, 1 do
Color.leds:set(i, r, g, b)
end
end
local Point = {}
Point.points_str_size = string.packsize(str_format)
Point.points_str = points_str
Point.setPoint = ap.goToLocalPoint
function Point.getPoint(index)
local t, x, y, z = strUnpack(str_format, Point.points_str, 1 + (index - 1) * Point.points_str_size)
t = t / 1000
x = x / 100
y = y / 100
z = z / 100
return t, x, y, z
end
local Config = {}
Config.t_after_prepare = 2
Config.t_after_takeoff = 1
Config.init_index = 1
Config.last_index = points_count
local obj = {}
obj.state = state.stop
obj.global_time_0 = 0
obj.t_init = 0
function obj.setConfig(init_index, last_index, time_after_prepare, time_after_takeoff)
Config.init_index = init_index or 1
Config.last_index = last_index or points_count
Config.t_after_prepare = time_after_prepare or 2
Config.t_after_takeoff = time_after_takeoff or 1
end
function obj:eventHandler(e)
if self.state ~= state.stop then
if e == Ev.SYNC_START then
self.global_time_0 = getGlobalTime() + Config.t_after_prepare + Config.t_after_takeoff
self:animInit()
end
end
end
function obj:animInit()
self.state = state.flight
ap.push(Ev.MCE_PREFLIGHT)
sleep(Config.t_after_prepare)
ap.push(Ev.MCE_TAKEOFF) -- Takeoff altitude should be set by AP parameter
self.t_init = Point.getPoint(Config.init_index)
Timer.callAtGlobal(self.global_time_0, function () self:animLoop(Config.init_index) end)
end
function obj:animLoop(point_index)
if self.state == state.flight and point_index < Config.last_index then
local _, x, y, z = Point.getPoint(point_index)
local _, r, g, b = Color.getColor(point_index)
local t = Point.getPoint(point_index + 1)
Color.setMatrix(r, g, b)
Point.setPoint(x, y, z)
Timer.callAtGlobal(self.global_time_0 + t - self.t_init, function () self:animLoop(point_index + 1) end)
else
local t = Point.getPoint(point_index)
local delay = 1
Timer.callAtGlobal(self.global_time_0 + t + delay - self.t_init, function () self:landing() end)
end
end
function obj:landing()
Color.setMatrix(0, 0, 0)
self.state = state.landing
ap.push(Ev.MCE_LANDING)
end
function obj:spin()
self.state = state.idle
end
function obj:start()
self.state = state.idle
self:eventHandler(Ev.SYNC_START)
end
Animation.__index = Animation
return setmetatable(obj, Animation)
end
function callback(event)
anim:eventHandler(event)
end
anim = Animation.new(points, _)
anim.setConfig(1)
anim:spin()
-- print (dump(anim))
callback(Ev.SYNC_START)