2.4. Remote control (Matlab, Labview, Scilab or Python)

../../_images/SCPI_web_lr.png

STEMlab board can be controlled remotely over LAN or wireless interface using Matlab, Labview, Scilab or Python via Red Pitaya SCPI (Standard Commands for Programmable Instrumentation) list of commands. SCPI interface/environment is commonly used to control T&M instruments for development, research or test automation purposes. SCPI uses a set of SCPI commands that are recognized by the instruments to enable specific actions to be taken (e.g.: acquiring data from fast analog inputs, generating signals and controlling other periphery of the Red Pitaya STEMlab platform). The SCPI commands are extremely useful when complex signal analysis is required where SW environment such as MATLAB provides powerful data analysis tools and SCPI commands simple access to raw data acquired on STEMlab board.

Features

  • Quickly write control routines and programs using Matlab, Labview, Scilab or Python
  • Use powerful data analysis tools of Matlab, Labview, Scilab or Python to analyze raw signals acquired by STEMlab board
  • Write testing scripts and routines
  • Incorporate your STEMlab and Labview into testing and production lines
  • Take quick measurements directly with your PC

2.4.1. Quick start

Start SCPI server, this is done simply by clicking the SCPI server icon and starting the SCPI server. When SCPI server is started the IP of your board will be shown. This IP you need to input in to your scripts. Starting SCPI server can be also done manually via Terminal(check bellow).

To run an examples follow instructions bellow:

  1. Go to your STEMlab main page and Select SCPI server.

    ../../_images/scpi-homepage.png ../../_images/scpi-development.png
  2. Start SCPI server by selecting RUN button. Please notice the IP of your STEMlab (192.168.178.100) board as it will be needed to connect to your board.

    ../../_images/scpi-app-run.png ../../_images/scpi-app-stop.png
  3. Follow the instructions bellow sutable to your environment.

    Note

    It is not possible to run SCPI commands/programs in parallel with web applications.

2.4.1.1. MATLAB

  1. Open MATLAB on your computer
  2. Copy the Code from blink tutorial example to MATLAB workspace
  3. Replace the IP in the example with the IP of your STEMlab board
  4. Hit RUN or F5 on your keyboard to run the code

Check demo video.

More examples about how to control STEMlab from MATLAB can be find here.

2.4.1.2. Python

The PyVISA library in combination with the PyVISA-py backend are used. To install them do:

$ sudo pip3 install pyvisa pyvisa-py
  1. Open the blink tutorial and copy the code to your favorite text editor

  2. Save the file as blink.py to your working folder → for example examples_py

  3. Copy and save the redpitaya_scpi.py `` script in to the same folder where you have saved blink.py example (in our case it will be examples_py).

    Note

    redpitaya_scpi.py script is a standard script needed to establish the connection between your PC and STEMlab board. Without having this script in the same folder as your python script the execution of your script will fail.

    ../../_images/scpi-examples.png
  4. Open the Terminal and go to the folder containing your python script (examples_py) and run: python blink.py IP where you give an STEMlab IP as the argument when calling an execution of the blink.py example. Example is given bellow where 192.168.178.108 is the IP of the STEMlab board.

    cd /home/zumy/Desktop/exmples_py
    python blink.py 192.168.178.108
    
    ../../_images/scpi-example-cli.png

More examples about how to control STEMlab from MATLAB can be find here.

2.4.1.3. LabVIEW

To set up the LabVIEW driver for Red Pitaya, download the Red_Pitaya_LabVIEW_Driver&Examples.zip file. Unpack it and copy the RedPitaya folder to your LabVIEW installations instr.lib folder e.g. C:/Program Files/National Instruments/LabVIEW 2010/instr.lib. The RedPitaya driver should appear after restarting LabVIEW in Block Diagram -> Instrument I/O -> Instr Drivers -> RedPitaya. Depending on your settings Instrument I/O may be hidden. Please consult LabVIEW Help on how to activate/deactivate those categories. You can access example VIs by going to:

  1. Help -> Find Examples...
  2. click Search tab
  3. Enter RedPitaya in Enter keyword(s) field

More examples about how to control STEMlab from MATLAB can be find here.

2.4.1.4. SCILAB

