Utilizing and Xcos for real-time control and measurement applications Grzegorz Skiba Embedded Solutions [email protected]

1 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Agenda

 MicroDAQ device and software overview  MicroDAQ toolbox for Scilab

 Features overview  Code generation for MicroDAQ DSP core  Tools  Examples  Q&A

2 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ device

 Real-time control and measurement system  Dedicated DSP core for signal and real-time processing  Ethernet, USB2.0 and WiFi connectivity  ADC, DAC, DIO, PWM, Encoder,...

3 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ device

MicroDAQ E1100 MicroDAQ E2000 MicroDAQ OEM

 375MHz CPU  up to 456MHz CPU  375MHz CPU  4GB flash memory  up to 32GB flash memory  4GB flash memory  Ethernet  Ethernet  Ethernet  Wi-Fi  Wi-Fi  USB2.0  USB2.0  USB2.0  USB1.1  32 DIO lines  16 DIO lines  32 DIO lines  8 analog inputs  up to 16 analog inputs  8 analog inputs  8 analog outputs  8 analog outputs  8 analog outputs  Matlab/, LabVIEW  Matlab/Simulink, LabVIEW, and Scilab support Scilab support  Matlab/Simulink, LabVIEW, Scilab support  Price from 200€  Price from 400€

4 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ device

 Processing units

 TI C6000 DSP core with floating/fixed point unit C6000 DSP ARM  ARM core with OS for handling communication, 375/456MHz 375/456MHz file storage, web interface Floating/fixed point with Linux  two PRU 32-bit RISC cores for real-time processing  Storage up to 32GB PRU0 PRU1 187/228MHz 187/228MHz  Digital I/O 32-bit RISC 32-bit RISC

 16/32 DIOs, 6 PWMs, 2 Encoders, UART  Analog I/O Analog input Analog output 166ksps, 8 channel, 12-bit, ±10V range 8 channel, 12-bit, 0-5V range 166ksps, 8 channel, 16-bit, ±10V range 8 channel, 12-bit, ±10V range 8 channel, 16-bit, ±10V range Simultanious sampling ADCs 600ksps, 8 channel, 12-bit, ±10V range 600ksps, 16 channel, 12-bit, ±10V range 500ksps, 16 channel, 16-bit, ±10V range 4000ksps, 2 channel, 16-bit, ±10V range

5 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ software

Scilab/Xcos Host UDP/TCP LabVIEW Matlab/Simulink MLink Linux/Windows Ethernet /C++ application Wi-Fi

 MLink software

 Windows/Linux support  Access to MicroDAQ resources  Loading DSP  Scilab support functions  DSPLib

 LabVIEW support package

 Matlab/Simulink Embedded Coder target  MicroDAQ toolbox for Scilab Available at: https://github.com/microdaq

6 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ toolbox features

 Automatic code generation for MicroDAQ DSP  Xcos blocks for MicroDAQ peripherals  Live data access from generated DSP application via Ethernet and WiFi with standard Xcos sinks  Application loading with Ethernet and WiFi  Standalone mode  Easy integration of custom user C code

7 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ toolbox features

 MicroDAQ hardware (ADC, DAC...) access macros  DSP model utilization with Scilab script, C/C++ application and LabVIEW  Execution profiling  Toolbox as a part for Atoms installer  Support for Linux and Windows

8 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ toolbox components

MLink

Texas Instruments Loading DSP binary Code Composer Studio 5 TCP data communication C6000 DSP SYS/BIOS RTOS MicroDAQ Toolbox

Scilab libs DSPLib

Precompiled Scilab DSP drivers for MicroDAQ libraries for DSP core TCP data communication

9 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Installation

 Atoms → Instrumentn Control → MicroDAQ Toolbox

 Install Code Composer Studio 5.5 – with C6000 DSP compiler and SYS/BIOS RTOS

 microdaq_setup

 Code Composer Studio installation paths

 SYS/BIOS RTOS compilation

 connect MicroDAQ device and setup IP settings

 check connection

-->mdaq_ping Connecting to [email protected]!

 Ready to go! 10 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ toolbox blocks

 MicroDAQ blocks for hardware access

 Special MicroDAQ blocks for setup model parameters

 Standard Xcos blocks compiled for DSP

 Host simulation mode and code generation

