Why place where i set string signal affects robot movement?

Typically: "How do I... ", "How can I... " questions
Post Reply
mareurbanek

Why place where i set string signal affects robot movement?

Post by mareurbanek »

Hello, I'm writing code where i want to communicate between several components, so i can control cooperation between them. When I'm setting string signal in IRB4600#1 on 122'th line of script, then IRB4600#2 receives that signal and perform as expected, but when i set string signal any further in my IRB4600#1 (script line > 122) then IRB4600#2 still receives that signal, but doesn't move (target moves, but robot doesn't follow).

I came across this problem before, but i solved it by setting signal earlier in the code and sim.wait() in another robot. What am I doing wrong or don't understand? Maybe there is a better way to communicate between several robots and components?

Here is my scene: http://s000.tinyupload.com/index.php?fi ... 2897558066

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Why place where i set string signal affects robot movement?

Post by coppelia »

Hello,

signals should be used carefully, since you should see then as global variables. If you set that variable several times before another script runs, then only the last set variable will be read (i.e. signals are variables, not buffers). If you want robot1 be able to send data to robot2 without losing intermediate states, then you can use something like (in case you wish to use signals):

Code: Select all

Robot1:
function writeValue(newValue)
    -- sim.setThreadAutomaticSwitch(false) -- if in a threaded child script
    local val=sim.getStringSignal('myCommunicationChannel') -- read existing data
    if not val then
        val={}
    else
        val=sim.unpackTable(val)
    end
    val[#val+1]=newValue -- append new data
    sim.setStringSignal('myCommunicationChannel',sim.packTable(val)) -- write updated data
    -- sim.setThreadAutomaticSwitch(true) -- if in a threaded child script
end

Robot2:
function readValue()
    -- sim.setThreadAutomaticSwitch(false) -- if in a threaded child script
    local retVal=nil
    local val=sim.getStringSignal('myCommunicationChannel') -- read data
    if val then
        val=sim.unpackTable(val)
        if #val>0 then
            retVal=table.remove(val,1) -- remove one data item
            sim.setStringSignal('myCommunicationChannel',sim.packTable(val)) -- write updated data
        end
    end
    -- sim.setThreadAutomaticSwitch(true) -- if in a threaded child script
    return retVal
end
Cheers

mareurbanek

Re: Why place where i set string signal affects robot movement?

Post by mareurbanek »

Thank you for your answer. I found that using sim.waitForSignal and setting integer signals in another robot gives me the best results.

Post Reply