https://github.com/wpbonelli/cspid
C# PID controller
https://github.com/wpbonelli/cspid
Last synced: about 1 year ago
JSON representation
C# PID controller
- Host: GitHub
- URL: https://github.com/wpbonelli/cspid
- Owner: wpbonelli
- License: unlicense
- Created: 2019-08-14T03:16:32.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-06T22:37:26.000Z (about 6 years ago)
- Last Synced: 2025-05-05T01:08:58.450Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 28.3 KB
- Stars: 10
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CSPID
CSPID (pronounced "speedy") is a C# PID (proportional-integral-derivative) controller targeting .NET Standard 2.0.
**Contents**
- [Installation](#installation)
- [Usage](#usage)
## Installation
Clone the repo with `git clone https://github.com/w-bonelli/CSPID.git` or pull the package from [Nuget.org](https://www.nuget.org/packages/CSPID/) with PowerShell: `Install-Package CSPID`.
## Usage
There's one class: `PIDController`. It has one method: `double Next(double error, double elapsed = 1)`. CSPID doesn't care how you measure your error or the passage of time. To construct a controller, provide:
- `errorRange`: the range you expect your error values to take
- `controlRange`: the range of values your control variable may take
You can tune gain in real time:
- `ProportionalGain`
- `IntegralGain`
- `DerivativeGain`
You can also impose a `MaximumStep` (the largest permissible change between successive control values).
```csharp
// create a controller
var controller = new PIDController(
errorRange: new Range(-5, 5), // Range models an inclusive range
controlRange: new Range(0, 10))
{
MaximumStep = double.MaxValue,
ProportionalGain = 1,
IntegralGain = 1,
DerivativeGain = 1
} as IPIDController; // interface is handy if you need to mock
// or do it the old-fashioned way
controller = new PIDController(
minimumError: -5,
maximumError: 5,
minimumControl: 0,
maximumControl: 10)
{
MaximumStep = double.MaxValue,
ProportionalGain = 1,
IntegralGain = 1,
DerivativeGain = 1
} as IPIDController;
// get a control value
var value1 = controller.Next(error: 1); // elapsed defaults to 1
// get another one
var value2 = controller.Next(error: -1, elapsed: 0.5);
```