11 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ special blocks

 SETUP block

 Simulation duration  Build type: debug/release  Build mode: Ext/Standalone  Execution profiling  ODE solver type  SIGNAL block

 receive live data from DSP  STOP block

12 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Xcos diagram for code generation

 SETUP block  Event generator to step time  Superblock for code generation  Tools → MicroDAQ build and load model  Execution mode

 Standalone – load and start model on DSP immediately  Ext – load model on DSP and wait for user action 13 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Model execution modes

DSP application Standalone mode Model (Real-time task)

DSP application

Model Ext mode (Real-time task)

TCP communication MLink Xcos (Idle task) TCP communication

14 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Code generation

Xcos diagram ### Generating block structure...

### Writing generated code... mdaq_ext_main.c or SETUP block mdaq_standalone_main.c ### Generating Makefile...

### Generating binary file... C code generation "C:\ti\ccsv5\tools\compiler\c6000_7.4.4/bin/cl6x" -c -g -mv6740… "C:\ti\ccsv5\tools\compiler\c6000_7.4.4/bin/cl6x" -c -g -mv6740… "C:\ti\ccsv5\tools\compiler\c6000_7.4.4/bin/cl6x" -c -g -mv6740… "C:\ti\ccsv5\tools\compiler\c6000_7.4.4/bin/cl6x" -c -g -mv6740… Makefile generation "MicroDAQ DSP application led_demo.out created successfully"

C6000 DSP compiler

SYS/BIOS RTOS

DSPLib DSP binary Loading DSP binary MicroDAQ blocks C6000 linker Scilab libs

User blocks lib

15 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Running DSP application with Xcos simulation MicroDAQ DSP Xcos Xcos diagram

Code generation

Initialize model execution Loading DSP on target create TCP server

Wait for connection

Xcos Start button action Create connection with host connects to MicroDAQ and starts Xcos simulation

Execute model and send data with Xcos simulation SIGNAL block receive data with SIGNAL block

16 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Running DSP application with Xcos simulation

Generated DSP code Xcos simulation

int sin_demo_isr(double t) ● When DSP is loaded { int local_flag; SIGNAL input is ignored int i; double *args[100]; /* Output computation */ ● /* Discrete activations */ SIGNAL block receives /* Blocks activated on the event number 1 */ /* Call of 'mdaq_sinus' (type 4 - blk nb 2) */ data from MicroDAQ block_sin_demo[1].nevprt = 1; local_flag = 1; mdaq_sinus(&block_sin_demo[1],local_flag); ● When DSP in not loaded

/* Call of 'mdaq_signal' (type 4 - blk nb 3) */ copy SIGNAL block input block_sin_demo[2].nevprt = 1; local_flag = 1; to output mdaq_signal(&block_sin_demo[2],local_flag);

/* Call of 'cscope' (type 4 - blk nb 4) */ block_sin_demo[3].nevprt = 1; local_flag = 1; cscope(&block_sin_demo[3],local_flag); return 0; }

● mdaq_signal() funciton puts data to IDLE communication task

● cscope() function is empty 17 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] DC motor controller

● Sample time: 0.001s

● Discrite PD controller

● H-bridge for driving DC motor

● Live data with standard CSCOPE block

● Loging data to workspace with standard 'To workspace' block

18 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Execution profiling

 Measure model execution time  Optimization  How 'fast' we can run model  Execution profiling macros

 mdaq_exec_profile  mdaq_exec_profile_show

19 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Custom user code

 For new user creating custom block is a tough task

 Block code and C code generation  Created code can be compiled for debug and release

 Block creation utility macros

 mdaq_block();  mdaq_block_add();  mdaq_block_build( %t );

20 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] User custom code

/* Generated with MicroDAQ toolbox ver: 1.0. */ -->my_block = mdaq_block(); #include "scicos_block4.h"

