Threaded script fuctions with Remote API

Typically: "How do I... ", "How can I... " questions
Post Reply
Fabi255
Posts: 1
Joined: 29 Jun 2018, 10:13

Threaded script fuctions with Remote API

Post by Fabi255 » 02 Aug 2018, 10:40

Hi,
i want to control an UR10 Robot and Bill via Matlab.
I already have a conection and the "display_textfunction" runs, but only in the child script of the robot not via Bill.

Error: [string -unknown location]:?: Not enough return arguments. (enableIk@UR10)
Error: [string -unknown location]:?: Function didn't produce expected return values, i.e. an int table, a float table, a string
.
.
.

I think the failure is on Matlab side:

Code: Select all

function test()
    clc
	disp('Program started');
	vrep=remApi('remoteApi');
	vrep.simxFinish(-1);
	clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);
   
    
	if (clientID>-1)
		disp('Connected to remote API server');
		pause(0.1);
		% Send a command to display a specific message in a dialog box:
		[res retInts retFloats retStrings retBuffer]=vrep.simxCallScriptFunction(clientID,'UR10',vrep.sim_scripttype_childscript,'displayText_function',[],[],'TEST UR',[],vrep.simx_opmode_blocking);
		if (res==vrep.simx_return_ok)
			fprintf('Returned message: %s\n',retStrings);
		else
			fprintf('Remote function call UR failed\n');
        end
        pause(1);
        

		[res retInts retFloats retStrings retBuffer]=vrep.simxCallScriptFunction(clientID,'UR10',vrep.sim_scripttype_childscript,'enableIk',[],[],[],[],vrep.simx_opmode_blocking);
		[res retInts retFloats retStrings retBuffer]=vrep.simxCallScriptFunction(clientID,'UR10',vrep.sim_scripttype_childscript,'MoveTo_PosWait',[],[],[],[],vrep.simx_opmode_blocking);
        
        pause(10);
        
        vrep.simxFinish(clientID);
	else
		disp('Failed connecting to remote API server');
    end
    
        [res retInts retFloats retStrings retBuffer]=vrep.simxCallScriptFunction(clientID,'Bill',vrep.sim_scripttype_childscript,'displayText_function',[],[],'TEST Bill',[],vrep.simx_opmode_blocking);
		if (res==vrep.simx_return_ok)
			fprintf('Returned message: %s\n',retStrings);
		else
			fprintf('Remote function call Bill failed\n');
        end
        pause(1);
             
	vrep.delete();
	disp('Program ended');
end


For better understanding the VREP side UR:

Code: Select all

enableIk=function(enable)

    --Ausrichtung übernehmen
    quat=sim.getObjectQuaternion(UR10,-1)

    if enable then
        sim.setObjectMatrix(Target,-1,sim.getObjectMatrix(UR10,-1))
        for i=1,#jointHandles,1 do
            sim.setJointMode(jointHandles[i],sim.jointmode_ik,1)
        end

        sim.setExplicitHandling(ikGroupHandle,0)
    else
        sim.setExplicitHandling(ikGroupHandle,1)
        for i=1,#jointHandles,1 do
            sim.setJointMode(jointHandles[i],sim.jointmode_force,0)
        end
    end
end

displayText_function=function(inInts,inFloats,inStrings,inBuffer)
    -- Simply display a dialog box that prints the text stored in inStrings[1]:
    if #inStrings>=1 then
        sim.displayDialog('Message from the remote API client',inStrings[1],sim.dlgstyle_ok,false)
        return {},{},{'message was displayed'},'' -- return a string
    end
end

setGripperData=function(open,velocity,force)
    if not velocity then
        velocity=0.11
    end
    if not force then
        force=20
    end
    if not open then
        velocity=-velocity
    end
    local data=sim.packFloatTable({velocity,force})
    sim.setStringSignal(modelName..'_rg2GripperData',data)
end

MoveTo_initialConfig=function()
    
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,posTransfer,targetVel)
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,posTransfer2,targetVel)
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,posTransfer1,targetVel)
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,posTransfer0,targetVel) 
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,initialConfig,targetVel) 
end

MoveTo_PosTransfer=function()

    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,initialConfig,targetVel)    
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,posTransfer0,targetVel)    
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,posTransfer1,targetVel)    
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,posTransfer2,targetVel)
    sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,maxVel,maxAccel,maxJerk,posTransfer,targetVel)
end

MoveTo_PosWait=function()
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,pos_wait,quat,nil)
end

WaitForBill=function(seconds)
    sim.wait(seconds)
end

