## No Clear Resolution for ZeroMQ client for Matlab

Typically: "How do I... ", "How can I... " questions
Fery64
Posts: 72
Joined: 02 Feb 2022, 15:58

### No Clear Resolution for ZeroMQ client for Matlab

Hi All

I run the code :

Code: Select all

addpath("C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\programming\zmqRemoteApi\clients\matlab")
client = RemoteAPIClient() 
but the result of the last line :

Code: Select all

client = RemoteAPIClient()
is :

Code: Select all

Warning: Objects of org/zeromq/ZMQ\$Socket class exist - not clearing java
> In javaclasspath>local_javapath (line 195)
In javaclasspath (line 124)
Warning: Objects of org/zeromq/ZContext class exist - not clearing java
> In javaclasspath>local_javapath (line 195)
In javaclasspath (line 124)

client =

RemoteAPIClient with no properties.
https://tinypic.host/i/client.yrV30

and when I click the RemoteAPIClient which is highlighted this window opens :

https://tinypic.host/i/win.yr7WY

I downloaded the jeromq.jar , but to run it I noticed that I need to install ROS plugin in Matlab. after installation and adding the path to my code as the above code, still I see that commands like sim.setJointTargetVelocity do not function. meaning that : the Same code ( now modified to ZeroMQ as above) that used to work in remAPI, does not work in ZeroMQ

Example :

in remAPI :

Code: Select all

obj = VrepConnector(port, step_time_vrep)

obj.sim = remApi('remoteApi');
obj.sim.simxSetJointTargetVelocity(obj.clientID, obj.robot_joints(i), u(i), obj.sim.simx_opmode_oneshot);

In ZeroMQ

Code: Select all

obj = VrepConnectorZMQ()

client = RemoteAPIClient();
obj.sim.setJointTargetVelocity(obj.robot_joints(i), u(i));
I hope it is clear that the above code is not complete and it is just an example to show the syntaxes are updated , and it not a minimal replicable code. The point is the warning and error and that the jointvelocity command does not do anything on the robot joint, while the command startsimulation opens the Graph windows up and there seems that a connection is established through ZeroMQ, yet no effect on certain commands like above

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

### Re: No Clear Resolution for ZeroMQ client for Matlab

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to as creating a minimal, complete and verifiable example (MCVE). Regardless of how it's communicated to you, it boils down to ensuring your code that reproduces the problem follows the following guidelines:

…Minimal – Use as little code as possible that still produces the same problem
…Complete – Provide all parts someone else needs to reproduce your problem in the question itself
…Reproducible – Test the code you're about to provide to make sure it reproduces the problem.

The rest of this post provides guidance on these aspects of writing a minimal, reproducible example.

Minimal

The more code there is to go through, the less likely people can find your problem. Streamline your example in one of two ways:
• Restart from scratch. Create a new program, adding in only what is needed to see the problem. Use simple, descriptive names for functions and variables – don’t copy the names you’re using in your existing code.
• Divide and conquer. If you’re not sure what the source of the problem is, start removing code a bit at a time until the problem disappears – then add the last part back.

Don't sacrifice clarity for brevity when creating a minimal example. Use consistent naming and indentation, and include code comments if needed. Use your code editor’s shortcut for formatting code.

Complete

Make sure all information necessary to reproduce the problem is included in the question itself.

Use individual code blocks for each file or snippet you include. Provide a description for the purpose of each block.
DO NOT use images of code. Copy the actual text from your code editor, paste it into the question, then format it as code. This helps others more easily read and test your code.

Reproducible

Describe the problem. "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question.
Eliminate any issues that aren't relevant to the problem. If your question isn’t about a compiler error, ensure that there are no compile-time errors. Double-check that your example reproduces the problem! If you inadvertently fixed the problem while composing the example but didn't test it again, you'd want to know that before asking someone else to help.
It might help to shut the system down and restart it, or transport the example to a fresh environment to confirm it really does provide an example of the problem.

Fery64
Posts: 72
Joined: 02 Feb 2022, 15:58

### Re: No Clear Resolution for ZeroMQ client for Matlab

up to the first part :

Code: Select all

client = RemoteAPIClient()
You really do not need any more code to test it. At least you could have tried and it would have been much more helpful than the long answer...

For the second part : I created a Class in an m-file "VrepConnectorZMQ.m" as follows for ZMQ :

Code: Select all

classdef VrepConnectorZMQ

properties
sim;				%Similar to fd
client;			%Used for server connection and server requests
robot_joints = []	%List of joint handles
%Integration step used for simulation
joint_pos=[];
end

methods

function obj = VrepConnectorZMQ()
client = RemoteAPIClient();
client.setStepping(true);
obj.sim = client.getObject('sim');			%RemoteAPI object
obj.sim.startSimulation();

for i = 1:7
obj.robot_joints(i) = obj.sim.getObject(strcat('/LBR_iiwa_14_R820_joint',int2str(i)));
end

for i = 1:7
obj.joint_pos(i) = obj.sim.getJointTargetPosition(obj.robot_joints(i));
end
% When simulation is not running, ZMQ message handling could be a bit
% slow, since the idle loop runs at 8 Hz by default. So let's make
% sure that the idle loop runs at full speed for this program:
%             defaultIdleFps = sim.getInt32Param(sim.intparam_idle_fps);
%             sim.setInt32Param(sim.intparam_idle_fps, 0);
end