-->my_block extern double get_scicos_time( void ); my_block = /* This function will executed once at the beginning of model execution */ static void init(scicos_block *block) name: "new_block" { desc: "Set new_block parameters" /* Block parameters */ param_name: [2x1 string] double *params = GetRparPtrs(block); param_size: [2x1 constant] /* param size = 1 */ double param1 = params[0]; param_def_val: list /* param size = 1 */ in: 1 double param2 = params[1]; out: 1 /* Add block init code here */ } -->my_block.param_name /* This function will be executed on every model step */ ans = static void inout(scicos_block *block) { !param1 ! /* Block parameters */ double *params = GetRparPtrs(block); ! ! /* param size = 1 */ !param2 ! double param1 = params[0]; /* param size = 1 */ double param2 = params[1]; -->mdaq_block_add(my_block); /* Block input ports */ double *u1 = GetRealInPortPtrs(block,1); int u1_size = GetInPortRows(block,1); /* u1_size = 1 */

/* Block output ports */ double *y1 = GetRealOutPortPtrs(block,1); int y1_size = GetOutPortRows(block,1); /* y1_size = 1 */

/* Add block code here (executed every model step) */ } 21 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] MicroDAQ hardware access macros

 Access to MicroDAQ hardware mdaq_pwm_write mdaq_pwm_init without code generation mdaq_pru_set mdaq_pru_get  Example mdaq_led_write link_id = mdaq_open(); mdaq_key_read ai_data = mdaq_ai_read(link_id, [1 2 3 4 5 6 7 8], 10, 1) mdaq_hs_ai_read mdaq_hs_ai_init mdaq_close(link_id); mdaq_enc_read mdaq_enc_init  IP settings managment mdaq_dio_write Scilab macros: mdaq_ping, mdaq_set_ip mdaq_dio_read mdaq_dio_func mdaq_dio_dir  Switching from Ethernet to Wi-Fi mdaq_ao_write mdaq_set_ip('10.10.2.1'); mdaq_ai_read

22 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Model utilization

Xcos Scilab script

LabVIEW C/C++ application

23 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Utilizing DSP model in Scilab script

// Start DSP application result = dsp_start('fft_demo_scig\fft_demo.out');

// Register signal ID and signal size dsp_signal(123, 1);

first_time = 1; a = []; // Process data from DSP sample_count = 500; for i=1:500 [result, s] = dsp_signal_get(sample_count);

t = 0:1/sample_count:1; N=size(t,'*'); //number of samples y=fft(s');

f=sample_count*(0:(N/2))/N; //associated frequency vector n=size(f,'*') if first_time == 1 then clf() plot(f,abs(y(1:n))) first_time = 0; a = gca(); else a.children.children.data(:,2) = abs(y(1:n))'; end end

// Stop DSP execution dsp_stop();

24 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Utilizing model with C/C++ application  Embed Xcos generated model in Windows/Linux application  MLink interface functions ● /* Scilab interface funcations */ EXTERNC MDAQ_API void scilab_dsp_start( const char *addr, int *port, const char *dspapp, int *link_id ); EXTERNC MDAQ_API void scilab_dsp_stop( int *link_id, int *result ); EXTERNC MDAQ_API void scilab_signal_register( int *link_id, int32_t *id, int32_t *size, int *result ); EXTERNC MDAQ_API void scilab_signal_read( int *link_id, double *data, int32_t *count, int *result ); EXTERNC MDAQ_API int scilab_mem_read( int *link_id, int start_idx, int len, float *data ); EXTERNC MDAQ_API int scilab_mem_write( int *link_id, int start_idx, float data[], int len );

 Example code scilab_dsp_start("10.10.1.1", &port, "q:\\analog_loop.out", &link_id); scilab_signal_register(&link_id, &id, &size, &result); for (count = 0; count < 100; count++) { scilab_signal_read(&link_id, buf, &size, &result); /* process DSP data */ scilab_mem_write(&link_id, 1, param, 2); } scilab_dsp_stop(&link_id, &result);

25 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Utilizing model with LabVIEW

 Analog loop  SIGNAL block  MEM read block for model parameter change

26 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Utilizing model with LabVIEW

● Real-time processing

● Live DSP data

● Parameter change during DSP execution

27 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Conclusions

 Code generation from Xcos diagram  Real-time procesing  Execution profiling  DSP binary utilization with LabVIEW and Windows/Linux C/C++ applications  Different hardware options avaliable  Easy to use  Free alternative for commercial solutions

28 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Q&A

29 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected] Thnak you!

30 Utilizing Scilab and Xcos for real-time control and measurement applications – ScilabTEC 2015 Embedded Solutions; Skiba Grzegorz; [email protected]