tinypid

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 2 Imported by: 1

README

TinyPID

PkgGoDev GoReportCard

logo

go.einride.tech/pid PID controllers for Go, lightly ported from float64 to float32 for easier use with TinyGo

Examples

pid.Controller

A basic PID controller.

import (
	"fmt"
	"time"

	pid "github.com/mikesmitty/tinypid"
)

func ExampleController() {
	// Create a PID controller.
	c := pid.Controller{
		Config: pid.ControllerConfig{
			ProportionalGain: 2.0,
			IntegralGain:     1.0,
			DerivativeGain:   1.0,
		},
	}
	// Update the PID controller.
	c.Update(pid.ControllerInput{
		ReferenceSignal:  10,
		ActualSignal:     0,
		SamplingInterval: 100 * time.Millisecond,
	})
	fmt.Printf("%+v\n", c.State)
	// Reset the PID controller.
	c.Reset()
	fmt.Printf("%+v\n", c.State)
	// Output:
	// {ControlError:10 ControlErrorIntegral:1 ControlErrorDerivative:100 ControlSignal:121}
	// {ControlError:0 ControlErrorIntegral:0 ControlErrorDerivative:0 ControlSignal:0}
}

Reference ≫

pid.AntiWindupController

A PID-controller with low-pass filtering of the derivative term, feed forward term, a saturated control output and anti-windup.

Reference ≫

pid.TrackingController

a PID-controller with low-pass filtering of the derivative term, feed forward term, anti-windup and bumpless transfer using tracking mode control.

Reference ≫

Documentation

Overview

Package pid provides PID controllers for Go.

Index

Examples

Constants

View Source
const MaxFloat32 = 0x1p127 * (1 + (1 - 0x1p-23))

Taken from math.MaxFloat32

Variables

This section is empty.

Functions

func Abs

func Abs(x float32) float32

func IsNaN

func IsNaN(f float32) bool

Types

type AntiWindupController

type AntiWindupController struct {
	// Config for the AntiWindupController.
	Config AntiWindupControllerConfig
	// State of the AntiWindupController.
	State AntiWindupControllerState
}

AntiWindupController implements a PID-controller with low-pass filter of the derivative term, feed forward term, a saturated control output and anti-windup.

