The connection between vrep and matlab is alright. I use handles for the joints of pioneer_p3dx motors and the front ultrasonic sensor. I managed to start it and stop it after 5sec. What i want to do is when the differential robot find an obstacle to stop. My code is below. WIth that code the robot doesnt move. Any idea?
Matlab code:
Code: Select all
vrep=remApi('remoteApi');
vrep.simxFinish(-1);
clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);
if (clientID>-1)
disp('Connected to remote API server');
%motors
[returnCode, left_Motor] = vrep.simxGetObjectHandle(clientID,'Pioneer_p3dx_leftMotor', vrep.simx_opmode_oneshot_wait);%left motor
[returnCode, right_Motor] = vrep.simxGetObjectHandle(clientID,'Pioneer_p3dx_rightMotor', vrep.simx_opmode_oneshot_wait);%right motor
%sensors
[returnCode, front_sensor] = vrep.simxGetObjectHandle(clientID,'Pioneer_p3dx_ultrasonicSensor5', vrep.simx_opmode_oneshot_wait);%fron sensor (5)
[returnCode,detectionState, detectedPoint,~,~] = vrep.simxReadProximitySensor(clientID, front_sensor, vrep.simx_opmode_streaming);
%start measuring
[returnCode] = vrep.simxSetJointTargetVelocity(clientID, left_Motor, 0.1, vrep.simx_opmode_oneshot_wait);% left motor velocity
[returnCode] = vrep.simxSetJointTargetVelocity(clientID, right_Motor, 0.1, vrep.simx_opmode_oneshot_wait);% right motor velocity
while 1
[returnCode, detectionState, detectedPoint,~,~] = vrep.simxReadProximitySensor(clientID, front_sensor, vrep.simx_opmode_buffer);% measurement refresh
disp(norm(detectedPoint))
if (detectedPoint<2.8)
[returnCode] = vrep.simxSetJointTargetVelocity(clientID, left_Motor, 0, vrep.simx_opmode_oneshot_wait);
[returnCode] = vrep.simxSetJointTargetVelocity(clientID, right_Motor, 0, vrep.simx_opmode_oneshot_wait);
end
end
vrep.simxFinish(-1);
else
disp('Not connected');
end
vrep.delete()
Thank you in advance!