UnterteilGreifen=function(pos_Unterteil_greifen)
    --Greifer auf
    setGripperData(true)
    sim.wait(0.5)
    --Unterteilanfahren hoch
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Unterteil_greifen[1],pos_Unterteil_greifen[2],pos_Unterteil_greifen[3]+0.2},quat,nil)
    --Unterteil anfahren
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,pos_Unterteil_greifen,quat,nil)
    --Greifer zu
    sim.wait(0.5)
    setGripperData(false) 
    sim.wait(1)
    --Unterteil hoch
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Unterteil_greifen[1],pos_Unterteil_greifen[2],pos_Unterteil_greifen[3]+0.2},quat,nil)

end

UnterteilAblegen=function(pos_Unterteil_ablegen)
    --Unterteil Ablegen oben
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Unterteil_ablegen[1],pos_Unterteil_ablegen[2],pos_Unterteil_ablegen[3]+0.2},quat,nil)
    --Unterteil Ablegen
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,pos_Unterteil_ablegen,quat,nil)
    --Greifer auf
    sim.wait(0.5)
    setGripperData(true)
    sim.wait(1.5)
    --Unterteil Ablegen oben
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Unterteil_ablegen[1],pos_Unterteil_ablegen[2],pos_Unterteil_ablegen[3]+0.2},quat,nil)
end

ZahnradGreifen=function(pos_Zahnrad_greifen)
    --Greifer auf
    setGripperData(true)
    sim.wait(0.5)
    --Zahnrad anfahren oben
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Zahnrad_greifen[1],pos_Zahnrad_greifen[2],pos_Zahnrad_greifen[3]+0.2},quat,nil)
    --Zahnrad anfahren
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,pos_Zahnrad_greifen,quat,nil)
    --Greifer zu
    sim.wait(0.5)
    setGripperData(false) 
    sim.wait(1)
    --Zahnrad hoch
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Zahnrad_greifen[1],pos_Zahnrad_greifen[2],pos_Zahnrad_greifen[3]+0.2},quat,nil)

end

ZahnradAblegen=function(pos_Zahnrad_ablegen)
    --Zahnrad Ablegen oben
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Zahnrad_ablegen[1],pos_Zahnrad_ablegen[2],pos_Zahnrad_ablegen[3]+0.2},quat,nil)
    --Zahnrad Ablegen
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,pos_Zahnrad_ablegen,quat,nil)
    --Greifer auf
    sim.wait(0.5)
    setGripperData(true)
    sim.wait(1.5)
    --Zahnrad Ablegen hoch
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Zahnrad_ablegen[1],pos_Zahnrad_ablegen[2],pos_Zahnrad_ablegen[3]+0.2},quat,nil)
end

OberteilGreifen=function(pos_Oberteil_greifen)
    --Greifer auf
    setGripperData(true)
    sim.wait(0.5)
    --Unterteilanfahren hoch
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Oberteil_greifen[1],pos_Oberteil_greifen[2],pos_Oberteil_greifen[3]+0.2},quat,nil)
    --Unterteil anfahren
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,pos_Oberteil_greifen,quat,nil)
    --Greifer zu
    sim.wait(0.5)
    setGripperData(false) 
    sim.wait(1)
    --Unterteil hoch
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Oberteil_greifen[1],pos_Oberteil_greifen[2],pos_Oberteil_greifen[3]+0.2},quat,nil)

end

OberteilAblegen=function(pos_Oberteil_ablegen)
    --Unterteil Ablegen oben
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Oberteil_ablegen[1],pos_Oberteil_ablegen[2],pos_Oberteil_ablegen[3]+0.2},quat,nil)
    --Unterteil Ablegen
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,pos_Unterteil_ablegen,quat,nil)
    --Greifer auf
    sim.wait(0.5)
    setGripperData(true)
    sim.wait(1.5)
    --Unterteil Ablegen oben
    sim.rmlMoveToPosition(Target,-1,-1,nil,nil,ikMaxVel,ikMaxAccel,ikMaxJerk,{pos_Oberteil_ablegen[1],pos_Oberteil_ablegen[2],pos_Oberteil_ablegen[3]+0.2},quat,nil)
end


