How to control User Parameter in another Script?

Typically: "How do I... ", "How can I... " questions
Post Reply
Shaoxiang Wang
Posts: 18
Joined: 23 May 2020, 15:39

How to control User Parameter in another Script?

Post by Shaoxiang Wang »

Hello,

i want to control the User Parameter of "suctionPad" in the script of "Target Dummy". So at the end of the Path, the User Parameter is changed to "false". Then the cylinder can fall.

But it's not successful. In "Target" script i used function
suctionPad= sim.getObjectHandle("suctionPad")
suctionPadScript = sim.getScriptAssociatedWithObject(suctionPad)
sim.setUserParameter(suctionPadScript,"active","false")

But error comes. It says "Object does not exist. (sim.setUserParameter)"

best regards
Shaoxiang

Image
Image

Code: Select all

    -- do some initialization here
    
-- Get handles and postions of dummies
    tip = sim.getObjectHandle("tip")
    ownHandle = sim.getObjectHandle("Target")
    
    suctionPad= sim.getObjectHandle("suctionPad")
    suctionPadScript = sim.getScriptAssociatedWithObject(suctionPad)
    
    startPath1=sim.getObjectHandle("Path1_1")
    startPath2=sim.getObjectHandle("Path1_2")
    mittelPath1=sim.getObjectHandle("Path2_1")
    mittelPath2=sim.getObjectHandle("Path2_2")
    endPath1=sim.getObjectHandle("Path3_1")
    endPath2=sim.getObjectHandle("Path3_2")
    pathLength1=sim.getPathLength(startPath1)
    pathLength2=sim.getPathLength(startPath2)
    pathLength3=sim.getPathLength(mittelPath1)
    pathLength4=sim.getPathLength(mittelPath2)
    pathLength5=sim.getPathLength(endPath1)
    pathLength6=sim.getPathLength(endPath2)
    
    
    speed= sim.getUserParameter(sim.handle_self,'speed')-- end-effector velocity in m/s
    sim.setUserParameter(ownHandle,'@enable','')
    timestep=sim.getSimulationTimeStep() -- in s
    
    currentPositionRelative1=0
    currentPositionRelative2=0
    currentPositionRelative3=0
    currentPositionRelative4=0
    currentPositionRelative5=0
    currentPositionRelative6=0
    moveForward=true
    epsilon1=0.01
    epsilon2=0.01
    epsilon3=0.01
    epsilon4=0.01
    epsilon5=0.01
    epsilon6=0.01

