pid

package module
v0.0.2-beta Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 License: Unlicense Imports: 2 Imported by: 0

README

Go PID Controller

This repository contains a simple and efficient implementation of a Proportional-Integral-Derivative (PID) controller in Go. PID controllers are widely used in control systems for various applications, including robotics, industrial processes, and more.

Features

  • Proportional, Integral, and Derivative control
  • Configurable gains (Kp, Ki, Kd)
  • Output limiting (Min/Max)
  • Deadband for ignoring small errors
  • Anti-windup protection
  • Saturation detection

Installation

To install this PID controller in your Go project, follow these steps:

go get github.com/yourusername/go-pid-controller

Configuration

The PID controller can be configured with the following parameters:

  • Kp: Proportional gain
  • Ki: Integral gain
  • Kd: Derivative gain
  • MinOutput: Minimum output value
  • MaxOutput: Maximum output value
  • Deadband: Error values smaller than this are treated as zero
  • AntiWindup: Enable/disable anti-windup protection

Example

Here's a simple example of how to use the PID controller:

package main

import (
    "fmt"
    "time"
    "github.com/tom-wray/go-pid-controller"
)

func main() {
    pid := pidcontroller.New(1.0, 0.1, 0.05, -100, 100)
    pid := PID{
        Kp: 1.0
        Ki: 0.1
        Kd: 0.05
        MinOutput: -100
        MaxOutput: 100
        Deadband: 0.1
        AntiWindup: true
    }
    setpoint := 50.0
    measuredValue := 0.0
    for i := 0; i < 100; i++ {
        error := setpoint - measuredValue
        output := pid.Calculate(error)
        // Simulate system response (replace with your actual system)
        measuredValue += output * 0.1
        fmt.Printf("Iteration %d: Error = %.2f, Output = %.2f, Measured Value = %.2f\n",
        i, error, output, measuredValue)
        time.Sleep(100 * time.Millisecond)
    }
}

You can also view the test file for different implementations of the controller.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PID

type PID struct {
	// parameters
	Kp         float64 // Proportional gain
	Ki         float64 // Integral gain
	Kd         float64 // Derivative gain
	MinOutput  float64 // Minimum output value
	MaxOutput  float64 // Maximum output value
	Deadband   float64 // Deadband to ignore small errors
	Saturated  bool    // Indicates if the output is saturated
	AntiWindup bool    // Enable/disable anti-windup protection
	// contains filtered or unexported fields
}

PID represents a Proportional-Integral-Derivative controller.

func (*PID) Reset

func (pid *PID) Reset()

Reset resets the PID controller's internal state. This includes resetting the previous error, integral sum, and last update time.

func (*PID) Update

func (pid *PID) Update(setpoint, measured float64) float64

Update calculates and returns the control output based on the current reference value.

Parameters:

  • setpoint: The desired setpoint
  • measured: The current measured value of the process variable

Returns:

  • float64: The calculated control output

Jump to

Keyboard shortcuts

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