Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: rabochid on December 01, 2010, 10:24:52 AM

Title: pid in microcontroller
Post by: rabochid 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
Title: Re: pid in microcontroller
Post by: waltr 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.
Title: Re: pid in microcontroller
Post by: rabochid on December 01, 2010, 12:22:58 PM
I meant how can i get the max and sampling time at pic clicked at 20MHZ
Title: Re: pid in microcontroller
Post by: waltr 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?
Title: Re: pid in microcontroller
Post by: rabochid 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
Title: Re: pid in microcontroller
Post by: rabochid on December 01, 2010, 02:07:08 PM
thank you waltr