Log in

View Full Version : Sonar Function Issue


Andrew Blair
19-02-2007, 20:56
I am using the Maxbotics sensor, obtaining data through the AD, and need to find the difference between two consecutive readings. In the following, I have a distance function that returns the distance to the target in inches, and I use that number in the following function delta_distance() to find the difference between consecutive readings.

The problem is, when I run the thing, my return is always the inverse of the actual distance reading, and the issue is, solved with a printf, that my temp variable is never changing. It remains at zero constantly.


/************************************************** *****************************
* FUNCTION NAME: distance
* PURPOSE: returns a realtime distance from sonar, in inches
* CALLED FROM: This file
* ARGUMENTS:
* Argument Type IO Description
* -------- ------------- -- -----------
* none
* RETURNS: unsigned int
************************************************** *****************************/

unsigned int distance(void)
{

return (unsigned int) Get_Analog_Value(rc_ana_in01)/s;
}




/************************************************** *****************************
* FUNCTION NAME: delta_distance
* PURPOSE: returns difference in range readings between cycles
* CALLED FROM: This file
* ARGUMENTS:
* Argument Type IO Description
* -------- ------------- -- -----------
* none
* RETURNS: signed int
************************************************** *****************************/
int delta_distance(void)
{
int temp;

temp -= distance();

return (int) temp;

temp=distance;
}
As well, if anyone can quickly through up a workable averaging function I'd be much obliged. I've tried without success, typically ending up with a constantly incrementing distance value, or erratic jumping output.

/************************************************** *****************************
* FUNCTION NAME: distance
* PURPOSE: returns a realtime distance from sonar, in inches
* CALLED FROM: This file
* ARGUMENTS:
* Argument Type IO Description
* -------- ------------- -- -----------
* none
* RETURNS: unsigned int
************************************************** *****************************/

unsigned int distance(void)
{
unsigned int temp;
i++;
unsigned int Distance;

temp+= (Get_Analog_Value(rc_ana_in01)/2)

if (i>=5)
{
Distance=temp/i;
i=0;

}


return (unsigned int)Distance;
}


Thanks!

kaszeta
20-02-2007, 12:02
As well, if anyone can quickly through up a workable averaging function I'd be much obliged.


Team 95 has a huge number of IR sensors (BigHAIR: Big Honkin Array of IR sensors), and we keep time history and a running average on them. Here's a snippet


long IR_range[6]; // current ranges, generated by another function
long IR_range_history[6][4]; // Last 4 readings, not necessarily in order (see below)
long IR_range_average[6]; // running average of last 4 readings

void Average_IR(void) {
static counter=0;

index=counter++%4; // Keeps from having to copy old data, just
// overwrite one column at a time

for (i=0;i<6;i++) {
IR_range_history[i][index]=IR_range[i];
IR_range_average[i]=0;
for (j=0;j<4;j++) {
IR_range_average[i]+=IR_range_history[i][j];
}
IR_range_average[i]/=4;
}
}

Alan Anderson
20-02-2007, 13:39
...my temp variable is never changing...


int delta_distance(void)
{
int temp;

temp -= distance();

return (int) temp;

temp=distance;
}


The temp variable in your code evaporates as soon as delta_distance() returns. If you want to keep a local variable's value between calls to a function, you need to declare it static:

int delta_distance(void)
{
static int last_distance = 0; // set to zero only on program start, retains value between function calls
int delta;
delta = distance()-last_distance; // compute difference between distances
last_distance = distance(); // remember this distance for next time
return delta;
}