function sysCall_threadmain()
    -- Put some initialization code here

  while sim.getSimulationState()~=sim.simulation_advancing_abouttostop do
    slowerWork=sim.getStringSignal("slowerWork")
    if slowerWork=="1" then
        slowerWork=true
    else
        slowerWork=false
    end
    
    -- path1
    if not slowerWork then
        pathAdvanceAbsolute=speed*timestep -- in m
        pathAdvanceRelative=pathAdvanceAbsolute/pathLength1
        if currentPositionRelative1 < 1-epsilon1 and moveForward then
            currentPositionRelative1=currentPositionRelative1+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(startPath1,currentPositionRelative1)
            pathOrientation=sim.getOrientationOnPath(startPath1,currentPositionRelative1)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
        end
    else
        pathAdvanceAbsolute=0.2*speed*timestep -- in m
        pathAdvanceRelative=pathAdvanceAbsolute/pathLength1
        if currentPositionRelative1 < 1-epsilon1 and moveForward then
            currentPositionRelative1=currentPositionRelative1+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(startPath1,currentPositionRelative1)
            pathOrientation=sim.getOrientationOnPath(startPath1,currentPositionRelative1)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
        end
    end
    
    -- path2
    if currentPositionRelative1 >= 1-epsilon1 then
       if not slowerWork then
        pathAdvanceAbsolute=speed*timestep -- in m
        pathAdvanceRelative=pathAdvanceAbsolute/pathLength2
        if currentPositionRelative2 < 1-epsilon2 and moveForward then
            currentPositionRelative2=currentPositionRelative2+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(startPath2,currentPositionRelative2)
            pathOrientation=sim.getOrientationOnPath(startPath2,currentPositionRelative2)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
        end
       else
          pathAdvanceAbsolute=0.2*speed*timestep -- in m
          pathAdvanceRelative=pathAdvanceAbsolute/pathLength2
         if currentPositionRelative2 < 1-epsilon2 and moveForward then
            currentPositionRelative2=currentPositionRelative2+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(startPath2,currentPositionRelative2)
            pathOrientation=sim.getOrientationOnPath(startPath2,currentPositionRelative2)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
         end
       end
      
    end
    
    -- path3
    
    if currentPositionRelative2 >= 1-epsilon2 then
    sim.setUserParameter(suctionPadScript,"active","false")
       if not slowerWork then
        pathAdvanceAbsolute=speed*timestep -- in m
        pathAdvanceRelative=pathAdvanceAbsolute/pathLength3
        if currentPositionRelative3 < 1-epsilon3 and moveForward then
            currentPositionRelative3=currentPositionRelative3+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(mittelPath1,currentPositionRelative3)
            pathOrientation=sim.getOrientationOnPath(mittelPath1,currentPositionRelative3)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
        end
       else
          pathAdvanceAbsolute=0.2*speed*timestep -- in m
          pathAdvanceRelative=pathAdvanceAbsolute/pathLength3
         if currentPositionRelative3 < 1-epsilon3 and moveForward then
            currentPositionRelative3=currentPositionRelative3+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(mittelPath1,currentPositionRelative3)
            pathOrientation=sim.getOrientationOnPath(mittelPath1,currentPositionRelative3)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
         end
       end
      
    end
    
    -- path4
    
    if currentPositionRelative3 >= 1-epsilon3 then
       if not slowerWork then
        pathAdvanceAbsolute=speed*timestep -- in m
        pathAdvanceRelative=pathAdvanceAbsolute/pathLength4
        if currentPositionRelative4 < 1-epsilon4 and moveForward then
            currentPositionRelative4=currentPositionRelative3+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(endPath1,currentPositionRelative4)
            pathOrientation=sim.getOrientationOnPath(endPath1,currentPositionRelative4)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
        end
       else
          pathAdvanceAbsolute=0.2*speed*timestep -- in m
          pathAdvanceRelative=pathAdvanceAbsolute/pathLength4
         if currentPositionRelative4 < 1-epsilon4 and moveForward then
            currentPositionRelative4=currentPositionRelative4+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(endPath1,currentPositionRelative4)
            pathOrientation=sim.getOrientationOnPath(endPath1,currentPositionRelative4)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
         end
       end
      
    end
    
    -- path5
    
    if currentPositionRelative4 >= 1-epsilon4 then
        
       if not slowerWork then
        pathAdvanceAbsolute=speed*timestep -- in m
        pathAdvanceRelative=pathAdvanceAbsolute/pathLength5
        if currentPositionRelative5 < 1-epsilon5 and moveForward then
            currentPositionRelative5=currentPositionRelative5+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(endPath2,currentPositionRelative5)
            pathOrientation=sim.getOrientationOnPath(endPath2,currentPositionRelative5)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
        end
       else
          pathAdvanceAbsolute=0.2*speed*timestep -- in m
          pathAdvanceRelative=pathAdvanceAbsolute/pathLength5
         if currentPositionRelative5 < 1-epsilon5 and moveForward then
            currentPositionRelative5=currentPositionRelative5+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(endPath2,currentPositionRelative5)
            pathOrientation=sim.getOrientationOnPath(endPath2,currentPositionRelative5)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
         end
       end
      
    end
    
    -- path6
    
    if currentPositionRelative5 >= 1-epsilon5 then
       if not slowerWork then
        pathAdvanceAbsolute=speed*timestep -- in m
        pathAdvanceRelative=pathAdvanceAbsolute/pathLength6
        if currentPositionRelative6 < 1-epsilon6 and moveForward then
            currentPositionRelative6=currentPositionRelative6+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(mittelPath2,currentPositionRelative6)
            pathOrientation=sim.getOrientationOnPath(mittelPath2,currentPositionRelative6)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
        end
       else
          pathAdvanceAbsolute=0.2*speed*timestep -- in m
          pathAdvanceRelative=pathAdvanceAbsolute/pathLength6
         if currentPositionRelative6 < 1-epsilon6 and moveForward then
            currentPositionRelative6=currentPositionRelative6+pathAdvanceRelative
            pathPosition=sim.getPositionOnPath(mittelPath2,currentPositionRelative6)
            pathOrientation=sim.getOrientationOnPath(mittelPath2,currentPositionRelative6)
            objectPositionIsSet = sim.setObjectPosition(ownHandle,-1,pathPosition)
            objectOrientationIsSet = sim.setObjectOrientation(ownHandle,-1,pathOrientation)
         end
       end
      
    end
    
    if currentPositionRelative6 >= 1-epsilon6 then
      currentPositionRelative1=0
      currentPositionRelative2=0
      currentPositionRelative3=0
      currentPositionRelative4=0
      currentPositionRelative5=0
      currentPositionRelative6=0
    end
    
  end
end

function sysCall_cleanup()
    -- Put some clean-up code here
end

-- See the user manual or the available code snippets for additional callback functions and details

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

Re: How to control User Parameter in another Script?

Post by fferri »

Shaoxiang Wang wrote: 04 Jun 2020, 21:11 suctionPad= sim.getObjectHandle("suctionPad")
suctionPadScript = sim.getScriptAssociatedWithObject(suctionPad)
sim.setUserParameter(suctionPadScript,"active","false")
sim.setUserParameter wants an object handle. Try with sim.setUserParameter(suctionPad,"active","false")

Shaoxiang Wang
Posts: 18
Joined: 23 May 2020, 15:39

Re: How to control User Parameter in another Script?

Post by Shaoxiang Wang »

Thanks for your advice! Now it can work well.

Post Reply