Obstacle avoidance using Remote API C++

Requests or suggestions for new features
Post Reply
Msalmanch
Posts: 13
Joined: 06 Feb 2017, 19:07

Obstacle avoidance using Remote API C++

Post by Msalmanch »

Hello,

I am using C++ remote API to control Khepera III robot. The remote connection was successful. I have made a code for the robot to move and its working fine. How to code the Infrared sensor for obstacle avoidance???

Regards

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

Re: Obstacle avoidance using Remote API C++

Post by coppelia »

Hello,

if the remote API connection is working fine, then you can stream the data from the infrared sensors to the remote API, process the signals there, then act on the wheels. Please be more specific for a more precise answer to your question.

Cheers

Msalmanch
Posts: 13
Joined: 06 Feb 2017, 19:07

Re: Obstacle avoidance using Remote API C++

Post by Msalmanch »

Sorry for the inconvenience. What i meant was which command will be used for the detection of obstacles using infrared sensor. I have tried the "simxReadProximitySensor" command but it always returns a true value even if there is nothing in front of the sensor. The code is give below:

Code: Select all

#include <iostream>
#include <conio.h>
using namespace std;
extern "C" {
#include "extApi.h"
}
int main(int argc, char** argv)
{
	int a;
	int b;
	int *leftHandMotor  = &a;
	int *rightHandMotor = &b;
        int clientID;
	

	//simxFinish(-1);
	

	clientID=simxStart("127.0.0.1",19999,true,true,2000,5);
	cout << clientID << endl;
    if (clientID!=-1)
    {
      cout << "Connected" << endl;
	  
	simxAddStatusbarMessage(clientID,"Connection Sucessful",simx_opmode_oneshot);
    
	simxGetObjectHandle(clientID,"K3_leftWheelMotor",leftHandMotor,simx_opmode_blocking);
	
	simxGetObjectHandle(clientID,"K3_rightWheelMotor",rightHandMotor,simx_opmode_blocking);
    
	simxSetJointTargetVelocity(clientID,a,1,simx_opmode_streaming);
	
	simxSetJointTargetVelocity(clientID,b,1,simx_opmode_streaming);
	}
	

	int c;
	
	int *sensor  = &c;
	
	int mysensor= simxGetObjectHandle(clientID,"K3_infraredSensor4",sensor,simx_opmode_blocking); 

	simxUChar detectionState;
   
	simxInt errorCode=simxReadProximitySensor(clientID,mysensor,&detectionState,NULL,NULL,NULL,simx_opmode_streaming);

	if (detectionState!=0)
{
    simxAddStatusbarMessage(clientID,"obstacle detected",simx_opmode_oneshot);

	}

	  
		  
		  getch();
	}

Regards.

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

Re: Obstacle avoidance using Remote API C++

Post by coppelia »

For instance, this line of code is wrong:

Code: Select all

int mysensor= simxGetObjectHandle(clientID,"K3_infraredSensor4",sensor,simx_opmode_blocking);
you are using the remote API wrongly, mysensor is not the sensor handle, but the returnValue. Make sure to carefully read this page too.

Cheers

Post Reply