1 /*
   2  * $RCSfile: Extcal.java,v $
   3  * $Revision: 1.11 $
   4  * $Date: 2009/12/25 18:44:28 $
   5  * jEdit:tabSize=4:indentSize=4:collapseFolds=1:
   6  *
   7  * program to externally calibrate USB-AI16-16
   8  */
   9 
  10 // {{{ build instructions
  11 /*
  12  * to compile:
  13  *   javac -cp ../../java/aiousb.jar Extcal.java
  14  * to run:
  15  *   java -cp ../../java/aiousb.jar:. Extcal
  16  */
  17 // }}}
  18 
  19 // {{{ imports
  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 /*API*/     deviceManager = new USBDeviceManager();
  35 /*API*/     deviceManager.open();
  36             System.out.println(
  37                   "USB-AI16-16A sample program version: " + VERSION + "\n"
  38 /*API*/         + "  AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
  39 /*API*/         + "  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 /*API*/     deviceManager.printDevices();
  70 /*API*/     USBDevice[] devices = deviceManager.getDeviceByProductID( USB_AI16_Family.getSupportedProductIDs() );
  71             if( devices.length > 0 ) {
  72                 USB_AI16_Family device = ( USB_AI16_Family ) devices[ 0 ];  // get first device found
  73 
  74                 /*
  75                  * set up A/D in proper mode for calibrating, including a default calibration table
  76                  */
  77                 System.out.print( "Calibrating A/D, may take a few seconds ... " );
  78 /*API*/         device.reset()
  79 /*API*/             .setCommTimeout( 1000 );
  80 /*API*/         device.adc()
  81 /*API*/             .setRangeAndDiffMode( AnalogInputSubsystem.RANGE_0_10V, false )
  82 /*API*/             .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
  83 /*API*/             .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN )
  84 /*API*/             .setOverSample( 100 )
  85 /*API*/             .setDiscardFirstSample( true )
  86 /*API*/             .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 ];     // voltage-count pairs
  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;                  // from while()
 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                     }   // catch( ...
 108                     if( SIMULATE_AD ) {
 109                         System.out.print( "  Enter A/D counts: " );
 110                         commandLine = input.readLine();
 111                         if( commandLine.isEmpty() )
 112                             break;              // from while()
 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                         }   // catch( ...
 119                     } else {
 120                         int counts;
 121                         try {
 122 /*API*/                     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                         }   // catch( ...
 127                         System.out.print( "  Read " + counts + " A/D counts ("
 128 /*API*/                     + 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                     }   // if( SIMULATE_AD )
 134                 }   // while( true )
 135                 if( numPoints >= 2 ) {
 136                     try {
 137 /*API*/                 String fileName = "ADC-Ext-Cal-Table-" + Long.toHexString( device.getSerialNumber() );
 138 /*API*/                 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                     }   // catch( ...
 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 /*API*/     deviceManager.close();
 151         } catch( Exception ex ) {
 152             System.err.println( "Error \'" + ex.toString() + "\' occurred while manipulating device" );
 153             if(
 154                 deviceManager != null
 155 /*API*/         && deviceManager.isOpen()
 156             )
 157 /*API*/         deviceManager.close();
 158         }   // catch( ...
 159     }   // main()
 160 }   // class Extcal
 161 
 162 /* end of file */