Difference between revisions of "Scilab/C4/Calling-User-Defined-Functions-in-XCOS/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of the Script:''' C'''alling user-defined functions in Xcos''' '''Author:''' Rupak Rokade, IIT Bombay '''Keywords: '''video tutorial,''' '''Xcos. {| style="border-s…')
 
Line 877: Line 877:
 
Hope you found this tutorial useful.  
 
Hope you found this tutorial useful.  
  
This is Rupak Rokade from IIT Bombay signing off.  
+
This is Anuradha Amruthkar from IIT Bombay signing off.  
  
  

Revision as of 15:23, 25 April 2013

Title of the Script: Calling user-defined functions in Xcos

Author: Rupak Rokade, IIT Bombay

Keywords: video tutorial, Xcos.


Visual Cues
Narration
Show slide
Welcome to the spoken tutorial on “Calling user-defined functions in Xcos
Show slide
In this tutorial we will learn,
# To write a squaring function in Scilab
  1. To use the scifunc block in Xcos
  2. To use MUX block to draw multiple plots
  3. To call functions having multiple inputs and outputs


Show slide
Ubuntu 12.04 is the operating system used with Scilab version 5.3.3 installed.
Show slide
You should have
  1. Basic knowledge of Scilab.
  2. Basic knowledge of Xcos
  3. If not, for relevant tutorials please visit spoken hyphen tutorial dot org


On computer
Start Scilab on your computer.



On scilab
In the Scilab console, type editor and press Enter



function y=squareit(a)

y=a^2;

endfunction

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



Show the variables
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.



On scilab
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.



Point as per narration
Switch to the Scilab console


Now, type Xcos and press Enter.


Two windows will open


A palette browser and an untitled Xcos window.

On scilab
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 User-Defined function in the pallet browser.


Locate the block scifunc_block_m in this section.


Drag and drop it in the untitled Xcos window.



On scilab
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.



Point to the input and output port sizes


Click on Ok.
Our function squareit has only one input and output variable.


Hence, we will keep the settings unchanged.


Click on Ok.

On scilab
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 y1=squareit(u1) In the text box available,


edit the default function name.


Type y1 equal to squareit open bracket u1 close bracket



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.


Cick on OK.

Click on Ok in the subsequent 3 windows 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 been configured.



Do as per narration
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 the untitled Xcos window.



Do as per narration
Place the block towards the left of the scifunc block for convenience.



Do as per narration
Now we need a block to plot the output variable.


In the palette browser window, click on Sinks section.


Drag and drop the CScope block in the untitled Xcos window.



Do as per narration
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.



Do as per narration
In the palette browser window, click on Sources section.


Drag and drop the clock underscore c block in the untitled Xcos window.



Do as per narration
Place it above the CScope block.



Point as per narration
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.

Do as per narration
In the palette browser window, click on Signal Routing section.


Drag and drop the MUX block in the untitled Xcos window.

Do as per narration
Place the block between the scifunc block and CScope block.
Do as per narration
Let me resize and re align 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



Do as per narration
Now connect the output port of scifunc block to the lower input port of the MUX.



Do as per narration
Connect the output port of MUX block to the input port of CScope block.



Do as per narration
Connect the output port of clock underscore c block to the event input port of CScope block.



Do as per narration
We also have to plot sine input.


We have to connect the Sinusoid generator block to the MUX.


Click on the upper input port of the MUX block.



Do as per narration and don't release the mouse button
Retract after showing this part
Then without releasing, move your mouse pointer towards the link between the Sinusoid generator block and scifunc block.



Do as per narration
To bend the link, release the mouse button or click at places



Do as per narration


Point to the 2 blocks
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.



Point as per narration
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
Click on Ok to close the configuration window.
On scilab
Now let us configure the CScope block.


Double click on the CScope block to open its configuration window.



Do as per narration
Change the Ymin parameter to minus 2 and Ymax parameter to 2



Do as per narration
Change the Refresh period value to 10.


Make a mental note of this value.



Do as per narration
Change the buffer size value to 2 .

click on OK

On scilab
Do as per narration
Now let us configure the CLOCK_c block


Double click on the block to open its configuration window.

Point as per narration
Keep the value of Period as 0.1
Do as per narration
Change the Initialisation Time value to be 0.


Click on OK

Do as per narration
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.



Point to Final Integration time
Change the Final Integration time to match the Refresh period of CScope block.



Do as per narration
The value of Refresh period was 10.


Hence, put the value of Final integration time as 10.



Click on OK
Do as per narration
Now, click on File and then click on Save to save the Xcos diagram.


I will save it as squareit.xcos


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.



Do as per narration
Switch to the Scilab editor window, which has the squareit.sci file open.



Do as per narration
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.



Do as per narration
Open the Xcos diagram file

Click on the Start button available on the menu bar of Xcos window


A graphic window will appear.



Point as per narration
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.



[y,z]
(a,b)
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



y=b+a^2;
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



Z=0.5*a;
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


Save the file



Now 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

On scilab


[y1,y2]=squareit(u1,u2)

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

Click on Ok in the subsequent 3 windows Another Scilab input value request window will open.


Keep on clicking Ok in the subsequent 3 windows that will appear.


The scifunc block has now been configured.



Switch to the palette browser window.


In the sources section, drag and drop the Constant underscore m block in the xcos window.

Close it near 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.



Do as per narration
Switch to the Scilab editor which has the squareit.sci file open.



Do as per narration
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.



Do as per narration
Click on the Start button available on the menu bar of Xcos window.


A graphic window will appear.



Point as per narration
This window will have three plots.


Input sine wave in black color,


output sine wave in green color and


the 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

Show slide
Now let us summarize.

In this tutorial we learnt,

  1. To write a squaring function in Scilab
  2. To use the scifunc block in Xcos
  3. To use MUX block to draw multiple plots
  4. To call functions having multiple input and output


Show slide


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


Show slide
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 at spoken hyphen tutorial dot org


Show slide
Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at
  • spoken hyphen tutorial dot org slash NMEICT hyphen Intro


Thanks for joining.

Hope you found this tutorial useful.

This is Anuradha Amruthkar from IIT Bombay signing off.


Thank You.

Contributors and Content Editors

Lavitha Pereira, PoojaMoolya