To use the SCPI commands you will need to set up Scilab sockets. The procedure is described below.

  1. Go to Scilab download page and download and Install Scilab for your OS
  2. Go to Scilab socket toolbox page and download the basic socket function for Scilab.
  3. Go to the extracted Scilab folder then to folder named contrib
  4. Copy socket_toolbox zip file to contrib folder
  5. Extract socket_toolbox zip file inside the contrib folder
  6. Delete socket_toolbox zip file because we dont need it any more
  7. Go to socket_toolbox folder
  8. Open loader.sce with your Scilab and press RUN (grey run button on SCILAB editor gui)

These last two steps must be executed each time you start Scilab. To install installing you must have an internet connection. Running the examples is same as on MATALB

  1. Copy the Code from blink tutorial example to MATLAB workspace
  2. Replace the IP in the example with the IP of your STEMlab board
  3. Press RUN to run the code

Different code examples can be found on the Examples page.

Note

Communicating with scpi server and working with web based instruments at the same time can diminish the performance of your Red Pitaya. This is because the same resource is used for both tasks.

More examples about how to control STEMlab from MATLAB can be find here.

2.4.2. Starting SCPI server manually

Assuming you have successfully connected to your STEMlab board using these instructions. Remotely connect using Putty on Windows machines or with SSH using Terminal on UNIX (macOSX/Linux) machines.

Connect to your STEMlab board via terminal on a Linux machine and start SCPI server with the following command:

systemctl start redpitaya_scpi &
../../_images/scpi-ssh.png

2.4.3. List of supported SCPI commands

Table of correlated SCPI and API commands on Red Pitaya.

SCPI API description
DIG:PIN:DIR <dir>,<gpio>
Examples:
DIG:PIN:DIR OUT,DIO0_N
DIG:PIN:DIR IN,DIO1_P
rp_DpinSetDirection Set direction of digital pins to output or input.
DIG:PIN <pin>,<state>
Examples:
DIG:PIN DIO0_N,1
DIG:PIN LED2,1
rp_DpinSetState Set state of digital outputs to 1 (HIGH) or 0 (LOW).
DIG:PIN? <pin> > <state>
Examples:
DIG:PIN? DIO0_N
DIG:PIN? LED2
rp_DpinGetState Get state of digital inputs and outputs.

2.4.3.1. LEDs and GPIOs

Parameter options:

  • <dir> = {OUT,IN}
  • <gpio> = {{DIO0_P...DIO7_P}, {DIO0_N...DIO7_N}}
  • <led> = {LED0...LED8}
  • <pin> = {gpio, led}
  • <state> = {0,1}

2.4.3.2. Analog Inputs and Outputs

Parameter options:

  • <ain> = {AIN0, AIN1, AIN2, AIN3}
  • <aout> = {AOUT0, AOUT1, AOUT2, AOUT3}
  • <pin> = {ain, aout}
  • <value> = {value in Volts}
SCPI API description
ANALOG:PIN <pin>,<value>
Examples:
ANALOG:PIN AOUT2,1.34
rp_ApinSetValue
Set analog voltage on slow analog outputs.
Voltage range of slow analog outputs is: 0 - 1.8 V
ANALOG:PIN? <pin> > <value>
Examples:
ANALOG:PIN? AOUT2 > 1.34
ANALOG:PIN? AIN1 > 1.12
rp_ApinGetValue
Read analog voltage from slow analog inputs.
Voltage range of slow analog inputs is: 0 3.3 V

2.4.3.3. Signal Generator

Parameter options:

  • <n> = {1,2} (set channel OUT1 or OUT2)
  • <state> = {ON,OFF} Default: OFF
  • <frequency> = {0Hz...62.5e6Hz} Default: 1000
  • <func> = {SINE, SQUARE, TRIANGLE, SAWU, SAWD, PWM, ARBITRARY} Default: SINE
  • <amplitude> = {-1V...1V} Default: 1
  • <offset> = {-1V...1V} Default: 0
  • <phase> = {-360deg ... 360deg} Default: 0
  • <dcyc> = {0%...100%} Default: 50
  • <array> = {value1, ...} max. 16k values, floats in the range -1 to 1
  • <burst> = {ON,OFF} Default: OFF
  • <count> = {1...50000, INF} INF = infinity/continuous, Default: 1
  • <time> = {1us-500s} Value in us.
  • <trigger> = {EXT_PE, EXT_NE, INT, GATED}
    • EXT = External
    • INT = Internal
    • GATED = gated busts
