2 trapezoidal velocity trajectory
7 #include "a/trajtrap.h"
10 constructor for trapezoidal velocity trajectory
11 @treturn a.trajtrap trapezoidal velocity trajectory userdata
14 int liba_trajtrap_new(lua_State
*L
)
16 a_trajtrap
*const ctx
= lua_newclass(L
, a_trajtrap
);
17 a_zero(ctx
, sizeof(a_trajtrap
));
18 lua_registry_get(L
, liba_trajtrap_new
);
19 lua_setmetatable(L
, -2);
24 generate for trapezoidal velocity trajectory
25 @tparam a.trajtrap ctx trapezoidal velocity trajectory userdata
26 @tparam number vm defines the maximum velocity during system operation
27 @tparam number ac defines the acceleration before constant velocity
28 @tparam number de defines the acceleration after constant velocity
29 @tparam number p0 defines the initial position
30 @tparam number p1 defines the final position
31 @tparam[opt] number v0 defines the initial velocity
32 @tparam[opt] number v1 defines the final velocity
33 @treturn number total duration
36 int liba_trajtrap_gen(lua_State
*L
)
38 a_float v0
= 0, v1
= 0;
39 a_float vm
, ac
, de
, p0
, p1
;
40 int const top
= lua_gettop(L
);
41 a_trajtrap
*const ctx
= (a_trajtrap
*)lua_touserdata(L
, 1);
44 vm
= (a_float
)luaL_checknumber(L
, 2);
45 ac
= (a_float
)luaL_checknumber(L
, 3);
46 de
= (a_float
)luaL_checknumber(L
, 4);
47 p0
= (a_float
)luaL_checknumber(L
, 5);
48 p1
= (a_float
)luaL_checknumber(L
, 6);
49 if (top
>= 7) { v0
= (a_float
)luaL_checknumber(L
, 7); }
50 if (top
>= 8) { v1
= (a_float
)luaL_checknumber(L
, 8); }
51 a_trajtrap_gen(ctx
, vm
, ac
, de
, p0
, p1
, v0
, v1
);
52 lua_pushnumber(L
, (lua_Number
)ctx
->t
);
59 calculate position for trapezoidal velocity trajectory
60 @tparam a.trajtrap ctx trapezoidal velocity trajectory userdata
61 @tparam number x difference between current time and initial time
62 @treturn number position output
65 int liba_trajtrap_pos(lua_State
*L
)
67 a_trajtrap
const *const ctx
= (a_trajtrap
const *)lua_touserdata(L
, 1);
70 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
71 lua_pushnumber(L
, (lua_Number
)a_trajtrap_pos(ctx
, x
));
78 calculate velocity for trapezoidal velocity trajectory
79 @tparam a.trajtrap ctx trapezoidal velocity trajectory userdata
80 @tparam number x difference between current time and initial time
81 @treturn number velocity output
84 int liba_trajtrap_vel(lua_State
*L
)
86 a_trajtrap
const *const ctx
= (a_trajtrap
const *)lua_touserdata(L
, 1);
89 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
90 lua_pushnumber(L
, (lua_Number
)a_trajtrap_vel(ctx
, x
));
97 calculate acceleration for trapezoidal velocity trajectory
98 @tparam a.trajtrap ctx trapezoidal velocity trajectory userdata
99 @tparam number x difference between current time and initial time
100 @treturn number acceleration output
103 int liba_trajtrap_acc(lua_State
*L
)
105 a_trajtrap
const *const ctx
= (a_trajtrap
const *)lua_touserdata(L
, 1);
108 a_float
const x
= (a_float
)luaL_checknumber(L
, 2);
109 lua_pushnumber(L
, (lua_Number
)a_trajtrap_acc(ctx
, x
));
115 static int liba_trajtrap_set(lua_State
*L
)
117 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
119 case 0xE8859EEB: // __name
120 case 0xA65758B2: // __index
121 case 0xAEB551C6: // __newindex
124 lua_getmetatable(L
, 1);
131 static int liba_trajtrap_get(lua_State
*L
)
133 a_trajtrap
const *const ctx
= (a_trajtrap
const *)lua_touserdata(L
, 1);
134 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
136 case 0x00000074: // t
137 lua_pushnumber(L
, (lua_Number
)ctx
->t
);
139 case 0x00003980: // p0
140 lua_pushnumber(L
, (lua_Number
)ctx
->p0
);
142 case 0x00003981: // p1
143 lua_pushnumber(L
, (lua_Number
)ctx
->p1
);
145 case 0x00003C92: // v0
146 lua_pushnumber(L
, (lua_Number
)ctx
->v0
);
148 case 0x00003C93: // v1
149 lua_pushnumber(L
, (lua_Number
)ctx
->v1
);
151 case 0x00003CC5: // vc
152 lua_pushnumber(L
, (lua_Number
)ctx
->vc
);
154 case 0x00003BBD: // ta
155 lua_pushnumber(L
, (lua_Number
)ctx
->ta
);
157 case 0x00003BC0: // td
158 lua_pushnumber(L
, (lua_Number
)ctx
->td
);
160 case 0x000039B1: // pa
161 lua_pushnumber(L
, (lua_Number
)ctx
->pa
);
163 case 0x000039B4: // pd
164 lua_pushnumber(L
, (lua_Number
)ctx
->pd
);
166 case 0x00003206: // ac
167 lua_pushnumber(L
, (lua_Number
)ctx
->ac
);
169 case 0x00003391: // de
170 lua_pushnumber(L
, (lua_Number
)ctx
->de
);
172 case 0xA65758B2: // __index
173 lua_registry_get(L
, liba_trajtrap_new
);
174 lua_num_set(L
, -1, "t", ctx
->t
);
175 lua_num_set(L
, -1, "p0", ctx
->p0
);
176 lua_num_set(L
, -1, "p1", ctx
->p1
);
177 lua_num_set(L
, -1, "v0", ctx
->v0
);
178 lua_num_set(L
, -1, "v1", ctx
->v1
);
179 lua_num_set(L
, -1, "vc", ctx
->vc
);
180 lua_num_set(L
, -1, "ta", ctx
->ta
);
181 lua_num_set(L
, -1, "td", ctx
->td
);
182 lua_num_set(L
, -1, "pa", ctx
->pa
);
183 lua_num_set(L
, -1, "pd", ctx
->pd
);
184 lua_num_set(L
, -1, "ac", ctx
->ac
);
185 lua_num_set(L
, -1, "de", ctx
->de
);
188 lua_getmetatable(L
, 1);
195 static int liba_trajtrap_(lua_State
*L
)
197 lua_pushcfunction(L
, liba_trajtrap_new
);
199 lua_call(L
, lua_gettop(L
) - 1, 1);
203 int luaopen_liba_trajtrap(lua_State
*L
)
205 static lua_fun
const funcs
[] = {
206 {"new", liba_trajtrap_new
},
207 {"gen", liba_trajtrap_gen
},
208 {"pos", liba_trajtrap_pos
},
209 {"vel", liba_trajtrap_vel
},
210 {"acc", liba_trajtrap_acc
},
212 lua_createtable(L
, 0, A_LEN(funcs
));
213 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
214 lua_createtable(L
, 0, 1);
215 lua_fun_set(L
, -1, "__call", liba_trajtrap_
);
216 lua_setmetatable(L
, -2);
218 static lua_fun
const metas
[] = {
219 {"__newindex", liba_trajtrap_set
},
220 {"__index", liba_trajtrap_get
},
222 lua_createtable(L
, 0, A_LEN(metas
) + A_LEN(funcs
) + 1);
223 lua_fun_reg(L
, -1, metas
, A_LEN(metas
));
224 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
225 lua_str_set(L
, -1, "__name", "a.trajtrap");
226 lua_registry_set(L
, liba_trajtrap_new
);