function sysCall_threadmain()
    -- Initialize some values:
    jointHandles={-1,-1,-1,-1,-1,-1}
    for i=1,6,1 do
        jointHandles[i]=sim.getObjectHandle('UR10_joint'..i)
    end
   
    modelBase=sim.getObjectAssociatedWithScript(sim.handle_self)
    modelName=sim.getObjectName(modelBase)

    simRemoteApi.start(19999)

    -- Set-up some of the RML vectors:
    vel=240
    accel=40
    jerk=80
    currentVel={0,0,0,0,0,0,0}
    currentAccel={0,0,0,0,0,0,0}
    maxVel={vel*math.pi/180,vel*math.pi/180,vel*math.pi/180,vel*math.pi/180,vel*math.pi/180,vel*math.pi/180}
    maxAccel={accel*math.pi/180,accel*math.pi/180,accel*math.pi/180,accel*math.pi/180,accel*math.pi/180,accel*math.pi/180}
    maxJerk={jerk*math.pi/180,jerk*math.pi/180,jerk*math.pi/180,jerk*math.pi/180,jerk*math.pi/180,jerk*math.pi/180}
    targetVel={0,0,0,0,0,0}

    ikMaxVel={0.4,0.4,0.4,1.8}
    ikMaxAccel={0.8,0.8,0.8,0.9}
    ikMaxJerk={0.6,0.6,0.6,0.8}


    ikGroupHandle=sim.getIkGroupHandle('UR10')
    Target=sim.getObjectHandle('UR10_target')
    UR10=sim.getObjectHandle('UR10_tip')
    


    --Koordinaten
    initialConfig={0,0,0,0,0,0}
    posTransfer0={135*math.pi/180,0*math.pi/180,0*math.pi/180,0*math.pi/180,0*math.pi/180,0*math.pi/180}
    posTransfer1={135*math.pi/180,30*math.pi/180,60*math.pi/180,0*math.pi/180,0*math.pi/180,0*math.pi/180}
    posTransfer2={90*math.pi/180,30*math.pi/180,60*math.pi/180,0*math.pi/180,0*math.pi/180,0*math.pi/180}
    posTransfer={90*math.pi/180,30*math.pi/180,60*math.pi/180,0*math.pi/180,-90*math.pi/180,0*math.pi/180}


    pos_wait={0.475,0.6748,1.7}

    pos_Unterteil_greifen={0.5,0.72,1.32}
    pos_Unterteil_ablegen={0.275,0.6,1.32}

    pos_Zahnrad_greifen={0.45,0.8,1.32}
    pos_Zahnrad_ablegen={0.275,0.65,1.32}
    
    pos_Oberteil_greifen={0.5,0.72,1.32}
    pos_Oberteil_ablegen={0.275,0.6,1.35}

    



    --Transferpostion
--    MoveTo_PosTransfer(true)

    --Inverse Kinematik On
--    enableIk(true) 

    --Warteposition
--    MoveTo_PosWait(true)

    --Unterteil greifen
--    UnterteilGreifen(pos_Unterteil_greifen)

    --Unterteil ablegen
--    UnterteilAblegen(pos_Unterteil_ablegen)
 
    --Warteposition (Zwischenstop)
--    MoveTo_PosWait(pos_wait)

    --Zahnrad greifen
--    ZahnradGreifen(pos_Zahnrad_greifen)
    
    --Zahnrad ablegen
--    ZahnradAblegen(pos_Zahnrad_ablegen)
    
   --Warteposition
--    MoveTo_PosWait(pos_wait)

    --Auf Bill warten
   WaitForBill(130)

    --Gehäuse Oberteil greifen
--    OberteilGreifen(pos_Oberteil_greifen)
    
    --Gehäuse Oberteil ablegen
--    OberteilAblegen(pos_Oberteil_ablegen)
    
    --Warteposition (Zwischenstop)
--    MoveTo_PosWait(true)

    --Inverse Kinematik Off
--    enableIk(false) 

    --Ausfahren JointPositions
--    MoveTo_initialConfig(true)


    sim.stopSimulation()

end
Bill:

Code: Select all

------------------------------------------------------------------------------ 
-- Following few lines automatically added by V-REP to guarantee compatibility 
-- with V-REP 3.1.3 and earlier: 
colorCorrectionFunction=function(_aShapeHandle_) 
  local version=sim.getInt32Parameter(sim.intparam_program_version) 
  local revision=sim.getInt32Parameter(sim.intparam_program_revision) 
  if (version<30104)and(revision<3) then 
      return _aShapeHandle_ 
  end 
  return '@backCompatibility1:'.._aShapeHandle_ 
end 
------------------------------------------------------------------------------ 
 
 
-- This is just a very basic example on how to make Bill move. 
-- In this case the two hands are linked, but they can also be independent
-- You can also directly manipulate the posture and other parameters of Bill by adjusting the
-- orientation of the 'Bill_posture_target' object for instance

displayText_function=function(inInts,inFloats,inStrings,inBuffer)
    -- Simply display a dialog box that prints the text stored in inStrings[1]:
    if #inStrings>=1 then
        sim.displayDialog('Message from the remote API client',inStrings[1],sim.dlgstyle_ok,false)
        return {},{},{'message was displayed'},'' -- return a string
    end
