1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import com.acces.aiousb.*;
21 import java.io.*;
22 import java.lang.*;
23 import java.util.*;
24
25
26 class Extcal {
27 public static final String VERSION = "1.11, 25 December 2009";
28 protected static final boolean SIMULATE_AD = false;
29 protected static final int CAL_CHANNEL = 0;
30
31 public static void main( String args[] ) {
32 USBDeviceManager deviceManager = null;
33 try {
34 deviceManager = new USBDeviceManager();
35 deviceManager.open();
36 System.out.println(
37 "USB-AI16-16A sample program version: " + VERSION + "\n"
38 + " AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
39 + " AIOUSB library version: " + deviceManager.getAIOUSBVersion() + ", " + deviceManager.getAIOUSBVersionDate() + "\n"
40 + " JRE version: " + System.getProperty( "java.version" ) + "\n"
41 + " OS version: " + System.getProperty( "os.name" )
42 + " " + System.getProperty( "os.arch" )
43 + " " + System.getProperty( "os.version" ) + "\n"
44 + "\n"
45 + " This program demonstrates external calibration of a USB-AI16-16 device\n"
46 + " on the USB bus. For simplicity, it uses the first such device found on\n"
47 + " the bus.\n"
48 + "\n"
49 + " This external calibration procedure allows you to inject a sequence of\n"
50 + " precise voltages into the USB-AI16-16 that will be used to calibrate its\n"
51 + " A/D. This procedure calibrates the A/D using channel " + CAL_CHANNEL + " on the 0-10V range.\n"
52 + " A minimum of 2 calibration points are required. The procedure is as follows:\n"
53 + "\n"
54 + " 1) Adjust a precision voltage source to a desired target voltage. These\n"
55 + " target voltages do not have to be evenly spaced or provided in any\n"
56 + " particular order.\n"
57 + "\n"
58 + " 2) When you have adjusted the precision voltage source to your desired\n"
59 + " target, type in the exact voltage being fed into the USB-AI16-16 and\n"
60 + " press <Enter>. This program will read the A/D and display the reading,\n"
61 + " asking you to accept it or not. If it looks acceptable, press 'y'.\n"
62 + " Otherwise, press any other key and the program will retake the reading.\n"
63 + "\n"
64 + " 3) When you are finished calibrating, press <Enter> without entering a\n"
65 + " voltage, and the A/D will be calibrated using the data entered, and\n"
66 + " the calibration table will be saved to a file in a format that can be\n"
67 + " subsequently loaded into the A/D.\n"
68 );
69 deviceManager.printDevices();
70 USBDevice[] devices = deviceManager.getDeviceByProductID( USB_AI16_Family.getSupportedProductIDs() );
71 if( devices.length > 0 ) {
72 USB_AI16_Family device = ( USB_AI16_Family ) devices[ 0 ];
73
74
75
76
77 System.out.print( "Calibrating A/D, may take a few seconds ... " );
78 device.reset()
79 .setCommTimeout( 1000 );
80 device.adc()
81 .setRangeAndDiffMode( AnalogInputSubsystem.RANGE_0_10V, false )
82 .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
83 .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN )
84 .setOverSample( 100 )
85 .setDiscardFirstSample( true )
86 .calibrate( false, false, null );
87 System.out.println( "successful" );
88 BufferedReader input = new BufferedReader( new InputStreamReader( System.in ) );
89 final int MAX_POINTS = 100;
90 double[] points = new double[ MAX_POINTS ];
91 int numPoints = 0;
92 while( numPoints < MAX_POINTS ) {
93 String commandLine;
94 System.out.print(
95 "Measuring calibration point " + ( numPoints + 1 ) + ":\n"
96 + " Feed a voltage into channel " + CAL_CHANNEL + " and enter voltage here\n"
97 + " (enter nothing to finish and calibrate A/D): "
98 );
99 commandLine = input.readLine();
100 if( commandLine.isEmpty() )
101 break;
102 try {
103 points[ numPoints * 2 ] = Double.parseDouble( commandLine );
104 } catch( NumberFormatException ex ) {
105 System.err.println( "Error: \'" + commandLine + "\' is not a valid voltage" );
106 continue;
107 }
108 if( SIMULATE_AD ) {
109 System.out.print( " Enter A/D counts: " );
110 commandLine = input.readLine();
111 if( commandLine.isEmpty() )
112 break;
113 try {
114 points[ numPoints++ * 2 + 1 ] = Integer.parseInt( commandLine );
115 } catch( NumberFormatException ex ) {
116 System.err.println( "Error: \'" + commandLine + "\' is not a valid count value" );
117 continue;
118 }
119 } else {
120 int counts;
121 try {
122 counts = device.adc().readCounts( CAL_CHANNEL );
123 } catch( Exception ex ) {
124 System.err.println( "Error: \'" + ex.toString() + "\' occurred while reading A/D input" );
125 continue;
126 }
127 System.out.print( " Read " + counts + " A/D counts ("
128 + device.adc().countsToVolts( CAL_CHANNEL, ( char ) counts )
129 + " volts), accept (y/n)? " );
130 commandLine = input.readLine();
131 if( commandLine.compareToIgnoreCase( "y" ) == 0 )
132 points[ numPoints++ * 2 + 1 ] = counts;
133 }
134 }
135 if( numPoints >= 2 ) {
136 try {
137 String fileName = "ADC-Ext-Cal-Table-" + Long.toHexString( device.getSerialNumber() );
138 device.adc().calibrate( Arrays.copyOf( points, numPoints * 2 ), false, fileName );
139 System.out.println( "External calibration of A/D successful, table saved in " + fileName );
140 } catch( Exception ex ) {
141 System.err.println(
142 "Error \'" + ex.toString() + "\' occurred while externally calibrating A/D."
143 + " This usually occurs because the input voltages or measured counts are not unique and ascending."
144 );
145 }
146 } else
147 System.err.println( "Error: you must provide at least two points" );
148 } else
149 System.out.println( "No USB-AI16-16 devices found on USB bus" );
150 deviceManager.close();
151 } catch( Exception ex ) {
152 System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
153 if(
154 deviceManager != null
155 && deviceManager.isOpen()
156 )
157 deviceManager.close();
158 }
159 }
160 }
161
162