Forum Discussion
Implementation of Digital PID controller in FPGA
I'm trying to implement a PI controller in FPGA using schematic entry method in altera quartus II . I'm using the following equivalent PI controller equation,
c(n) = c(n-1)+kp*(e(n)-e(n-1))+Ki*T*e(n-1) where, c(n) = controller output Kp= proportional constant Ki=integral constant e(n)=Reference -actual input T=sampling period I'm implementing it for 8 bit data. I couldnot implement c(n-1) in the above equation using schematic entry. Can someone help me implement this? I would be grateful if you can suggest me other ways of implementing PI controller like in VHDL.30 Replies
- Altera_Forum
Honored Contributor
I'm using a package of extended arithmetic functions for such purposes. Basically I leave it to the compiler to find an appropriate way to realize the intended behaviour. You may want to compare the achieved effectivity with handcoded saturation arithmetic, I'm satisfied with it up to now.
FUNCTION SUM (X1,X2: SIGNED) RETURN SIGNED IS VARIABLE S:SIGNED(X1'length-1 downto 0); BEGIN S:=X1+X2; IF X1>=0 AND X2>=0 AND S<0 THEN S:= (others => '1'); S(S'left):='0'; ELSIF X1<0 AND X2<0 AND S>=0 THEN S(S'left-1 downto 0):= (others => '0'); S(S'left):='1'; END IF; RETURN S; END; - Altera_Forum
Honored Contributor
Thank you very much for your help!! The problem is that i've always used schematic blocks and i've never used vhdl language, and so i don't know how to integrate this code with the adders i've used. can you help me in that operation? thank you so much!!
- Altera_Forum
Honored Contributor
Hi all,
Te code of FvM is well written but I am not sure what is its purpose. You only need to clip (saturate) if you are going to discard one or more MSBs. If you discard LSBs then there will never be the need for saturation, possibly you may go for rounding. In short: Truncation of one LSB = divide by 2 so no clipping needed but rounding. Truncation of one MSB is relatively = multiply by 2 because you would discard one LSB bit less so it needs clipping To implement saturation in schematic or HDL: Just test the MSB you want to discard: if it equals the opposite of sign bit then fill up data with that opposite. Note however, in many real systems you should avoid clipping as it affects signal integrity. - Altera_Forum
Honored Contributor
Simply, when adding numbers without providing extra result bits, the result may overflow to opposite sign, e. g. in case of an integrator
In most real control applications, overflow results in fatal failure, e. g. with said PID controller. Saturation arithmetic prevents from this situation.integ <= integ + x;integ <= SUM(integ,x); - Altera_Forum
Honored Contributor
Hi FvM,
Thanks for the clarification, I agree totally that in cases of feedback overflow will occur and be fatal. This saturation is different from my clipping. We better say overflow protection. Apologies, I can see now what your function is doing, in effect precomputing to prevent overflow. it seems like DSP Vs control systems conflict. - Altera_Forum
Honored Contributor
Hi POWERGUI
To sum up, I hope the following method will work: (we only need saturation if there is feedback. If there is no feedback then we can accommodate the result). Assume adder inputs are x1, x2 each 8 bits 2's complement and adder result is S of 9 bits. for feedback x2 = S(8) & S(6:0). i.e. discard S(7). This brings us back to the concept of MSB discarding algorithm, thus: if S(7) is opposite S(8) then fill up all S(or x2) with opposite of S(8). - Altera_Forum
Honored Contributor
s ur controller working fine now...im supposed to implement a PID controller in spartan kit...
- Altera_Forum
Honored Contributor
Hi !
I want to implement PI Controller in FPGA SPARTRAN 3e kit, using VHDL. Can you please guide me . I am very new in this field. I studied VHDL 3 years ago, and now i have to implement this task. i have to brush bup my VHDL knowledge. Can you guide me step by step. i will be really obliged. also i have code in matlab for same PI controller but i have to convert that to VHDL. Now it depends should i convert that to vhdl. or else i should write a new code. Hope to listen from you. :) - Altera_Forum
Honored Contributor
me too i'm working on that but as i see no one response
- Altera_Forum
Honored Contributor
--- Quote Start --- me too i'm working on that but as i see no one response --- Quote End --- I don't want to be unpolite, but asking for general help about PID implementations without a specific question seems to me like expecting others to write your private tutorial instead of looking into available text books first. When you are asking for help in the forum, you should clarify that you spent some effort to learn about the matter before. Apart from this reservation, you'll surely notice that forum members willingly answer any kind of beginners questions, even ignorant ones. Regards Frank