SCPI API description
OUTPUT<n>:STATE <state>
Examples:
OUTPUT1:STATE ON
rp_GenOutEnable
rp_GenOutDisable
Disable or enable fast analog outputs.
SOUR<n>:FREQ:FIX <frequency>
Examples:
SOUR2:FREQ:FIX 100000
rp_GenFreq Set frequency of fast analog outputs.
SOUR<n>:FUNC <func>
Examples:
SOUR2:FUNC TRIANGLE
rp_GenWaveform Set waveform of fast analog outputs.
SOUR<n>:VOLT <amplitude>
Examples:
SOUR2:VOLT 0.5
rp_GenAmp
Set amplitude voltage of fast analog outputs.
Amplitude + offset value must be less than maximum output range ± 1V
SOUR<n>:VOLT:OFFS <offset>
Examples:
SOUR1:VOLT:OFFS 0.2
rp_GenOffset
Set offset voltage of fast analog outputs.
Amplitude + offset value must be less than maximum output range ± 1V
SOUR<n>:PHAS <phase>
Examples:
SOUR2:PHAS 30
rp_GenPhase Set phase of fast analog outputs.
SOUR<n>:DCYC <par>
Examples:
SOUR1:DCYC 20
rp_GenDutyCycle Set duty cycle of PWM waveform.
SOUR<n>:TRAC:DATA:DATA <array>
Examples:
SOUR1:TRAC:DATA:DATA
1,0.5,0.2
rp_GenArbWaveform Import data for arbitrary waveform generation.
SOUR<n>:BURS:STAT <burst>
Examples:
SOUR1:BURS:STAT ON
SOUR1:BURS:STAT OFF
rp_GenMode Enable or disable burst (pulse) mode. Red Pitaya will generate R number of N periods of signal and then stop. Time between bursts is P.
SOUR<n>:BURS:NCYC <count>
Examples:
SOUR1:BURS:NCYC 3
rp_GenBurstCount Set N number of periods in one burst.
SOUR1:BURS:NOR <count>
Examples:
SOUR1:BURS:NOR 5
rp_GenBurstRepetitions Set R number of repeated bursts.
SOUR1:BURS:INT:PER <time>
Examples:
SOUR1:BURS:INT:PER 1000000
rp_GenBurstPeriod Set P total time of one burst in in micro seconds. This includes the signal and delay.
SOUR<n>:TRIG:SOUR <trigger>
Examples:
SOUR1:TRIG:SOUR EXT
rp_GenTriggerSource Set trigger source for selected signal.
SOUR<n>:TRIG:IMM
Examples:
SOUR1:TRIG:IMM
rp_GenTrigger Triggers selected source immediately.
TRIG:IMM
rp_GenTrigger Triggers both sources immediately.
GEN:RST
  Reset generator to default settings.

2.4.3.4. Acquire

Parameter options:

  • <n> = {1,2} (set channel IN1 or IN2)

2.4.3.4.1. Control

SCPI API description
ACQ:START rp_AcqStart Starts acquisition.
ACQ:STOP rp_AcqStop Stops acquisition.
ACQ:RST rp_AcqReset Stops acquisition and sets all parameters to default values.

2.4.3.4.2. Sampling rate & decimation

Parameter options:

  • <decimation> = {1,8,64,1024,8192,65536} Default: 1
  • <average> = {OFF,ON} Default: ON
SCPI API description
ACQ:DEC <decimation> rp_AcqSetDecimation Set decimation factor.
ACQ:DEC? > <decimation>
Example:
ACQ:DEC? > 1
rp_AcqGetDecimation Get decimation factor.
ACQ:AVG <average>
rp_AcqSetAveraging Enable/disable averaging.
ACQ:AVG? > <average>
Example:
ACQ:AVG? > ON
rp_AcqGetAveraging Get averaging status.

2.4.3.5. Trigger

Parameter options:

  • <source> = {DISABLED, NOW, CH1_PE, CH1_NE, CH2_PE, CH2_NE, EXT_PE, EXT_NE, AWG_PE, AWG_NE} Default: DISABLED
  • <status> = {WAIT, TD}
  • <time> = {value in ns}
  • <counetr> = {value in samples}
  • <gain> = {LV, HV}
  • <level> = {value in mV}
