DC motor PID loop Info needed.

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

  1. MaxHeadRoom

    Thread Starter Expert

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

    AAC Fanatic!

    Nov 23, 2012
    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

    Distinguished Member

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

    Code (Text):
    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
    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.
    MaxHeadRoom likes this.
  5. crutschow


    Mar 14, 2008
    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.
  6. MaxHeadRoom

    Thread Starter Expert

    Jul 18, 2013
    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.