graphs

Typically: "How do I... ", "How can I... " questions
Post Reply
tttt
Posts: 6
Joined: 01 Mar 2024, 03:44

graphs

Post by tttt »

What should I do if I want to remotely transfer the shortest distance recorded in graphs from the robotic arm to the obstacle to matlab instead of saving it to a csv file? It's a set of distances that the robotic arm travels from moving to a stop, not one distance read by sim.simxReadDistance

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

Re: graphs

Post by fferri »

You can call any function defined in a child script from external programs (e.g. remote API clients).

So you can have a Lua function in a child script to make several readings and return a table of values, e.g.:

Code: Select all

sim = require('sim')

function sysCall_init()
    sensorHandle = sim.getObject('...')
    sensorData = {0, 0, 0, 0, 0, 0, 0, 0}
end

function sysCall_sensing()
    -- read your sensors here, e.g.:
    sensorData[1] = ...
    sensorData[2] = ...
    ...
end

function getSensorReadings()
    -- return last readings
    return sensorData
end
then use sim.callScriptFunction to remotely call the getSensorReadings function and get the returned values.

If you want to make it synchronous, to make sure not miss one simulation step, use sim.setStepping and trigger the next simulation step with sim.step.

See also the example here.

tttt
Posts: 6
Joined: 01 Mar 2024, 03:44

Re: graphs

Post by tttt »

hello,fferri
My lua script and matlab script are as follows. Now I want to use sim.simxCallScriptFunction in matlab to execute the sendTableToMatlab function in the lua script to return the allDistances table to the matlab side or turn it into a string signal and return it to the matlab side. It will not work. If You can directly define the values of a and b in the sendTableToMatlab function and then return it to receive it. How can I get allDistances in matlab?Thanks!

lua:

Code: Select all

simRemoteApi.start(19999)

function sysCall_init()
    -- do some initialization here
    proximitySensorHandle = sim.getDistanceHandle('myc')
    allDistances = {}
end

function sysCall_actuation()
    -- put your actuation code here
    recordDistances(proximitySensorHandle)
end

function sysCall_sensing()
    -- put your sensing code here
end

function sysCall_cleanup()
    -- do some clean-up here
    --sendTableToMatlab(allDistances)
    --sim.setScriptAttribute(sim.handle_self, sim_customizationscriptattribute_lastbeforeinstanceswitch, "sendTableToMatlab")
end

-- See the user manual or the available code snippets for additional callback functions and details
function recordDistances(a)
    local result, distance = sim.readDistance(a)
    if result > 0 then
        table.insert(allDistances, distance)
    end
end

function sendTableToMatlab(inInts, inFloats, inStrings, inBuffer)
    local distancesString = table.concat(allDistances, ",")
    local a = 10
    sim.setStringSignal('distancesSignal', distancesString)
    print(distancesString)
    return {a},{},{distancesString},''
end
matlab:

Code: Select all

clear all;
close all;
clc;
sim=remApi('remoteApi'); % 加载库函数并构建对象
sim.simxFinish(-1); % 断开所有之前的连接
clientID=sim.simxStart('127.0.0.1',19997,true,true,5000,5);  %启动链接,指向vrep中的scence,返回链接ID
if (clientID>-1)
    disp('Connected to remote API server');

    sim.simxStartSimulation(clientID,sim.simx_opmode_oneshot);
    [returnCode, retInts, retFloats, retStrings, retBuffer] = sim.simxCallScriptFunction(clientID, 'UR55', sim.sim_scripttype_childscript, 'sendTableToMatlab', [], [], [], '', sim.simx_opmode_blocking);
    if returnCode == sim.simx_return_ok
        disp('Distances returned from V-REP');
    end
        
    [r, distancesSignal] = sim.simxGetStringSignal(clientID, 'distancesSignal', sim.simx_opmode_blocking);
       
else
    disp('Failed connecting to remote API server');
end
sim.simxStopSimulation(clientID,sim.simx_opmode_oneshot);
sim.delete(); % 断开连接

disp('Program ended');

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

Re: graphs

Post by fferri »

It is highly recommended to use the ZeroMQ remote API, since it is much easier and flexible. Additionally, you'll have access to many more API functions, than with the legacy remote API.

The legacy remote API is not supported anymore since a long time.

tttt
Posts: 6
Joined: 01 Mar 2024, 03:44

Re: graphs

Post by tttt »

Thanks!

Post Reply