end

WaitForUR=function(seconds)
    sim.moveToPosition(hands,modelBase,posWarten,nil,v,a)
    sim.wait(seconds)
end

Bauteil_LinksHinten=function()
    --Teil 1 Greifen
    sim.moveToPosition(hands,modelBase,posLinksHinten,nil,v,a)
    sim.wait(0.5)
    --Fügen
    sim.moveToPosition(hands,modelBase,posFuegen,nil,v,a)
    sim.wait(1)
end

Bauteil_LinksVorne=function()
    --Teil 2 Greifen
    sim.moveToPosition(hands,modelBase,posLinksVorne,nil,v,a)
    sim.wait(0.5)
    --Fügen
    sim.moveToPosition(hands,modelBase,posFuegen,nil,v,a)
    sim.wait(1)
end

Bauteil_MitteVorne=function()
    --Teil 3 Greifen
    sim.moveToPosition(hands,modelBase,posMitteVorne,nil,v,a)
    sim.wait(0.5)
    --Fügen
    sim.moveToPosition(hands,modelBase,posFuegen,nil,v,a)
    sim.wait(1)
end

Bauteil_RechtsVorne=function()
    --Teil 4 Greifen
    sim.moveToPosition(hands,modelBase,posRechtsVorne,nil,v,a)
    sim.wait(0.5)
    --Fügen
    sim.moveToPosition(hands,modelBase,posFuegen,nil,v,a)
    sim.wait(1)
end

Bauteil_RechtsMitte=function()
    --Teil 5 Greifen
    sim.moveToPosition(hands,modelBase,posRechtsMitte,nil,v,a)
    sim.wait(0.5)
    --Fügen
    sim.moveToPosition(hands,modelBase,posFuegen,nil,v,a)
    sim.wait(1)
end



function sysCall_threadmain()
    -- Put some initialization code here:
    hands=sim.getObjectHandle('Bill_hands_demo')
    modelBase=sim.getObjectAssociatedWithScript(sim.handle_self)
    randomColors=sim.getScriptSimulationParameter(sim.handle_self,'randomColors')
    v=0.4 -- movement velocity
    a=0.1 -- movement acceleration
    J={0.6,0.6,0.6,0.8}

    HairColors={4,{0.30,0.22,0.14},{0.75,0.75,0.75},{0.075,0.075,0.075},{0.75,0.68,0.23}}
    skinColors={2,{0.61,0.54,0.45},{0.52,0.45,0.35}}
    shirtColors={5,{0.27,0.36,0.54},{0.54,0.27,0.27},{0.31,0.51,0.33},{0.46,0.46,0.46},{0.18,0.18,0.18}}
    trouserColors={2,{0.4,0.34,0.2},{0.12,0.12,0.12}}
    shoeColors={2,{0.12,0.12,0.12},{0.25,0.12,0.045}}

    rightHand=sim.getObjectHandle('Bill_rightHand_target')
    leftHand=sim.getObjectHandle('Bill_leftHand_target')

    Path1=sim.getObjectHandle('Path1')
    Target=sim.getObjectHandle('Bill_posture_target')

    -- Here we execute the regular thread code:

-- Hands:
-- Erste Zahl "wie weit nach vorne"
-- Zweite Zahl "links rechts" (rechts negativ)
-- Dritte Zahl "höhe"

    posWarten={0.05,-0.1,0.75}
    posFuegen={0.2,-0.05,0.65}
    posLinksHinten={0.4,-0.02,0.65}
    posLinksVorne={0.1,-0.05,0.65}
    posMitteVorne={0.1,-0.2,0.65}
    posRechtsVorne={0.1,-0.35,0.65}
    posRechtsMitte={0.2,-0.35,0.65}

    sim.setObjectPosition(rightHand,-1,{0.275,0.4,1.22})
    sim.setObjectPosition(leftHand,-1,{0.175,0.4,1.22})

     
  --  sim.followPath(Target,Path1,3,1,v,a)
  
    WaitForUR(100)

 --   Bauteil_LinksHinten(true)

 --   Bauteil_MitteVorne(true)

 --   Bauteil_RechtsMitte(true)   

 --   WaitForUR(10)

end

I hope you could help me to fix my problem? Thank you very much.

Regards,
Fabian

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

Re: Threaded script fuctions with Remote API

Post by coppelia » 03 Aug 2018, 06:55

Hello Fabian,

from the documentation here, your called functions always need to return 3 tables and a string.

so at least, you should add following line at the end of function enableIk:

Code: Select all

return {},{},{},''
Cheers

Post Reply