Sonar unable to get and set sonarQuietMode

Supporting 2.6 and up
Post Reply
morob05
Fresh meat
Posts: 2
Joined: Fri Mar 02, 2018 5:21 pm
Contact:

Sonar unable to get and set sonarQuietMode

Post by morob05 »

According to the API the sonar (DST1200_0) operates in quietMode as default. So i wanted to set it in loud mode, however it seems no matter what i tell PhidgetDistanceSensor_setSonarQuietMode to do, the sonar stays in quitMode, also i am unable to fetch the value with PhidgetDistanceSensor_getSonarQuietMode. Code example:

Code: Select all

	PhidgetDistanceSensorHandle ch;
	PhidgetDistanceSensor_create(&ch);
	Phidget_setHubPort((PhidgetHandle)ch, 4);
	Phidget_setChannel((PhidgetHandle)ch, 0);
	Phidget_open((PhidgetHandle)ch); 
	PhidgetDistanceSensor_setSonarQuietMode(ch, false); 
If i run that code, the sonar clearly stays in quietMode. There's no difference in loudness no matter if i type true or false in PhidgetDistanceSensor_setSonarQuietMode.

Code: Select all

	PhidgetDistanceSensorHandle ch;
	PhidgetDistanceSensor_create(&ch);
	Phidget_setHubPort((PhidgetHandle)ch, 4);
	Phidget_setChannel((PhidgetHandle)ch, 0);
	Phidget_open((PhidgetHandle)ch); 
	int quietMode;
	PhidgetDistanceSensor_getSonarQuietMode(ch, &quietMode); 
	std::cout<<quietMode<<std::endl;//Prints 0 even though API says its 1 as default
If i run this code, the terminal prints 0 even though API says default value is 1. What am i doing wrong?
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Sonar unable to get and set sonarQuietMode

Post by mparadis »

It's best to set properties like QuietMode in the attach handler to ensure that the channel is fully attached first. It's possible that setQuietMode is executing before the channel has finished opening.
fraser
Engineering
Posts: 138
Joined: Thu Nov 19, 2009 4:41 pm
Contact:

Re: Sonar unable to get and set sonarQuietMode

Post by fraser »

The sonar is definitely not yet attached at that instance of code. Doing something like calling Phidget_openWaitForAttachment() instead of Phidget_open() will block the function until the device attaches, or times-out per your argument to the call. However, often even after calling openWaitForAttachment, the device could still take a small amount of time to successfully return polling functions.

But more importantly, make sure you check your return codes in c/c++.

For example, define a function to output error codes, and call that function after every phidget call. (shown with a temperature object polling for Attached property)

Code: Select all

void DisplayError(PhidgetReturnCode returnCode)
{
	PhidgetReturnCode prc;
	const char* error;
	prc = Phidget_getErrorDescription(returnCode, &error);
	if (EPHIDGET_OK != prc)
	{
		printf("Error getting error description\n");
		DisplayError(prc);
	}
	else
	{
		printf("->Error Description: %s<-\n", error);
	}
}

Code: Select all

bool IsAttached(PhidgetTemperatureSensorHandle temperature)
{
	PhidgetReturnCode prc;
	int attached;

	prc = Phidget_getAttached((PhidgetHandle)temperature, &attached);
	if (EPHIDGET_OK != prc){
		printf("Failed on getAttached. [%x]\n", prc);
		DisplayError(prc);
	}
	if (attached)
		return true;
	else return false;
}
You can adapt this code to work for the sonar:

Code: Select all

int QuietMode(PhidgetDistanceSensorHandle dst1200)
{
	PhidgetReturnCode prc;
	int quietmode;

	prc = PhidgetDistanceSensor_getSonarQuietMode(dst1200, &quietmode);
	if (EPHIDGET_OK != prc){
		printf("Failed on getQuietMode. [%x]\n", prc);
		DisplayError(prc);
	}
	return quietmode;
}
morob05
Fresh meat
Posts: 2
Joined: Fri Mar 02, 2018 5:21 pm
Contact:

Re: Sonar unable to get and set sonarQuietMode

Post by morob05 »

Thanks for the replies. Yeah, i realized this after trying different stuff in code. I initially though that it would be set in the PhidgetDistanceSensorHandle since the function is called with a handle, and that it would then automatically know that it was set when the phidget attaches. But phidget must apparently be attached before setting quietMode, when i wrote an attach handler, like in the example code, and set quietmode there everything worked as i would expect :) I have some different questions regarding ideas on how to work with multiple sonars, 8 sonars to be exact, and preventing the sonars from interfering with each other, but i'll see if interference even turns out to be a problem in my project and will make a new post about it come that time :)
fraser
Engineering
Posts: 138
Joined: Thu Nov 19, 2009 4:41 pm
Contact:

Re: Sonar unable to get and set sonarQuietMode

Post by fraser »

You could see interference, but it would depend on the setup. If all of your sensors are facing a somewhat different direction, the strongest signal would generally be the reflection from that particular sensor. However if you had several all facing one direction, that could be interesting.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 0 guests