Scilab/C4/Calling-User-Defined-Functions-in-XCOS/English
Title of the Script: Calling user-defined functions in Xcos
Author: Rupak Rokade, IIT Bombay
Keywords: video tutorial, Xcos.
|
|
|
Welcome to the spoken tutorial on “Calling user-defined functions in Xcos” |
|
In this tutorial we will learn, |
# To write a squaring function in Scilab
| |
|
Ubuntu 12.04 is the operating system used with Scilab version 5.3.3 installed. |
|
You should have
|
|
Start Scilab on your computer.
|
|
In the Scilab console, type editor and press Enter
|
function y=squareit(a)
y=a^2; endfunction |
Then type the following code
|
|
The function has one input variable a and one output variable y
|
|
Let us save this file in the desired directory.
|
|
Switch to the Scilab console
|
|
We will make the Xcos diagram.
|
Switch to the palette browser window
| |
|
I will zoom the untitled Xcos window for better view.
I will use the Zoom button as you see. Now double click on the scifunc block to configure it.
|
|
Our function squareit has only one input and output variable.
|
|
A new Scilab input value request window will open.
|
In the text box y1=squareit(u1) | In the text box available,
|
Point as per the narration | Note that, here the input and output variables are u1 and y1 respectively.
|
Point as per the narration
Cick on OK. |
These should be strictly in u and y form and not as per the variable names used in the actual function.
|
Click on Ok in the subsequent 3 windows | Another Scilab input value request window will open.
The scifunc block is now been configured.
|
|
Next we will include a sinusoid generator block.
|
|
Place the block towards the left of the scifunc block for convenience.
|
|
Now we need a block to plot the output variable.
|
|
Place the block towards the right of the scifunc block
|
Notice that the CScope block has a red input port.
| |
|
In the palette browser window, click on Sources section.
|
|
Place it above the CScope block.
|
|
Notice that the CScope block has only one input port.
|
Hence, we need a multiplexer block,
| |
|
In the palette browser window, click on Signal Routing section.
|
|
Place the block between the scifunc block and CScope block. |
|
Let me resize and re align the Mux block
Now let us connect the blocks together.
|
|
Now connect the output port of scifunc block to the lower input port of the MUX.
|
|
Connect the output port of MUX block to the input port of CScope block.
|
|
Connect the output port of clock underscore c block to the event input port of CScope block.
|
|
We also have to plot sine input.
|
|
Then without releasing, move your mouse pointer towards the link between the Sinusoid generator block and scifunc block.
|
|
To bend the link, release the mouse button or click at places
|
|
As you bring the pointer on the link, the link turns green.
|
Now let us see the configuration of other blocks
| |
|
To do this, double click on the Sinusoid generator block.
|
|
Click on Ok to close the configuration window. |
|
Now let us configure the CScope block.
|
|
Change the Ymin parameter to minus 2 and Ymax parameter to 2
|
|
Change the Refresh period value to 10.
|
|
Change the buffer size value to 2 .
click on OK |
|
Now let us configure the CLOCK_c block
|
|
Keep the value of Period as 0.1 |
|
Change the Initialisation Time value to be 0.
|
|
Now let us change the Simulation parameters.
|
|
Change the Final Integration time to match the Refresh period of CScope block.
|
|
The value of Refresh period was 10.
|
Click on OK | |
|
Now, click on File and then click on Save to save the Xcos diagram.
|
However, it is advised to save it in the folder where you have saved the squareit.sci file.
Click on OK
| |
Note that the scifunc block will call the squareit function.
| |
|
Switch to the Scilab editor window, which has the squareit.sci file open.
|
|
Click on the Execute button available on the menu bar of the editor.
This will load the squareit function.
|
|
Open the Xcos diagram file
Click on the Start button available on the menu bar of Xcos window
|
|
This window will have two plots.
|
Notice that the squaring function implemented in the squareit function, has indeed squared the input sine wave.
| |
Now, let us see how to edit the scifunc block to call a function which has more than one input and output variables.
| |
|
Switch to scilab editor window.
|
|
We will change the function to shift the squared output by 1 unit.
y is equal to b plus a raise to two put a semicolon in the end
|
|
Also generate an output whose amplitude will be half of the input.
|
Now switch to the xcos window
| |
Similarly, in the output port size field, put a semi colon after 1 comma 1 and type 1 comma 1 again
Click on Ok | |
|
A new Scilab input value request window will open.
|
Click on Ok in the subsequent 3 windows | Another Scilab input value request window will open.
|
Switch to the palette browser window.
Close it near the sinusoid generator block.
| |
Connect the Constant underscore m block to the lower input of the scifunc block
| |
Double click on the MUX block.
| |
Let me resize the MUX block and I will connect MUX and CSCOPE block properly.
Connect the lower output port of the scifunc block to the lower input port of MUX block
| |
|
Switch to the Scilab editor which has the squareit.sci file open.
|
|
Click on the Execute button available on the menu bar of the editor.
|
|
Click on the Start button available on the menu bar of Xcos window.
|
|
This window will have three plots.
|
Notice that the function has indeed squared the input sine wave and also shifted it by an offset of 1 unit, which is as expected.
| |
|
Now let us summarize.
In this tutorial we learnt,
|
|
Watch the video available at the following link
|
|
The Spoken Tutorial Project Team
|
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
Thanks for joining.
Hope you found this tutorial useful. This is Anuradha Amruthkar from IIT Bombay signing off.
|