Problem with code (Vrep + Matlab)

Typically: "How do I... ", "How can I... " questions
Post Reply
karabill
Posts: 6
Joined: 14 Jun 2017, 11:20

Problem with code (Vrep + Matlab)

Post by karabill » 16 Jun 2017, 00:33

Im new in vrep so be kind with me :) Im doing my bachelor thesis and i have to do some algorithms for path planning. For example, bug 1&2 algorithm, A*(A-star ) and dijkstra. I have understand exactly what i have to do but i dont know the program at all and i cant write the code. We didnt use it in my university so its something new for me.

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()

Vrep scene:
Image


Thank you in advance!

coppelia
Site Admin
Posts: 7450
Joined: 14 Dec 2012, 00:25

Re: Problem with code (Vrep + Matlab)

Post by coppelia » 16 Jun 2017, 08:11

Hello,

make sure you have carefully read and understood this page. When you actually try to read data out of the local buffer with simx_opmode_buffer, then you should check the return code to be 0, otherwise the data is not valid.
Also, when you set the joint target velocities, there is no need to use the blocking mode (simx_opmode_blocking, or simx_opmode_oneshot_wait), you should better use simx_opmode_oneshot.

Cheers

Post Reply