# Implementing PID feedback control

I'm a Junior computer engineer working on a project involving a 3d printer. It is a mobile printer with the print head attached to wheels which move along the x,y axis of the surface it is printing on. The wheel movements may be off by less than a millimeter as the printer is moving. This causes a serious problem after printing a few layers as each layer does not land on top of the other.

I am in the process of implementing a sensor to detect the location of the print head with respect to some arbitrary reference point. My task is to implement a feedback control loop to correct the printer's movements and have the object printed properly.

I have an idea of how I want to solve this; using the sensor to detect the current location, then read the code to check what is the desired location, I then implement code to move the difference.

My question is where does the concept of PID control even fit in? The following equations were taken from wikipedia for time/frequency domains.

I am confused about the values of t and s in the above equations. I have numeric values for input, output, error and the K constants. None of the values are functions so I don't understand what happens with the derivative and integration involved. Sorry if this seems like a dumb question but I am a complete novice to control theory and have been learning from online articles.

You do in fact have a function. You have a value that changes, $e(t)$ is the error in your system (error in function of time). It will take you a while to learn to read math notation, don't worry took me ages to learn. But basically the formulation reads as follows, term by term:

• Proportional term: $K_P * (v_m - v_d)$, where $v_m$ is the measured value and $v_d$ is what you desire the value to be. $K_P$ is just a multiplier on how aggressively you want to maneuver the system.

Essentially the proportional term works by adjusting the system towards to desired value. But may never reach it due to needing to overcome forces and reaches equilibrium before hitting target.

• The integral term: Is just the sum of errors over time. So what you do is you calculate $v_m-v_d$ and add it to a stored value from the previous time you calculated this same thing multiplying that by $K_I$. Again the $K_I$ is just a multiplier how much you want this effect to affect the maneuvering.

The purpose of the integral term is to gradually overcome the persistent error and account for any changes in the system (hydraulic fluid leak etc).

• The derivative term is the error from last time minus the error now. Its not needed in most systems, but it has a slowing effect on the other values. It is also somewhat noisy so you may want to calculate it with a more sophisticated method than just from two previous samples.

Its best not to look too much on the mathematical formulation. Best look up some code that does it, its much easier.

• Like this article on PID and code and avoiding small problems.
• Ok so from your explanation I kinda get how error can be a function with respect to time as with each movement of the robot, a new error value is obtained. But how to I go about obtaining this function? Since the difference between desired location and actual location will not always be the same? – Jay Oct 10 '16 at 4:47
• @Jay and that is why its a function. You calculate it every time again and again. – joojaa Oct 10 '16 at 4:50
• @Jay to clarify, its a unknown function you just get the output from your system. Its a mathematical handwaving saying that something variable happens here the values you get out of it are what the error spits out from your measurement. The measurement is the function, you do not get a mathematical function at all. – joojaa Oct 10 '16 at 4:55
• if the function is unknown then what is the derivative of e(t) supposed to be? – Jay Oct 10 '16 at 4:57
• @Jay If you have a model of the system, then you can predict its dynamics in advance. Because implementing a PID controller without any knowledge of the system you are trying to control might lead to an unstable feedback loop. – fibonatic Oct 10 '16 at 6:07

e(t) and u(t) are functions.

e(t) shows the difference between required location and current location. u(t) is the voltage input to the motor.

That means let suppose your printer head is at 4cm w.r.t reference point at t=1s but it must be at 5cm w.r.t reference point so the difference will be 1cm at t=1s. So the voltage input the motor will be u(1)=Kp(1)+Ki(1+e(t-1))+Kd(1-e(t-1))/1. So when t=1.1s e(1.1) will decrease due to previous effort on the motor by u(1). And soon error will reduce to zero. Hence u(t) and e(t) are varying with time they are functions.