Controlling elements from other elements´ scripts

Typically: "How do I... ", "How can I... " questions
Post Reply
altella
Posts: 11
Joined: 04 Jun 2019, 14:01

Controlling elements from other elements´ scripts

Post by altella »

Hello all,

First of all sorry if my questions is too simple, but I can not find a proper response from the scene examples.
Can I control or command a movement of a robot, from the script of a conveyor belt, for example?
The situation is as follows:
1.- In my scene, I have a robot and a conveyor belt. Each element has its own script.
2.- In the coveyor belt, I use a proximity sensor to detect that a part is in position and to stop the coveyor belt.
3.- the the robot performs operations over that part and I want to start again the coveyor belt once the robot has reached a safe position.

How can I command the robot to go to the safe position from the conveyor belt? OR in another way, how can I be notified in the conveyor belt script that the robot has reached the safe position, so that I can start the conveyor belt again?

Thank you very much in advance,

Alberto

fferri
Posts: 1222
Joined: 09 Sep 2013, 19:28

Re: Controlling elements from other elements´ scripts

Post by fferri »

Certainly you can control the conveyor (or anything else) from your robot's child script.

However, this leads to "spaghetti code", where it is difficult for a third person (or you, in the future) to understand why an object behaves the way it does.

It would be better to decouple the objects, enforcing a modular design.

One way to tho this, is to use signals. From the documentation:
Signals can be seen as global variables. Three types of signals are currently supported: integer-type signals, floating-type signals and string-type signals. Signals can be defined, redefined, read and cleared. At simulation end, all signals created by the main script or any child script will be cleared.
For example, you can use in robot's script:

Code: Select all

-- robot writes 1 to signal "startConveyor":
sim.setIntegerSignal("startConveyor",1)
And in the conveyor script (perhaps in the actuation callback):

Code: Select all

-- conveyor reads the data from the signal "startConveyor":
local signalData=sim.getIntegerSignal("startConveyor")
if signalData == 1 then
    -- clear the signal so this code won't be executed again, repeatedly:
    sim.clearIntegerSignal("startConveyor")
    
    -- do the right things to start the conveyor
    -- ...
end

Post Reply