help with code [newbie]

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

help with code [newbie]

Postby karabill » 17 Jul 2017, 16:03

Hello forum,
I am trying to control a differential wheeled robot(pionner_p3dx) in vrep via matlab. I put a collection object of cuboids in scene i made thes detectable and etc. So far i have managed to go straight ahead the robot and stop it when the obstacle is detected. Then i pause it for 2sec and I tried to turn it left. Then the robot turn left like it lags or doing so many steps and after a while it makes many circles around it. What am i doing wrong? please help

Code: Select all


%Connect matlab to vrep
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');
       % Send message from matlab to vrep
      vrep.simxAddStatusbarMessage(clientID,'Bug 1 Algorithm',vrep.simx_opmode_oneshot);
       
        %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);%front sensor (5)
        [returnCode,detectionState, detectedPoint,~,~] = vrep.simxReadProximitySensor(clientID, front_sensor, vrep.simx_opmode_streaming); %start measuring
       
        [returnCode, right_sensor8] = vrep.simxGetObjectHandle(clientID,'Pioneer_p3dx_ultrasonicSensor8', vrep.simx_opmode_oneshot_wait);%right sensor (8)
        [returnCode,detectionStateR8, detectedPointR8,~,~] = vrep.simxReadProximitySensor(clientID, right_sensor8, vrep.simx_opmode_streaming); %start measuring
       
        [returnCode, right_sensor9] = vrep.simxGetObjectHandle(clientID,'Pioneer_p3dx_ultrasonicSensor9', vrep.simx_opmode_oneshot_wait);%right sensor (9)
        [returnCode,detectionStateR9, detectedPointR9,~,~] = vrep.simxReadProximitySensor(clientID, right_sensor9, vrep.simx_opmode_streaming); %start measuring

   
       
        [returnCode] = vrep.simxSetJointTargetVelocity(clientID, left_Motor, 1.5,  vrep.simx_opmode_oneshot);% left motor velocity _wait
        [returnCode] = vrep.simxSetJointTargetVelocity(clientID, right_Motor, 1.5,  vrep.simx_opmode_oneshot);% right motor velocity
 
         while 1
        [returnCode, detectionState, detectedPoint,~,~] = vrep.simxReadProximitySensor(clientID, front_sensor, vrep.simx_opmode_buffer)% measurement refresh
        [returnCode, detectionStateR8, detectedPointR8,~,~] = vrep.simxReadProximitySensor(clientID, right_sensor8, vrep.simx_opmode_buffer)% measurement refresh
        [returnCode, detectionStateR9, detectedPointR9,~,~] = vrep.simxReadProximitySensor(clientID, right_sensor9, vrep.simx_opmode_buffer)% measurement refresh
        disp(norm(detectedPoint))
       
            if (detectionState == 1)
                if(detectedPoint < 0.5)
               
           %obstacle detected and robot stops
                    [returnCode] = vrep.simxSetJointTargetVelocity(clientID, left_Motor, 0,  vrep.simx_opmode_oneshot);
                    [returnCode] = vrep.simxSetJointTargetVelocity(clientID, right_Motor, 0,  vrep.simx_opmode_oneshot);
                    pause(2)
                   
                    %turn the robot left for 2sec. I am trying to turn it 90 degrees(parallel with obstacle)
                       for i=1:20
                        [returnCode] = vrep.simxSetJointTargetVelocity(clientID, left_Motor, 0,  vrep.simx_opmode_oneshot);
                        [returnCode] = vrep.simxSetJointTargetVelocity(clientID, right_Motor, 1,  vrep.simx_opmode_oneshot);
                        pause(i)
                       end
                       
                      %stop robot
                                [returnCode] = vrep.simxSetJointTargetVelocity(clientID, left_Motor, 0,  vrep.simx_opmode_oneshot);
                                [returnCode] = vrep.simxSetJointTargetVelocity(clientID, right_Motor, 0,  vrep.simx_opmode_oneshot);
                end
           
           
               
            end
         end
           

       
        vrep.simxFinish(-1);
else
    disp('Not connected');
end
vrep.delete()



code explain:
I move the robot ahead and when the sensor5 detects the obstacle i stop the robot. Then i am trying to turn it left for 2sec(if i dont make a mistake with the time in for loop) and then stop it.(but it never stops.)

I would appreciate if you could read my code and help me because im doing my bachelor thesis and i havent seen before vrep and i explore it by myself.

Thanks in advance,
Karabill

Return to “General questions”

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 19 guests