https://github.com/gabetax/pid_controller
Ruby implementation of a PID Controller
https://github.com/gabetax/pid_controller
control-theory pid-controller ruby
Last synced: about 1 year ago
JSON representation
Ruby implementation of a PID Controller
- Host: GitHub
- URL: https://github.com/gabetax/pid_controller
- Owner: gabetax
- License: apache-2.0
- Created: 2017-11-27T01:22:30.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2020-06-24T01:33:42.000Z (almost 6 years ago)
- Last Synced: 2025-02-28T00:51:22.520Z (about 1 year ago)
- Topics: control-theory, pid-controller, ruby
- Language: Ruby
- Homepage:
- Size: 29.3 KB
- Stars: 14
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PidController
[](https://badge.fury.io/rb/pid_controller)
[](https://travis-ci.org/gabetax/pid_controller)
This is a Ruby implementation of a [PID Controller](https://en.wikipedia.org/wiki/PID_controller). A PID controller is a feedback system that is configured with a target setpoint, can read measurements of the system to see how close we are to the setpoint, and will omit an output. Every day examples include:
- Cruise control
- Thermostats
- Quadcopters (appearently, because they predominate search results)
- Database load (yay! This is why the purpose I'm actually writing this for).
## Recommended reading
- [PID without a PhD](https://www.wescottdesign.com/articles/pid/pidWithoutAPhd.pdf)
- [Throttling Utilities in the IBM DB2 Universal Database Server](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.660.9095&rep=rep1&type=pdf)
## Usage
I mentioned databases, so here's an example of how we can prevent a low priority task (e.g. bulk deletion) from contending with customer traffic:
```ruby
sensor = MySQLSensor.new # Use your imagination
controller = PIDController.new(
setpoint: 60.0,
kp: 5.0,
ki: 1.0,
kd: 0.1,
output_max: 0.0,
integral_max: 0.0
)
Event.where(account_id: account_id).in_batches do |relation|
relation.delete_all
backoff = -1 * controller << sensor.cpu_utilization
sleep backoff if backoff > 0
end
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
## Contributing
Bug reports and pull requests are welcome on GitHub at