https://github.com/cheind/py-control

PID controller playground in Python

https://github.com/cheind/py-control

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (8.6%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

PID controller playground in Python

Basic Info
  • Host: GitHub
  • Owner: cheind
  • Language: Python
  • Default Branch: master
  • Size: 240 KB
Statistics
  • Stars: 11
  • Watchers: 1
  • Forks: 6
  • Open Issues: 0
  • Releases: 0
Created over 9 years ago · Last pushed over 9 years ago
Metadata Files
Readme

Readme.md

py-control,

a Python playground to experiment with PID controllers. In particular the effects of individual PID terms can be observed in a scenario where a n-dimensional particle, driven by forces, needs to be controlled.

In particle_trajectory.py a one-dimensional particle is asked to to follow a trajectory that changes over time. The target trajectory is given by sharp step function. It is assumed that the particle position cannot be directly controlled, but rather the PID controller outputs a force that is being applied to the particle. Over time these forces are then integrated to velocities and particle position.

The image below illustrates the effects of different PID parameters on the particle's trajectory. Blue is the target trajectory. Green shows a P-controller, red a PD-controller and a trajectory of an auto-tuned particle is shown in mint.

particle_trajectory

Note, how the proportional controller overshoots (it basically accelerates all the way until it recognizes it's too late). The dynamic process is implement by inheriting from ctrl.Process and providing the necessary methods.

```Python class MoveParticleProcess(ctrl.Process): """Models a dynamic system in which a particle driven by forces follows a desired trajectory. """

def __init__(self, particle=ctrl.Particle(), pid=ctrl.PID()):
    super(MoveParticleProcess, self).__init__()
    self.particle = particle
    self.pid = pid

def target(self, t):
    """Return setpoint position for particle to reach.
    Simple step function at t == 1. and t==15.
    """
    if t < 5. or t >= 15.:
        return np.asarray([0.])
    else:
        return np.array([1.])

def sense(self, t):
    """Sense particle position."""
    return self.particle.x

def correct(self, error, dt):
    """Compute correction based on error."""
    return self.pid.update(error, dt)

def actuate(self, u, dt):
    """Update particle position. 
    Takes the correction value `u` and interprets it as force acting on the particle, 
    then upates the motion equations by `dt`.
    """
    self.particle.add_force(u)
    self.particle.update(dt)

```

You can then run the simulation by

```Python process = MoveParticleProcess( particle=ctrl.Particle(x0=[0], v0=[0], inv_mass=1.), pid=ctrl.PID(kp=0.1, ki=0.0, kd=1.0) ) result = process.loop(tsim=100, dt=0.1)

plt.plot(result['t'], result['x']) # Plot trajectory over time ```

You may want to experiment with different processes yourself. For example, by additing external forces to the MoveParticleProcess

Python def actuate(self, u, dt): """Update particle position. """ self.particle.add_force(u) self.particle.add_force(np.array([-0.5 * self.particle.mass])) self.particle.update(dt)

you will notice that only a PID controller that includes an integral term can follow the trajectory smoothly. The PD controller stabilizes at a line below the target trajectory.

particle_trajectory

Owner

  • Name: Christoph Heindl
  • Login: cheind
  • Kind: user
  • Location: Austrian area

I am a computer scientist working at the interface of perception, robotics and deep learning.

GitHub Events

Total
Last Year

Issues and Pull Requests

Last synced: about 1 year ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels