Author Topic: PIC18f4431 PID loop implementation  (Read 2088 times)

0 Members and 1 Guest are viewing this topic.

Offline dmtaTopic starter

  • Newbie
  • Posts: 6
PIC18f4431 PID loop implementation
« on: June 29, 2013, 02:35:24 pm »
Hi all !!!!!

I want to implement a PID motor position controller in PIC18F4431. I read the motor position using a quadrature encoder and use the built in QEI to get the motor position. The PID loop is calculated every 0.52S inside the TIMER1 overflow ISR. This is my code (all variables are global).

How do I now convert this to PWM to drive the motor ?

Code: [Select]
void interrupt(){
if(PIR1.TMR1IF==1){                   // every 0.52 seconds the PID O/P is calculated
         
          ERR_P1 = ERR_P2;            // get previous position error
          ERR_P2 = SET_P - POS;       // calculate present position error
          ERR_AC = ERR_AC + ERR_P2;   // calculate accumilated error
          PID_P  = (float)Kp*ERR_P2 + (float)Ki*ERR_AC*0.52 + (float)Kd*(ERR_P2-ERR_P1)/(0.52);
                   
          PIR1.TMR1IF = 0;
}
}

BTW I am using a 360 pulses per revolution quadrature encoder and operating in the X4 mode of the QEI module i.e. when the shaft rotates 1 round (360°) the motor max value of "POS" will be 360×4-1=1439
« Last Edit: June 29, 2013, 02:37:00 pm by dmta »
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1055
  • Country: fi
Re: PIC18f4431 PID loop implementation
« Reply #1 on: June 29, 2013, 03:18:46 pm »
Simply write the output of your PID calculation into PWM duty cycle set register, possibly with scale/offset/limit operations.

Regards,
Janne
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf