An open API service indexing awesome lists of open source software.

https://github.com/wpbonelli/cspid

C# PID controller
https://github.com/wpbonelli/cspid

Last synced: about 1 year ago
JSON representation

C# PID controller

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);
```