Author Topic: pid in microcontroller  (Read 3079 times)

0 Members and 1 Guest are viewing this topic.

Offline rabochidTopic starter

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
  • From the cradle to the grave
pid in microcontroller
« on: December 01, 2010, 10:24:52 AM »
hi i want to implement a pid algorithm in pic16f877 we have u (n) = k * e (n) + kd [e (n ')-e (n'-1)]
how can I calculate n and n '
spped max for my motor 550RPM
encoder 120count/rev
pic is clocked at 20MHz

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: pid in microcontroller
« Reply #1 on: December 01, 2010, 10:47:22 AM »
u (n) and e (n)
are math functions at n sample of data.
The n is the discrete sample number.

e (n ')
is the first derivative of the Error term at sample n

e (n'-1)
is the first derivative of the Error term at the sample before n

Each of these functions are a short cut to another equation so that the PID equation is easier the write and doesn't become overly complicated at the theoretical level.

The equation you posted is the theoretical math. It isn't easy to turn this directly into code.
Microchip does have a App Notes using a PID algorithm for motor control with example source code. See AN532 & AN937. These explain how to implement PID in a PIC.

Note that the equation you posted is only PD (proportional/Derivative) not PID which also includes a term for the Integral.

Offline rabochidTopic starter

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
  • From the cradle to the grave
Re: pid in microcontroller
« Reply #2 on: December 01, 2010, 12:22:58 PM »
I meant how can i get the max and sampling time at pic clicked at 20MHZ

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: pid in microcontroller
« Reply #3 on: December 01, 2010, 01:06:31 PM »
I meant how can i get the max and sampling time at pic clicked at 20MHZ

The sampling time is determined by the system response time and can be determined by modeling or, better, measuring. Then you when you write the code you determine a time interval that is appropriate and convenient for the processor used. Using one of the PIC's hardware timers is a good way.

Your PIC runs at 200nsec per instruction cycle. One of the PIC's hardware timer/counters and an ISR can be used to set a sampling interval.

Did you read AN532 & AN937?.
Did you google PID, PID timing, etc and read?
« Last Edit: December 01, 2010, 01:57:23 PM by waltr »

Offline rabochidTopic starter

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
  • From the cradle to the grave
Re: pid in microcontroller
« Reply #4 on: December 01, 2010, 01:57:19 PM »
I'm sorry i mean max simpling time
in the theory we use the theorem of shannon  Tsmin>=2*Ts
and Tmax from the ADC Tc

Offline rabochidTopic starter

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
  • From the cradle to the grave
Re: pid in microcontroller
« Reply #5 on: December 01, 2010, 02:07:08 PM »
thank you waltr

 

SMF spam blocked by CleanTalk