source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/control/DtoA.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: 14.6 KB
Line 
1/* Class DtoA
2*
3* This class contains constructor and methods that will
4* 1. Simulate a Digital to Analogue Convertor (DAC)
5* or
6* 2. Simply act as a marker to be used in OpenPath and
7* ClosedLoop to indicate the presence of an DAC. In the
8* latter case the output is equal to the input plus any delay set.
9*
10* This class is a subclass of the superclass BlackBox.
11*
12* Author: Michael Thomas Flanagan.
13*
14* Created: 27 June 2003
15* Revised: 18 August 2003, 9 May 2005, April 2008, 7 November 2009, 18 January 2011
16*
17* DOCUMENTATION:
18* See Michael T Flanagan's JAVA library on-line web page:
19* http://www.ee.ucl.ac.uk/~mflanaga/java/DtoA.html
20* http://www.ee.ucl.ac.uk/~mflanaga/java/
21*
22* Copyright (c) 2003 - 2011 Michael Thomas Flanagan
23*
24* PERMISSION TO COPY:
25* Permission to use, copy and modify this software and its documentation for
26* NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
27* to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
28*
29* Dr Michael Thomas Flanagan makes no representations about the suitability
30* or fitness of the software for any or for a particular purpose.
31* Michael Thomas Flanagan shall not be liable for any damages suffered
32* as a result of using, modifying or distributing this software or its derivatives.
33*
34***************************************************************************************/
35
36package agents.anac.y2015.agentBuyogV2.flanagan.control;
37
38import agents.anac.y2015.agentBuyogV2.flanagan.complex.*;
39import agents.anac.y2015.agentBuyogV2.flanagan.math.Conv;
40
41public class DtoA extends BlackBox{
42
43 private int nBits = 0; // Number of bits, n
44 private long maximumDecimal = 0; // 2^n-1
45 private double vRef = 0.0D; // Reference voltage
46 private int[] vBinary = null; // array holding binary input
47 private boolean trueDtoA = true; // if true, a real DAC is simulated
48 // if false, the instance is simply an DtoA marker
49 private double outputVoltage = 0.0D;// output voltage
50 private double voltageInput =0.0D; // input as voltage - if this is the input the output is put equal to this input
51 private String binaryInput = ""; // input as a binary String
52 private long decimalInput = 0L; // input as decimal representation of a binary String
53 private boolean inputSet = false; // = true when input is set
54
55 // Constructor
56 // Simulates a DAC
57 public DtoA(int nBits, double vRef ){
58 super("DtoA");
59 super.setSnumer(new ComplexPoly(1.0D));
60 super.setSdenom(new ComplexPoly(1.0D));
61 super.setZtransformMethod(1);
62 this.nBits = nBits;
63 this.vBinary = new int[nBits+1];
64 this.maximumDecimal = (long)Math.pow(2, this.nBits)-1L;
65 this.vRef = vRef;
66 this.trueDtoA = true;
67 }
68
69 // Constructor
70 // Simply marks an DtoA event
71 public DtoA(){
72 super("DtoA");
73 this.trueDtoA = false;
74 super.sNumerDeg = 0;
75 super.sDenomDeg = 0;
76 super.setSnumer(new ComplexPoly(1.0D));
77 super.setSdenom(new ComplexPoly(1.0D));
78 super.setZtransformMethod(1);
79 }
80
81 // Return the true DtoA option
82 public boolean getTrueDtoAoption(){
83 if(this.trueDtoA){
84 System.out.println("This instance of DtoA is a true simulation of an ADC");
85 System.out.println("getTrueDtoAoption has returned 'true'");
86 }
87 else{
88 System.out.println("This instance of DtoA is not a true simulation of an ADC");
89 System.out.println("It is simple an 'D to A marker'");
90 System.out.println("getTrueDtoAoption has returned 'false'");
91 }
92 return this.trueDtoA;
93 }
94
95 // Set input entered as a Sting representing the binary inut in two's complement
96 // of n+1 bits where n = this.nBits and the extra bit is the sign bit
97 public void setInput(String input){
98 this.binaryInput = input.trim();
99 int len = this.binaryInput.length();
100 if(len>this.nBits+1)throw new IllegalArgumentException("length of input String is greater than the DAC bit number plus one");
101 if(len<this.nBits+1){
102 System.out.println("Class - DtoA; method - setInput(String)");
103 System.out.println("The input String is less than DAC number of bits plus one");
104 System.out.println("String assumed to represent a postive unsigned binary number");
105 System.out.println("unfilled bits assigned zeros");
106 for(int i=len; i<this.nBits+1; i++)this.binaryInput = '0'+this.binaryInput;
107 len = this.nBits+1;
108 }
109
110 // Convert String to int array
111 int ii = 0;
112 int jj = 0;
113 char c =' ';
114 for(int i=len-1; i>=0; i--){
115 c = this.binaryInput.charAt(i);
116 if(c=='1'){
117 ii=1;
118 }
119 else{
120 if(c=='0'){
121 ii = 0;
122 }
123 else{
124 throw new IllegalArgumentException("String input must be '0's or '1's");
125 }
126 }
127 jj = len-i-1;
128 this.vBinary[jj] = ii;
129 }
130
131 // Check if input is negative
132 long sign = 1L;
133 int[] vPosBinary = Conv.copy(this.vBinary);
134 if(this.vBinary[len-1]==1){
135 sign = -1L;
136 vPosBinary = this.negateNegativeBinary(vPosBinary);
137 }
138
139 // convert positive binary to decimal equivalent
140 this.decimalInput = DtoA.binaryToDecimal(vPosBinary);
141
142 // adjust sign
143 if(sign==-1L)this.decimalInput = -this.decimalInput;
144
145 // convert to voltage
146 this.outputVoltage = (this.decimalInput*this.vRef)/(this.maximumDecimal+1L);
147
148 this.inputSet = true;
149 }
150
151 // Set input entered as an integer array representing the binary inut in two's complement
152 // of n+1 bits where n = this.nBits and the extra bit is the sign bit
153 // Zeroth array element is the least significant bit (LSB)
154 public void setInput(int[] input){
155 int len = input.length;
156 if(len>this.nBits+1)throw new IllegalArgumentException("length of input array is greater than the DAC bit number plus one");
157 for(int i=0; i<len; i++)this.vBinary[i]=input[i];
158 if(len<this.nBits+1){
159 System.out.println("Class - DtoA; method - setInput(String)");
160 System.out.println("The input array is less than DAC number of bits plus one");
161 System.out.println("Array assumed to represent a postive unsigned binary number");
162 System.out.println("unfilled bits assigned zeros");
163 for(int i=len; i<this.nBits+1; i++)this.vBinary[i] = 0;
164 len = this.nBits+1;
165 }
166
167 // convert to String
168 this.binaryInput="";
169 for(int i=this.nBits; i>=0; i--){
170 this.binaryInput = this.binaryInput + this.vBinary[i];
171 }
172
173 // Check if input is negative
174 long sign = 1L;
175 int[] vPosBinary = Conv.copy(this.vBinary);
176 if(this.vBinary[len-1]==1){
177 sign = -1L;
178 vPosBinary = this.negateNegativeBinary(this.vBinary);
179 }
180
181 // convert positive binary to decimal equivalent
182 this.decimalInput = DtoA.binaryToDecimal(vPosBinary);
183
184 // adjust sign
185 if(sign==-1L)this.decimalInput = -this.decimalInput;
186
187 // convert to voltage
188 this.outputVoltage = (this.decimalInput*this.vRef)/(this.maximumDecimal+1L);
189
190 this.inputSet = true;
191 }
192
193
194 // Set input entered as a decimal equivalent the binary input
195 public void setInput(long input){
196 if(Math.abs(input)>this.maximumDecimal)throw new IllegalArgumentException("abs(input), "+input+", is greater than the maximum decimal representation, "+this.maximumDecimal+", allowed by the set number of bits, "+this.nBits);
197 this.decimalInput = input;
198
199 // convert to voltage
200 this.outputVoltage = (input*this.vRef)/(this.maximumDecimal+1L);
201
202 // convert decimal to binary
203 long dec = this.decimalInput;
204 int sign = 1;
205 if(dec<0){
206 sign = -1;
207 dec = -dec;
208 }
209
210 for(int i=0; i<this.nBits+1; i++)this.vBinary[i] = 0;
211 boolean test = true;
212 int ii = 0;
213 while(test){
214 this.vBinary[ii] = (int) (dec % 2);
215 dec = dec/2L;
216 ii++;
217 if(dec==0L)test = false;
218 }
219
220 // if decimal was negative negate binary
221 if(sign==-1L)this.vBinary = AtoD.negateBinary(this.vBinary);
222
223 // convert to String
224 this.binaryInput="";
225 for(int i=this.nBits; i>=0; i--){
226 this.binaryInput = this.binaryInput + this.vBinary[i];
227 }
228
229 this.inputSet = true;
230 }
231
232 // Enter input as a voltage
233 public void setInput(double input){
234
235 if(this.trueDtoA){
236 if(Math.abs(input)>this.vRef){
237 throw new IllegalArgumentException("The input voltage in this simulation of a DAC must be less than nor equal to the reference voltage\nIf you choose the constructor without an argument list, i.e. an instance of DtoA that is simply a DAC marker\nyou may imput any voltage and the output will be made equal to that voltage");
238 }
239 else{
240 this.voltageInput=input;
241 AtoD adc = new AtoD(this.nBits, this.vRef);
242 adc.setInput(input);
243 this.decimalInput = adc.decimalOutput();
244 this.binaryInput = adc.binaryOutput();
245 this.vBinary = adc.binaryArray();
246 }
247 }
248 else{
249 this.outputVoltage = input;
250 }
251 super.sNumer.resetCoeff(0, new Complex(this.outputVoltage/this.voltageInput, 0.0D));
252
253 this.inputSet = true;
254 }
255
256 // Convert positive binary to decimal equivalent
257 private static long binaryToDecimal(int[] binary){
258 long decimal = 0L;
259 for(int i=0; i<binary.length; i++){
260 decimal += (long)(Math.pow(2,i)*binary[i]);
261 }
262 return decimal;
263 }
264
265 // Negate a ngative binary number
266 // Two's complement
267 private static int[] negateNegativeBinary(int[] binary){
268 int nBin = binary.length;
269 int[] negate = new int[nBin];
270 int[] minusOne = new int[nBin];
271 for(int i=0; i<nBin; i++){
272 minusOne[i]=1;
273 negate[i]=0;
274 }
275 // subtract one
276 negate = DtoA.addBinary(negate, minusOne);
277 // invert all bits
278 for(int i=0; i<nBin; i++){
279 if(binary[i] == 0)negate[i] = 1;
280 }
281
282 return negate;
283 }
284
285 // Add two binary numbers
286 private static int[] addBinary(int[] aa, int[] bb){
287 int n = aa.length;
288 int m = bb.length;
289 int lenMax = n;
290 int lenMin = m;
291 if(m>n){
292 lenMax = m;
293 lenMin = n;
294 }
295 int[] addition = new int[lenMax];
296 int carry = 0;
297 int sum = 0;
298 for(int i=0; i<lenMin; i++){
299 sum = aa[i] + bb[i] + carry;
300 switch(sum){
301 case 0: addition[i] = 0;
302 carry = 0;
303 break;
304 case 1: addition[i] = 1;
305 carry = 0;
306 break;
307 case 2: addition[i] = 0;
308 carry = 1;
309 break;
310 case 3: addition[i] = 1;
311 carry = 1;
312 break;
313 }
314 }
315
316 return addition;
317 }
318
319 // Return output
320 public double getOutput(){
321 if(!this.inputSet)throw new IllegalArgumentException("No input has been entered");
322 return this.outputVoltage;
323 }
324
325 // Return decimal input
326 public long getDecimalInput(){
327 if(!this.inputSet)throw new IllegalArgumentException("No input has been entered");
328 if(!this.trueDtoA){
329 System.out.println("Class - DtoA; method - getDecimalInput");
330 System.out.println("This instance of DtoA is not a true simulation of an DAC");
331 System.out.println("It is simple an 'D to A marker'");
332 System.out.println("getDecimalInput has returned 0L");
333 this.decimalInput = 0L;
334 }
335
336 return this.decimalInput;
337 }
338
339 // Return binary input as a String
340 public String getBinaryInput(){
341 if(!this.inputSet)throw new IllegalArgumentException("No input has been entered");
342 if(!this.trueDtoA){
343 System.out.println("Class - DtoA; method - getBinaryInput");
344 System.out.println("This instance of DtoA is not a true simulation of an DAC");
345 System.out.println("It is simple an 'D to A marker'");
346 System.out.println("getBinaryInput has returned null");
347 this.binaryInput = null;
348 }
349
350 return this.binaryInput;
351 }
352
353 // Return binary input as int array (zeroth element = LSD)
354 public int[] getBinaryArray(){
355 if(!this.inputSet)throw new IllegalArgumentException("No input has been entered");
356 if(!this.trueDtoA){
357 System.out.println("Class - DtoA; method - getBinaryInput");
358 System.out.println("This instance of DtoA is not a true simulation of an DAC");
359 System.out.println("It is simple an 'D to A marker'");
360 System.out.println("getBinaryArray has returned null");
361 this.vBinary = null;
362 }
363
364 return this.vBinary;
365 }
366
367 // Deep copy
368 public DtoA copy(){
369 if(this==null){
370 return null;
371 }
372 else{
373 DtoA bb = new DtoA();
374 this.copyBBvariables(bb);
375
376 bb.nBits = this.nBits;
377 bb.maximumDecimal = this.maximumDecimal;
378 bb.vRef = this.vRef;
379 bb.vBinary = Conv.copy(this.vBinary);
380 bb.trueDtoA = this.trueDtoA;
381 bb.outputVoltage = this.outputVoltage;
382 bb.voltageInput = this.voltageInput;
383 bb.binaryInput = this.binaryInput;
384 bb.decimalInput = this.decimalInput;
385 bb.inputSet = this.inputSet;
386
387 return bb;
388 }
389 }
390
391 // Clone - overrides Java.Object method clone
392 public Object clone(){
393 return (Object)this.copy();
394 }
395}
Note: See TracBrowser for help on using the repository browser.