View Single Post
  #1   Spotlight this post!  
Unread 19-02-2007, 20:56
Andrew Blair's Avatar
Andrew Blair Andrew Blair is offline
SAE Formula is FIRST with Gasoline.
FRC #0306 (CRT)
Team Role: Alumni
 
Join Date: Feb 2005
Rookie Year: 2004
Location: Corry
Posts: 1,193
Andrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond reputeAndrew Blair has a reputation beyond repute
Send a message via AIM to Andrew Blair Send a message via Yahoo to Andrew Blair
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!
__________________
Reading makes a full man, conference a ready man, and writing an exact man.
-Sir Francis Bacon

"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
-Albert Einstein