Jump to content
  • 0

Data sampling with USB-231 while controlling other instruments


Jolle

Question

I have bought a USB231 for voltage measurements on four batteries together with four different DC load. USB231 and all four DC loads will be controlled from python (DC load is controlled via serial commands).

My wish is to do the following:

  • Start sampling data on one channel with 2000 Samples/s
  • turn on one DC load
  • Ensure sampling of 1sec after DC load is turned on
  • Turn off DC load and stop sampling
  • Go to next analog channel and DC load and redo test above

I have looked at the examples given on https://github.com/mccdaq but I find them a bit confusing and complicated as they seem to be generic codes which works for all MCC units. When trying a_in_scan_background.py i do get measurements, but I cannot figure out how to start the DC load while sampling is ongoing and ensure the correct sample rate.

Do there exist any guide for the USB-231 which describes how to do what I want to accomplish?

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Maybe it helped I eventually have been able to do what I was looking for. My script might not be the prettiest and it will crash in case one forgets to connect the equipment, but I find it much easier to read and understand, so in case someone else are looking for what I did, I hope it will help them. It might cause problems if anyone tries to acquire data with large number of samples in which case one should look into JRys example.

The script creates a memory buffer 1,2 times the necessary based on sample_time and sample rate. It then starts the data acquisition and sleeps in sample_time seconds and then stops sampling after which is takes out the data from the memory buffer. I have inserted comments at the places I start and stop the DC load for my application. When the sampling has ended the data is rounded to three digits and returned to the data_set variable which is plotted using matplotlib.

USB-231_data_example.png

USB-231_analog_sampling.py

Link to comment
Share on other sites

  • 0

Hello,

Take a look at the attached script. It's similar tot the a_in_scan_continous.py except it prints a line a data after it determined that a half buffer of new data is available. It prints one scan from the half buffer because printing lots of data to a console make it hard to print other messages and it's a slow process. The loop terminates when 200000 samples (50,000 per channel) have been collected. You would turn on the load before executing the ul.a_in_scan() and off after ul.stop_background. Then set ul.a_in_scan() to read the next channel and do it again.  Or, continuously read all four channel controlling the loads from within the while loop.

Best regards,

John

a_in_scan_USB200.py

Link to comment
Share on other sites

  • 0

Thank you for your answer, i have been able to get a bit further but I am not able to get data out as I would like. First of all I would like to know if I have understood the following variables from your file correctly:

rate - sampling rate in samples/second

points_pr_channel - the buffer size for each of the channels which are being used for measurements (how does this correlate to the 2047 sample memory described in the USB-231 datasheet?)

curr_count < 200000 - Ensures that a total of 200.000 measurements are made before measurement is finished

You example prints a single value from the buffered data but I would like the entire dataset so I can look at the measured voltage at my specified sampling rate. But where is the entire data stored? My goal is to get an array (or similar) I can put into a dataframe together with other measurements and finally output a csv file with the measurements.

I know how to create the dataframe and csv file, but how do I get all the measured datapoints out of the script into an array I can work on?

Link to comment
Share on other sites

  • 0

Hello,

The 2047 FIFO buffer is managed & read by the low-level driver and is separate from your buffer created with the command: memhandle = ul.scaled_win_buf_alloc(total_count).  The only stipulation for your buffer is the size must be a multiple of the number of channels.

The reference memhandle is not acessible to you. To get around this, a second pointer is created that points to the same memory location. This is accomplished with the  command: buf_data = cast(memhandle, POINTER(c_double)). buf_data now points to the same memory location as memhandle. The memory is sized to hold 40,000 values. When the driver reaches the end, it starts at the beginning. The program I had attached, loops until the status command curr_index parameter indicates the buffer is more than half full. When this happens, it prints the first row of data then returns to the loop until. When it detects that the other half has fresh data, it prints the first row of it. For example, when current index > 20,000 the first 20,000 locations in buf_data have fresh data. When current count < 20,000 meaning the buffer rolled over, the upper half has fresh data. To make sure the buffer halfs are not double read, a boolean flag used.

Note, the example uses the command ul.scaled_win_buf_to_array(memhandle, buf_data, int(half_count), int(half_count)). It copys a memory location to another memory location. The way this command is used, the upper half data copied over the lower half. If it were to be left out, the print routine for the upper half would look like this:

              print(
                        '{:.3f}\t {:.3f}\t {:.3f}\t {:.3f}\t {:d}\t {:d}'.format(buf_data[int(half_count], buf_data[int(half_count) + 1],
                                                                                        buf_data[int(half_count + 2)],
                                                                                        buf_data[int(half_count + 3)],
                                                                                        curr_index,
                                                                                        curr_count)) 

I hope this helps...

John

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...