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