OpenCores
URL https://opencores.org/ocsvn/pid_controler/pid_controler/trunk

Subversion Repositories pid_controler

[/] [pid_controler/] [trunk/] [pid_controller.vhd] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 aTomek1328
-------------------------------------------------------------------------------
2
-- Title      : Digital PID Controller
3
-- Project    : 
4
-------------------------------------------------------------------------------
5
-- File       : pid_controller.vhd
6
-- Author     : Tomasz Turek  <tomasz.turek@gmail.com>
7
-- Company    : SzuWar ZOO
8
-- Created    : 12:56:06 20-07-2010
9 5 aTomek1328
-- Last update: 20:35:49 13-10-2010
10 3 aTomek1328
-- Platform   : Xilinx ISE 10.1.03
11
-- Standard   : VHDL'93/02
12
-------------------------------------------------------------------------------
13
-- Description:
14
--                            PID CONTROLLER
15
--                                
16 5 aTomek1328
--                                 ___________                               ___
17
--                                |           |                             |   |
18
--                            |-->|  KP Gain  |---------------------------->| + |
19
--                            |   |___________|                             |   |
20
--                            |                     ___________             |   |
21
--                            |                    |  -iDelayD |            |   |   
22
--                            |                 |->| Z         |<-|         |   |    
23
--                            |                 |  |___________|  |         |   |
24
--                            |                 |                 |   ___   |   |
25
--                            |                 |                 |  |   |  |   |    _________
26
--  _______     ___________   |    ___________  |                 |->| - |  |   |   |         |
27
-- |       |   |           |  |   |           | |                    |   |->| + |-->| correct |
28
-- | error |-->|  KM Gain  |--|-->|  KD Gain  |-|------------------->| + |  |   |   |_________|
29
-- |_______|   |___________|  |   |___________|                      |___|  |   |
30
--                            |                      ____                   |   |
31
--                            |                     |  -1|                  |   |
32
--                            |                  |--| Z  |<---|             |   |
33
--                            |                  |  |____|    |             |   |
34
--                            |                  |    ___     |             |   |
35
--                            |                  |   |   |    |             |   |
36
--                            |    ___________   |-->| + |    |             |   |
37
--                            |   |           |      |   |----|------------>| + |
38
--                            |-->|  KI Gain  |----->| + |                  |   |
39
--                                |___________|      |___|                  |___|
40 3 aTomek1328
--
41
-------------------------------------------------------------------------------
42
-- Copyright (c) 2010 SzuWar ZOO
43
-------------------------------------------------------------------------------
44
-- Revisions  :
45
-- Date                  Version  Author  Description
46
-- 12:56:06 20-07-2010   1.0      aTomek  Created
47
-- 19:29:34 04-10-2010   1.1      aTomek  Created
48 5 aTomek1328
-- 20:35:02 13-10-2010   1.2      aTomek  Created
49 3 aTomek1328
-------------------------------------------------------------------------------
50
 
51
library IEEE;
52
use IEEE.STD_LOGIC_1164.ALL;
53
use IEEE.STD_LOGIC_ARITH.ALL;
54
use IEEE.STD_LOGIC_UNSIGNED.ALL;
55
 
56
entity pid_controller is
57
 
58
   generic
59
      (
60
            -- size of input and output data --
61
            iDataWidith    : integer range 8 to 32 := 8;
62
            -- proportionally gain --
63
            iKP            : integer range 0 to 7  := 3;  -- 0 - /2, 1 - /4, 2 - /8, 3 - /16, 4 - /32, 5 - /64, 6 - /128, 7 - /256
64
            -- integral gain --
65
            iKI            : integer range 0 to 7  := 2;  -- 0 - /2, 1 - /4, 2 - /8, 3 - /16, 4 - /32, 5 - /64, 6 - /128, 7 - /256
66
            -- differential gain --
67
            iKD            : integer range 0 to 7  := 2;  -- 0 - /2, 1 - /4, 2 - /8, 3 - /16, 4 - /32, 5 - /64, 6 - /128, 7 - /256
68
            -- master gain --
69
            iKM            : integer range 0 to 7  := 1;  -- 0 - /1, 1 - /2, 2 - /4, 3 - /8 , 4 - /16, 5 - /32, 6 - /64 , 7 - /128
70
            -- delay between samples of error --
71
            iDelayD        : integer range 1 to 16 := 10;
72
            -- 0 - controller use derivative of PATERN_I and PATERN_ESTIMATION_I, 1 - controller use error to work --
73
            iWork          : integer range 0 to 1  := 1
74
            );
75
 
76
   port
77
      (
78
            CLK_I               : in  std_logic;
79
            RESET_I             : in  std_logic;
80
            -- error  --
81
            ERROR_I             : in  std_logic_vector(iDataWidith - 1 downto 0);
82
            -- threshold --
83
            PATERN_I            : in  std_logic_vector(iDataWidith - 1 downto 0);
84
            -- current sample --
85
            PATERN_ESTIMATION_I : in  std_logic_vector(iDataWidith - 1 downto 0);
86
            -- correction --
87
            CORRECT_O           : out std_logic_vector(iDataWidith - 1 downto 0)
88
            );
