Page 2 of 2
Re: Java ZMQ Remote API
Posted: 08 Apr 2024, 10:03
by lumeqi
Thank you, I can only create one sim. I have previously tried to control the opening and closing of the gripper through "setInt32Signal" and it was successful. But I don't know how to pass a callback function when using sim.moveToConfig.
In Example.java, I saw registering a callback function, but I didn't find out how to use it.
Re: Java ZMQ Remote API
Posted: 08 Apr 2024, 10:08
by lumeqi
Code: Select all
public class Example {
public Example() {
}
public static Object[] myFunc(Object[] items) {
Object[] retVals = new Object[]{21};
return retVals;
}
public static void main(String[] _args) throws IOException, CborException {
RemoteAPIClient client = new RemoteAPIClient();
RemoteAPIObjects._sim sim = client.getObject().sim();
client.registerCallback("myFunc", Example::myFunc);
sim.setStepping(new Object[]{true});
sim.startSimulation(new Object[0]);
double simTime = 0.0;
while((simTime = sim.getSimulationTime(new Object[0])) < 3.0) {
System.out.printf("Simulation time: %.2f [s]%n", simTime);
sim.step(new Object[0]);
}
sim.stopSimulation(new Object[0]);
}
}
I know
Code: Select all
client.registerCallback("myFunc", Example::myFunc);
used to register a callback, but where was this callback function called?
Sorry, I have another question. Does the callback function have to return data of type "Object []" and what is the meaning of this data.
Code: Select all
client.registerCallback("configCallBack", ConfigCallBack::onCallBack);
sim.moveToConfig(
-1, currentConfig, currentV, currentA,
vel, accel, jerk,
configs, targetV,
??, data, cyclicJoints, 0.0
);
If
ConfigCallBack is the class I defined and
onCallBack is the callback function in the class I defined, what should I write in the "??" position in my code? Thanks.
Re: Java ZMQ Remote API
Posted: 08 Apr 2024, 10:25
by fferri
Make sure to use the latest version of CoppeliaSim. You are using an old version.
In the latest version of Example.java there are more examples.
About your other question: Object[]
is an array of Object
which is the superclass of all types, indicating a no better specified type.
Object[]
is used as a return argument in Java as it is the only way to return multiple values from a function.
Re: Java ZMQ Remote API
Posted: 08 Apr 2024, 13:17
by lumeqi
Thanks! I'm sorry to bother you again. I saw the code for calling the callback function:
Code: Select all
Long retVal = sim.testCB(21,"myFunc@func",42); // sim.testCB is calling back above "myFunc"
But may this method not be used for parameter passing? Because the
sim.testCB method returns a value of type
Long, when I write code like this, I get an error:
Code: Select all
Java. lang. RuntimeException: C:/.../lua/sim.lua: 712: attempt to call a number value (local 'callback')
My code (Where "configs" and "joints" are the parameters I passed.):
Code: Select all
client.registerCallback("configCallBack", ConfigCallBack::onCallBack);
sim.moveToConfig(
-1, currentConfig, currentV, currentA,
vel, accel, jerk,
configs, targetV,
sim.testCB(configs, "configCallBack@func", joints), joints, cyclicJoints, 0.0
);
Perhaps the
sim.testCB function can only be called directly and cannot be passed as a parameter. So what should I do? (I have downloaded and checked the latest version of the code)
Re: Java ZMQ Remote API
Posted: 08 Apr 2024, 13:36
by lumeqi
I found a solution in the source code, and I should pass the callback function as a parameter as follows:
Code: Select all
sim.moveToConfig(
-1, currentConfig, currentV, currentA,
vel, accel, jerk,
configs, targetV,
"configCallBack@func", joints, cyclicJoints, 0.0
);
I tried to write it as "configCallBack" before, but it failed. It seems to be because the suffix "@func" needs to be added.