Using matlab remoteAPI through function problem

Typically: "How do I... ", "How can I... " questions
Post Reply
enbo
Posts: 3
Joined: 17 Dec 2018, 02:59

Using matlab remoteAPI through function problem

Post by enbo » 15 Jan 2019, 15:32

Hi ,
I have a problem . I use matlab remote API, and I know to use

Code: Select all

vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
% 2. close all the potential link
vrep.simxFinish(-1);   
% 3. wait for connecting vrep, detect every 0.2s
while true
    clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);
    if clientID>-1 
        break;
    else
        pause(0.2);
        disp('please run the simulation on vrep...')
    end
end
while (vrep.simxGetConnectionId(clientID) ~= -1)
......
end
this structure is ok for me. but when I use a init and function structure I meet some error.

I use 2 .m scripts vrepconnect.m and dynamic.m

vreconnect.m do some init steps:

Code: Select all

global vrep jointHandle tip dt ap; 
jointNum = 2;
baseName='Base';
torqueLimit=[50,50];
jointName='Joint';  
displayOn=false;
vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
% 2. close all the potential link
vrep.simxFinish(-1);   
% 3. wait for connecting vrep, detect every 0.2s
while true
    clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);
    if clientID>-1 
        break;
    else
        pause(0.2);
        disp('please run the simulation on vrep...')
    end
end
disp('Connection success!')
tstep = 0.005;  % 5ms per simulation pass  
vrep.simxSetFloatingParameter(clientID,vrep.sim_floatparam_simulation_time_step,dt,vrep.simx_opmode_oneshot);
% 5. open the synchronous mode to control the objects in vrep
vrep.simxSynchronous(clientID,true);

%% Simulation Initialization
vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot);

vrep.simxSynchronousTrigger(clientID);         % every calls this function, verp is triggered, 50ms by default
In dynamic.m do some control and get next state:

Code: Select all

function dz = dynamics(control)
    global   clientID  torqueLimit jointHandle  vrep  ;
    .......
    vrep.simxSetJointTargetVelocity(clientID,jointHandle(i),setVel,vrep.simx_opmode_oneshot);
    end
and the error is :

Code: Select all

错误使用 calllib
参数必须为标量。
出错 remApi/simxSetJointTargetVelocity (line 1819)
            [rtn ] = calllib(obj.libName,'mtlb_simxSetJointTargetVelocity',clientID,objectHandle_,targetVelocity_,operationMode_);
出错 dynamics_ta (line 58)
         vrep.simxSetJointTargetVelocity(clientID,jointHandle(i),setVel,vrep.simx_opmode_oneshot);
出错 odearguments (line 90)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.
出错 ode45 (line 115)
    odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
出错 simulate (line 64)
[T y] = ode45(dynamics, [0 dt/2 dt], x0s, OPTIONS, u0{:});
出错 rollout (line 82)
I only want to init vrep once,and pass control parameters through other functions .
It seems that use

Code: Select all

global vrep
is not a useful method.

What should I do, I am struggling for this problem for a long time .

thanks.

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

Re: Using matlab remoteAPI through function problem

Post by coppelia » 16 Jan 2019, 08:42

Have a look how things were done in the B0-based remote API (this is not the same as the legacy remote API, but can give you an idea).

Cheers

Post Reply