Skip to content

Instantly share code, notes, and snippets.

@adammw
Created June 18, 2016 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammw/e7d9babaf809d027cfdfd61845c1e0a1 to your computer and use it in GitHub Desktop.
Save adammw/e7d9babaf809d027cfdfd61845c1e0a1 to your computer and use it in GitHub Desktop.
import coppelia.CharWA;
import coppelia.FloatWA;
import coppelia.IntW;
import coppelia.remoteApi;
public class SensorTest
{
public static void main(String[] args)
{
// Establish VREP Connection
System.out.println("Establishing connection to remote API server");
remoteApi vrep = new remoteApi();
vrep.simxFinish(-1); // just in case, close all opened connections
int clientId = vrep.simxStart("127.0.0.1",19997,true,true,5000,5);
if (clientId == -1) {
System.err.println("Failed connecting to remote API server");
System.exit(1);
return;
}
System.out.println("Connected to remote API server");
vrep.simxAddStatusbarMessage(clientId, "SensorTest started", remoteApi.simx_opmode_oneshot);
// Read sensor data
CharWA measuredDataString = new CharWA(1000);
vrep.simxReadStringStream(clientId, "measuredDataAtThisTime", measuredDataString, remoteApi.simx_opmode_streaming);
FloatWA measuredData = new FloatWA(0);
measuredData.initArrayFromCharArray(measuredDataString.getArray());
printData(measuredData.getArray());
// While we remain connected
while(vrep.simxGetConnectionId(clientId) != -1) {
if (vrep.simxReadStringStream(clientId, "measuredDataAtThisTime", measuredDataString, remoteApi.simx_opmode_buffer) == remoteApi.simx_return_ok) {
measuredData.initArrayFromCharArray(measuredDataString.getArray());
printData(measuredData.getArray());
}
}
// Before closing the connection to V-REP, make sure that the last command sent out had time to arrive.
IntW pingTime = new IntW(0);
vrep.simxGetPingTime(clientId, pingTime);
// Close the connection to V-REP
vrep.simxFinish(clientId);
System.out.println("Program ended");
}
static void printData(float[] data) {
for (int i = 0; i < (data.length - 3); i += 3) {
System.out.println(String.format("X: %+.3f Y: %+.3f Z: %+.3f", data[i], data[i+1], data[i+2]));
}
System.out.println("==============");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment