Difference between revisions of "Scilab/C4/Conditional-operations-in-Xcos/English"
Nancyvarkey (Talk | contribs) |
|||
Line 22: | Line 22: | ||
In this tutorial, we will learn how to: | In this tutorial, we will learn how to: | ||
− | + | *Use the '''If then Else conditional operator''' in '''Xcos'''. | |
− | + | *Display the output of the '''conditional operations.''' | |
− | + | *Halt the '''simulation''' after a '''condition''' is reached. | |
− | + | ||
|- | |- | ||
| | | | ||
Line 34: | Line 33: | ||
To record this tutorial, I am using | To record this tutorial, I am using | ||
− | + | *'''Ubuntu 18.04 OS''' and | |
− | + | *'''Scilab version 6.1.0''' | |
− | + | ||
|- | |- | ||
| | | | ||
Line 45: | Line 43: | ||
To follow this tutorial, you should have : | To follow this tutorial, you should have : | ||
− | + | *Basic knowledge of '''Scilab''' and '''Xcos.''' | |
− | + | *If not, then go through the '''Scilab''' spoken tutorials on this website. | |
− | + | ||
− | If not, then go through the '''Scilab''' spoken tutorials on this website. | + | |
|- | |- | ||
| | | | ||
Line 55: | Line 51: | ||
'''Code Files''' | '''Code Files''' | ||
| | | | ||
− | + | *The files used in this tutorial are provided in the '''Code files''' link. | |
− | + | *Please download and extract the files. | |
− | + | *Make a copy and then use them while practising. | |
− | + | ||
|- | |- | ||
| Cursor on the '''Untitled Xcos''' window. | | Cursor on the '''Untitled Xcos''' window. | ||
− | | | + | |I have already opened the '''Xcos''' window. |
− | I have already opened the '''Xcos''' window. | + | |
We can see two windows named '''Untitled Xcos''' and the '''Palette browser.''' | We can see two windows named '''Untitled Xcos''' and the '''Palette browser.''' | ||
Line 84: | Line 78: | ||
|- | |- | ||
| cursor on the '''ConditionalOperations.xcos''' window. | | cursor on the '''ConditionalOperations.xcos''' window. | ||
− | | This file has a simple '''Xcos''' | + | | This file has a simple '''Xcos simulation''' diagram. |
|- | |- | ||
| Cursor on '''ConditionalOperations.xcos window''' | | Cursor on '''ConditionalOperations.xcos window''' | ||
| | | | ||
− | Let us add conditional expressions to this file. | + | Let us add '''conditional expressions''' to this file. |
− | To add conditional expressions we will use the '''Expression''' | + | To add '''conditional expressions''' we will use the '''Expression block'''. |
|- | |- | ||
| | | | ||
Line 101: | Line 95: | ||
Click on the '''User-Defined-Functions''' section. | Click on the '''User-Defined-Functions''' section. | ||
− | In the right panel, select the '''Expression''' | + | In the right panel, select the '''Expression block'''. |
|- | |- | ||
− | | Drag the ''' | + | | Drag the '''Expression block''' and drop it on the '''ConditionalOperations.xcos window.''' |
− | | | + | | Drag and drop the '''Expression block''' on the '''ConditionalOperations.xcos''' window. |
|- | |- | ||
| | | | ||
Line 110: | Line 104: | ||
Double click on the '''Expression''' block. | Double click on the '''Expression''' block. | ||
− | | Double click on the '''Expression block''' to configure its properties. | + | | Double click on the '''Expression block''' to '''configure''' its properties. |
|- | |- | ||
| | | | ||
− | On '''Scilab Multiple Request''' | + | On '''Scilab Multiple Request''' window, |
Double click on '''Number of Input''' textbox to highlight it >> Change it to '''1'''. | Double click on '''Number of Input''' textbox to highlight it >> Change it to '''1'''. | ||
| | | | ||
− | First is the ''' | + | First is the '''Number of inputs'''. |
By default it is '''2''' which means it can receive 2 input values. | By default it is '''2''' which means it can receive 2 input values. | ||
Line 130: | Line 124: | ||
Next, is the '''conditional expression'''. | Next, is the '''conditional expression'''. | ||
− | This is the expression which will be evaluated by the '''Expression''' | + | This is the '''expression''' which will be evaluated by the '''Expression block'''. |
− | In '''Xcos,''' by default variables like '''u1, u2''' are used while writing the conditional expressions. | + | In '''Xcos,''' by default '''variables''' like '''u1, u2''' are used while writing the '''conditional expressions'''. |
− | Let us change the default expression. | + | Let us change the default '''expression'''. |
− | Enter the expression as shown here. | + | Enter the '''expression''' as shown here. |
|- | |- | ||
| | | | ||
Line 142: | Line 136: | ||
Double click on the scilab '''expression''' textbox to highlight it. | Double click on the scilab '''expression''' textbox to highlight it. | ||
− | | This means that, if '''''u1 > 10''''' then the block should | + | | This means that, if '''''u1 > 10''''' then the '''block''' should '''return 1'''. |
|- | |- | ||
| | | | ||
Line 152: | Line 146: | ||
| Cursor on '''ConditionalOperations.xcos window''' | | Cursor on '''ConditionalOperations.xcos window''' | ||
| | | | ||
− | Let us display the output of the '''Expression''' | + | Let us display the output of the '''Expression block'''. |
− | To do this, we will use the '''AFFICH_m''' | + | To do this, we will use the '''AFFICH_m block'''. |
|- | |- | ||
| | | | ||
− | On ''' | + | On '''Palette browser''', |
click on '''sinks''' >> click on '''AFFICH_m.''' | click on '''sinks''' >> click on '''AFFICH_m.''' | ||
| | | | ||
− | Go to the ''' | + | Go to the '''Palette browser''' window. |
− | Click on the '''Sinks''' | + | Click on the '''Sinks palette'''. |
− | In the right panel, select the '''AFFICH_m''' | + | In the right panel, select the '''AFFICH_m block'''. |
|- | |- | ||
| Drag the '''AFFICH_m''' block and drop it on the '''ConditionalOperations.xcos window.''' | | Drag the '''AFFICH_m''' block and drop it on the '''ConditionalOperations.xcos window.''' | ||
− | | | + | | Drag and drop the '''AFFICH_m block''' on the '''ConditionalOperations.xcos''' window. |
|- | |- | ||
| | | | ||
On '''ConditionalOperations.xcos Window''', | On '''ConditionalOperations.xcos Window''', | ||
− | Connect as per narration | + | Connect as per narration |
| | | | ||
Let us make the connections. | Let us make the connections. | ||
− | Select the '''black input port''' of the '''EXPRESSION''' | + | Select the '''black input port''' of the '''EXPRESSION block'''. |
− | Connect it to the line joining the '''RAMP''' and the '''CSCOPE''' | + | Connect it to the line joining the '''RAMP''' and the '''CSCOPE block'''. |
|- | |- | ||
| | | | ||
Line 186: | Line 180: | ||
Connect as per narration'''.''' | Connect as per narration'''.''' | ||
| | | | ||
− | Next, Select the '''black output port''' of the '''EXPRESSION''' | + | Next, Select the '''black output port''' of the '''EXPRESSION block'''. |
− | Connect it to the '''black input port''' of the '''AFFICH_m''' | + | Connect it to the '''black input port''' of the '''AFFICH_m block'''. |
|- | |- | ||
| | | | ||
Line 195: | Line 189: | ||
Connect as per narration'''.''' | Connect as per narration'''.''' | ||
| | | | ||
− | Lastly, | + | Lastly, select the '''red input port''' of the '''AFFICH_m block'''. |
− | Connect it to the line joining the '''CLOCK_c''' and the '''CSCOPE''' | + | Connect it to the line joining the '''CLOCK_c''' and the '''CSCOPE block'''. |
− | Our file is ready for simulation. | + | Our file is ready for '''simulation'''. |
|- | |- | ||
| | | | ||
Line 206: | Line 200: | ||
Click on '''Simulation''' >> Click on '''Setup.''' | Click on '''Simulation''' >> Click on '''Setup.''' | ||
| | | | ||
− | Before that, we need to change the execution time to | + | Before that, we need to change the '''execution''' time to real-time. |
On the '''menu bar''', click on '''Simulation.''' | On the '''menu bar''', click on '''Simulation.''' | ||
Line 226: | Line 220: | ||
Click on '''Start''' button on the toolbar >> click on '''OK''' button on the Warning Message. | Click on '''Start''' button on the toolbar >> click on '''OK''' button on the Warning Message. | ||
| | | | ||
− | Let us run this simulation by clicking on the '''Start''' | + | Let us '''run''' this '''simulation''' by clicking on the '''Start''' button on the '''toolbar'''. |
A '''Warning Message''' will appear. | A '''Warning Message''' will appear. | ||
Line 237: | Line 231: | ||
Cursor on '''AFFICH_m''' block. | Cursor on '''AFFICH_m''' block. | ||
| | | | ||
− | We can see that the '''AFFICH_m''' | + | We can see that the '''AFFICH_m block''' displays '''0''' as long as the graph is less than '''10''' . |
− | After the graph exceeds '''10''' the '''AFFICH_m''' | + | After the graph exceeds '''10''' the '''AFFICH_m block''' displays '''1'''. |
|- | |- | ||
| Cursor on the '''Graphic Window.''' | | Cursor on the '''Graphic Window.''' | ||
| | | | ||
− | Suppose we want our simulation to end after some condition has been achieved. | + | Suppose we want our '''simulation''' to end after some '''condition''' has been achieved. |
− | To do this, we will use the '''IFTHEL_f''' | + | To do this, we will use the '''IFTHEL_f block'''. This is the '''IF-ELSE block'''. |
|- | |- | ||
| Close the '''Graphic Window.''' | | Close the '''Graphic Window.''' | ||
Line 259: | Line 253: | ||
Click on the '''Event Handling''' section. | Click on the '''Event Handling''' section. | ||
− | In the right panel, select the '''IFTHEL_f''' | + | In the right panel, select the '''IFTHEL_f block'''. |
|- | |- | ||
− | | Drag the '''IFTHEL_f''' | + | | Drag the '''IFTHEL_f block''' and drop it on the '''ConditionalOperations.xcos''' window. |
− | | '''Drag and drop''' the '''IFTHEL_f''' | + | | '''Drag and drop''' the '''IFTHEL_f block''' on the '''ConditionalOperations.xcos''' window. |
|- | |- | ||
| | | | ||
Line 268: | Line 262: | ||
Cursor on '''IFTHEL_m''' block. | Cursor on '''IFTHEL_m''' block. | ||
− | | Now, to halt the simulation we would need the '''HALT_f''' | + | | Now, to halt the '''simulation''' we would need the '''HALT_f block'''. |
|- | |- | ||
| | | | ||
Line 279: | Line 273: | ||
Click on the '''Event Handling''' section. | Click on the '''Event Handling''' section. | ||
− | In the right panel, select the '''HALT_f''' | + | In the right panel, select the '''HALT_f block'''. |
|- | |- | ||
| Drag the '''HALT_f''' block and drop it on the '''ConditionalOperations.xcos window.''' | | Drag the '''HALT_f''' block and drop it on the '''ConditionalOperations.xcos window.''' | ||
− | | '''Drag and drop''' the '''HALT_f''' | + | | '''Drag and drop''' the '''HALT_f block''' on the '''ConditionalOperations.xcos''' window. |
|- | |- | ||
| | | | ||
Line 288: | Line 282: | ||
Cursor on '''HALT_f''' block. | Cursor on '''HALT_f''' block. | ||
− | | The '''HALT_f''' | + | | The '''HALT_f block''' will end the '''simulation''' when the '''condition''' in the '''IFTHEL_f block''' is satisfied. |
|- | |- | ||
| | | | ||
Line 297: | Line 291: | ||
Let us make the connections. | Let us make the connections. | ||
− | Select the '''black input port''' of the '''IFTHEL_f''' | + | Select the '''black input port''' of the '''IFTHEL_f block'''. |
− | Connect it to the line joining the '''EXPRESSION''' and the '''AFFICH_m''' | + | Connect it to the line joining the '''EXPRESSION''' and the '''AFFICH_m block'''. |
|- | |- | ||
| | | | ||
Line 306: | Line 300: | ||
Connect as per narration'''.''' | Connect as per narration'''.''' | ||
| | | | ||
− | Next, | + | Next, select any of the '''red output ports''' of the '''IFTHEL_f block'''. |
− | Connect it to the '''red input port''' of the '''HALT_f''' | + | Connect it to the '''red input port''' of the '''HALT_f block'''. |
|- | |- | ||
| | | | ||
Line 315: | Line 309: | ||
Connect as per narration'''.''' | Connect as per narration'''.''' | ||
| | | | ||
− | Lastly, | + | Lastly, select the '''red input port''' of the '''IFTHEL_f block'''. |
− | Connect it to the line entering the '''red input port''' of the '''AFFICH_m | + | Connect it to the line entering the '''red input port''' of the '''AFFICH_m block'''. |
|- | |- | ||
| | | | ||
Line 324: | Line 318: | ||
Click on '''Start''' button on the toolbar >> click on '''OK''' button on the Warning Message. | Click on '''Start''' button on the toolbar >> click on '''OK''' button on the Warning Message. | ||
| | | | ||
− | Let us run this simulation again by clicking on the '''Start''' | + | Let us '''run''' this '''simulation''' again by clicking on the '''Start''' button on the '''toolbar'''. |
A '''Warning Message''' will appear. | A '''Warning Message''' will appear. | ||
Line 335: | Line 329: | ||
Cursor on '''AFFICH_m''' block. | Cursor on '''AFFICH_m''' block. | ||
| | | | ||
− | Observe that as soon as the value in the '''AFFICH_m''' | + | Observe that as soon as the value in the '''AFFICH_m block''' becomes '''1''', the '''simulation''' stops. |
− | So this way we can use conditional operations in '''Xcos'''. | + | So this way we can use '''conditional operations''' in '''Xcos'''. |
|- | |- | ||
| | | | ||
Line 348: | Line 342: | ||
In this tutorial, we have learnt to: | In this tutorial, we have learnt to: | ||
− | + | *Use the '''If then Else conditional operator''' in '''Xcos'''. | |
− | + | *Display the output of the '''conditional operations'''. | |
− | + | *Halt the '''simulation''' after a '''condition''' is reached. | |
− | + | ||
|- | |- | ||
| | | | ||
Line 360: | Line 353: | ||
As an assignment | As an assignment | ||
− | Create a '''Xcos''' | + | Create a '''Xcos Simulation''' file to: |
− | + | *Plot a straight line with slope 5. | |
− | + | *Apply a '''conditional operator''' which will output 1 if the value is divisible by 9. | |
− | + | *Halt the '''simulation''' when the '''condition''' evaluates to '''True'''. | |
− | + | ||
|- | |- | ||
| Show Slide: '''About Spoken Tutorial project''' | | Show Slide: '''About Spoken Tutorial project''' |
Latest revision as of 17:37, 21 September 2021
tTitle of script: Conditional operations in Xcos
Author: Rupak Rokade and Utkarsh
Keywords: Xcos, conditional operation, scilab, if-else, loops
Visual Cue | Narration |
---|---|
Show Slide: Title Slide |
Hello and welcome to the Spoken Tutorial on “Conditional Operations in Xcos”. |
Show Slide: Learning Objectives |
In this tutorial, we will learn how to:
|
Show Slide: System Requirements |
To record this tutorial, I am using
|
Show Slide: Pre-requisites |
To follow this tutorial, you should have :
|
Show Slide: Code Files |
|
Cursor on the Untitled Xcos window. | I have already opened the Xcos window.
We can see two windows named Untitled Xcos and the Palette browser. |
On the Untitled Xcos window, Cursor on Toolbar >> Click on Open file option >> Click on ConditionalOperations.xcos >> Click on OK. |
Go to the Untitled Xcos window. Click on the Open File icon on the Toolbar. A window will appear. Go to the Downloads folder and select the file ConditionalOperations.xcos. Then click on the OK button. I have already created this file for demonstration. |
cursor on the ConditionalOperations.xcos window. | This file has a simple Xcos simulation diagram. |
Cursor on ConditionalOperations.xcos window |
Let us add conditional expressions to this file. To add conditional expressions we will use the Expression block. |
On palette browser, click on user-defined-functions >> click on Expression. |
Go to the palette browser window. Click on the User-Defined-Functions section. In the right panel, select the Expression block. |
Drag the Expression block and drop it on the ConditionalOperations.xcos window. | Drag and drop the Expression block on the ConditionalOperations.xcos window. |
On ConditionalOperations.xcos window, Double click on the Expression block. |
Double click on the Expression block to configure its properties. |
On Scilab Multiple Request window, Double click on Number of Input textbox to highlight it >> Change it to 1. |
First is the Number of inputs. By default it is 2 which means it can receive 2 input values. Let us change it to 1. |
On Scilab Multiple Request Window, Double click on scilab expression textbox to highlight it >> type (u1>10)*1 . |
Next, is the conditional expression. This is the expression which will be evaluated by the Expression block. In Xcos, by default variables like u1, u2 are used while writing the conditional expressions. Let us change the default expression. Enter the expression as shown here. |
On Scilab Multiple Request Window, Double click on the scilab expression textbox to highlight it. |
This means that, if u1 > 10 then the block should return 1. |
On Scilab Multiple Request Window, Click on the OK button. |
Click on the OK button. |
Cursor on ConditionalOperations.xcos window |
Let us display the output of the Expression block. To do this, we will use the AFFICH_m block. |
On Palette browser, click on sinks >> click on AFFICH_m. |
Go to the Palette browser window. Click on the Sinks palette. In the right panel, select the AFFICH_m block. |
Drag the AFFICH_m block and drop it on the ConditionalOperations.xcos window. | Drag and drop the AFFICH_m block on the ConditionalOperations.xcos window. |
On ConditionalOperations.xcos Window, Connect as per narration |
Let us make the connections. Select the black input port of the EXPRESSION block. Connect it to the line joining the RAMP and the CSCOPE block. |
On ConditionalOperations.xcos Window, Connect as per narration. |
Next, Select the black output port of the EXPRESSION block. Connect it to the black input port of the AFFICH_m block. |
On ConditionalOperations.xcos Window, Connect as per narration. |
Lastly, select the red input port of the AFFICH_m block. Connect it to the line joining the CLOCK_c and the CSCOPE block. Our file is ready for simulation. |
On the menu bar, Click on Simulation >> Click on Setup. |
Before that, we need to change the execution time to real-time. On the menu bar, click on Simulation. Click on Setup. |
On Set parameters window, Change Real Time Scaling to 1 >> Click on OK button. |
Change Real Time Scaling to 1. Click on the OK button. |
On the ConditionalOperations.xcos window, Click on Start button on the toolbar >> click on OK button on the Warning Message. |
Let us run this simulation by clicking on the Start button on the toolbar. A Warning Message will appear. Ignore the message and click on the OK button. |
On the ConditionalOperations.xcos window, Cursor on AFFICH_m block. |
We can see that the AFFICH_m block displays 0 as long as the graph is less than 10 . After the graph exceeds 10 the AFFICH_m block displays 1. |
Cursor on the Graphic Window. |
Suppose we want our simulation to end after some condition has been achieved. To do this, we will use the IFTHEL_f block. This is the IF-ELSE block. |
Close the Graphic Window. | Let’s close the Graphic window. |
On palette browser, click on Event-Handling >> click on IFTHEL_f. |
Go to the palette browser window. Click on the Event Handling section. In the right panel, select the IFTHEL_f block. |
Drag the IFTHEL_f block and drop it on the ConditionalOperations.xcos window. | Drag and drop the IFTHEL_f block on the ConditionalOperations.xcos window. |
On the ConditionalOperations.xcos window, Cursor on IFTHEL_m block. |
Now, to halt the simulation we would need the HALT_f block. |
On palette browser, click on Event-Handling >> click on HALT_f. |
Go to the palette browser window. Click on the Event Handling section. In the right panel, select the HALT_f block. |
Drag the HALT_f block and drop it on the ConditionalOperations.xcos window. | Drag and drop the HALT_f block on the ConditionalOperations.xcos window. |
On the ConditionalOperations.xcos window, Cursor on HALT_f block. |
The HALT_f block will end the simulation when the condition in the IFTHEL_f block is satisfied. |
On ConditionalOperations.xcos Window, Connect as per narration. |
Let us make the connections. Select the black input port of the IFTHEL_f block. Connect it to the line joining the EXPRESSION and the AFFICH_m block. |
On ConditionalOperations.xcos Window, Connect as per narration. |
Next, select any of the red output ports of the IFTHEL_f block. Connect it to the red input port of the HALT_f block. |
On ConditionalOperations.xcos Window, Connect as per narration. |
Lastly, select the red input port of the IFTHEL_f block. Connect it to the line entering the red input port of the AFFICH_m block. |
On the ConditionalOperations.xcos window, Click on Start button on the toolbar >> click on OK button on the Warning Message. |
Let us run this simulation again by clicking on the Start button on the toolbar. A Warning Message will appear. Ignore the message and click on the OK button. |
On the ConditionalOperations.xcos window, Cursor on AFFICH_m block. |
Observe that as soon as the value in the AFFICH_m block becomes 1, the simulation stops. So this way we can use conditional operations in Xcos. |
Show Slide: Summary |
This brings us to the end of this tutorial. Let us summarise. In this tutorial, we have learnt to:
|
Show Slide: Assignment |
As an assignment Create a Xcos Simulation file to:
|
Show Slide: About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project. Please download and watch it |
Show Slide: Spoken Tutorial Workshops |
The Spoken Tutorial Project team conducts workshops and gives certificates. For more details, please write to us. |
Show Slide: Answers for THIS Spoken Tutorial |
Please post your timed queries in this forum. |
Show Slide: FOSSEE Forum | Please post your general and technical queries on Scilab in this forum. |
Show Slide: Textbook Companion project. |
The FOSSEE team coordinates the Textbook Companion project. We give Certificates and Honorarium to the contributors. For more details, please visit this website. |
Show Slide: Lab Migration |
The FOSSEE team coordinates the Lab Migration project. For more details, please visit this website. |
Show Slide: Acknowledgement | Spoken Tutorial and FOSSEE projects are funded by the Ministry of Education, Government of India. |
Show Slide: Thank you |
This is Utkarsh, FOSSEE intern 2021, IIT Bombay signing off. Thanks for joining. |