Read proximity from threaded script

Typically: "How do I... ", "How can I... " questions
Post Reply
fullmanido
Posts: 4
Joined: 23 Jul 2018, 22:02

Read proximity from threaded script

Post by fullmanido » 31 Jul 2018, 22:19

Hello, I am trying to enable a robot if the sensor is active. I have the proximity in a non-threaded script, now I need to update the signal in the threaded script. The packed table needs to be monitored, should I use a parameter?

--threaded script

Code: Select all

setTestData=function(bool)
conveyorName='ConveyorBelt'
   data=simGetStringSignal(conveyorName..'_CBS1')
    if data then
        CBSData=simUnpackFloatTable(data)
        CBS1Sesnor1=(CBSData[1])
        CBS1Sesnor2=(CBSData[2])
    end
end
thanks!

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

Re: Read proximity from threaded script

Post by coppelia » 03 Aug 2018, 05:51

Hello,

not sure I understand your question. Why not simply printing the unpacked table to the status bar, e.g.:

Code: Select all

if data then
    CBSData=simUnpackFloatTable(data)
    CBS1Sesnor1=(CBSData[1])
    CBS1Sesnor2=(CBSData[2])
    print("Sensor1",CBS1Sesnor1)
    print("Sensor2",CBS1Sesnor2)
end
Cheers

fullmanido
Posts: 4
Joined: 23 Jul 2018, 22:02

Re: Read proximity from threaded script

Post by fullmanido » 03 Aug 2018, 22:01

Hello,

Thank you for your reply. I am trying to send an "OK to run" signal to the UR5 example. The proximity is located at the end if the conveyor, I don't want the robot to initialize without an object present on the conveyor. I really need a global variable that I can transfer from a Non-threaded script to a while loop in a threaded script.

---Conveyor code (not-threaded)
-- would like to send the signal to a threaded while loop

Code: Select all

if (sim_call_type==sim_childscriptcall_actuation) then 
             if simReadProximitySensor(sensor)>0 then
                 beltVelocity=0
                 sensor1=1
                 sensor2=0
             end
     local data=simPackFloatTable({sensor1,sensor2})
     simSetStringSignal(modelName..'_CBS1',data)
end

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

Re: Read proximity from threaded script

Post by coppelia » 06 Aug 2018, 19:20

If you need a global variable, why not use a string signal for instance?

e.g. sim.setStringSignal('signalName',sim.packTable(...))

Cheers

fullmanido
Posts: 4
Joined: 23 Jul 2018, 22:02

Re: Read proximity from threaded script

Post by fullmanido » 07 Aug 2018, 22:05

.....
Last edited by fullmanido on 20 Aug 2018, 12:58, edited 1 time in total.

fullmanido
Posts: 4
Joined: 23 Jul 2018, 22:02

Re: Read proximity from threaded script

Post by fullmanido » 20 Aug 2018, 12:58

Found the decent way to use without too use Globals much time slice, used the UR5 test simulation.
--NON THREADED

Code: Select all

if if simGetSimulationState()~=sim_simulation_advancing_abouttostop then 
        if simReadProximitySensor(sensor)>1 then
             simSetFloatSignal('testvalue2',9)
        end
        if simReadProximitySensor(sensor)<1 then
             simSetFloatSignal('testvalue2',8)
        end
end
--THREADED SCRIPT
--PLACE IN WHILE STATEMENT

Code: Select all

    if simGetSimulationState()~=sim_simulation_advancing_abouttostop then
         t=simGetFloatSignal('testvalue2')
              if (t) then
                  if(t)then
                       if(t<9) then
                          print("the value is less than 9")
                        end
                        if(t>8) then
                          print("the value is greater than 8")
                       end
                   end
             end
    end

Post Reply