Difference between revisions of "Lab Software"

From OptoelectronicsWiki
Jump to: navigation, search
(Excel)
 
Line 118: Line 118:
 
==== Matlab useful tips ====
 
==== Matlab useful tips ====
 
[http://www.mathworks.com/matlabcentral/newsreader/view_thread/270109 Change plot cursor precision]: Edit 'default_getDatatipText.m' file. On line 31 change the DEFAULT_DIGITS = 4; Located in: \toolbox\matlab\graphics\@graphics\@datacursor directory.
 
[http://www.mathworks.com/matlabcentral/newsreader/view_thread/270109 Change plot cursor precision]: Edit 'default_getDatatipText.m' file. On line 31 change the DEFAULT_DIGITS = 4; Located in: \toolbox\matlab\graphics\@graphics\@datacursor directory.
 +
*Define immediately after using 'gpib' command
 +
instrname.InputBufferSize = 10001; to increase buffer size for reading large data sets
 +
 +
instrname.OutputBufferSize = 10001; to increase buffer size for reading large data sets

Latest revision as of 02:37, 15 November 2012

Excel[edit]

File:ImportFiles.txt

Macro for importing text files from a folder into an excel document.

Python[edit]

File:PythonStartUpKit.zip

-femto instrument control interface for Python- File:Femto.zip

Matlab[edit]

The benefit of using Matlab to program lab equipment is that it's fairly transparent - the program does exactly what you tell it to do in the order you tell it to. The downside is it does require you to write every single command, which can be tedious. Matlab considers instruments as VISA objects, which can be written to like files. To contact an instrument, you first create the VISA object, then open it using fopen, then write the relevant commands/read from it, then close it.


How to open a line of communication with an instrument

First, you create a VISA object using the command visa:

	instrname = visa('ni', address);

where “instrname” is whatever you feel like calling your instrument, 'ni' is the version of VISA you wish to use to communicate with the object in question (always use ni – it's about what library you have installed on your computer, not what USB/GPIB cable you have), and “address” is the address of the instrument in question, of the form 'GPIB0::##::INSTR'. The object instrname is worth investigating in the array editor. VISA objects have many properties that deal with how Matlab treats the instrument, and sometimes the default settings (most notably the timeout limit) get in the way.

Creating the object alone doesn't do anything. You have to connect it to the instrument. To do this, you “open” it like a file:

	fopen(instrname);

You also have to close the object before the instrument is released – if you try to open an object that's still open, you get an error. Putting the command:

	fclose(instrfind) 

at the beginning of your script is the functional equivalent of putting clear there. The instrfind command returns a structure with the GPIB addresses and connection statuses of all attached equipment – so it’s useful in other contexts, too. Once you’ve linked to the instrument, as far as Matlab is concerned, it is a file – you write to it using fprintf, and read from it using fscanf. Once you've opened the instrument, you're ready to begin sending commands to it using SCPI.


Programming guides and SCPI

Many instruments come with their own programming guides. The more expensive something is, the more likely it is that it will have a well-written programming guide posted on the manufacturers' website, and there are some posted on the wiki. This is probably the best resource available to you: most are organized by the relevant functions of the particular piece of equipment, there may be some features that are unique to your instrument, and your instrument may require slightly nonstandard commands. It may also have examples, which are always good ways to come up with new programming ideas. Keithley's guide can be found here. If you can't find a guide for your particular instrument, the general SCPI one lives here. If you aren't sure what some command does, you can always open a direct line of communication to the instrument using NI-MAX, send the command, and see what happens. These things are pretty robust from a firmware standpoint, so there is no chance you'll make it explode by doing this, just make sure the instrument isn’t connected to any devices before experimenting.


Simple programming in SCPI

Here is a short sample program that gets the identity of the instrument and stores it in a string:

	SigGenerator = visa('ni', 'GPIB0::10::INSTR'); 
	fopen(SigGenerator) 
	fprintf(SigGenerator, '*IDN?'); 
	identity = fscanf(SigGenerator); 
	fclose(SigGenerator); 

Some notes on syntax: fprintf works just like in C – you can use placeholders like %d and then add another argument at the end of your input string. Question marks mean you expect a response back. If you don’t put in a question mark and then try to read from the instrument, the connection will time out. Asterisks are often used as prefixes to three-letter commands; I don't know what they mean, but they're only used on really common commands. Colons are used to separate “menus” from each other – kind of how >s are commonly used to indicate where you go within a particular program to find an option Semicolons are used to separate two commands from each other.


More complex programming in SCPI – issues to think about

Timing Matlab thinks the instrument is a file – it will not automatically wait for the instrument to complete a command before sending the next one, because there’s no need to do that with a file. The instrument itself will not wait to complete a command before responding, unless you tell it to. It certainly won't wait for the value to stabilize before sending it to you. One way around this is to insert a pause in the script, for example:

	%set center frequency 
	fprintf(SpecAnalyzer, 'FREQ:CENTER %f GHz', Cfreq) 
	%set span 
	fprintf(SpecAnalyzer, 'FREQ:SPAN %f MHz', span) 
	%pause for reading 
	pause(5) 
	%get trace data 
	fprintf(SpecAnalyzer, 'TRAC:DATA? TRACE1') 
	trace = fscanf(SpecAnalyzer); 

When the pause is there to allow the system to stabilize, this is probably the best way to do things. If you need a pause in order to allow the instrument to complete a measurement (as above), it probably isn't. A better way to do this would be:

	%set center frequency 
	fprintf(SpecAnalyzer, 'FREQ:CENTER %f GHz', Cfreq) 
	%set span 
	fprintf(SpecAnalyzer, 'FREQ:SPAN %f MHz; *WAI', span) 
	%get trace data 
	fprintf(SpecAnalyzer, 'TRAC:DATA? TRACE1') 
	trace = fscanf(SpecAnalyzer); 

This works by telling the spectrum analyzer to wait (*WAI) before doing anything else. It is better because you don't wait too much or too little. Adding *WAI to the end of every line doesn't significantly slow processing time, or change the results you get, though it may make your code less aesthetically pleasing.


Matlab is bad at manipulating strings

Unfortunately, unless you tell them to do otherwise (and sometimes you can't), instruments will send data to Matlab in string format. It is best to convert this to a number immediately using str2num (do NOT use str2dbl. Matlab claims it's faster, which may be true, but it also gives you the wrong results). The big problem that usually emerges is when you think you should be able to use printf-type syntax and instead you get an error. For example,

       SigGenerator = visa('ni', 'GPIB0::%d::INSTR', addr);

will not put “addr” in for %d, it will wonder why there’s an extra argument there. You can usually get around this by using strcat:

	address = strcat(‘GPIB0::’, num2str(addr), ‘::INSTR’);
	SigGenerator = visa(‘ni’, address);

Corollary: some programmers (e.g. me) are bad at manipulating strings. If you write:

	fprintf(SpecAnalyzer, 'FREQ:CENTER %s GHz', Cfreq) 

what you will tell the instrument to do is set the center frequency to the ASCII character(s) that represent the number stored in Cfreq. For example, if Cfreq is 1, the program will actually tell the analyzer to set the center frequency to 49 GHz, because 49 is the (decimal) ASCII value of “1.”


Matlab doesn't have infinite program memory

Matlab's input buffer (this is probably not what it is actually called) is not as large as perhaps it should be. An ASCII character is 8 bits, the average data point is 17-18 characters, and Matlab's input buffer is about 720 bytes, which gives it enough room for about 40 data points. This is not an issue with the Keithleys because you're only taking one data point at a time, but if you ask for a full spectrum from a spectrum analyzer, you'll probably overflow the buffer. You can get around this either by not getting the full spectrum at all (you can move a marker to the point of interest – this is the fastest way to do it), or just reading one data point at a time.


For unclear reasons, different manufacturers use slightly different forms of SCPI

Unfortunately, just because a program worked on one piece of equipment doesn't guarantee it'll work on another if the new piece of equipment has a different manufacturer or differs significantly in age from the first. This is inconvenient, but the good news is that modifying an already existing script to run on a new piece of equipment is usually easy, because only a few of the commands will be wrong and Matlab (unlike LabView) will tell you which ones those are.


More complex programming in SCPI – example

This Media:SensitiveReadSpectrum.m is a program that gets the peak value of a spectrum. First, it zooms in around some center frequency. Then it dynamically adjusts the reference level to be even with the peak height. Then it does some number of sweeps, averages the result, and gets the height of the peak.

Matlab useful tips[edit]

Change plot cursor precision: Edit 'default_getDatatipText.m' file. On line 31 change the DEFAULT_DIGITS = 4; Located in: \toolbox\matlab\graphics\@graphics\@datacursor directory.

  • Define immediately after using 'gpib' command

instrname.InputBufferSize = 10001; to increase buffer size for reading large data sets

instrname.OutputBufferSize = 10001; to increase buffer size for reading large data sets