Sci cosim: Difference between revisions

From Octave
Jump to navigation Jump to search
No edit summary
Line 49: Line 49:


== Usage cases ==
== Usage cases ==
In all usage case, described below, user doesn't need to have any knowledge about using Scilab interface and in the most cases he doesn't have to know Scilab scripting language. You can use only Xcos and do the rest of things in Octave.
1. Simple dynamic systems rapid prototyping.
In this case you, actually, we need only Xcos. In other cases, described below, we also assume that Xcos is used for dynamic system simulation. The good starting point is to read [http://www.scilab.org/content/download/1107/10095/file/Xcos_beginners.pdf "Xcos for very beginners" guide] (even if you are familiar with Simulink).
2. Dynamic system rapid prototyping, when systems parameters have to be calculated by script.
In this case you write and execute all scripts you need using Octave, and then transfer scripts results to Scilab workspace. For example, we have Octave script "calc_pid_params.m" that can tune PID controller parameters and return them as Kp, Ki and Kd variables to Octave workspace. In case we want to use those parameters in Xcos simulation we should run in Octave
''sci_setvar('Kp','Ki','Kd');''
After that we can use those varibles in Xcos simulation. Of course, before running ''sci_setvar'', you should setup connection with Scilab using Sciconnect
3. Dynamic system rapid prototyping, when simulation results should be processed by script.
In this case we have to move all simulation results you need into Octave workspace and then process them with any Octave script. For example, if your Xcos simulation return two vectors "speed" and "pos", to transfer them into Octave you will need to run
''sci_getvar('speed','pos');''
After that two new variables with names "speed" and "pos" will appear in Octave. If there were variables with such name, they will be changed to the ones received from Scilab. After that you can use them, as if they were created in Octave.
4. Automated Dynamic system simulation
In this case we use Octave scripts for setting up dynamic model parameters and result analysis. The Xcos simulation can be started both from Xcos GUI or from Octave using ''sci_sim'' function. In case of using ''sci_sim'' function the current directory in Octave contain .xcos model file. The current directory in Scilab doesn't matter, because it will be changed automatically. If you don't need Xcos GUI, you can ran  Scilab in console mode, it will cause no problem for starting Xcos simulations from Octave. The example of this usage is distributed with Scilab co-simulation package as demo.
5. Using Scilab co-simulation package to use Scilab toolboxes
In this case you need to connect to Scilab using ''sci_connect'' function. Transfer function arguments to Scilab workspace with ''sci_setvar'' function. After that you can execute any of Scilab function from Octave with ''sci_cmd''. And when you need to return results from Scilab you have to use ''sci_getvar''. For example, you want to generate key for BlowFish chiper using Scilab Cryptography Toolbox from your Octave script. First of all, you should know, that any call of ''sci_setvar'', ''sci_getvar'' or ''sci_sim'' command will change Scilab current folder to the same you use in Octave, but ''sci_cmd'' won't. That means, if you use only sci_cmd function, you should change Scilab current folder, before running any scripts. It can be done directly from Octave by transferring any variable to Scilab using ''sci_setvar'' or with command
''sci_cmd(['cd ' pwd]);''
After current folder in Scilab and Octave are synchronized you can generate the key by Octave command:
''sci_cmd('keyGen_blowfish("sampleKey", "sampleIV", "samplePublicKey", "samplePrivateKey");');''
As with Octave, Scilab is initially distributed without any toolboxes. That means, before using Scilab Cryptography Toolbox or any other toolbox for Scilab you need to install it with ATOMS manager.


== Author and Licensing ==
== Author and Licensing ==

Revision as of 18:24, 5 December 2015

Octave-Scilab co-simulation package

Toolbox provides the functions for variable exchange between Octave and Scilab workspaces, and for the remote commands execution in Scilab, such as starting xcos simulation. The main goal of this toolbox is to make an alternative for Simulink in Octave from Scilab xcos. But it can be also used to uses functions from Scilab toolboxes, that are unavailable for Octave.

Installation

1. Install Octave version 4.0.0 or higher and Scilab v.5.5.2 or higher

2. Install sockets package in Octave by executing

pkg install -forge -auto sockets

3. Download sci_cosim package from https://github.com/amromanov/sci_cosim

4. Install sci_cosim package in Octave by executing

pkg install -auto sci_cosim_0.1.2.tar.gz

5. Start Scilab GUI and install Toolbox Backdoor by pressing in main menu Applications → Module manager - ATOMS → Technical → Backdoor → Install

6. Restart Scilab. If every thing was done correct, after restart there will message in command window "BackDoor: listening to connections on TCP port"

Running demo

1. Download demo from https://github.com/amromanov/sci_cosim/demo.tar.gz

2. Extract it to any folder, where your user has read/write rights

3. Run Scilab. It doesn't matter for the co-simulation package if you are using GUI version (scilab) or console one (scilab-adv-cli).

4. Run Octave and change folder to the one, where you have extracted demo

5. Run sci_sim_example.m

Function description

sci_connect     - connects to running SciLab with Toolbox Backdoor. It should be used at least once per Octave session.
sci_disconnect  - disconnects from SciLab with Toolbox Backdoor. 
sci_setvar      - transfers variables, which names are listed in function arguments, from Octave to Scilab workspace.  
sci_getvar      - transfers variables, which names are listed in function arguments, from Scilab to Octave workspace.
sci_cmd         - executes single command in Scilab. 
sci_sim        - starts xcos simulation in Scilab. In the most cases, .xcos file should be in the Octave current directory. To set simulation duration use End block in Xcos and transfer its parameter as variable from Octave, using sci_setvar.

For more detailed usage description execute help FUNCTION_NAME in Octave.

Usage cases

In all usage case, described below, user doesn't need to have any knowledge about using Scilab interface and in the most cases he doesn't have to know Scilab scripting language. You can use only Xcos and do the rest of things in Octave.

1. Simple dynamic systems rapid prototyping.

In this case you, actually, we need only Xcos. In other cases, described below, we also assume that Xcos is used for dynamic system simulation. The good starting point is to read "Xcos for very beginners" guide (even if you are familiar with Simulink).

2. Dynamic system rapid prototyping, when systems parameters have to be calculated by script.

In this case you write and execute all scripts you need using Octave, and then transfer scripts results to Scilab workspace. For example, we have Octave script "calc_pid_params.m" that can tune PID controller parameters and return them as Kp, Ki and Kd variables to Octave workspace. In case we want to use those parameters in Xcos simulation we should run in Octave

sci_setvar('Kp','Ki','Kd');

After that we can use those varibles in Xcos simulation. Of course, before running sci_setvar, you should setup connection with Scilab using Sciconnect

3. Dynamic system rapid prototyping, when simulation results should be processed by script.

In this case we have to move all simulation results you need into Octave workspace and then process them with any Octave script. For example, if your Xcos simulation return two vectors "speed" and "pos", to transfer them into Octave you will need to run

sci_getvar('speed','pos');

After that two new variables with names "speed" and "pos" will appear in Octave. If there were variables with such name, they will be changed to the ones received from Scilab. After that you can use them, as if they were created in Octave.

4. Automated Dynamic system simulation In this case we use Octave scripts for setting up dynamic model parameters and result analysis. The Xcos simulation can be started both from Xcos GUI or from Octave using sci_sim function. In case of using sci_sim function the current directory in Octave contain .xcos model file. The current directory in Scilab doesn't matter, because it will be changed automatically. If you don't need Xcos GUI, you can ran Scilab in console mode, it will cause no problem for starting Xcos simulations from Octave. The example of this usage is distributed with Scilab co-simulation package as demo.

5. Using Scilab co-simulation package to use Scilab toolboxes

In this case you need to connect to Scilab using sci_connect function. Transfer function arguments to Scilab workspace with sci_setvar function. After that you can execute any of Scilab function from Octave with sci_cmd. And when you need to return results from Scilab you have to use sci_getvar. For example, you want to generate key for BlowFish chiper using Scilab Cryptography Toolbox from your Octave script. First of all, you should know, that any call of sci_setvar, sci_getvar or sci_sim command will change Scilab current folder to the same you use in Octave, but sci_cmd won't. That means, if you use only sci_cmd function, you should change Scilab current folder, before running any scripts. It can be done directly from Octave by transferring any variable to Scilab using sci_setvar or with command

sci_cmd(['cd ' pwd]);

After current folder in Scilab and Octave are synchronized you can generate the key by Octave command:

sci_cmd('keyGen_blowfish("sampleKey", "sampleIV", "samplePublicKey", "samplePrivateKey");');

As with Octave, Scilab is initially distributed without any toolboxes. That means, before using Scilab Cryptography Toolbox or any other toolbox for Scilab you need to install it with ATOMS manager.

Author and Licensing

Author: Romanov Alexey (romanov at mirea.ru)

Moscow, Control Problems Department MIREA, 2015

Distributed under the New BSD License