# DC motor PID loop Info needed.

Discussion in 'Programmer's Corner' started by MaxHeadRoom, Mar 30, 2016.

Jul 18, 2013
10,813
2,491
Anyone implent PID feedback via simple opto sensor on a DC motor?
Preferably in Assembly?
Max.

2. ### GopherT AAC Fanatic!

Nov 23, 2012
6,297
4,019
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.

3. ### nigelwright7557 Senior Member

May 10, 2008
488
71
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.

4. ### GopherT AAC Fanatic!

Nov 23, 2012
6,297
4,019

Code (Text):
1.
2. previous_error = 0
3. integral = 0
4. start:
5. error = setpoint - measured_value
6. integral = integral + error*dt
7. derivative = (error - previous_error)/dt
8. output = Kp*error + Ki*integral + Kd*derivative
9. previous_error = error
10. wait(dt)
11. goto start
12.
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.

5. ### crutschow Expert

Mar 14, 2008
13,462
3,353
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.