Hey there,
for my project I need to measure the voltage of a automobile battery with 1kHz.
Therefore I bought a LabJack U3-HV. My plan is to stream the values and add a timestamp.
My plan is to:
- do initializations etc (load necessary .dll, read firmware, check for HV-Version, errorhandling, ...)
- set AIN0 to -10V/+20V range
- initialize the necessary stream
- add timestamps to data
I couldn't figure out how to configure AIN0 to -10V/+20V range:
// configure AIN0 as -10V/+20V ErrorHandler( LJUD.ePut(Handle[0], LJUD.LJ_ioPUT_AIN_RANGE, 0, LJUD.LJ_rgAUTO, 0) , 0, new Exception());
And whether the stream configuration is okay:
// configure the stream: // set the scan rate. ErrorHandler( LJUD.AddRequest(Handle[0], LJUD.LJ_ioPUT_CONFIG, LJUD.LJ_chSTREAM_SCAN_FREQUENCY, scanRate, 0, 0) , 0, new Exception()); // give the driver a 5 second buffer (scanRate * 1 channel * 5 seconds). ErrorHandler( LJUD.AddRequest(Handle[0], LJUD.LJ_ioPUT_CONFIG, LJUD.LJ_chSTREAM_BUFFER_SIZE, scanRate * 1 * 5, 0, 0) , 0, new Exception()); // configure reads to retrieve whatever data is available without waiting (wait mode LJ_swNONE). ErrorHandler( LJUD.AddRequest(Handle[0], LJUD.LJ_ioPUT_CONFIG, LJUD.LJ_chSTREAM_WAIT_MODE, LJUD.LJ_swNONE, 0, 0) , 0, new Exception()); // reset, than define the scan list as AIN0 ErrorHandler( LJUD.AddRequest(Handle[0], LJUD.LJ_ioCLEAR_STREAM_CHANNELS,0, 0, 0, 0) , 0, new Exception()); ErrorHandler( LJUD.AddRequest(Handle[0], LJUD.LJ_ioADD_STREAM_CHANNEL, 0, 0, 0, 0) , 0, new Exception());
When running the code, I get the following output:
LabJack U3-HV: Initialisierung (UD-Treiber v3.35, LJUDJava v0.3((console)), Hardware v1.3((console)), Firmware v1.46)((console)) LabJack U3-HV: Messung läuft, Abtastfrequenz: 1000.0Hz((console))AIN0 = 4,271V, scans: 75, commBacklog = 0 AIN0 = 4,271V, scans: 100, commBacklog = 0 AIN0 = 4,271V, scans: 100, commBacklog = 0 AIN0 = 4,271V, scans: 100, commBacklog = 0 AIN0 = 4,271V, scans: 100, commBacklog = 0 AIN0 = 4,266V, scans: 100, commBacklog = 0 AIN0 = 4,266V, scans: 100, commBacklog = 0 AIN0 = 4,271V, scans: 100, commBacklog = 0 AIN0 = 4,271V, scans: 100, commBacklog = 0 AIN0 = 4,277V, scans: 100, commBacklog = 0 AIN0 = 4,266V, scans: 100, commBacklog = 0 AIN0 = 4,266V, scans: 100, commBacklog = 0 LabJack U3-HV: Messung beendet nach 1.2 Sekunden.((console))
So, it seems to work fine with a 100ms delay with a very stable number of 100 scans?!
----------------------------------------------------------------------------------------------------------------------------------
My complete public void run() - code is:
int Errorcode; int[] Handle = { 0 }; int GetNextIteration; int i = 0, k = 0; int[] IOType = { 0 }; int[] Channel = { 0 }; double[] Value = { 0 }; double[] Backlog = { 0 }; double scanRate = 1000; long delayms = 100; double numScans = 2*scanRate*delayms/1000; // 2x the expected # of scans (2*scanRate*delayms/1000) TODO double[] numScansRequested = { 0 }; double[] adblData = new double[4000]; // Max buffer size (#channels*numScansRequested) TODO int[] iZero = { 0 }; double[] dZero = { 0 }; NumberFormat formatter = new DecimalFormat("0.000"); // initialization errorOccured = false; // application information -> console PanelConsole.append("\nLabJack U3-HV: Initialisierung (UD-Treiber v" + LJUD.GetDriverVersion() + ", LJUDJava v" + LJUD.LJUDJAVA_VERSION); // Open the first found LabJack U3-HV and check, whether this was successful Errorcode = LJUD.OpenLabJack(LJUD.LJ_dtU3, LJUD.LJ_ctUSB, "1", 1, Handle); if (Errorcode == 1010) { errorOccured = true; PanelMeasurementDisplay.setStopMeasurement(); PanelConsole.append("\nFEHLER! LabJack U3-HV wird von einer anderen Anwendung verwendet, Messung wird beendet"); JOptionPane.showMessageDialog(null, "LabJack U3-HV wird von einer anderen Anwendung verwendet, Messung wird beendet!\nBitte entsprechendes Programm beenden!"); } else { ErrorHandler(Errorcode, 0, new Exception()); } // if LabJack U3-HV connected, ... if(!errorOccured) { // read and display the hardware version of this U3. ErrorHandler( LJUD.eGet(Handle[0], LJUD.LJ_ioGET_CONFIG, LJUD.LJ_chHARDWARE_VERSION, Value, 0) , 0, new Exception()); PanelConsole.append(", Hardware v" + Value[0]); // read and display the firmware version of this U3. ErrorHandler( LJUD.eGet(Handle[0], LJUD.LJ_ioGET_CONFIG, LJUD.LJ_chFIRMWARE_VERSION, Value, 0) , 0, new Exception()); PanelConsole.append(", Firmware v" + Value[0] + ")"); // check, whether U3 is HV-model ErrorHandler( LJUD.eGet(Handle[0], LJUD.LJ_ioGET_CONFIG, LJUD.LJ_chU3HV, Value, 0) , 0, new Exception()); if(Value[0] == 1) { // set all pin assignments to the factory default condition ErrorHandler( LJUD.ePut(Handle[0], LJUD.LJ_ioPIN_CONFIGURATION_RESET, 0, 0, 0) , 0, new Exception()); // configure AIN0 as -10V/+20V ErrorHandler( LJUD.ePut(Handle[0], LJUD.LJ_ioPUT_AIN_RANGE, 0, LJUD.LJ_rgAUTO, 0) , 0, new Exception()); //TODO // configure the stream: // set the scan rate. ErrorHandler( LJUD.AddRequest(Handle[0], LJUD.LJ_ioPUT_CONFIG, LJUD.LJ_chSTREAM_SCAN_FREQUENCY, scanRate, 0, 0) , 0, new Exception()); // give the driver a 5 second buffer (scanRate * 1 channel * 5 seconds). ErrorHandler( LJUD.AddRequest(Handle[0], LJUD.LJ_ioPUT_CONFIG, LJUD.LJ_chSTREAM_BUFFER_SIZE, scanRate * 1 * 5, 0, 0) , 0, new Exception()); // configure reads to retrieve whatever data is available without waiting (wait mode LJ_swNONE). ErrorHandler( LJUD.AddRequest(Handle[0], LJUD.LJ_ioPUT_CONFIG, LJUD.LJ_chSTREAM_WAIT_MODE, LJUD.LJ_swNONE, 0, 0) , 0, new Exception()); // reset, than define the scan list as AIN0 ErrorHandler(LJUD.AddRequest(Handle[0], LJUD.LJ_ioCLEAR_STREAM_CHANNELS, 0, 0, 0, 0) , 0, new Exception()); ErrorHandler(LJUD.AddRequest(Handle[0], LJUD.LJ_ioADD_STREAM_CHANNEL, 0, 0, 0, 0) , 0, new Exception()); // execute the list of requests. Errorcode = LJUD.GoOne(Handle[0]); ErrorHandler(Errorcode, 0, new Exception()); // get all the results just to check for errors. Errorcode = LJUD.GetFirstResult(Handle[0], IOType, Channel, Value, iZero, dZero); ErrorHandler(Errorcode, 0, new Exception()); GetNextIteration = 0; // Used by the error handling function. while (Errorcode < LJUD.LJE_MIN_GROUP_ERROR) { Errorcode = LJUD.GetNextResult(Handle[0], IOType, Channel, Value, iZero, dZero); if (Errorcode != LJUD.LJE_NO_MORE_DATA_AVAILABLE) ErrorHandler(Errorcode, GetNextIteration, new Exception()); GetNextIteration++; } // start the stream. ErrorHandler( LJUD.eGet(Handle[0], LJUD.LJ_ioSTART_STREAM, 0, Value, 0) , 0, new Exception()); // read actual scan rate and display PanelConsole.append("\nLabJack U3-HV: Messung läuft, Abtastfrequenz: " + Value[0] + "Hz"); // read data while (!PanelMeasurementDisplay.getStopMeasurement()) { // wait for delay in ms Thread.sleep(delayms); // init array so we can easily tell if it has changed for (k = 0; k < numScans * 2; k++) adblData[k] = 9999.0; // read the data numScansRequested[0] = numScans; ErrorHandler( LJUD.eGet(Handle[0], LJUD.LJ_ioGET_STREAM_DATA, LJUD.LJ_chALL_CHANNELS, numScansRequested, adblData) , 0, new Exception()); // display data at the panel PanelMeasurementDisplay.setVoltage(formatter.format(adblData[0])); // get backlog Errorcode = LJUD.eGet(Handle[0], LJUD.LJ_ioGET_CONFIG, LJUD.LJ_chSTREAM_BACKLOG_COMM, Backlog, 0); // write informations -> console System.out.println("AIN0 = " + formatter.format(adblData[0]) + "V, scans: " + (long) numScansRequested[0] + ", commBacklog = " + (long) Backlog[0]); // increment i i++; } } // Stop the stream Errorcode = LJUD.eGet(Handle[0], LJUD.LJ_ioSTOP_STREAM, 0, dZero, 0); if(!errorOccured) { ErrorHandler(Errorcode, 0, new Exception()); PanelConsole.append("\nLabJack U3-HV: Messung beendet nach " + i/(scanRate/100) + " Sekunden."); } } else { System.out.println("KEIN HV"); //TODO } }