1 /*
   2  * $RCSfile: Extcal.java,v $
   3  * $Revision: 1.5 $
   4  * $Date: 2009/11/29 18:30:40 $
   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.5, 29 November 2009";
  28     protected static final boolean SIMULATE_AD = false;
  29 
  30     public static void main( String args[] ) {
  31 /*API*/ 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 /*API*/         + "  AIOUSB Java library version: " + deviceManager.VERSION_NUMBER + ", " + deviceManager.VERSION_DATE + "\n"
  37 /*API*/         + "  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 /*API*/     deviceManager.open();
  68 /*API*/     Vector<USBDevice> devices = deviceManager.getDeviceByProductID( deviceManager.USB_AI16_16A, deviceManager.USB_AI12_128E );
  69             if( devices.size() > 0 ) {
  70 /*API*/         deviceManager.printDevices();
  71                 USB_AI16_Family device = ( USB_AI16_Family ) devices.get( 0 );  // get first device found
  72 
  73                 /*
  74                  * set up A/D in proper mode for calibrating, including a default calibration table
  75                  */
  76                 System.out.print( "Calibrating A/D, may take a few seconds ... " );
  77 /*API*/         device.reset()
  78 /*API*/             .setCommTimeout( 1000 );
  79 /*API*/         device.adc()
  80 /*API*/             .setGainCodeAndDiffMode( AnalogInputSubsystem.GAIN_CODE_0_10V, false )
  81 /*API*/             .setCalMode( AnalogInputSubsystem.CAL_MODE_NORMAL )
  82 /*API*/             .setTriggerMode( AnalogInputSubsystem.TRIG_MODE_SCAN )
  83 /*API*/             .setOversample( 100 )
  84 /*API*/             .setDiscardFirstSample( true )
  85 /*API*/             .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 ];     // voltage-count pairs
  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;                  // from while()
 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                     }   // catch( ...
 107                     if( SIMULATE_AD ) {
 108                         System.out.print( "  Enter A/D counts: " );
 109                         commandLine = input.readLine();
 110                         if( commandLine.isEmpty() )
 111                             break;              // from while()
 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                         }   // catch( ...
 118                     } else {
 119                         int counts;
 120                         try {
 121 /*API*/                     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                         }   // catch( ...
 126                         System.out.print( "  Read " + counts + " A/D counts ("
 127 /*API*/                     + 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                     }   // if( SIMULATE_AD )
 133                 }   // while( true )
 134                 if( numPoints >= 2 ) {
 135                     try {
 136 /*API*/                 String fileName = "ADC-Ext-Cal-Table-" + Long.toHexString( device.getSerialNumber() );
 137 /*API*/                 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                     }   // catch( ...
 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 /*API*/     deviceManager.close();
 150         } catch( Exception ex ) {
 151             System.err.println( "Error \'" + ex.toString() + "\' occurred while configuring device manager or device" );
 152 /*API*/     if( deviceManager.isOpen() )
 153 /*API*/         deviceManager.close();
 154         }   // catch( ...
 155     }   // main()
 156 }   // class Extcal
 157 
 158 /* end of file */