From 879600fda74c8dc1950f2846643081920a342607 Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 06:46:21 -0800 Subject: [PATCH 01/14] Corrected PID calculation Corrects PID calculation to follow PID conventions and terminology. 1) Removes POnE parameter such that Proportional term always acts on the current error between the setpoint and the input. 2) Corrects Derivative term to calculate based on the rate of change of the error as opposed to the difference in process value, providing a predictive damping effect to improve stability and reduce overshoot. --- modules/pid/init.lua | 89 ++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 56 deletions(-) diff --git a/modules/pid/init.lua b/modules/pid/init.lua index 6c041356..67e183b5 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -1,7 +1,6 @@ --!native export type PID = { - POnE: boolean, Reset: (self: PID) -> (), Calculate: (self: PID, setpoint: number, input: number, deltaTime: number) -> number, Debug: (self: PID, name: string, parent: Instance?) -> (), @@ -10,40 +9,25 @@ export type PID = { --[=[ @class PID + Authors: Sleitnick, ΣTΞCH (SigmaTech) + The PID class simulates a [PID controller](https://en.wikipedia.org/wiki/PID_controller). PID is an acronym for _proportional, integral, derivative_. PIDs are input feedback loops that try to reach a specific goal by measuring the difference between the input and the desired value, and then returning a new desired input. - + A common example is a car's cruise control, which would give a PID the current speed and the desired speed, and the PID controller would return the desired throttle input to reach the desired speed. Original code based upon the [Arduino PID Library](https://github.com/br3ttb/Arduino-PID-Library). + + Calculations were rewritten by ΣTΞCH to more closely follow PID conventions. + This means the removal of the POnE parameter as well as the calculation of the D controller on the error as opposed to the process variable. ]=] local PID = {} PID.__index = PID ---[=[ - @within PID - @prop POnE boolean - - POnE stands for "Proportional on Error". - - Set to `true` by default. - - - `true`: The PID applies the proportional calculation on the _error_. - - `false`: The PID applies the proportional calculation on the _measurement_. - - Setting this value to `false` may help the PID move smoother and help - eliminate overshoot. - - ```lua - local pid = PID.new(...) - pid.POnE = true|false - ``` -]=] - --[=[ @param min number -- Minimum value the PID can output @param max number -- Maximum value the PID can output @@ -60,19 +44,13 @@ 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 - + self._kp = kp -- Proportional coefficient (P) + self._ki = ki -- Integral coefficient (I) + self._kd = kd -- Derivative coefficient (D) + self._lastError = 0 -- Store the last error for derivative calculation + self._integralSum = 0 -- Store the sum Σ of errors for integral calculation return self end @@ -81,13 +59,13 @@ end ]=] function PID:Reset() self._lastInput = 0 - self._outputSum = 0 + self._integralSum = 0 end --[=[ - @param setpoint number -- The desired point to reach - @param input number -- The current inputted value - @param deltaTime number -- Delta time + @param setpoint number -- The desired point to reach + @param processVariable number -- The measured value of the system to compare against the setpoint + @param deltaTime number -- Delta time. This is the time between each PID calculation @return output: number Calculates the new output based on the setpoint and input. For example, @@ -98,36 +76,35 @@ end local cruisePID = PID.new(0, 1, ...) local desiredSpeed = 50 - RunService.Heartbeat:Connect(function() - local throttle = cruisePID:Calculate(desiredSpeed, car.CurrentSpeed) + RunService.Heartbeat:Connect(function(dt) + local throttle = cruisePID:Calculate(desiredSpeed, car.CurrentSpeed, dt) car:SetThrottle(throttle) end) ``` ]=] -function PID:Calculate(setpoint: number, input: number, deltaTime: number) - local ki = self._ki * deltaTime - local kd = self._kd * deltaTime +function PID:Calculate(setpoint: number, processVariable: number, deltaTime: number) + -- Calculate the error e(t) = SP - PV(t) + local error = setpoint - processVariable - local err = setpoint - input - local dInput = input - self._lastInput - self._outputSum += ki * err + -- Proportional term + local P_out = self._kp * error - if not self.POnE then - self._outputSum -= self._kp * dInput - end + -- Integral term + self._integralSum = self._integralSum + error * deltaTime + local I_out = self._ki * self._integralSum - self._outputSum = math.clamp(self._outputSum, self._min, self._max) + -- Derivative term + local derivative = (error - self._lastError) / deltaTime + local D_out = self._kd * derivative - local output = 0 - if self.POnE then - output = self._kp * err - end + -- Σ Combine terms + local output = P_out + I_out + D_out - output += self._outputSum - kd * dInput + -- Clamp output to min/max output = math.clamp(output, self._min, self._max) - self._lastInput = input - + -- Save the current error for the next derivative calculation + self._lastError = error return output end From 5fa790cdb8afde66c82743b81ecd1655c070fd2f Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:21:54 -0800 Subject: [PATCH 02/14] Corrected PID calculation --- modules/pid/init.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/pid/init.lua b/modules/pid/init.lua index 67e183b5..30eb8533 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -31,9 +31,9 @@ PID.__index = PID --[=[ @param min number -- Minimum value the PID can output @param max number -- Maximum value the PID can output - @param kp number -- Proportional coefficient - @param ki number -- Integral coefficient - @param kd number -- Derivative coefficient + @param kp number -- Proportional coefficient (P) + @param ki number -- Integral coefficient (I) + @param kd number -- Derivative coefficient (D) @return PID Constructs a new PID. @@ -46,11 +46,11 @@ function PID.new(min: number, max: number, kp: number, ki: number, kd: number): local self = setmetatable({}, PID) self._min = min self._max = max - self._kp = kp -- Proportional coefficient (P) - self._ki = ki -- Integral coefficient (I) - self._kd = kd -- Derivative coefficient (D) - self._lastError = 0 -- Store the last error for derivative calculation - self._integralSum = 0 -- Store the sum Σ of errors for integral calculation + self._kp = kp + self._ki = ki + self._kd = kd + self._lastError = 0 -- Store the last error for derivative calculation + self._integralSum = 0 -- Store the sum Σ of errors for integral calculation return self end From 37abf34526a03355eeae49fb62999ed008f68296 Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:24:13 -0800 Subject: [PATCH 03/14] Bump PID version numbers --- README.md | 2 +- modules/pid/package.json | 2 +- modules/pid/wally.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cff549c4..af05afb4 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ | [Log](https://sleitnick.github.io/RbxUtil/api/Log) | `Log = "sleitnick/log@0.1.1"` | Log class for logging to PlayFab | | [Net](https://sleitnick.github.io/RbxUtil/api/Net) | `Net = "sleitnick/net@0.2.0"` | Static networking module | | [Option](https://sleitnick.github.io/RbxUtil/api/Option) | `Option = "sleitnick/option@1.0.5"` | Represent optional values in Lua | -| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/pid@1.2.1"` | PID Controller class | +| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/pid@1.2.2"` | PID Controller class | | [Quaternion](https://sleitnick.github.io/RbxUtil/api/Quaternion) | `Quaternion = "sleitnick/quaternion@0.2.3"` | Quaternion class | | [Sequent](https://sleitnick.github.io/RbxUtil/api/Sequent) | `Sequent = "sleitnick/sequent@0.1.0"` | Sequent class | | [Ser](https://sleitnick.github.io/RbxUtil/api/Ser) | `Ser = "sleitnick/ser@1.0.5"` | Ser class for serialization and deserialization | diff --git a/modules/pid/package.json b/modules/pid/package.json index 0959d400..f1f4beea 100644 --- a/modules/pid/package.json +++ b/modules/pid/package.json @@ -1,6 +1,6 @@ { "name": "@rbxutil/pid", - "version": "1.2.1", + "version": "1.2.2", "main": "init.lua", "repository": "github:Sleitnick/RbxUtil", "license": "MIT", diff --git a/modules/pid/wally.toml b/modules/pid/wally.toml index 32991217..1ffe02ce 100644 --- a/modules/pid/wally.toml +++ b/modules/pid/wally.toml @@ -1,7 +1,7 @@ [package] name = "sleitnick/pid" description = "PID Controller class" -version = "1.2.1" +version = "1.2.2" license = "MIT" authors = ["Stephen Leitnick"] registry = "https://github.com/UpliftGames/wally-index" From 661bfc4dacaa50f983b0532f53e696a03b742dfd Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:27:34 -0800 Subject: [PATCH 04/14] Stylua fixes --- modules/pid/init.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/pid/init.lua b/modules/pid/init.lua index 30eb8533..f1f81b5c 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -49,7 +49,7 @@ function PID.new(min: number, max: number, kp: number, ki: number, kd: number): self._kp = kp self._ki = ki self._kd = kd - self._lastError = 0 -- Store the last error for derivative calculation + self._lastError = 0 -- Store the last error for derivative calculation self._integralSum = 0 -- Store the sum Σ of errors for integral calculation return self end @@ -63,9 +63,9 @@ function PID:Reset() end --[=[ - @param setpoint number -- The desired point to reach - @param processVariable number -- The measured value of the system to compare against the setpoint - @param deltaTime number -- Delta time. This is the time between each PID calculation + @param setpoint number -- The desired point to reach + @param processVariable number -- The measured value of the system to compare against the setpoint + @param deltaTime number -- Delta time. This is the time between each PID calculation @return output: number Calculates the new output based on the setpoint and input. For example, From 5b204c362a5c4fbe46654502627ce5ce2d493484 Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:30:47 -0800 Subject: [PATCH 05/14] Bump PID - Major version 2.0.0 --- README.md | 2 +- modules/pid/init.lua | 4 ++-- modules/pid/package.json | 2 +- modules/pid/wally.toml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index af05afb4..31b32696 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ | [Log](https://sleitnick.github.io/RbxUtil/api/Log) | `Log = "sleitnick/log@0.1.1"` | Log class for logging to PlayFab | | [Net](https://sleitnick.github.io/RbxUtil/api/Net) | `Net = "sleitnick/net@0.2.0"` | Static networking module | | [Option](https://sleitnick.github.io/RbxUtil/api/Option) | `Option = "sleitnick/option@1.0.5"` | Represent optional values in Lua | -| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/pid@1.2.2"` | PID Controller class | +| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/pid@2.0.0"` | PID Controller class | | [Quaternion](https://sleitnick.github.io/RbxUtil/api/Quaternion) | `Quaternion = "sleitnick/quaternion@0.2.3"` | Quaternion class | | [Sequent](https://sleitnick.github.io/RbxUtil/api/Sequent) | `Sequent = "sleitnick/sequent@0.1.0"` | Sequent class | | [Ser](https://sleitnick.github.io/RbxUtil/api/Ser) | `Ser = "sleitnick/ser@1.0.5"` | Ser class for serialization and deserialization | diff --git a/modules/pid/init.lua b/modules/pid/init.lua index f1f81b5c..bbc75fa3 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -49,8 +49,8 @@ function PID.new(min: number, max: number, kp: number, ki: number, kd: number): self._kp = kp self._ki = ki self._kd = kd - self._lastError = 0 -- Store the last error for derivative calculation - self._integralSum = 0 -- Store the sum Σ of errors for integral calculation + self._lastError = 0 -- Store the last error for derivative calculation + self._integralSum = 0 -- Store the sum Σ of errors for integral calculation return self end diff --git a/modules/pid/package.json b/modules/pid/package.json index f1f4beea..2588aea5 100644 --- a/modules/pid/package.json +++ b/modules/pid/package.json @@ -1,6 +1,6 @@ { "name": "@rbxutil/pid", - "version": "1.2.2", + "version": "2.0.0", "main": "init.lua", "repository": "github:Sleitnick/RbxUtil", "license": "MIT", diff --git a/modules/pid/wally.toml b/modules/pid/wally.toml index 1ffe02ce..000a6d6e 100644 --- a/modules/pid/wally.toml +++ b/modules/pid/wally.toml @@ -1,7 +1,7 @@ [package] name = "sleitnick/pid" description = "PID Controller class" -version = "1.2.2" +version = "2.0.0" license = "MIT" authors = ["Stephen Leitnick"] registry = "https://github.com/UpliftGames/wally-index" From 5fbac50ad2efc9df9d41b7ed4ba20582d5e26f6f Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 11:19:48 -0800 Subject: [PATCH 06/14] Update PID Documentation --- modules/pid/index.d.ts | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/modules/pid/index.d.ts b/modules/pid/index.d.ts index 25fa9218..97852802 100644 --- a/modules/pid/index.d.ts +++ b/modules/pid/index.d.ts @@ -14,28 +14,15 @@ declare namespace PID { } interface PID { - /** - * POnE stands for "Proportional on Error". - * - * Set to `true` by default. - * - * - `true`: The PID applies the proportional calculation on the _error_. - * - `false`: The PID applies the proportional calculation on the _measurement_. - * - * Setting this value to `false` may help the PID move smoother and help - * eliminate overshoot. - */ - POnE: boolean; - /** * Calculates the new output based on the setpoint and input. * * @param setpoint The goal for the PID. - * @param input The current input. + * @param processVariable The measured value of the system to compare against the setpoint. * @param deltaTime Delta time. * @returns The updated output. */ - Calculate(setpoint: number, input: number, deltaTime: number): number; + Calculate(setpoint: number, processVariable: number, deltaTime: number): number; /** * Resets the PID. From 72196796ba13c2058159c7414c47d4b46d1c8fd7 Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 11:31:50 -0800 Subject: [PATCH 07/14] error -> err as to not shadow error function --- modules/pid/init.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/pid/init.lua b/modules/pid/init.lua index bbc75fa3..4b9bba18 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -84,17 +84,17 @@ end ]=] function PID:Calculate(setpoint: number, processVariable: number, deltaTime: number) -- Calculate the error e(t) = SP - PV(t) - local error = setpoint - processVariable + local err = setpoint - processVariable -- Proportional term - local P_out = self._kp * error + local P_out = self._kp * err -- Integral term - self._integralSum = self._integralSum + error * deltaTime + self._integralSum = self._integralSum + err * deltaTime local I_out = self._ki * self._integralSum -- Derivative term - local derivative = (error - self._lastError) / deltaTime + local derivative = (err - self._lastError) / deltaTime local D_out = self._kd * derivative -- Σ Combine terms @@ -104,7 +104,7 @@ function PID:Calculate(setpoint: number, processVariable: number, deltaTime: num output = math.clamp(output, self._min, self._max) -- Save the current error for the next derivative calculation - self._lastError = error + self._lastError = err return output end From 139093307a6cf5988edc4a2b1960932e957b7fd1 Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 11:33:07 -0800 Subject: [PATCH 08/14] Remove attribution --- modules/pid/init.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/pid/init.lua b/modules/pid/init.lua index 4b9bba18..15ecddbd 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -9,8 +9,6 @@ export type PID = { --[=[ @class PID - Authors: Sleitnick, ΣTΞCH (SigmaTech) - The PID class simulates a [PID controller](https://en.wikipedia.org/wiki/PID_controller). PID is an acronym for _proportional, integral, derivative_. PIDs are input feedback loops that try to reach a specific goal by measuring the difference between the input and the desired value, and then returning a new From a62f5d3e9b9bcb76be997baa59eae9e1190c1e05 Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 11:40:28 -0800 Subject: [PATCH 09/14] camelCase conventions --- modules/pid/init.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/pid/init.lua b/modules/pid/init.lua index 15ecddbd..52720dd1 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -85,18 +85,18 @@ function PID:Calculate(setpoint: number, processVariable: number, deltaTime: num local err = setpoint - processVariable -- Proportional term - local P_out = self._kp * err + local pOut = self._kp * err -- Integral term self._integralSum = self._integralSum + err * deltaTime - local I_out = self._ki * self._integralSum + local iOut = self._ki * self._integralSum -- Derivative term local derivative = (err - self._lastError) / deltaTime - local D_out = self._kd * derivative + local dOut = self._kd * derivative - -- Σ Combine terms - local output = P_out + I_out + D_out + -- Combine terms + local output = pOut + iOut + dOut -- Clamp output to min/max output = math.clamp(output, self._min, self._max) From 0c557f04c2279b0595c67e77e6a0634e26c539af Mon Sep 17 00:00:00 2001 From: SigmaTech <59387280+SigmaThetaTech@users.noreply.github.com> Date: Tue, 6 Feb 2024 11:41:21 -0800 Subject: [PATCH 10/14] PID.lua is no longer based on the Arduino library --- modules/pid/init.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/pid/init.lua b/modules/pid/init.lua index 52720dd1..0ea668e1 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -17,11 +17,6 @@ export type PID = { A common example is a car's cruise control, which would give a PID the current speed and the desired speed, and the PID controller would return the desired throttle input to reach the desired speed. - - Original code based upon the [Arduino PID Library](https://github.com/br3ttb/Arduino-PID-Library). - - Calculations were rewritten by ΣTΞCH to more closely follow PID conventions. - This means the removal of the POnE parameter as well as the calculation of the D controller on the error as opposed to the process variable. ]=] local PID = {} PID.__index = PID From b9aa29a2a52471583eada5cc48a5283ab52808b0 Mon Sep 17 00:00:00 2001 From: Stephen Leitnick Date: Tue, 6 Feb 2024 20:07:41 -0500 Subject: [PATCH 11/14] Rojo --- aftman.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aftman.toml b/aftman.toml index 7985a613..1a705533 100644 --- a/aftman.toml +++ b/aftman.toml @@ -1 +1 @@ -tools = { rojo = "rojo-rbx/rojo@7.3.0" , run-in-roblox = "rojo-rbx/run-in-roblox@0.3.0" , wally = "UpliftGames/wally@0.3.2" , selene = "Kampfkarren/selene@0.26.1" , stylua = "JohnnyMorganz/StyLua@0.19.1" } +tools = { rojo = "rojo-rbx/rojo@7.4.0" , run-in-roblox = "rojo-rbx/run-in-roblox@0.3.0" , wally = "UpliftGames/wally@0.3.2" , selene = "Kampfkarren/selene@0.26.1" , stylua = "JohnnyMorganz/StyLua@0.19.1" } From c885c34663afbbb3702a1cbd5107c4b7ca718dfb Mon Sep 17 00:00:00 2001 From: Stephen Leitnick Date: Tue, 6 Feb 2024 20:19:10 -0500 Subject: [PATCH 12/14] Cleanup --- modules/pid/index.d.ts | 6 +++--- modules/pid/init.lua | 13 +++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/pid/index.d.ts b/modules/pid/index.d.ts index 97852802..1b77f39b 100644 --- a/modules/pid/index.d.ts +++ b/modules/pid/index.d.ts @@ -5,9 +5,9 @@ declare namespace PID { * * @param min Minimum output. * @param max Maximum output. - * @param kp Proportional coefficient. - * @param ki Integral coefficient. - * @param kd Derivative coefficient. + * @param kp Proportional gain coefficient. + * @param ki Integral gain coefficient. + * @param kd Derivative gain coefficient. */ new (min: number, max: number, kp: number, ki: number, kd: number): PID; } diff --git a/modules/pid/init.lua b/modules/pid/init.lua index 0ea668e1..26d116df 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -24,9 +24,9 @@ PID.__index = PID --[=[ @param min number -- Minimum value the PID can output @param max number -- Maximum value the PID can output - @param kp number -- Proportional coefficient (P) - @param ki number -- Integral coefficient (I) - @param kd number -- Derivative coefficient (D) + @param kp number -- Proportional gain coefficient (P) + @param ki number -- Integral gain coefficient (I) + @param kd number -- Derivative gain coefficient (D) @return PID Constructs a new PID. @@ -43,7 +43,7 @@ function PID.new(min: number, max: number, kp: number, ki: number, kd: number): self._ki = ki self._kd = kd self._lastError = 0 -- Store the last error for derivative calculation - self._integralSum = 0 -- Store the sum Σ of errors for integral calculation + self._integralSum = 0 -- Store the sum of errors for integral calculation return self end @@ -51,7 +51,7 @@ end Resets the PID to a zero start state. ]=] function PID:Reset() - self._lastInput = 0 + self._lastError = 0 self._integralSum = 0 end @@ -75,7 +75,7 @@ end end) ``` ]=] -function PID:Calculate(setpoint: number, processVariable: number, deltaTime: number) +function PID:Calculate(setpoint: number, processVariable: number, deltaTime: number): number -- Calculate the error e(t) = SP - PV(t) local err = setpoint - processVariable @@ -98,6 +98,7 @@ function PID:Calculate(setpoint: number, processVariable: number, deltaTime: num -- Save the current error for the next derivative calculation self._lastError = err + return output end From 020e14cb9ce974ca97fb160663969f8a3f0e6776 Mon Sep 17 00:00:00 2001 From: Stephen Leitnick Date: Wed, 7 Feb 2024 16:30:50 -0500 Subject: [PATCH 13/14] PID Debug --- modules/pid/init.lua | 26 +++++++++++++++++++++++--- modules/pid/package.json | 2 +- modules/pid/wally.toml | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/modules/pid/init.lua b/modules/pid/init.lua index 26d116df..c34a46e0 100644 --- a/modules/pid/init.lua +++ b/modules/pid/init.lua @@ -124,7 +124,7 @@ function PID:Debug(name: string, parent: Instance?) local folder = Instance.new("Folder") folder.Name = name - folder:AddTag("__pidebug__") + folder:AddTag("PIDDebug") local function Bind(attrName, propName) folder:SetAttribute(attrName, self[propName]) @@ -149,12 +149,32 @@ function PID:Debug(name: string, parent: Instance?) folder:SetAttribute("Output", self._min) local lastOutput = 0 - self.Calculate = function(...) - lastOutput = PID.Calculate(...) + self.Calculate = function(s, sp, pv, ...) + lastOutput = PID.Calculate(s, sp, pv, ...) folder:SetAttribute("Output", lastOutput) return lastOutput end + local delayThread: thread? = nil + folder:SetAttribute("ShowDebugger", false) + folder:GetAttributeChangedSignal("ShowDebugger"):Connect(function() + if delayThread then + task.cancel(delayThread) + end + + local showDebugger = folder:GetAttribute("ShowDebugger") + + if showDebugger then + delayThread = task.delay(0.1, function() + delayThread = nil + if folder:GetAttribute("ShowDebugger") then + folder:SetAttribute("ShowDebugger", false) + warn("Install the PID Debug plugin: https://create.roblox.com/store/asset/16279661108/PID-Debug") + end + end) + end + end) + folder.Parent = parent or workspace self._debug = folder end diff --git a/modules/pid/package.json b/modules/pid/package.json index 2588aea5..57790cf6 100644 --- a/modules/pid/package.json +++ b/modules/pid/package.json @@ -1,6 +1,6 @@ { "name": "@rbxutil/pid", - "version": "2.0.0", + "version": "2.1.0", "main": "init.lua", "repository": "github:Sleitnick/RbxUtil", "license": "MIT", diff --git a/modules/pid/wally.toml b/modules/pid/wally.toml index 000a6d6e..7c9d6260 100644 --- a/modules/pid/wally.toml +++ b/modules/pid/wally.toml @@ -1,7 +1,7 @@ [package] name = "sleitnick/pid" description = "PID Controller class" -version = "2.0.0" +version = "2.1.0" license = "MIT" authors = ["Stephen Leitnick"] registry = "https://github.com/UpliftGames/wally-index" From 42052c78cb8f8fdffd233a6564726a637e50af8f Mon Sep 17 00:00:00 2001 From: Stephen Leitnick Date: Thu, 8 Feb 2024 08:46:58 -0500 Subject: [PATCH 14/14] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 31b32696..f17c3f14 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ | [Log](https://sleitnick.github.io/RbxUtil/api/Log) | `Log = "sleitnick/log@0.1.1"` | Log class for logging to PlayFab | | [Net](https://sleitnick.github.io/RbxUtil/api/Net) | `Net = "sleitnick/net@0.2.0"` | Static networking module | | [Option](https://sleitnick.github.io/RbxUtil/api/Option) | `Option = "sleitnick/option@1.0.5"` | Represent optional values in Lua | -| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/pid@2.0.0"` | PID Controller class | +| [PID](https://sleitnick.github.io/RbxUtil/api/PID) | `PID = "sleitnick/pid@2.1.0"` | PID Controller class | | [Quaternion](https://sleitnick.github.io/RbxUtil/api/Quaternion) | `Quaternion = "sleitnick/quaternion@0.2.3"` | Quaternion class | | [Sequent](https://sleitnick.github.io/RbxUtil/api/Sequent) | `Sequent = "sleitnick/sequent@0.1.0"` | Sequent class | | [Ser](https://sleitnick.github.io/RbxUtil/api/Ser) | `Ser = "sleitnick/ser@1.0.5"` | Ser class for serialization and deserialization |