OCCRA
Go to Post It's good to learn from experience. It's better to learn from somebody else's experience. - Jay TenBrink [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Events   CD-Media   CD-Spy   FRC-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 02-19-2007, 07:56 PM
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
  #2   Spotlight this post!  
Unread 02-20-2007, 11:02 AM
kaszeta's Avatar
kaszeta kaszeta is offline
Registered User
FRC #0095 (Grasshoppers)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Lebanon, NH
Posts: 334
kaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of light
Re: Sonar Function Issue

Quote:
Originally Posted by Andrew Blair View Post
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;
   }
}
  #3   Spotlight this post!  
Unread 02-20-2007, 12:39 PM
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 7,847
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Send a message via AIM to Alan Anderson
Re: Sonar Function Issue

Quote:
Originally Posted by Andrew Blair View Post
...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;
}
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SONAR Sensor Software Driver MikeDubreuil Programming 55 03-08-2008 10:03 AM
Maxbotix Sonar Sensor DonRotolo Electrical 17 02-04-2007 11:27 AM
VEX Sonar question Joohoo Programming 2 05-06-2006 12:18 AM
Help with sonar sensor supertw Electrical 3 01-24-2006 10:41 PM
FreeLibrary() Function Raven_Writer Programming 0 08-09-2003 02:39 PM


All times are GMT -5. The time now is 06:41 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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