source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/control/OpenLoop.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 20.8 KB
Line 
1/* Class OpenLoop
2*
3* This class supports the creation of a path of Black Boxes
4* i.e. of instances of BlackBox and of any of its subclasses,
5* e.g. PropIntDeriv, FirstOrder, and the methods to combine
6* these into both a single instance of BlackBox and a Vector
7* of analogue segments, digital segments and converters.
8*
9* Author: Michael Thomas Flanagan.
10*
11* Created: August 2002
12* Updated: 12 July 2003, 10 May 2005, 2 July 2006, 7 June 2007, 6 April 2008, 5 July 2008, 2-7 November 2009
13*
14* DOCUMENTATION:
15* See Michael T Flanagan's JAVA library on-line web page:
16* http://www.ee.ucl.ac.uk/~mflanaga/java/OpenLoop.html
17* http://www.ee.ucl.ac.uk/~mflanaga/java/
18*
19*
20* Copyright (c) 2002 - 2009 Michael Thomas Flanagan
21*
22* PERMISSION TO COPY:
23* Permission to use, copy and modify this software and its documentation for
24* NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
25* to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
26*
27* Dr Michael Thomas Flanagan makes no representations about the suitability
28* or fitness of the software for any or for a particular purpose.
29* Michael Thomas Flanagan shall not be liable for any damages suffered
30* as a result of using, modifying or distributing this software or its derivatives.
31*
32***************************************************************************************/
33
34package agents.anac.y2015.agentBuyogV2.flanagan.control;
35
36import java.util.ArrayList;
37import java.util.Vector;
38
39import agents.anac.y2015.agentBuyogV2.flanagan.complex.Complex;
40import agents.anac.y2015.agentBuyogV2.flanagan.complex.ComplexPoly;
41
42public class OpenLoop extends BlackBox{
43 private ArrayList<BlackBox> openPath = new ArrayList<BlackBox>(); // open path boxes
44 private ArrayList<Object> segments = new ArrayList<Object>(); // start of segment, end of segment, type of each segment, i.e. analogue, digital, AtoD, DtoA, ZOH
45
46 private int nBoxes = 0; // number of boxes in original path
47 private int nSeg = 0; // number of analogue, digital, AtoD, ZOH segments
48
49 private boolean checkPath = false; // true if segment has been called
50 private boolean checkNoMix = true; // true - no ADC or DAC
51 private boolean checkConsolidate = false; // true if consolidate has been called
52
53 private boolean[] adcs = null; // true if box = ADC
54 private boolean[] dacs = null; // true ifbox = DAC
55 private boolean[] zeroHolds = null; // true ifbox = Zero Order Hold
56
57 // Constructor
58 public OpenLoop(){
59 super("OpenLoop");
60 }
61
62 // Add box to the open path
63 public void addBoxToPath(BlackBox box){
64 this.openPath.add(box);
65 this.nBoxes++;
66 }
67
68 // Consolidate all boxes into appropriate segments and combine all boxes into one box
69 public void consolidate(){
70 // Empty segments ArrayList if openPath ArrayList has been updated
71 if(!segments.isEmpty()){
72 segments.clear();
73 this.nBoxes = 0;
74 this.nSeg = 0;
75 this.checkNoMix = true;
76 this.checkPath = false;
77 }
78
79 // Find analogue, digital and conversion segments in OpenLoop
80 this.segment();
81
82 // Combine all boxes into a single box and make this instance that combined box
83 BlackBox aa = null;
84 if(this.nSeg==1){
85 if(this.nBoxes==1){
86 aa = (BlackBox) this.openPath.get(0);
87 }
88 else{
89 aa = (BlackBox) this.segments.get(3);
90 }
91 }
92 else{
93 aa = this.combineSegment(0, this.nBoxes);
94 }
95 super.sNumer = aa.sNumer.copy();
96 super.sDenom = aa.sDenom.copy();
97 super.sNumerPade = aa.sNumerPade.copy();
98 super.sDenomPade = aa.sDenomPade.copy();
99 super.sNumerDeg = aa.sNumerDeg;
100 super.sDenomDeg = aa.sDenomDeg;
101 super.sNumerDegPade = aa.sNumerDegPade;
102 super.sDenomDegPade = aa.sDenomDegPade;
103 super.sNumerSet = true;
104 super.sDenomSet = true;
105 super.deadTime = aa.deadTime;
106 super.sZeros = Complex.copy(aa.sZeros);
107 super.sPoles = Complex.copy(aa.sPoles);
108 super.sZerosPade = Complex.copy(aa.sZerosPade);
109 super.sPolesPade = Complex.copy(aa.sPolesPade);
110 super.padeAdded=true;
111 if(super.sNumerDeg==0){
112 super.sNumerScaleFactor = super.sNumer.coeffCopy(0);
113 }
114 else{
115 super.sNumerScaleFactor = BlackBox.scaleFactor(super.sNumer, super.sZeros);
116 }
117 if(super.sDenomDeg==0){
118 super.sDenomScaleFactor = super.sDenom.coeffCopy(0);
119 }
120 else{
121 super.sDenomScaleFactor = BlackBox.scaleFactor(super.sDenom, super.sPoles);
122 }
123 this.checkConsolidate = true;
124 }
125
126 // Find analogue and digital segments
127 public void segment(){
128 // Find ADCs, DACs and ZeroOrderHolds
129 this.adcs = new boolean[nBoxes];
130 int nADCs = 0;
131 this.dacs = new boolean[nBoxes];
132 int nDACs = 0;
133 this.zeroHolds = new boolean[nBoxes];
134 int nZeroHolds = 0;
135 String thisName = null;
136 for(int i=0; i<nBoxes; i++){
137 adcs[i] = false;
138 dacs[i] = false;
139 zeroHolds[i] = false;
140 BlackBox aa = openPath.get(i);
141 thisName = aa.fixedName;
142 if(thisName.equals("ADC")){
143 adcs[i]=true;
144 nADCs++;
145 }
146 else{
147 if(thisName.equals("DAC")){
148 dacs[i]=true;
149 nDACs++;
150 }
151 else{
152 if(thisName.equals("ZeroOrderHold")){
153 zeroHolds[i]=true;
154 nZeroHolds++;
155 }
156 }
157 }
158 }
159
160 if(nADCs==0 && nDACs==0){
161 this.nSeg = 1; // number of analogue, digital, AtoD, ZOH segments
162 this.checkNoMix = true; // true - no ADC or DAC
163 this.checkPath = true; // true if segment has been called
164 this.segments.add(new Integer(0));
165 this.segments.add(new Integer(nBoxes-1));
166 this.segments.add("analogue");
167 BlackBox bb = this.combineSegment(0, nBoxes-1);
168 this.segments.add(bb);
169 }
170 else{
171 this.nSeg = 0;
172 int adc0 = 0;
173 int dac0 = 0;
174 boolean adcFirst = false;
175 if(nADCs>0 && nDACs>0){
176 // first adc or dac
177 boolean test0 = true;
178 adc0 = 0;
179 while(test0){
180 if(adcs[adc0]){
181 test0 = false;
182 }
183 else{
184 adc0++;
185 if(adc0>=nBoxes){
186 test0 = false;
187 }
188 }
189 }
190 test0 = true;
191 while(test0){
192 if(dacs[dac0]){
193 test0 = false;
194 }
195 else{
196 dac0++;
197 if(dac0>=nBoxes){
198 test0 = false;
199 }
200 }
201 }
202 if(adc0<dac0)adcFirst =true;
203 }
204 else{
205 if(nADCs>0)adcFirst=true;
206 }
207 boolean adswitch = adcFirst;
208 this.nSeg++;
209
210 int nextStart = 0;
211 if(adcFirst){
212 this.segments.add(new Integer(0));
213 this.segments.add(new Integer(adc0));
214 this.segments.add("digital");
215 BlackBox bb = this.combineSegment(0, adc0);
216 this.segments.add(bb);
217 nextStart = adc0+1;
218 }
219 else{
220 this.segments.add(new Integer(0));
221 this.segments.add(new Integer(dac0));
222 this.segments.add("analogue");
223 BlackBox bb = this.combineSegment(0, dac0);
224 this.segments.add(bb);
225 nextStart = dac0+1;
226 }
227
228 // Find all analogue and digital segments
229 boolean test1 = true;
230 if(nextStart>=this.nBoxes)test1 = false;
231 while(test1){
232 if(adswitch){
233 nextStart = nextDigitalSegment(nextStart);
234 adswitch = false;
235 }
236 else{
237 nextStart = nextAnalogueSegment(nextStart);
238 adswitch = true;
239 }
240 if(nextStart>=this.nBoxes)test1 = false;
241 }
242
243 }
244
245
246
247 }
248
249 // Find next digital segment
250 private int nextDigitalSegment(int box0){
251 // next adc
252 int nextAdc = nBoxes;
253 boolean endFound = false;
254 boolean test = true;
255 int ii = box0;
256 while(test){
257 if(this.adcs[ii]){
258 nextAdc = ii;
259 test = false;
260 }
261 else{
262 ii++;
263 if(ii>=nBoxes)test = false;
264 }
265 }
266
267 // next dac
268 int nextDac = nBoxes;
269 test = true;
270 ii = box0;
271 while(test){
272 if(dacs[ii]){
273 nextDac = ii;
274 test = false;
275 }
276 else{
277 ii++;
278 if(ii>=nBoxes){
279 test = false;
280 endFound = true;
281 }
282 }
283 }
284 if(endFound)nextDac = nBoxes-1;
285 if(nextAdc<nextDac)throw new IllegalArgumentException("Two consecutive ADCs with no intervening DAC");
286 this.nSeg++;
287 this.segments.add(new Integer(0));
288 this.segments.add(new Integer(nextDac));
289 this.segments.add("digital");
290 BlackBox bb = this.combineSegment(0, nextDac);
291 this.segments.add(bb);
292
293 return nextDac + 1;
294 }
295
296 // Find next analogue segment
297 private int nextAnalogueSegment(int box0){
298 // next adc
299 int nextAdc = nBoxes;
300 boolean endFound = false;
301 boolean test = true;
302 int ii = box0;
303 while(test){
304 if(this.adcs[ii]){
305 nextAdc = ii;
306 test = false;
307 }
308 else{
309 ii++;
310 if(ii>=nBoxes){
311 test = false;
312 endFound = true;
313 }
314 }
315 }
316
317 // next dac
318 int nextDac = nBoxes;
319 test = true;
320 ii = box0;
321 while(test){
322 if(dacs[ii]){
323 nextDac = ii;
324 test = false;
325 }
326 else{
327 ii++;
328 if(ii>=nBoxes){
329 test = false;
330 }
331 }
332 }
333 if(endFound)nextAdc = nBoxes-1;
334 if(nextDac<nextAdc)throw new IllegalArgumentException("Two consecutive DACs with no intervening ADC");
335 this.nSeg++;
336 this.segments.add(new Integer(0));
337 this.segments.add(new Integer(nextAdc));
338 this.segments.add("digital");
339 BlackBox bb = this.combineSegment(0, nextAdc);
340 this.segments.add(bb);
341
342 return nextAdc+1;
343 }
344
345 // Combine all boxes between iLow and iHigh into one box
346 public BlackBox combineSegment(int iLow, int iHigh){
347 ArrayList<Complex> zeros = new ArrayList<Complex>();
348 ArrayList<Complex> poles = new ArrayList<Complex>();
349 ArrayList<Complex> zerosPade = new ArrayList<Complex>();
350 ArrayList<Complex> polesPade = new ArrayList<Complex>();
351
352
353 BlackBox aa = new BlackBox(); // Black Box to be returned
354
355 int nBoxSeg = iHigh - iLow + 1; // number of boxes in segment
356
357 BlackBox bb = openPath.get(iLow); // first box in segment
358 if(!bb.padeAdded)bb.transferPolesZeros();
359
360 aa.sNumerPade = bb.sNumerPade.copy();
361 aa.sDenomPade = bb.sDenomPade.copy();
362 aa.sNumer = bb.sNumer.copy();
363 aa.sDenom = bb.sDenom.copy();
364
365 aa.sNumerDegPade = bb.sNumerDegPade;
366 aa.sDenomDegPade = bb.sDenomDegPade;
367 aa.sNumerDeg = bb.sNumerDeg;
368 aa.sDenomDeg = bb.sDenomDeg;
369
370 if(aa.sNumerDegPade>0){
371 Complex[] bbsZerosPade = Complex.copy(bb.sZerosPade);
372 for(int i=0; i<aa.sNumerDegPade; i++)zerosPade.add(bbsZerosPade[i]);
373 }
374 if(aa.sDenomDegPade>0){
375 Complex[] bbsPolesPade = Complex.copy(bb.sPolesPade);
376 for(int i=0; i<aa.sDenomDegPade; i++)polesPade.add(bbsPolesPade[i]);
377 }
378 if(aa.sNumerDeg>0){
379 Complex[] bbsZeros = Complex.copy(bb.sZeros);
380 for(int i=0; i<aa.sNumerDeg; i++)zeros.add(bbsZeros[i]);
381 }
382 if(aa.sDenomDeg>0){
383 Complex[] bbsPoles = Complex.copy(bb.sPoles);
384 for(int i=0; i<aa.sDenomDeg; i++)poles.add(bbsPoles[i]);
385 }
386
387 aa.deadTime = bb.deadTime;
388 aa.sNumerScaleFactor = bb.sNumerScaleFactor.copy();
389 aa.sDenomScaleFactor = bb.sDenomScaleFactor.copy();
390
391 for(int i=1; i<nBoxSeg; i++){
392 bb = this.openPath.get(i+iLow);
393 if(!bb.padeAdded)bb.transferPolesZeros();
394 if(aa.sNumerPade==null){
395 if(bb.sNumerPade!=null){
396 aa.sNumerPade = bb.sNumerPade.copy();
397 }
398 }
399 else{
400 if(bb.sNumerPade!=null){
401 aa.sNumerPade = aa.sNumerPade.times(bb.sNumerPade);
402 }
403 }
404
405 if(aa.sNumer==null){
406 if(bb.sNumer!=null){
407 aa.sNumer = bb.sNumer.copy();
408 }
409 }
410 else{
411 if(bb.sNumer!=null){
412 aa.sNumer = aa.sNumer.times(bb.sNumer);
413 }
414 }
415
416 if(aa.sDenom==null){
417 if(bb.sDenom!=null){
418 aa.sDenom = bb.sDenom.copy();
419 }
420 }
421 else{
422 if(bb.sDenom!=null){
423 aa.sDenom = aa.sDenom.times(bb.sDenom);
424 }
425 }
426
427 if(aa.sDenomPade==null){
428 if(bb.sDenomPade!=null){
429 aa.sDenomPade = bb.sDenomPade.copy();
430 }
431 }
432 else{
433 if(bb.sDenomPade!=null){
434 aa.sDenomPade = aa.sDenomPade.times(bb.sDenomPade);
435 }
436 }
437
438 aa.sNumerDegPade += bb.sNumerDegPade;
439 aa.sDenomDegPade += bb.sDenomDegPade;
440 aa.sNumerDeg += bb.sNumerDeg;
441 aa.sDenomDeg += bb.sDenomDeg;
442
443 aa.sNumerScaleFactor = bb.sNumerScaleFactor.times(aa.sNumerScaleFactor);
444 aa.sDenomScaleFactor = bb.sDenomScaleFactor.times(aa.sDenomScaleFactor);
445
446 aa.deadTime += bb.deadTime;
447
448 if(bb.sNumerDegPade>0){
449 Complex[] bbsZerosPade = Complex.copy(bb.sZerosPade);
450 for(int ii=0; ii<bb.sNumerDegPade; ii++)zerosPade.add(bbsZerosPade[ii]);
451 }
452 if(bb.sDenomDegPade>0){
453 Complex[] bbsPolesPade = Complex.copy(bb.sPolesPade);
454 for(int ii=0; ii<bb.sDenomDegPade; ii++)polesPade.add(bbsPolesPade[ii]);
455 }
456 if(bb.sNumerDeg>0){
457 Complex[] bbsZeros = Complex.copy(bb.sZeros);
458 for(int ii=0; ii<bb.sNumerDeg; ii++)zeros.add(bbsZeros[ii]);
459 }
460 if(bb.sDenomDeg>0){
461 Complex[] bbsPoles = Complex.copy(bb.sPoles);
462 for(int ii=0; ii<bb.sDenomDeg; ii++)poles.add(bbsPoles[ii]);
463 }
464 }
465
466 if(aa.sNumerDegPade>0){
467 aa.sZerosPade = Complex.oneDarray(aa.sNumerDegPade);
468 for(int ii=0; ii<aa.sNumerDegPade; ii++)aa.sZerosPade[ii] = zerosPade.get(ii);
469 }
470 if(aa.sDenomDegPade>0){
471 aa.sPolesPade = Complex.oneDarray(aa.sDenomDegPade);
472 for(int ii=0; ii<aa.sDenomDegPade; ii++)aa.sPolesPade[ii] = polesPade.get(ii);
473 }
474 if(aa.sNumerDeg>0){
475 aa.sZeros = Complex.oneDarray(aa.sNumerDeg);
476 for(int ii=0; ii<aa.sNumerDeg; ii++)aa.sZeros[ii] = zeros.get(ii);
477 }
478 if(aa.sDenomDeg>0){
479 aa.sPoles = Complex.oneDarray(aa.sDenomDeg);
480 for(int ii=0; ii<aa.sDenomDeg; ii++)aa.sPoles[ii] = poles.get(ii);
481 }
482 return aa;
483
484 }
485
486 // Return number of boxes in path
487 public int getNumberOfBoxes(){
488 if(!checkConsolidate)this.consolidate();
489 return this.nBoxes;
490 }
491
492 // Return segment ArrayList
493 public ArrayList<Object> getSegmentsArrayList(){
494 if(!checkConsolidate)this.consolidate();
495 return this.segments;
496 }
497
498 // Return segment Vector
499 public Vector<Object> getSegmentsVector(){
500 if(!checkConsolidate)this.consolidate();
501 ArrayList<Object> seg = this.segments;
502 Vector<Object> ret = null;
503 if(seg!=null){
504 int n = seg.size();
505 ret = new Vector<Object>(n);
506 for(int i=0; i<n; i++)ret.addElement(seg.get(i));
507 }
508 return ret;
509 }
510
511 // Return number of segments in path
512 public int getNumberOfSegments(){
513 if(!checkConsolidate)this.consolidate();
514 return this.nSeg;
515 }
516
517 // Return name of all boxes in path
518 public String getNamesOfBoxes(){
519 if(!checkConsolidate)this.consolidate();
520 String names = "";
521 for(int i=0; i<this.nBoxes; i++){
522 BlackBox bb = openPath.get(i);
523 names = names + i +": "+bb.getName() + " ";
524 }
525 return names;
526 }
527
528 // Remove all boxes from the path
529 public void removeAllBoxes(){
530 // Empty openPath ArrayList
531 if(!openPath.isEmpty()){
532 openPath.clear();
533 }
534
535 // Empty segments ArrayList
536 if(!segments.isEmpty()){
537 segments.clear();
538 }
539 this.nSeg = 0;
540 this.checkNoMix = true;
541 this.checkPath = false;
542 this.nBoxes = 0;
543 this.checkConsolidate = false;
544 this.adcs = null;
545 this.dacs = null;
546 this.zeroHolds = null;
547
548 }
549
550 // return checkNoMix
551 public boolean getCheckNoMix(){
552 return this.checkNoMix;
553 }
554
555
556 // Deep copy
557 public OpenLoop copy(){
558 if(this==null){
559 return null;
560 }
561 else{
562 OpenLoop bb = new OpenLoop();
563 this.copyBBvariables(bb);
564
565 bb.nBoxes = this.nBoxes;
566 bb.nSeg = this.nSeg;
567 bb.checkPath = this.checkPath;
568 bb.checkNoMix = this.checkNoMix;
569 bb.checkConsolidate = this.checkConsolidate;
570 if(this.openPath.size()==0){
571 bb.openPath = new ArrayList<BlackBox>();
572 }
573 else{
574 for(int i=0; i<openPath.size(); i++)bb.openPath.add((this.openPath.get(i)).copy());
575 }
576 if(this.segments.size()==0){
577 bb.segments = new ArrayList<Object>();
578 }
579 else{
580 int j=0;
581 for(int i=0; i<this.nSeg; i++){
582 Integer holdI1 = (Integer)this.segments.get(j);
583 int ii = holdI1.intValue();
584 bb.segments.add(new Integer(ii));
585 j++;
586 Integer holdI2 = (Integer)this.segments.get(j);
587 ii = holdI2.intValue();
588 bb.segments.add(new Integer(ii));
589 j++;
590 String holdS = (String)this.segments.get(j);
591 bb.segments.add(holdS);
592 j++;
593 bb.segments.add(((BlackBox)this.segments.get(j)).copy());
594 j++;
595 }
596 }
597
598 return bb;
599 }
600 }
601
602 // Clone - overrides Java.Object method clone
603 public Object clone(){
604 return (Object)this.copy();
605 }
606}
Note: See TracBrowser for help on using the repository browser.