1 | /* Class Transducer
|
---|
2 | *
|
---|
3 | * This class contains the constructor to create an instance of
|
---|
4 | * a Transducer with a time constant, tConst, and a gain, tGain
|
---|
5 | * and the methods needed to use this process in simulation
|
---|
6 | * of control loops.
|
---|
7 | *
|
---|
8 | * This class is a subclass of the superclass BlackBox.
|
---|
9 | *
|
---|
10 | * Author: Michael Thomas Flanagan.
|
---|
11 | *
|
---|
12 | * Created: October 2009
|
---|
13 | * Updates: 2-7 November 2009, 24 May 2010
|
---|
14 | *
|
---|
15 | *
|
---|
16 | * DOCUMENTATION:
|
---|
17 | * See Michael T Flanagan's JAVA library on-line web page:
|
---|
18 | * http://www.ee.ucl.ac.uk/~mflanaga/java/Transducer.html
|
---|
19 | * http://www.ee.ucl.ac.uk/~mflanaga/java/
|
---|
20 | *
|
---|
21 | * Copyright (c) 2002 - 2010 Michael Thomas Flanagan
|
---|
22 | *
|
---|
23 | * PERMISSION TO COPY:
|
---|
24 | *
|
---|
25 | * Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
|
---|
26 | * provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
|
---|
27 | * and associated documentation or publications.
|
---|
28 | *
|
---|
29 | * Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
|
---|
30 | * and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
|
---|
31 | *
|
---|
32 | * Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
|
---|
33 | * the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
|
---|
34 | *
|
---|
35 | * Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
|
---|
36 | * Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
|
---|
37 | * or its derivatives.
|
---|
38 | *
|
---|
39 | ***************************************************************************************/
|
---|
40 |
|
---|
41 |
|
---|
42 | package agents.anac.y2015.agentBuyogV2.flanagan.control;
|
---|
43 | import agents.anac.y2015.agentBuyogV2.flanagan.complex.Complex;
|
---|
44 | import agents.anac.y2015.agentBuyogV2.flanagan.complex.ComplexPoly;
|
---|
45 | import agents.anac.y2015.agentBuyogV2.flanagan.plot.*;
|
---|
46 |
|
---|
47 | public class Transducer extends BlackBox{
|
---|
48 |
|
---|
49 | private double tGain = 1.0D; // transducer gain
|
---|
50 | private double tConst = 0.0D; // transducer time constant
|
---|
51 | private double aConst = 1.0D; // a constant in equivalent first order process
|
---|
52 | private double bConst = 1.0D; // b constant in equivalent first order process
|
---|
53 | private double cConst = 0.0D; // c constant in equivalent first order process
|
---|
54 |
|
---|
55 | // Constructor
|
---|
56 | // gain set to 1; time constant set to 0
|
---|
57 | public Transducer(){
|
---|
58 | super("Transducer");
|
---|
59 | super.sPoles = Complex.oneDarray(1);
|
---|
60 | super.setSnumer(new ComplexPoly(1.0D));
|
---|
61 | super.setSdenom(new ComplexPoly(1.0D, 1.0D));
|
---|
62 | super.setZtransformMethod(1);
|
---|
63 | super.addDeadTimeExtras();
|
---|
64 | }
|
---|
65 |
|
---|
66 | // Constructor
|
---|
67 | // within constants set from argument list
|
---|
68 | public Transducer(double tGain, double tConst){
|
---|
69 | super("Transducer");
|
---|
70 | this.tGain = tGain;
|
---|
71 | this.tConst = tConst;
|
---|
72 | this.aConst = tConst;
|
---|
73 | this.bConst = 1.0;
|
---|
74 | this.cConst = tGain;
|
---|
75 | super.sPoles = Complex.oneDarray(1);
|
---|
76 | super.setSnumer(new ComplexPoly(this.cConst));
|
---|
77 | super.setSdenom(new ComplexPoly(this.bConst, this.aConst));
|
---|
78 | super.setZtransformMethod(1);
|
---|
79 | super.addDeadTimeExtras();
|
---|
80 | }
|
---|
81 |
|
---|
82 | // Constructor
|
---|
83 | // time constant set to zero
|
---|
84 | public Transducer(double tGain){
|
---|
85 | super("Transducer");
|
---|
86 | this.tGain = tGain;
|
---|
87 | this.tConst = 0.0;
|
---|
88 | this.aConst = 0.0;
|
---|
89 | this.bConst = 1.0;
|
---|
90 | this.cConst = tGain;
|
---|
91 | super.sPoles = Complex.oneDarray(1);
|
---|
92 | super.setSnumer(new ComplexPoly(this.cConst));
|
---|
93 | super.setSdenom(new ComplexPoly(this.bConst, this.aConst));
|
---|
94 | super.setZtransformMethod(1);
|
---|
95 | super.addDeadTimeExtras();
|
---|
96 | }
|
---|
97 | // Set coefficients
|
---|
98 | public void setCoeff(double tGain, double tConst){
|
---|
99 | this.tGain = tGain;
|
---|
100 | this.tConst = tConst;
|
---|
101 | this.aConst = tConst;
|
---|
102 | this.bConst = 1.0;
|
---|
103 | this.cConst = tGain;
|
---|
104 | Complex[] num = Complex.oneDarray(1);
|
---|
105 | num[0].reset(this.cConst, 0.0);
|
---|
106 | super.sNumer.resetPoly(num);
|
---|
107 | Complex[] den = Complex.oneDarray(2);
|
---|
108 | den[0].reset(this.bConst, 0.0);
|
---|
109 | den[1].reset(this.aConst, 0.0);
|
---|
110 | super.sDenom.resetPoly(den);
|
---|
111 | this.calcPolesZerosS();
|
---|
112 | super.addDeadTimeExtras();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void setTimeConstant(double tConst){
|
---|
116 | this.tConst = tConst;
|
---|
117 | this.aConst = tConst;
|
---|
118 | Complex co = new Complex(this.aConst, 0.0);
|
---|
119 | super.sDenom.resetCoeff(1, co);
|
---|
120 | this.calcPolesZerosS();
|
---|
121 | super.addDeadTimeExtras();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void setGain(double tGain){
|
---|
125 | this.tGain = tGain;
|
---|
126 | this.cConst = tGain;
|
---|
127 | Complex co = new Complex(this.cConst, 0.0);
|
---|
128 | super.sNumer.resetCoeff(0, co);
|
---|
129 | this.calcPolesZerosS();
|
---|
130 | super.addDeadTimeExtras();
|
---|
131 | }
|
---|
132 |
|
---|
133 | // Get coefficients
|
---|
134 | public double getGain(){
|
---|
135 | return this.tGain;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public double getTimeConstant(){
|
---|
139 | return this.tConst;
|
---|
140 | }
|
---|
141 |
|
---|
142 | // Calculate the zeros and poles in the s-domain
|
---|
143 | protected void calcPolesZerosS(){
|
---|
144 | super.sPoles = Complex.oneDarray(1);
|
---|
145 | super.sPoles[0].setReal(-bConst/aConst);
|
---|
146 | super.sNumerScaleFactor = super.sNumer.coeffCopy(0);
|
---|
147 | super.sNumerScaleFactor = BlackBox.scaleFactor(super.sNumer, super.sPoles);
|
---|
148 |
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | // Plots the time course for a step input
|
---|
153 | public void stepInput(double stepMag, double finalTime){
|
---|
154 |
|
---|
155 | if(this.tConst==0.0){
|
---|
156 | // Calculate time course outputs
|
---|
157 | int n = 51; // number of points on plot
|
---|
158 | double incrT = finalTime/(double)(n-2); // plotting increment
|
---|
159 | double cdata[][] = new double [2][n]; // plotting array
|
---|
160 |
|
---|
161 | cdata[0][0]=0.0D;
|
---|
162 | cdata[0][1]=0.0D;
|
---|
163 | for(int i=2; i<n; i++){
|
---|
164 | cdata[0][i]=cdata[0][i-1]+incrT;
|
---|
165 | }
|
---|
166 | double kpterm = this.tGain*stepMag;
|
---|
167 | cdata[1][0]=0.0D;
|
---|
168 | for(int i=1; i<n; i++){
|
---|
169 | cdata[1][i] = kpterm;
|
---|
170 | }
|
---|
171 | if(super.deadTime!=0.0D)for(int i=0; i<n; i++)cdata[0][i] += super.deadTime;
|
---|
172 |
|
---|
173 | // Plot
|
---|
174 | PlotGraph pg = new PlotGraph(cdata);
|
---|
175 |
|
---|
176 | pg.setGraphTitle("Step Input Transient: Step magnitude = "+stepMag);
|
---|
177 | pg.setGraphTitle2(this.getName());
|
---|
178 | pg.setXaxisLegend("Time");
|
---|
179 | pg.setXaxisUnitsName("s");
|
---|
180 | pg.setYaxisLegend("Output");
|
---|
181 | pg.setPoint(0);
|
---|
182 | pg.setLine(3);
|
---|
183 | pg.plot();
|
---|
184 | }
|
---|
185 | else{
|
---|
186 | super.stepInput(stepMag, finalTime);
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | // Perform z transform using an already set delta T
|
---|
192 | public void zTransform(){
|
---|
193 | if(super.deltaT==0.0D)System.out.println("z-transform attempted in Transducer with a zero sampling period");
|
---|
194 | super.deadTimeWarning("zTransform");
|
---|
195 | if(ztransMethod==0){
|
---|
196 | this.mapstozAdHoc();
|
---|
197 | }
|
---|
198 | else{
|
---|
199 | Complex[] ncoef = null;
|
---|
200 | Complex[] dcoef = null;
|
---|
201 | switch(this.integMethod){
|
---|
202 | // Trapezium rule
|
---|
203 | case 0: ncoef = Complex.oneDarray(2);
|
---|
204 | ncoef[0].reset(this.deltaT*this.cConst,0.0D);
|
---|
205 | ncoef[1].reset(this.deltaT*this.cConst,0.0D);
|
---|
206 | super.zNumer=new ComplexPoly(1);
|
---|
207 | super.zNumer.resetPoly(ncoef);
|
---|
208 | super.zNumerDeg=1;
|
---|
209 | dcoef = Complex.oneDarray(2);
|
---|
210 | dcoef[0].reset(this.bConst*this.deltaT - 2*this.aConst,0.0D);
|
---|
211 | dcoef[1].reset(this.bConst*this.deltaT + 2*this.aConst,0.0D);
|
---|
212 | super.zDenom=new ComplexPoly(1);
|
---|
213 | super.zDenom.resetPoly(dcoef);
|
---|
214 | super.zDenomDeg=1;
|
---|
215 | super.zZeros = Complex.oneDarray(1);
|
---|
216 | super.zZeros[0].reset(-1.0D, 0.0D);
|
---|
217 | super.zPoles = Complex.oneDarray(1);
|
---|
218 | super.zPoles[0].reset((2.0D*this.aConst-super.deltaT*this.bConst)/(2.0D*this.aConst+super.deltaT*this.bConst), 0.0D);
|
---|
219 | break;
|
---|
220 | // Backward rectangulr rule
|
---|
221 | case 1: ncoef = Complex.oneDarray(2);
|
---|
222 | ncoef[0].reset(0.0D,0.0D);
|
---|
223 | ncoef[1].reset(this.cConst*this.deltaT,0.0D);
|
---|
224 | super.zNumer=new ComplexPoly(1);
|
---|
225 | super.zNumer.resetPoly(ncoef);
|
---|
226 | super.zNumerDeg=1;
|
---|
227 | dcoef = Complex.oneDarray(2);
|
---|
228 | dcoef[0].reset(this.bConst*this.deltaT + this.aConst,0.0D);
|
---|
229 | dcoef[1].reset(this.aConst,0.0D);
|
---|
230 | super.zDenom=new ComplexPoly(1);
|
---|
231 | super.zDenom.resetPoly(dcoef);
|
---|
232 | super.zDenomDeg=1;
|
---|
233 | super.zZeros = Complex.oneDarray(1);
|
---|
234 | super.zZeros[0].reset(0.0D, 0.0D);
|
---|
235 | super.zPoles = Complex.oneDarray(1);
|
---|
236 | super.zPoles[0].reset(this.aConst/(super.deltaT*this.bConst+this.aConst), 0.0D);
|
---|
237 | break;
|
---|
238 | // Foreward rectangular rule
|
---|
239 | case 2: ncoef = Complex.oneDarray(1);
|
---|
240 | ncoef[0].reset(this.cConst*this.deltaT,0.0D);
|
---|
241 | super.zNumer=new ComplexPoly(0);
|
---|
242 | super.zNumer.resetPoly(ncoef);
|
---|
243 | super.zNumerDeg=0;
|
---|
244 | dcoef = Complex.oneDarray(2);
|
---|
245 | dcoef[0].reset(-this.aConst,0.0D);
|
---|
246 | dcoef[1].reset(this.bConst*this.deltaT - this.aConst,0.0D);
|
---|
247 | super.zDenom=new ComplexPoly(1);
|
---|
248 | super.zDenom.resetPoly(dcoef);
|
---|
249 | super.zDenomDeg=1;
|
---|
250 | super.zPoles = Complex.oneDarray(1);
|
---|
251 | super.zPoles[0].reset(this.aConst/(super.deltaT*this.bConst-this.aConst), 0.0D);
|
---|
252 | break;
|
---|
253 | default: System.out.println("Integration method option in Transducer must be 0,1 or 2");
|
---|
254 | System.out.println("It was set at "+integMethod);
|
---|
255 | System.out.println("z-transform not performed");
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | // Perform z transform setting delta T
|
---|
261 | public void zTransform(double deltaT){
|
---|
262 | super.setDeltaT(deltaT);
|
---|
263 | this.zTransform();
|
---|
264 | }
|
---|
265 |
|
---|
266 | // Get the s-domain output for a given s-value and a given input.
|
---|
267 | public Complex getOutputS(Complex sValue, Complex iinput){
|
---|
268 | super.sValue=sValue;
|
---|
269 | super.inputS=iinput;
|
---|
270 | return this.getOutputS();
|
---|
271 | }
|
---|
272 |
|
---|
273 | // Get the s-domain output for the stored input and s-value.
|
---|
274 | public Complex getOutputS(){
|
---|
275 | Complex num = Complex.plusOne();
|
---|
276 | num = num.times(this.cConst);
|
---|
277 | Complex den = new Complex();
|
---|
278 | den = this.sValue.times(this.aConst);
|
---|
279 | den = den.plus(this.bConst);
|
---|
280 | Complex term = new Complex();
|
---|
281 | term = num.over(den);
|
---|
282 | super.outputS = term.times(super.inputS);
|
---|
283 | if(super.deadTime!=0.0D)super.outputS = super.outputS.times(Complex.exp(super.sValue.times(-super.deadTime)));
|
---|
284 | return super.outputS;
|
---|
285 | }
|
---|
286 |
|
---|
287 | // Calculate the current time domain output for a given input and given time
|
---|
288 | // resets deltaT
|
---|
289 | public void calcOutputT(double ttime, double inp){
|
---|
290 | super.setInputT(ttime, inp);
|
---|
291 | this.calcOutputT();
|
---|
292 | }
|
---|
293 |
|
---|
294 | // Get the output for the stored sampled input, time and deltaT.
|
---|
295 | public void calcOutputT(){
|
---|
296 | super.deadTimeWarning("calcOutputT()");
|
---|
297 | super.outputT[sampLen-1] = (this.bConst*super.inputT[sampLen-1] + this.aConst*(super.inputT[sampLen-1]-super.inputT[sampLen-2])/super.deltaT)/this.cConst;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | // Get the s-domain zeros
|
---|
302 | public Complex[] getSzeros(){
|
---|
303 | System.out.println("This standard first order process (class Transducer) has no s-domain zeros");
|
---|
304 | return null;
|
---|
305 | }
|
---|
306 |
|
---|
307 | // Deep copy
|
---|
308 | public Transducer copy(){
|
---|
309 | if(this==null){
|
---|
310 | return null;
|
---|
311 | }
|
---|
312 | else{
|
---|
313 | Transducer bb = new Transducer();
|
---|
314 | this.copyBBvariables(bb);
|
---|
315 |
|
---|
316 | bb.aConst = this.aConst;
|
---|
317 | bb.bConst = this.bConst;
|
---|
318 | bb.cConst = this.cConst;
|
---|
319 |
|
---|
320 | return bb;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | // Clone - overrides Java.Object method clone
|
---|
325 | public Object clone(){
|
---|
326 | return (Object)this.copy();
|
---|
327 | }
|
---|
328 | }
|
---|