Skip to content

Commit 28bbf9f

Browse files
committed
Moving the Constructors to reformat the page layout
1 parent 6fb03d8 commit 28bbf9f

3 files changed

Lines changed: 318 additions & 320 deletions

File tree

datapack/data/advanced_math/modules/pid.lua

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,53 @@
1010
local expect = require "cc.expect"
1111
local expect = expect.expect
1212

13+
--- Constructors
14+
--
15+
-- @section Constructors
16+
17+
--- Constructs a new PID controller for either a scalar, vector, or quaternion target.
18+
--
19+
-- @tparam number|vector|quaternion target The setpoint to reach
20+
-- @tparam number p Proportional gain - how aggressively to respond to the current error
21+
-- @tparam number i Integral gain - how aggressively to eliminate accumulated error
22+
-- @tparam number d Derivative gain - how aggressively to dampen the rate of change
23+
-- @tparam boolean discrete Whether to treat the PID as discrete or continuous
24+
-- @treturn The PID initialized with the given arguments
25+
-- @usage pid = pid.new(target)
26+
-- @export
27+
-- @see quaternion
28+
function new(target, p, i, d, discrete)
29+
expect(1, target, "number", "Vector", "Quaternion")
30+
expect(2, p, "number", "nil")
31+
expect(3, i, "number", "nil")
32+
expect(4, d, "number", "nil")
33+
expect(5, discrete, "boolean", "nil")
34+
35+
local controller = {
36+
sp = target or 1,
37+
kp = p or 1,
38+
ki = i or 0,
39+
kd = d or 0,
40+
discrete = discrete or true
41+
}
42+
if type(target) == "number" then
43+
controller.step = scalarStep
44+
controller.integral = 0
45+
controller.prev_error = 0
46+
elseif type(target) == "table" then
47+
if vector and getmetatable(target).__name == "Vector" then
48+
controller.step = vectorStep
49+
controller.integral = vector.new()
50+
controller.prev_error = vector.new()
51+
elseif quaternion then
52+
controller.step = quaternionStep
53+
controller.integral = vector.new()
54+
controller.prev_error = vector.new()
55+
end
56+
end
57+
return setmetatable(controller, metatable)
58+
end
59+
1360
--- Performs a PID control step if the setpoint is a scalar (number) value
1461
--
1562
-- @tparam pid self The PID instance
@@ -166,9 +213,7 @@ local pid = {
166213
-- @treturn number|vector|quaternion The control output
167214
-- @usage output = pid:step(value)
168215
-- @usage output = pid:step(value, 0.5)
169-
-- @see scalarStep
170-
-- @see vectorStep
171-
-- @see quaternionStep
216+
-- @see quaternion
172217
step = function() end, -- to be replaced in constructor
173218

174219
--- Enables/disables the clamping of the output value
@@ -227,51 +272,4 @@ local metatable = {
227272
__tostring = pid.tostring
228273
}
229274

230-
--- Constructors
231-
--
232-
-- @section Constructors
233-
234-
--- Constructs a new PID controller for either a scalar, vector, or quaternion target.
235-
--
236-
-- @tparam number|vector|quaternion target The setpoint to reach
237-
-- @tparam number p Proportional gain - how aggressively to respond to the current error
238-
-- @tparam number i Integral gain - how aggressively to eliminate accumulated error
239-
-- @tparam number d Derivative gain - how aggressively to dampen the rate of change
240-
-- @tparam boolean discrete Whether to treat the PID as discrete or continuous
241-
-- @treturn The PID initialized with the given arguments
242-
-- @usage pid = pid.new(target)
243-
-- @export
244-
-- @see quaternion
245-
function new(target, p, i, d, discrete)
246-
expect(1, target, "number", "Vector", "Quaternion")
247-
expect(2, p, "number", "nil")
248-
expect(3, i, "number", "nil")
249-
expect(4, d, "number", "nil")
250-
expect(5, discrete, "boolean", "nil")
251-
252-
local controller = {
253-
sp = target or 1,
254-
kp = p or 1,
255-
ki = i or 0,
256-
kd = d or 0,
257-
discrete = discrete or true
258-
}
259-
if type(target) == "number" then
260-
controller.step = scalarStep
261-
controller.integral = 0
262-
controller.prev_error = 0
263-
elseif type(target) == "table" then
264-
if vector and getmetatable(target).__name == "Vector" then
265-
controller.step = vectorStep
266-
controller.integral = vector.new()
267-
controller.prev_error = vector.new()
268-
elseif quaternion then
269-
controller.step = quaternionStep
270-
controller.integral = vector.new()
271-
controller.prev_error = vector.new()
272-
end
273-
end
274-
return setmetatable(controller, metatable)
275-
end
276-
277275
return {new = new}

datapack/data/advanced_math/rom/apis/matrix.lua

Lines changed: 115 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,120 @@
77
--
88
-- @module matrix
99

10+
--- Constructors
11+
--
12+
-- @section Constructors
13+
14+
--- Constructs a new matrix of rows by columns, filling it using the provided function or scalar.
15+
--
16+
-- @tparam number rows The number of rows in the matrix
17+
-- @tparam number columns The number of columns in the matrix
18+
-- @tparam function|number|nil func A function taking (row, column) to generate values, or a scalar to fill all elements
19+
-- @treturn Matrix A new matrix
20+
-- @usage m = matrix.new(3, 3, function(r, c) return r + c end)
21+
-- @usage m = matrix.new(2, 4, 5) -- fills all elements with 5
22+
-- @usage m = matrix.new(2, 2) -- fills all elements with 1
23+
-- @export
24+
function new(rows, columns, func)
25+
expect(1, rows, "number", "nil")
26+
expect(2, columns, "number", "nil")
27+
expect(3, func, "function", "number", "nil")
28+
29+
local m = {}
30+
m.rows = rows or 1
31+
m.columns = columns or 1
32+
for r = 1, rows do
33+
m[r] = {}
34+
for c = 1, columns do
35+
if type(func) == "function" then
36+
m[r][c] = func(r, c)
37+
elseif type(func) == "number" then
38+
m[r][c] = func
39+
else
40+
m[r][c] = 1
41+
end
42+
end
43+
end
44+
return setmetatable(m, metatable)
45+
end
46+
47+
--- Constructs a matrix from a 2D array (table of tables).
48+
--
49+
-- @tparam table arr A 2D array representing the matrix data
50+
-- @treturn Matrix A new matrix
51+
-- @usage m = matrix.from2DArray({{1, 2}, {3, 4}})
52+
-- @export
53+
function from2DArray(arr)
54+
expect(1, arr, "table")
55+
if getmetatable(arr) ~= nil then
56+
error("Invalid Argument! Takes a 2D array!")
57+
end
58+
59+
return new(#arr, #arr[1], function(r, c) return arr[r][c] or 0 end)
60+
end
61+
62+
--- Constructs a matrix from a vector, as either a row or column matrix.
63+
--
64+
-- @tparam table v The vector to convert
65+
-- @tparam boolean row Whether to create a row matrix (true) or column matrix (false). Defaults to true.
66+
-- @treturn Matrix A new matrix representing the vector
67+
-- @usage m = matrix.fromVector(vector.new(1, 2, 3), true) -- row matrix
68+
-- @usage m = matrix.fromVector(vector.new(1, 2, 3), false) -- column matrix
69+
-- @export
70+
function fromVector(v, row)
71+
expect(1, v, "Vector")
72+
expect(2, row, "boolean", "nil")
73+
74+
row = row or true
75+
local m = {}
76+
if row then
77+
m[1] = {v.x, v.y, v.z}
78+
else
79+
m[1] = {v.x}
80+
m[2] = {v.y}
81+
m[3] = {v.z}
82+
end
83+
return from2DArray(m)
84+
end
85+
86+
--- Constructs a rotation matrix from a quaternion.
87+
--
88+
-- @tparam table q The quaternion to convert
89+
-- @treturn Matrix A new 3x3 rotation matrix
90+
-- @usage m = matrix.fromQuaternion(quaternion.new(1, vector.new(0, 0, 0)))
91+
-- @export
92+
-- @see quaternion
93+
function fromQuaternion(q)
94+
if not quaternion then
95+
error("Quaternion API is not loaded!")
96+
end
97+
expect(1, q, "Quaternion")
98+
99+
q = q:normalize()
100+
local w = q.a
101+
local x = q.v.x
102+
local y = q.v.y
103+
local z = q.v.z
104+
105+
local m = {
106+
{1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y)},
107+
{2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x)},
108+
{2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)}
109+
}
110+
return from2DArray(m)
111+
end
112+
113+
--- Constructs an identity matrix of given dimensions.
114+
--
115+
-- @tparam number rows The number of rows
116+
-- @tparam number columns The number of columns
117+
-- @treturn Matrix A new identity matrix
118+
-- @usage m = matrix.identity(3, 3)
119+
-- @export
120+
function identity(rows, columns)
121+
return new(rows, columns)
122+
end
123+
10124
--- A matrix, with dimensions `rows` x `columns`.
11125
--
12126
-- This is suitable for representing linear transformations, systems of equations,
@@ -601,118 +715,4 @@ local metatable = {
601715
__len = matrix.length,
602716
__tostring = matrix.tostring,
603717
__eq = matrix.equals
604-
}
605-
606-
--- Constructors
607-
--
608-
-- @section Constructors
609-
610-
--- Constructs a new matrix of rows by columns, filling it using the provided function or scalar.
611-
--
612-
-- @tparam number rows The number of rows in the matrix
613-
-- @tparam number columns The number of columns in the matrix
614-
-- @tparam function|number|nil func A function taking (row, column) to generate values, or a scalar to fill all elements
615-
-- @treturn Matrix A new matrix
616-
-- @usage m = matrix.new(3, 3, function(r, c) return r + c end)
617-
-- @usage m = matrix.new(2, 4, 5) -- fills all elements with 5
618-
-- @usage m = matrix.new(2, 2) -- fills all elements with 1
619-
-- @export
620-
function new(rows, columns, func)
621-
expect(1, rows, "number", "nil")
622-
expect(2, columns, "number", "nil")
623-
expect(3, func, "function", "number", "nil")
624-
625-
local m = {}
626-
m.rows = rows or 1
627-
m.columns = columns or 1
628-
for r = 1, rows do
629-
m[r] = {}
630-
for c = 1, columns do
631-
if type(func) == "function" then
632-
m[r][c] = func(r, c)
633-
elseif type(func) == "number" then
634-
m[r][c] = func
635-
else
636-
m[r][c] = 1
637-
end
638-
end
639-
end
640-
return setmetatable(m, metatable)
641-
end
642-
643-
--- Constructs a matrix from a 2D array (table of tables).
644-
--
645-
-- @tparam table arr A 2D array representing the matrix data
646-
-- @treturn Matrix A new matrix
647-
-- @usage m = matrix.from2DArray({{1, 2}, {3, 4}})
648-
-- @export
649-
function from2DArray(arr)
650-
expect(1, arr, "table")
651-
if getmetatable(arr) ~= nil then
652-
error("Invalid Argument! Takes a 2D array!")
653-
end
654-
655-
return new(#arr, #arr[1], function(r, c) return arr[r][c] or 0 end)
656-
end
657-
658-
--- Constructs a matrix from a vector, as either a row or column matrix.
659-
--
660-
-- @tparam table v The vector to convert
661-
-- @tparam boolean row Whether to create a row matrix (true) or column matrix (false). Defaults to true.
662-
-- @treturn Matrix A new matrix representing the vector
663-
-- @usage m = matrix.fromVector(vector.new(1, 2, 3), true) -- row matrix
664-
-- @usage m = matrix.fromVector(vector.new(1, 2, 3), false) -- column matrix
665-
-- @export
666-
function fromVector(v, row)
667-
expect(1, v, "Vector")
668-
expect(2, row, "boolean", "nil")
669-
670-
row = row or true
671-
local m = {}
672-
if row then
673-
m[1] = {v.x, v.y, v.z}
674-
else
675-
m[1] = {v.x}
676-
m[2] = {v.y}
677-
m[3] = {v.z}
678-
end
679-
return from2DArray(m)
680-
end
681-
682-
--- Constructs a rotation matrix from a quaternion.
683-
--
684-
-- @tparam table q The quaternion to convert
685-
-- @treturn Matrix A new 3x3 rotation matrix
686-
-- @usage m = matrix.fromQuaternion(quaternion.new(1, vector.new(0, 0, 0)))
687-
-- @export
688-
-- @see quaternion
689-
function fromQuaternion(q)
690-
if not quaternion then
691-
error("Quaternion API is not loaded!")
692-
end
693-
expect(1, q, "Quaternion")
694-
695-
q = q:normalize()
696-
local w = q.a
697-
local x = q.v.x
698-
local y = q.v.y
699-
local z = q.v.z
700-
701-
local m = {
702-
{1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y)},
703-
{2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x)},
704-
{2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)}
705-
}
706-
return from2DArray(m)
707-
end
708-
709-
--- Constructs an identity matrix of given dimensions.
710-
--
711-
-- @tparam number rows The number of rows
712-
-- @tparam number columns The number of columns
713-
-- @treturn Matrix A new identity matrix
714-
-- @usage m = matrix.identity(3, 3)
715-
-- @export
716-
function identity(rows, columns)
717-
return new(rows, columns)
718-
end
718+
}

0 commit comments

Comments
 (0)