PID Controller Library

I would like to share a C++ library that was born out of my senior design project last year. Our project called for a PID controller that could handle custom C++ classes as the control signals and could meet several other minor needs. While there's plenty of C and C++ PID controllers available on the internet, none quite fit our needs. So, I coded a generic PID controller that could be adapted to varying needs.

I have redesigned, recoded, and updated that PID controller for use for anyone who might find it useful. It can be used on any platform that supports C++ and template classes. Officially I will support Arduino, PIC32, and Atmel AVR and SAM. I will also support Windows and Unix/Linux applications as well.

The library is available here: https://github.com/Raven45/PID-Controller/releases

Documentation is available here: https://github.com/Raven45/PID-Controller/wiki

I spent some time (wasted some time) recently trying to apply this Arduino PID library to a project for controlling the speed of a small DC motor in a model train.

I was completely new to this and the first problem was that it has no explanation of what are the units in which the PID inputs and outputs need to be. And second, what are circumstances in which the concept will or won't work.

It is as if the library is presented for the use of people who are already fully familiar with PID.

From a quick look at your documentation those matters also seem to be missing.

It would be very useful if you could write a "walk-through" for a complete example from start to finish. What needs to be controlled, how you choose the sensors, how you build a program around all of that.

...R

Robin2: It is as if the library is presented for the use of people who are already fully familiar with PID.

From a quick look at your documentation those matters also seem to be missing.

It would be very useful if you could write a "walk-through" for a complete example from start to finish. What needs to be controlled, how you choose the sensors, how you build a program around all of that.

...R

I understand your frustration. It is second nature to myself simply because I've spent the last 8 years in engineering school. I will take a look at writing a complete tutorial, though this will take time. The reason for this is because the information you're looking for is not necessarily a tutorial on PID control, but a tutorial on piecing together a complete, closed-loop control algorithm, of which PID is only but one piece of the puzzle. In addition, a complete tutorial will also have to include tuning the PID controller if it's to be considered a complete tutorial.

I will take a look at this.

Robin2: ...the first problem was that it has no explanation of what are the units in which the PID inputs and outputs need to be.

A PID controller is nothing more than a transfer function, meaning that it has no units; the units of the output will be exactly the same units as the input.

For motor control, I typically use volts since most dc motors are controlled by varying the voltage with PWM.

Robin2: And second, what are circumstances in which the concept will or won't work.

Depends on the overall control algorithm (which some people may refer to as a control loop). Typically, it will work only if you have a feedback loop, meaning you wouldn't use PID in open loop control.

AudioGoose45: A PID controller is nothing more than a transfer function, meaning that it has no units; the units of the output will be exactly the same units as the input.

For motor control, I typically use volts since most dc motors are controlled by varying the voltage with PWM.

I guess I did not express myself clearly. Suppose that the output is to be a number of volts. But the input can't be volts. It will be something like the number of microseconds for one revolution of the motor.

Depends on the overall control algorithm (which some people may refer to as a control loop). Typically, it will work only if you have a feedback loop, meaning you wouldn't use PID in open loop control.

Again I have been unclear. I am aware that it needs to be a feedback system. But I doubt if it can be "bolted on" to any system with feedback and be expected to solve the problem. What sort of problems is it suited to and what is it not suited to?

To be more specific I assumed I could "bolt PID on" to control my small DC motor; develop and test it with no load on the motor (for convenience) and then install the motor in my model train and "it would just work". From what I know now that was a completely unrealistic expectation.

And the examples I have seen have generally been of systems with slow effects - such as heating and ventilation. Rather than an electric motor which seems to be able to halve or double its speed in a single revolution (or maybe two or three).

...R PS. Thanks for your offer to think about writing a "complete" tutorial

Ok, question for you. In your dc motor application, what are you trying to control? Are you trying to control the speed (in rpm) or something else, such as the torque?

Most motor control applications are controlling speed. A good example is the motor on a drone; the PID controller is regulating the speed of the propellers to a certain speed in rpm. They do this by reading in the speed in rpms, calculating an error in rpms, doing a conversion from rpms to volts (using something called the motor voltage constant Kv), and then input that error in volts to the PID controller. The PID controller will output a signal in volts, which is then converted to a PWM duty cycle, which is then applied to the physical motor. An unloaded motor will "snap" to its target speed while a motor with a load will "ramp up" if the PID controller is tuned correctly.

Controlling torque is much different and a little more difficult. In this case you would read the current used by the motor through a current sensor and then convert the units from amps to torque using the motor torque constant (Kt).

Robin2: To be more specific I assumed I could "bolt PID on" to control my small DC motor; develop and test it with no load on the motor (for convenience) and then install the motor in my model train and "it would just work". From what I know now that was a completely unrealistic expectation.

Well yes, because you are changing the "plant;" an unloaded motor and a motor attached to a model train are going to have different values for the inertia.

AudioGoose45: calculating an error in rpms, doing a conversion from rpms to volts (using something called the motor voltage constant Kv), and then input that error in volts to the PID controller.

Well yes, because you are changing the "plant;" an unloaded motor and a motor attached to a model train are going to have different values for the inertia.

I just picked out these two parts to illustrate the sort of information that a newbie to PID will not know - and I consider myself moderately competent at mechanics and programming.

And I have no idea what is the Kv value for my motor - but I don't want this Thread to become a discussion about my specific project.

Interestingly I got my first satellite TV receiver a few days ago and the instruction manual suffers from the same sort of problem as the Arduino PID library. It gives instructions for doing X but does not explain why or when X is appropriate amd it gives no advice about how the typical user would use the device (how to find the channel you want etc). However a TV receiver is easier to figure out :)

...R

Robin2: And I have no idea what is the Kv value for my motor - but I don't want this Thread to become a discussion about my specific project.

Fortunately, it's easy to figure out if you have a way of measuring the speed in rpms. It's (Kv = Speed of the shaft / applied voltage). You'll get a more accurate measure of Kv if you measure the speed while it's unloaded and I would average the results from several readings across several speeds. The units should be in rpm/volts. After you have Kv, it's used as a conversion factor to convert from rpms to volts and vice versa.

Robin2: Interestingly I got my first satellite TV receiver a few days ago and the instruction manual suffers from the same sort of problem as the Arduino PID library. It gives instructions for doing X but does not explain why or when X is appropriate amd it gives no advice about how the typical user would use the device (how to find the channel you want etc). However a TV receiver is easier to figure out :)

Controls isn't easy like a tv antenna. People strike out their entire careers on this subject and they make quite a substantial amount money doing it. Hang in there, once it clicks it'll become second nature.

When you publish your tutorial I will read it carefully. Until then I will be content to leave PID to one side. :)

...R

@AudioGoose45

Thank you for your work on the PID Library! Your explanations are great for understanding the how the factors work in your library!

I think that Robin2 is asking about how to tune a controller when you are first starting out. For instance, a RPM controller for a heavy Diesel engine would be set-up with different settings than a light race car engine RPM controller.

In my experience, the response of large water valve is very slow, while a small water valve can be almost instantaneous. In these examples, the starting point for a PID controller would be very different.

Most of the advice that I have read, recommends starting with only the 'P" part of the PID and then tuning "D" and finally "I". How you go about it depends on the hardware you are working with.....

Your readers might benefit from any advice you could give for starting levels of PID.

Thanks for the work, Mark