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
* To use the scifunc block in Xcos
* To use MUX block to draw multiple plots
* To call functions having multiple inputs and outputs.
Ubuntu 12.04 is the operating system used with Scilab version 5.3.3 installed.
You should have basic knowledge of Scilab,
basic knowledge of Xcos.
If not, for relevant tutorials, please visit spoken hyphen tutorial dot org.
Start Scilab on your computer.
In the Scilab console, type editor and press Enter.
Then type the following code:
function space y is equal to squareit open bracket a close bracket.
Press the Enter key and type:
y is equal to a raise to 2
put a semicolon in the end.
The function has one input variable a and one output variable y.
The name of the function is squareit.
This function will do the job of squaring the variable a.
It will store the result in y.
Let us save this file in the desired directory.
I will save this file with file name squareit and extension .sci.
Here we are following the convention of saving functions in .sci format.
Switch to the Scilab console.
Now, type Xcos and press Enter.
Two windows will open,
a Palette browser and an Untitled Xcos window.
Now we will make the Xcos diagram.
This will access the function squareit created just now.
This can be done by using scifunc block.
Switch to the Palette browser window.
Click on the User-Defined function in the pallet browser.
Locate the block scifunc_block_m in this section.
Drag and drop it in untitled Xcos 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.
A window named Scilab Multiple Values Request will open.
This window will allow you to change the number of input and output ports of the scifunc block.
Our function squareit has only one input and output variable.
Hence, we will keep the settings unchanged.
Click on OK.
A new Scilab Input Value Request window will open.
In the text box, type the function name along with input and output variables.
This function will be called by the scifunc block.
In the text box available,
edit the default function name.
Type y1 equal to squareit open bracket u1 close the bracket.
Note that here the input and output variables are u1 and y1 respectively.
These should be strictly in u and y form and not as per the variable names used in the actual function.
Cick on OK.
Another Scilab Input Value Request window will open.
Keep clicking on OK in the subsequent 3 windows that will appear.
The scifunc block is now configured.
Next we will include a sinusoid generator block.
In the Palette browser window, click on Sources section.
Drag and drop the Sinusoid generator block in Untitled Xcos window.
Place the block towards the left of the scifunc block, for convenience.
Now we need a block to plot the output variable.
In the Palette browser window, click on the Sinks section.
Drag and drop the CScope block in the Untitled Xcos window.
Place the block towards the right of the scifunc block.
Place it away from scifunc block, for convenience.
Notice that the CScope block has a red input port.
This is an event input.
we need an event generator block.
In the Palette browser window, click on Sources section.
Drag and drop the CLOCK underscore c block in the Untitled Xcos window.
Place it above the CScope block.
Notice that the CScope block has only one input port.
But we want to plot both the input and output variables in a single plot window.
Hence, we need a multiplexer block.
This block will multiplex two inputs and generate output on one output port.
In the Palette browser window, click on Signal Routing section.
Drag and drop the MUX block in the Untitled Xcos window.
Place the block between the scifunc block and CScope block.
Let me resize and realign the Mux block.
Now, let us connect the blocks together.
Connect the output port of Sinusoid generator block to the input port of scifunc block.
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 the sine input.
We have to connect the Sinusoid generator block to the MUX.
Click on the upper input port of the MUX block.
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.
Release the mouse button or click once to create a link between these two blocks.
Now let us see the configuration of other blocks.
We can change the frequency, magnitude and phase of the sinusoid generator block.
To do this, double click on the Sinusoid generator block.
The configuration window will open.
We will keep the Magnitude and Frequency as 1 and Phase as 0.
Click on OK to close the configuration window.
Now let us configure the CScope block.
Double click on the CScope block to open its configuration window.
Change the Ymin parameter to minus 2 and Ymax parameter to 2.
Change the Refresh period value to 10.
Make a mental note of this value.
Change the Buffer size value to 2 .
click on OK.
Now let us configure the CLOCK_c block.
Double click on the block to open its configuration window.
Keep the value of Period as 0.1.
Change the Initialisation Time to be 0.
Click on OK.
Now let us change the Simulation parameters.
On the menu bar of the Untitled Xcos window click on the Simulation tab.
Now click on Setup from the drop down menu.
Change the Final Integration time to match the Refresh period of CScope block.
The value of Refresh period was 10.
Hence, put the value of Final integration time as 10.
Click on OK.
Now, click on File and then click on Save to save the Xcos diagram.
Choose a desired directory 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.
This means that we should first load the squareit function before we execute the Xcos diagram.
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.
Now we can execute the Xcos diagram.
Open the Xcos diagram file.
Click on the Start button available on the menu bar of Xcos window.
A graphic window will appear.
This window will have two plots.
Input sine wave in black color and output sine wave in green color.
Notice that the squaring function implemented in the squareit function, has indeed squared the input sine wave.
Hence, the output sine wave has been shifted to the positive axis.
Close the plot window.
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.
Edit the squareit function to have two input and output variables.
Edit the output variable as open square bracket y comma z close the square bracket .
Edit the input variables as open bracket a comma b close bracket.
We will change the function to shift the squared output by 1 unit.
Edit the main function line as:
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.
Go to next line by pressing Enter key and type:
z is equal to 0.5 multiplied by a put a semicolon in the end.
Now save the file.
Switch to the Xcos window.
Double click on the scifunc block to configure it.
In the input port size field, put a semi colon after 1 comma 1 and type 1 comma one again.
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.
In the text box,
put a comma after y1 and type y2,
put y1 and y2 in square brackets,
now put a comma after u1 and type u2.
Click on OK.
Another Scilab Input Value Request window will open.
Keep clicking on OK in the subsequent 3 windows that will appear.
The scifunc block is now configured.
Let me realign the scifunc block.
Switch to the Palette browser window.
In the Sources section, drag and drop the Constant underscore m block in the Xcos window.
Place it below the Sinusoid generator block.
Connect the Constant underscore m block to the lower input of the scifunc block.
The default value of this block is 1.
Keep it unchanged.
Double click on the MUX block.
Change the input port size to 3.
Click on OK.
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.
Click on File and choose Save to save the xcos file.
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.
This will load the squareit function.
Now we can execute the Xcos diagram.
Click on the Start button available on the menu bar of the Xcos window.
A graphic window will appear.
This window will have three plots.
Input sine wave in black color,
output sine wave in green color and
amplitude scaled input in red colour.
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.
We also get the amplitude scaled of the input sine wave, as expected.
Close the plot window.
Now let us summarize.
In this tutorial we learnt:
* To write a squaring function in Scilab
* To use the scifunc block in Xcos
* To use MUX block to draw multiple plots
* To call functions having multiple input and output.
Watch the video available at the following link.
It summarizes the Spoken Tutorial project.
If you do not have good bandwidth, you can download and watch it.
The spoken tutorial project Team:
Conducts workshops using spoken tutorials.
Gives certificates for those who pass an online test.
For more details, please write to contact@spoken-tutorial.org
Spoken Tutorial Project is a part of Talk to a Teacher project.
It is supported by the National Mission on Eduction through ICT, MHRD, Government of India.
More information on this mission is available at spoken-tutorial.org/NMEICT-Intro.
Thank you for joining. Hope you found this tutorial useful.
This is Anuradha Amruthkar from IIT Bombay, signing off.