89
 
90
end entity pid_controller;
91
 
92
architecture rtl of pid_controller is
93
-------------------------------------------------------------------------------
94
-- functions --
95
-------------------------------------------------------------------------------
96
-- purpose: make a std_logic_vector of size c_size and build from c_value --
97
   function f_something ( constant c_size : integer; signal c_value : std_logic) return std_logic_vector is
98
 
99
      variable var_temp : std_logic_vector(c_size - 1 downto 0);
100
 
101
   begin  -- function f_something --
102
 
103
      var_temp := (others => c_value);
104
 
105
      return var_temp;
106
 
107
   end function f_something;
108
-- examples:
109
-- f_something(c_size => 3 , c_value => 'Z')  == "ZZZ"
110
-- f_something(c_size => 3 , c_value => '1')  == "111"
111
-- ...
112
-------------------------------------------------------------------------------
113
-- types --
114
-------------------------------------------------------------------------------
115
   -- delay register --
116
   type type_sr is array (0 to iDelayD - 1) of std_logic_vector(iDataWidith - 1 downto 0);
117
 
118
-------------------------------------------------------------------------------
119
-- signals --
120
-------------------------------------------------------------------------------
121
   signal v_error    : std_logic_vector(iDataWidith - 1 downto 0);
122
   signal v_error_KM : std_logic_vector(iDataWidith - 1 downto 0);
123
   signal v_error_KP : std_logic_vector(iDataWidith - 1 downto 0);
124
   signal v_error_KD : std_logic_vector(iDataWidith - 1 downto 0);
125
   signal v_error_KI : std_logic_vector(iDataWidith - 1 downto 0);
126
   signal t_div_late : type_sr;
127
   signal v_div      : std_logic_vector(iDataWidith - 1 downto 0);
128
   signal v_acu_earl : std_logic_vector(iDataWidith - 1 downto 0);
129
   signal v_acu      : std_logic_vector(iDataWidith - 1 downto 0);
130
   signal v_sum      : std_logic_vector(iDataWidith - 1 downto 0);
131
 
132
begin  -- architecture rtl --
133
 
134
-- choice source of input data --
135
   v_error <= ERROR_I                                                                             when iWork = 1 else
136
              conv_std_logic_vector(signed(PATERN_I) - signed(PATERN_ESTIMATION_I) , iDataWidith) when iWork = 0 else
137
              (others => '0');
138
-- master gain execute by shift of iKM bits to the right --
139
   v_error_KM <= v_error                                                                                                when iKM = 0 else
140
                 f_something(c_size => iKM , c_value => v_error(iDataWidith - 1)) & v_error(iDataWidith - 1 downto iKM) when iKM > 0 else
141
                 (others => '0');
142
 
143
-- proportionally gain execute by shift of (iKP - 1) bits to the right --
144
   v_error_KP <= f_something(c_size => iKP + 1 , c_value => v_error_KM(iDataWidith - 1)) & v_error_KM(iDataWidith - 1 downto iKP + 1);
145
 
146
-- derivative gain execute by shift of (iKD - 1) bits to the right --
147
   v_error_KD <= f_something(c_size => iKD + 1 , c_value => v_error_KM(iDataWidith - 1)) & v_error_KM(iDataWidith - 1 downto iKD + 1);
148
 
149
-- integral gain execute by shift of (iKI + 1) bits to the right --
150
   v_error_KI <= f_something(c_size => iKI + 1 , c_value => v_error_KM(iDataWidith - 1)) & v_error_KM(iDataWidith - 1 downto iKI + 1);
151
 
152
   DI00: process (CLK_I) is
153
   begin  -- process DI00
154
 
155
      if rising_edge(CLK_I) then
156
 
157
         -- synchronous reset --
158
         if RESET_I = '1' then
159
 
160
            t_div_late <= (others => (others => '0'));
161
            v_div      <= (others => '0');
162
            v_acu      <= (others => '0');
163
            v_acu_earl <= (others => '0');
164
 
165
         else
166
 
167
            -- delay register --
168
            t_div_late <= v_error_KD & t_div_late(0 to iDelayD - 2);
169
 
170
            -- difference between samples --
171
            v_div <= conv_std_logic_vector(signed(v_error_KD) - signed(t_div_late(iDelayD - 1)) , iDataWidith);
172
 
173
            -- integration of error --
174
            v_acu <= conv_std_logic_vector(signed(v_error_KI) + signed(v_acu_earl) , iDataWidith);
175
            -- sum of N - 1 samples of error --
176
            v_acu_earl <= v_acu;
177
 
178
 
179
         end if;
180
 
181
      end if;
182
 
183
   end process DI00;
184
 
185
   -- first stage of adder -- 
186
   v_sum <= conv_std_logic_vector(signed(v_acu) + signed(v_div) , iDataWidith);
187
   -- correction and second stage of adder --
188
   CORRECT_O <= conv_std_logic_vector(signed(v_error_KP) + signed(v_sum) , iDataWidith) when RESET_I  = '0' else
189
                (others => '0');
190
 
191
end architecture rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.