Skip to content

Commit

Permalink
Merge pull request #185 from Sleitnick/feature/pid-dt
Browse files Browse the repository at this point in the history
PID delta time
  • Loading branch information
Sleitnick authored Jan 16, 2024
2 parents 936cf43 + 311bfbc commit d426fec
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 17 deletions.
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
| [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.0"` | 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 |
Expand Down
2 changes: 1 addition & 1 deletion modules/pid/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface PID {
* @param input The current input.
* @returns The updated output.
*/
Calculate(setpoint: number, input: number): number;
Calculate(setpoint: number, input: number, deltaTime: number): number;

/**
* Resets the PID.
Expand Down
49 changes: 36 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 Down Expand Up @@ -99,10 +103,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 +122,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 +145,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 +162,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.0",
"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.0"
license = "MIT"
authors = ["Stephen Leitnick"]
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
exclude = ["node_modules", "package.json", "**/*.ts"]

0 comments on commit d426fec

Please sign in to comment.