SCPI API DESCRIPTION
ACQ:TRIG <source>
Example:
ACQ:TRIG CH1_PE
rp_AcqSetTriggerSrc Disable triggering, trigger immediately or set trigger source & edge.
ACQ:TRIG:STAT?
Example:
ACQ:TRIG:STAT? > WAIT
rp_AcqGetTriggerState Get trigger status. If DISABLED -> TD else WAIT.
ACQ:TRIG:DLY <time>
Example:
ACQ:TRIG:DLY 2314
rp_AcqSetTriggerDelay Set trigger delay in samples.
ACQ:TRIG:DLY? > <time>
Example:
ACQ:TRIG:DLY? > 2314
rp_AcqGetTriggerDelay Get trigger delay in samples.
ACQ:TRIG:DLY:NS <time>
Example:
ACQ:TRIG:DLY:NS 128
rp_AcqSetTriggerDelayNs Set trigger delay in ns.
ACQ:TRIG:DLY:NS? > <time>
Example:
ACQ:TRIG:DLY:NS? > 128ns
rp_AcqGetTriggerDelayNs Get trigger delay in ns.
ACQ:SOUR<n>:GAIN <gain>
Example:
ACQ:SOUR1:GAIN LV
rp_AcqSetGain Set gain settings to HIGH or LOW. This gain is referring to jumper settings on Red Pitaya fast analog inputs.
ACQ:TRIG:LEV <level>
Example:
ACQ:TRIG:LEV 125 mV
rp_AcqSetChannelThreshold Set trigger level in mV.
ACQ:TRIG:LEV? > level
Example:
ACQ:TRIG:LEV? > 123 mV
rp_AcqGetChannelThreshold Get trigger level in mV.

2.4.3.6. Data pointers

Parameter options:

  • <pos> = {position inside circular buffer}
SCPI API DESCRIPTION
ACQ:WPOS? > pos
Example:
ACQ:WPOS? > 1024
rp_AcqGetWritePointer Returns current position of write pointer.
ACQ:TPOS? > pos
Example:
ACQ:TPOS? > 512
rp_AcqGetWritePointerAtTrig Returns position where trigger event appeared.

2.4.3.7. Data read

  • <units> = {RAW, VOLTS}
  • <format> = {FLOAT, ASCII} Default FLOAT
SCPI API DESCRIPTION
ACQ:DATA:UNITS <units>
Example:
ACQ:GET:DATA:UNITS RAW
rp_AcqScpiDataUnits Selects units in which acquired data will be returned.
ACQ:DATA:FORMAT <format>
Example:
ACQ:GET:DATA:FORMAT ASCII
rp_AcqScpiDataFormat Selects format acquired data will be returned.
ACQ:SOUR<n>:DATA:STA:END? >
<start_pos>,<end_pos>
Example:
ACQ:SOUR1:GET:DATA 10,13 >
{123,231,-231}
rp_AcqGetDataPosRaw
rp_AcqGetDataPosV
Read samples from start to stop position.
<start_pos> = {0,1,...,16384}
<stop_pos> = {0,1,...116384}
ACQ:SOUR<n>:DATA:STA:N?
<start_pos>,<m> > ...
Example:
ACQ:SOUR1:DATA? 10,3 >
{1.2,3.2,-1.2}
rp_AcqGetDataRaw
rp_AcqGetDataV
Read m samples from start position on.
ACQ:SOUR<n>:DATA?
Example:
ACQ:SOUR2:DATA? >
{1.2,3.2,...,-1.2}
rp_AcqGetOldestDataRaw
rp_AcqGetOldestDataV
Read full buf.
Size starting from oldest sample in buffer (this is first sample after trigger delay).
Trigger delay by default is set to zero (in samples or in seconds).
If trigger delay is set to zero it will read full buf. size starting from trigger.
ACQ:SOUR<n>:DATA:OLD:N?<m>
Example:
ACQ:SOUR2:DATA:OLD? 3 >
{1.2,3.2,-1.2}
rp_AcqGetOldestDataRaw
rp_AcqGetOldestDataV
Read m samples after trigger delay, starting from oldest sample in buffer
(this is first sample after trigger delay).
Trigger delay by default is set to zero (in samples or in seconds).
If trigger delay is set to zero it will read m samples starting from trigger.
ACQ:SOUR<n>:DATA:LAT:N?<m>
Example:
ACQ:SOUR1:DATA:LAT? 3 >
{1.2,3.2,-1.2}
rp_AcqGetLatestDataRaw
rp_AcqGetLatestDataV
Read m samples before trigger delay.
Trigger delay by default is set to zero (in samples or in seconds).
If trigger delay is set to zero it will read m samples before trigger.
ACQ:BUF:SIZE? > <size>
Example:
ACQ:BUF:SIZE? > 16384
rp_AcqGetBufSize Returns buffer size.