function ApplyControl(obj, u)
for i = 1:7
obj.sim.setJointTargetVelocity(obj.robot_joints(i), u(i));
end

end


Then from the m file use ApplyControl and you will see the robot will not move. For the robot, take any 7 DOF robot you want. like Kuka.

My own guess is that the line

Code: Select all

obj.sim.startSimulation();
is inside the function obj, but I tried to insert it in other functions or the main file but there was no success.

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

### Re: No Clear Resolution for ZeroMQ client for Matlab

I don't see anywhere in your code a call to client.step(), of course nothing will move.

Either call client.step() as many times as needed to advance the simulation several steps, or do not use client.setStepping(true) in the first place.

Fery64
Posts: 72
Joined: 02 Feb 2022, 15:58

### Re: No Clear Resolution for ZeroMQ client for Matlab

I tried to include the

Code: Select all

 client.step()
in the obj function, and again calling the applymotion function. The simulation stalls, no motion happens.
I need to run a synchronous simulation, so I need to use the same structure used in the Example file

Code: Select all

synchronousImageTransmission.m

So why doesn't it work in my case?

Code: Select all

function obj = VrepConnectorZMQ()
client = RemoteAPIClient();
client.setStepping(true);
obj.sim = client.getObject('sim');			%RemoteAPI object
obj.sim.startSimulation()
client.step();

for i = 1:7
obj.robot_joints(i) = obj.sim.getObject(strcat('/LBR_iiwa_14_R820_joint',int2str(i)));
end

for i = 1:7
obj.joint_pos(i) = obj.sim.getJointTargetPosition(obj.robot_joints(i));
end

% When simulation is not running, ZMQ message handling could be a bit
% slow, since the idle loop runs at 8 Hz by default. So let's make
% sure that the idle loop runs at full speed for this program:
%             defaultIdleFps = sim.getInt32Param(sim.intparam_idle_fps);
%             sim.setInt32Param(sim.intparam_idle_fps, 0);
end
And the function ApplyControl the same

Code: Select all

function ApplyControl(obj, u)
for i = 1:7
obj.sim.setJointTargetVelocity(obj.robot_joints(i), u(i));
end

end
If I take the client.step(); inside the ApplyControl function, then the error will be

Code: Select all

function ApplyControl(obj, u)
obj.client.step();
for i = 1:7
obj.sim.setJointTargetVelocity(obj.robot_joints(i), u(i));
end

end
[/code]

The error in Matlab:

Code: Select all

Dot indexing is not supported for variables of this type.
So while I need to run a synchronous simulation, this has not worked out yet

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

### Re: No Clear Resolution for ZeroMQ client for Matlab

client.step() advances the simulation just by one step (50ms by default).

You need to call it continuously.

Refer to the examples in zmqRemoteApi/clients/matlab.

Fery64
Posts: 72
Joined: 02 Feb 2022, 15:58

### Re: No Clear Resolution for ZeroMQ client for Matlab

Since I have written in OOP form, the example is not a direct help : what you are suggesting is not doable in the OOP format. I have written these functions under a class. and I have to call the ApplyControl function from the main.mfile. The problem with this is that I can not call client property inside ApplyControl function while I have defined it inside obj function. As long as I use the following code, Matlab stalls and the robot does not move! The steptime =0.05
It does not matter where I use the client.step() but inside the ApplyControl function it has to be

Code: Select all

obj.client.step()
and it bugs me with the error

Code: Select all

Dot indexing is not supported for variables of this type.

Code: Select all

function obj = VrepConnectorZMQ()

client = RemoteAPIClient();
client.setStepping(true);
obj.sim = client.getObject('sim');			%RemoteAPI object
obj.sim.startSimulation()

for i = 1:7
obj.robot_joints(i) = obj.sim.getObject(strcat('/LBR_iiwa_14_R820_joint',int2str(i)));
end

for i = 1:7
obj.joint_pos(i) = obj.sim.getJointTargetPosition(obj.robot_joints(i));
end

% When simulation is not running, ZMQ message handling could be a bit
% slow, since the idle loop runs at 8 Hz by default. So let's make
% sure that the idle loop runs at full speed for this program:
%             defaultIdleFps = sim.getInt32Param(sim.intparam_idle_fps);
%             sim.setInt32Param(sim.intparam_idle_fps, 0);
end

function ApplyControl(obj, u,steptime)

startTime = obj.sim.getSimulationTime();
t = startTime;
while t<steptime
obj.client.step();
for i = 1:7
obj.sim.setJointTargetVelocity(obj.robot_joints(i), u(i));

end
t = obj.sim.getSimulationTime();

end

end

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

### Re: No Clear Resolution for ZeroMQ client for Matlab

Fery64 wrote: 24 Nov 2022, 22:17

Code: Select all

@@
Dot indexing is not supported for variables of this type.
The message is pretty clear.

Just keep client and sim as global variables.

Fery64
Posts: 72
Joined: 02 Feb 2022, 15:58

### Re: No Clear Resolution for ZeroMQ client for Matlab

If it's clear to you, may you please tell me how should I make the sim and client global?

I used

properties (Access = public) but this comment did not help and same error came up.

I doubt if properties (Access = public, Constant) would work since I do not know if sim is a constant

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

### Re: No Clear Resolution for ZeroMQ client for Matlab

Maybe try to use the global keyword, or stick with what the provided examples do, if you don't bother learning the language.