The anti-windup mechanism uses an actuator saturation model as defined in Chapter 6 of Åström and Murray, Feedback Systems: An Introduction to Scientists and Engineers, 2008 (http://www.cds.caltech.edu/~murray/amwiki)

The ControlError, ControlErrorIntegrand, ControlErrorIntegral and ControlErrorDerivative are prevented from reaching +/- inf by clamping them to [-MaxFloat32, MaxFloat32].

func (*AntiWindupController) DischargeIntegral

func (c *AntiWindupController) DischargeIntegral(dt time.Duration)

DischargeIntegral provides the ability to discharge the controller integral state over a configurable period of time.

func (*AntiWindupController) Reset

func (c *AntiWindupController) Reset()

Reset the controller state.

func (*AntiWindupController) Update

Update the controller state.

type AntiWindupControllerConfig

type AntiWindupControllerConfig struct {
	// ProportionalGain is the P part gain.
	ProportionalGain float32
	// IntegralGain is the I part gain.
	IntegralGain float32
	// DerivativeGain is the D part gain.
	DerivativeGain float32
	// AntiWindUpGain is the anti-windup tracking gain.
	AntiWindUpGain float32
	// IntegralDischargeTimeConstant is the time constant to discharge the integral state of the PID controller (s)
	IntegralDischargeTimeConstant float32
	// LowPassTimeConstant is the D part low-pass filter time constant => cut-off frequency 1/LowPassTimeConstant.
	LowPassTimeConstant time.Duration
	// MaxOutput is the max output from the PID.
	MaxOutput float32
	// MinOutput is the min output from the PID.
	MinOutput float32
}

AntiWindupControllerConfig contains config parameters for a AntiWindupController.

type AntiWindupControllerInput

type AntiWindupControllerInput struct {
	// ReferenceSignal is the reference value for the signal to control.
	ReferenceSignal float32
	// ActualSignal is the actual value of the signal to control.
	ActualSignal float32
	// FeedForwardSignal is the contribution of the feed-forward control loop in the controller output.
	FeedForwardSignal float32
	// SamplingInterval is the time interval elapsed since the previous call of the controller Update method.
	SamplingInterval time.Duration
}

AntiWindupControllerInput holds the input parameters to an AntiWindupController.

type AntiWindupControllerState

type AntiWindupControllerState struct {
	// ControlError is the difference between reference and current value.
	ControlError float32
	// ControlErrorIntegrand is the control error integrand, which includes the anti-windup correction.
	ControlErrorIntegrand float32
	// ControlErrorIntegral is the control error integrand integrated over time.
	ControlErrorIntegral float32
	// ControlErrorDerivative is the low-pass filtered time-derivative of the control error.
	ControlErrorDerivative float32
	// ControlSignal is the current control signal output of the controller.
	ControlSignal float32
	// UnsaturatedControlSignal is the control signal before saturation.
	UnsaturatedControlSignal float32
}

AntiWindupControllerState holds mutable state for a AntiWindupController.

type Controller

type Controller struct {
	// Config for the Controller.
	Config ControllerConfig
	// State of the Controller.
	State ControllerState
}

Controller implements a basic PID controller.

Example
package main

import (
	"fmt"
	"time"

	pid "github.com/mikesmitty/tinypid"
)

func main() {
	// Create a PID controller.
	c := pid.Controller{
		Config: pid.ControllerConfig{
			ProportionalGain: 2.0,
			IntegralGain:     1.0,
			DerivativeGain:   1.0,
		},
	}
	// Update the PID controller.
	c.Update(pid.ControllerInput{
		ReferenceSignal:  10,
		ActualSignal:     0,
		SamplingInterval: 100 * time.Millisecond,
	})
	fmt.Printf("%+v\n", c.State)
	// Reset the PID controller.
	c.Reset()
	fmt.Printf("%+v\n", c.State)
}
Output:

{ControlError:10 ControlErrorIntegral:1 ControlErrorDerivative:100 ControlSignal:121}
{ControlError:0 ControlErrorIntegral:0 ControlErrorDerivative:0 ControlSignal:0}

func (*Controller) Reset

func (c *Controller) Reset()

Reset the controller state.

func (*Controller) Update

func (c *Controller) Update(input ControllerInput)

Update the controller state.

type ControllerConfig

type ControllerConfig struct {
	// ProportionalGain determines ratio of output response to error signal.
	ProportionalGain float32
	// IntegralGain determines previous error's affect on output.
	IntegralGain float32
	// DerivativeGain decreases the sensitivity to large reference changes.
	DerivativeGain float32
}

ControllerConfig contains configurable parameters for a Controller.

type ControllerInput

type ControllerInput struct {
	// ReferenceSignal is the reference value for the signal to control.
	ReferenceSignal float32
	// ActualSignal is the actual value of the signal to control.
	ActualSignal float32
	// SamplingInterval is the time interval elapsed since the previous call of the controller Update method.
	SamplingInterval time.Duration
}

ControllerInput holds the input parameters to a Controller.

type ControllerState

type ControllerState struct {
	// ControlError is the difference between reference and current value.
	ControlError float32
	// ControlErrorIntegral is the integrated control error over time.
	ControlErrorIntegral float32
	// ControlErrorDerivative is the rate of change of the control error.
	ControlErrorDerivative float32
	// ControlSignal is the current control signal output of the controller.
	ControlSignal float32
}

ControllerState holds mutable state for a Controller.

type PIController

type PIController struct {
	// Config for the Controller.
	Config PIControllerConfig
	// State of the Controller.
	State PIControllerState
}

PIController implements a basic PI controller.

func (*PIController) Reset

func (c *PIController) Reset()

Reset the controller state.

func (*PIController) Update

func (c *PIController) Update(input PIControllerInput)

Update the controller state.

type PIControllerConfig

type PIControllerConfig struct {
	// ProportionalGain determines ratio of output response to error signal.
	ProportionalGain float32
	// IntegralGain determines previous error's affect on output.
	IntegralGain float32
	// MaxIntegralError is the maximum value of the integral error.
	MaxIntegralError float32
	// MinIntegralError is the minimum value of the integral error.
	MinIntegralError float32
	// MaxOutput is the max output from the PID.
	MaxOutput float32
	// MinOutput is the min output from the PID.
	MinOutput float32
}

PIControllerConfig contains configurable parameters for a Controller.

type PIControllerInput

type PIControllerInput struct {
	// ReferenceSignal is the reference value for the signal to control.
	ReferenceSignal float32
	// ActualSignal is the actual value of the signal to control.
	ActualSignal float32
	// SamplingInterval is the time interval elapsed since the previous call of the controller Update method.
	SamplingInterval time.Duration
}

PIControllerInput holds the input parameters to a Controller.

type PIControllerState

type PIControllerState struct {
	// ControlErrorIntegral is the integrated control error over time.
	ControlErrorIntegral float32
	// ControlSignal is the current control signal output of the controller.
	ControlSignal float32
}

PIControllerState holds mutable state for a Controller.

type TrackingController

type TrackingController struct {
	// Config for the TrackingController.
	Config TrackingControllerConfig
	// State of the TrackingController.
	State TrackingControllerState
}

TrackingController implements a PID-controller with low-pass filter of the derivative term, feed forward term, anti-windup and bumpless transfer using tracking mode control.

The anti-windup and bumpless transfer mechanisms use a tracking mode as defined in Chapter 6 of Åström and Murray, Feedback Systems: An Introduction to Scientists and Engineers, 2008 (http://www.cds.caltech.edu/~murray/amwiki)

The ControlError, ControlErrorIntegrand, ControlErrorIntegral and ControlErrorDerivative are prevented from reaching +/- inf by clamping them to [-MaxFloat32, MaxFloat32].

func (*TrackingController) DischargeIntegral

func (c *TrackingController) DischargeIntegral(dt time.Duration)

DischargeIntegral provides the ability to discharge the controller integral state over a configurable period of time.

func (*TrackingController) Reset

func (c *TrackingController) Reset()

Reset the controller state.

func (*TrackingController) Update

Update the controller state.

type TrackingControllerConfig

type TrackingControllerConfig struct {
	// ProportionalGain is the P part gain.
	ProportionalGain float32
	// IntegralGain is the I part gain.
	IntegralGain float32
	// DerivativeGain is the D part gain.
	DerivativeGain float32
	// AntiWindUpGain is the anti-windup tracking gain.
	AntiWindUpGain float32
	// IntegralDischargeTimeConstant is the time constant to discharge the integral state of the PID controller (s)
	IntegralDischargeTimeConstant float32
	// LowPassTimeConstant is the D part low-pass filter time constant => cut-off frequency 1/LowPassTimeConstant.
	LowPassTimeConstant time.Duration
	// MaxOutput is the max output from the PID.
	MaxOutput float32
	// MinOutput is the min output from the PID.
	MinOutput float32
}

TrackingControllerConfig contains configurable parameters for a TrackingController.

type TrackingControllerInput

type TrackingControllerInput struct {
	// ReferenceSignal is the reference value for the signal to control.
	ReferenceSignal float32
	// ActualSignal is the actual value of the signal to control.
	ActualSignal float32
	// FeedForwardSignal is the contribution of the feed-forward control loop in the controller output.
	FeedForwardSignal float32
	// AppliedControlSignal is the actual control command applied by the actuator.
	AppliedControlSignal float32
	// SamplingInterval is the time interval elapsed since the previous call of the controller Update method.
	SamplingInterval time.Duration
}

TrackingControllerInput holds the input parameters to a TrackingController.

type TrackingControllerState

type TrackingControllerState struct {
	// ControlError is the difference between reference and current value.
	ControlError float32
	// ControlErrorIntegrand is the integrated control error over time.
	ControlErrorIntegrand float32
	// ControlErrorIntegral is the control error integrand integrated over time.
	ControlErrorIntegral float32
	// ControlErrorDerivative is the low-pass filtered time-derivative of the control error.
	ControlErrorDerivative float32
	// ControlSignal is the current control signal output of the controller.
	ControlSignal float32
	// UnsaturatedControlSignal is the control signal before saturation used for tracking the
	// actual control signal for bumpless transfer or compensation of un-modeled saturations.
	UnsaturatedControlSignal float32
}

TrackingControllerState holds the mutable state a TrackingController.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL