Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Sonar Function Issue (http://www.chiefdelphi.com/forums/showthread.php?t=54598)

Andrew Blair 19-02-2007 20:56

Sonar Function Issue
 
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.

Code:

/*******************************************************************************
* 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.
Code:

/*******************************************************************************
* 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

Re: Sonar Function Issue
 
Quote:

Originally Posted by Andrew Blair (Post 581993)
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

Code:

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

Re: Sonar Function Issue
 
Quote:

Originally Posted by Andrew Blair (Post 581993)
...my temp variable is never changing...

Code:

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:
Code:

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;
}



All times are GMT -5. The time now is 04:52.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi