Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ThoughtSpinnr committed Jan 17, 2024
2 parents e4ce428 + 6902f30 commit 11bc8e3
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 90 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v4

- name: Install Aftman
uses: ok-nick/setup-aftman@v0
uses: ok-nick/setup-aftman@v0.4.2

- name: Lint
run: |
Expand All @@ -32,5 +32,5 @@ jobs:
- uses: JohnnyMorganz/stylua-action@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: v0.18.2
version: v0.19.1
args: --check ./modules
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"editor.defaultFormatter": "JohnnyMorganz.stylua",
"editor.formatOnSave": true,
},
"[luau]": {
"editor.defaultFormatter": "JohnnyMorganz.stylua",
"editor.formatOnSave": true,
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.formatOnSave": true
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright © 2022 Stephen Leitnick
Copyright © 2024 Stephen Leitnick

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
| [Log](https://sleitnick.github.io/RbxUtil/api/Log) | `Log = "sleitnick/[email protected]"` | Log class for logging to PlayFab |
| [Net](https://sleitnick.github.io/RbxUtil/api/Net) | `Net = "sleitnick/[email protected]"` | Static networking module |
| [Option](https://sleitnick.github.io/RbxUtil/api/Option) | `Option = "sleitnick/[email protected]"` | Represent optional values in Lua |
| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/pid@1.1.0"` | PID Controller class |
| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/pid@1.2.1"` | PID Controller class |
| [Quaternion](https://sleitnick.github.io/RbxUtil/api/Quaternion) | `Quaternion = "sleitnick/[email protected]"` | Quaternion class |
| [Sequent](https://sleitnick.github.io/RbxUtil/api/Sequent) | `Sequent = "sleitnick/[email protected]"` | Sequent class |
| [Ser](https://sleitnick.github.io/RbxUtil/api/Ser) | `Ser = "sleitnick/[email protected]"` | Ser class for serialization and deserialization |
| [Shake](https://sleitnick.github.io/RbxUtil/api/Shake) | `Shake = "sleitnick/[email protected]"` | Shake class for making things shake |
| [Signal](https://sleitnick.github.io/RbxUtil/api/Signal) | `Signal = "sleitnick/[email protected]"` | Signal class |
| [Silo](https://sleitnick.github.io/RbxUtil/api/Silo) | `Silo = "sleitnick/silo@0.1.2"` | State container class |
| [Silo](https://sleitnick.github.io/RbxUtil/api/Silo) | `Silo = "sleitnick/silo@0.2.0"` | State container class |
| [Streamable](https://sleitnick.github.io/RbxUtil/api/Streamable) | `Streamable = "sleitnick/[email protected]"` | Streamable class and StreamableUtil |
| [Symbol](https://sleitnick.github.io/RbxUtil/api/Symbol) | `Symbol = "sleitnick/[email protected]"` | Symbol |
| [TableUtil](https://sleitnick.github.io/RbxUtil/api/TableUtil) | `TableUtil = "sleitnick/[email protected]"` | Table utility functions |
Expand Down
3 changes: 2 additions & 1 deletion modules/pid/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ interface PID {
*
* @param setpoint The goal for the PID.
* @param input The current input.
* @param deltaTime Delta time.
* @returns The updated output.
*/
Calculate(setpoint: number, input: number): number;
Calculate(setpoint: number, input: number, deltaTime: number): number;

/**
* Resets the PID.
Expand Down
50 changes: 37 additions & 13 deletions modules/pid/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
-- PID
-- August 11, 2020
--!native

export type PID = {
POnE: boolean,
Reset: (self: PID) -> (),
Calculate: (self: PID, setpoint: number, input: number) -> number,
Calculate: (self: PID, setpoint: number, input: number, deltaTime: number) -> number,
Debug: (self: PID, name: string, parent: Instance?) -> (),
Destroy: (self: PID) -> (),
}
Expand Down Expand Up @@ -61,14 +60,19 @@ PID.__index = PID
]=]
function PID.new(min: number, max: number, kp: number, ki: number, kd: number): PID
local self = setmetatable({}, PID)

self._min = min
self._max = max

self._kp = kp
self._ki = ki
self._kd = kd

self._lastInput = 0
self._outputSum = 0

self.POnE = true

return self
end

Expand All @@ -83,6 +87,7 @@ end
--[=[
@param setpoint number -- The desired point to reach
@param input number -- The current inputted value
@param deltaTime number -- Delta time
@return output: number
Calculates the new output based on the setpoint and input. For example,
Expand All @@ -99,10 +104,13 @@ end
end)
```
]=]
function PID:Calculate(setpoint: number, input: number)
local err = (setpoint - input)
local dInput = (input - self._lastInput)
self._outputSum += (self._ki * err)
function PID:Calculate(setpoint: number, input: number, deltaTime: number)
local ki = self._ki * deltaTime
local kd = self._kd * deltaTime

local err = setpoint - input
local dInput = input - self._lastInput
self._outputSum += ki * err

if not self.POnE then
self._outputSum -= self._kp * dInput
Expand All @@ -115,7 +123,7 @@ function PID:Calculate(setpoint: number, input: number)
output = self._kp * err
end

output += self._outputSum - self._kd * dInput
output += self._outputSum - kd * dInput
output = math.clamp(output, self._min, self._max)

self._lastInput = input
Expand All @@ -138,6 +146,7 @@ function PID:Debug(name: string, parent: Instance?)
if not game:GetService("RunService"):IsStudio() then
return
end

if self._debug then
return
end
Expand All @@ -154,11 +163,26 @@ function PID:Debug(name: string, parent: Instance?)
end)
end

Bind("Min", "_min")
Bind("Max", "_max")
Bind("KP", "_kp")
Bind("KI", "_ki")
Bind("KD", "_kd")
folder:SetAttribute("MinMax", NumberRange.new(self._min, self._max))
folder:GetAttributeChangedSignal("MinMax"):Connect(function()
local minMax = folder:GetAttribute("MinMax") :: NumberRange
self._min = minMax.Min
self._max = minMax.Max
self:Reset()
end)

Bind("kP", "_kp")
Bind("kI", "_ki")
Bind("kD", "_kd")

folder:SetAttribute("Output", self._min)

local lastOutput = 0
self.Calculate = function(...)
lastOutput = PID.Calculate(...)
folder:SetAttribute("Output", lastOutput)
return lastOutput
end

folder.Parent = parent or workspace
self._debug = folder
Expand Down
16 changes: 16 additions & 0 deletions modules/pid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@rbxutil/pid",
"version": "1.2.1",
"main": "init.lua",
"repository": "github:Sleitnick/RbxUtil",
"license": "MIT",
"types": "index.d.ts",
"files": [
"./",
"!*.toml",
"!*.json"
],
"publishConfig": {
"access": "public"
}
}
3 changes: 2 additions & 1 deletion modules/pid/wally.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "sleitnick/pid"
description = "PID Controller class"
version = "1.1.0"
version = "1.2.1"
license = "MIT"
authors = ["Stephen Leitnick"]
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
exclude = ["node_modules", "package.json", "**/*.ts"]
10 changes: 5 additions & 5 deletions modules/silo/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ local Util = {}
Util.None = newproxy()

-- Recursive table freeze.
function Util.DeepFreeze(tbl: AnyTable): AnyTable
function Util.DeepFreeze<T>(tbl: AnyTable): AnyTable
table.freeze(tbl)
for _, v in pairs(tbl) do
for _, v in tbl do
if type(v) == "table" then
Util.DeepFreeze(v)
end
Expand All @@ -22,9 +22,9 @@ function Util.DeepFreeze(tbl: AnyTable): AnyTable
end

-- Recursive table copy.
function Util.DeepCopy(tbl: AnyTable): AnyTable
function Util.DeepCopy<T>(tbl: AnyTable): AnyTable
local newTbl = table.clone(tbl)
for k, v in pairs(newTbl) do
for k, v in newTbl do
if type(v) == "table" then
newTbl[k] = Util.DeepCopy(v)
end
Expand All @@ -36,7 +36,7 @@ end
-- Similar to the spread operator in JavaScript.
function Util.Extend(original: AnyTable, extension: AnyTable): AnyTable
local t = Util.DeepCopy(original)
for k, v in pairs(extension) do
for k, v in extension do
if type(v) == "table" then
if type(original[k]) == "table" then
t[k] = Util.Extend(original[k], v)
Expand Down
Loading

0 comments on commit 11bc8e3

Please sign in to comment.