# DC motor PID loop Info needed.

Joined Jul 18, 2013
22,057
Anyone implent PID feedback via simple opto sensor on a DC motor?
Preferably in Assembly?
Max.

#### GopherT

Joined Nov 23, 2012
8,012
Anyone implent PID feedback via simple opto sensor on a DC motor?
Preferably in Assembly?
Max.
I have and it was a mess - I don't have it but we used it on a small line-following robot - controlled the left and right wheels to keep on the center.

Doing it in C was infinitely easier and actually possible to explain to someone else.

I'll look if the robotics team still has the files. The team I was helping moved to arduino Teensy since it is so small, fast (70MHz) and easy to use.

#### nigelwright7557

Joined May 10, 2008
532
I have done a few projects with PID control.
There is a basic formula to use to calculate the output.
Sadly I left it behind when I changed jobs so cant remember much about it now.
I think there were 3 error variables (3 measures of input from opto) and the formula was done on these to produce a shift in output voltage.

#### GopherT

Joined Nov 23, 2012
8,012
I have done a few projects with PID control.
There is a basic formula to use to calculate the output.
Sadly I left it behind when I changed jobs so cant remember much about it now.
I think there were 3 error variables (3 measures of input from opto) and the formula was done on these to produce a shift in output voltage.

Code:
previous_error = 0
integral = 0
start:
error = setpoint - measured_value
integral = integral + error*dt
derivative = (error - previous_error)/dt
output = Kp*error + Ki*integral + Kd*derivative
previous_error = error
wait(dt)
goto start
If you check for errors on a regular interval, you can ignore the (dt), or change in time term. Just adjust the coefficients of P, I and D, Which are Kp, Ki and Kd, respectively. These terms indicate the importance of each term in correcting errors.

#### crutschow

Joined Mar 14, 2008
26,071
Have you considered a Fuzzy Logic control loop?
It uses a series of If-Then-Else statements but I'm not sure how complex that becomes in assembly.

Joined Jul 18, 2013
22,057
I think I will try using/adapting the Microchip program for the working Pendulum project they have in their library, it has PID loop Assembly and C versions.
Max.