A global variable shared by multiple blocks

Hi!

I’m a very be beginner. I have, it seems, a basic question. Is it possible to put a variable (e.g. CONST block) in the schematic that can be used subsequently in other blocks? See the attached picture of what I want to get.

I would like to have the visible value of a parameter, which can be quickly changed with a double-click. I want to use this parameter in several blocks and not change each one individually in the dialog box, nop. the PID block.

Maybe there is another way to get the expected functionality?

Regards!
Robert

Yes, for this you can use the context of the simulation. Go to the upper buttons of the window and click on “Simulation”, and in the dropdown click on “set context”. This will open a window with a text box. Inside this box you can write variables, and you can write their name in all the system and it’s subsystems (meaning the superblocks). If you change the values there before running the simulation it will also change everywhere else in the simulation that you wrote that variable.
I use this function of xcos extensibely in all my proyects.

1 Like

Thank you for your response!

Can the variables from the Context menu somehow be assigned to elements in the diagram so that they can be easily seen? I understand that when putting them into the Context window, you have to press the OK button at the end and the window closes.

Robert

I’m not quite sure what you want to do, but if you just want to see the value in a variable you can write the variable in a “constant” block and connect it to an affich block wich will display it’s value. Remember to connect a clock to the afich block.

Hi!

I am really a very beginner. In my simulation I have 11 parameters that are inside blocks and superblocks and you can’t see them. And I wanted to somehow collect them in one place to keep an eye on them. At first I wanted to put them in the diagram, and somehow associate them with the variables in each block, but I couldn’t manage it. Eventually I wrote a sce script where I display them and control the simulation. Take a look at the attachment.

That’s nice. You could do it another way too. You could have those values as variables in the scilab terminal and bring them to xcos through multiple FROMWSB blocks.
The FROMWSB block expects the variable to have a time property and a value property, you can make the time be 0 and have the value be the one that you want. You also configure the parameter “Output at end” to 1 to make the block hold that value to the end. This way you can change the variables before starting the simulation using sliders, or whatever you like.
Check the docs: FROMWSB - Data from Scilab workspace to Xcos

I reread the initial question. You want to be able to change and monitor variables with a gui, but also to use those variables in multiple places. I don’t know if you can bring things from the scilab terminal using the context box, but you could use a GOTO block with the FROMWSB block and have multiple FROM blocks that distribute that constant everywhere in the simulation.

Now I also want to kniw if you can use variables from the terminal in the context box. You can certainly use functions because I’ve already done so, but variables… maybe.

I did a couple experiments. You can use a value from the terminal as a constant in xcos without any complex steps, you just execute the code that declares that variable in the terminal and you can use it in xcos.
For example, I write a = 10 in the terminal, and xcos recognizes that name as valid when you write it in a block. When you start the simulation xcos copies it’s value and it turns into a constant, so changing it’s value in the terminal, for example a=5 during the simulation does nothing, it will remain as 10 until the simulation ends.
If you use that same name a in the context box of the simulation, the simulation will use that value instead of the one you have in the terminal.

So yeah, you can have variables in the terminal that you change before each simulation using the sliders and the simulation will use their values everywhere that you wrote them. You could even make the parameters a list that you unpack inside the context manager, for example:
simulation_parameters = tlist(["params", "T_reg", "T_Pon", "K_p", "K_i", "DT", "T_p", "R_r", "R_f", "L_m"], [],[],[],[],[],[],[],[],[]);
Have the gui modify params’s values and inside the xcos context box write:
[T_reg, T_Pon, K_p, K_i, DT, T_p, R_r, R_f, L_m] = simulation_parameters(:)
This way you can have only 1 variable name ocuppied in the terminal.

Edit: sorry for going on a bit of a tangent at the end, it annoys me to occupy too much namespace in a program when it isn’t needed.