If you cannot complete relatively simple tasks, why would anyone assign you something more challenging?? - Chris Fultz [more]
 Chief Delphi Sonar Function Issue
 Team 51 CD-Events CD-Media CD-Swap CD-Spy FRC-Spy Unsung FIRST Heroes WFA
The Chief Delphi Forums are sponsored by Innovation First International, Inc.

#1
02-19-2007, 07:56 PM
 Andrew Blair SAE Formula is FIRST with Gasoline. FRC #0306 (CRT) Team Role: Alumni Join Date: Feb 2005 Rookie Year: 2004 Location: Corry Posts: 1,193
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
02-20-2007, 11:02 AM
 kaszeta Registered User FRC #0095 (Grasshoppers) Team Role: Mentor Join Date: Feb 2004 Rookie Year: 2002 Location: Lebanon, NH Posts: 334
Re: Sonar Function Issue

Quote:
 Originally Posted by Andrew Blair 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
02-20-2007, 12:39 PM
 Alan Anderson Software Architect FRC #0045 (TechnoKats) Team Role: Mentor Join Date: Feb 2004 Rookie Year: 2004 Location: Kokomo, Indiana Posts: 7,051
Re: Sonar Function Issue

Quote:
 Originally Posted by Andrew Blair ...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;
}```

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements     User Announcements FIRST     General Forum         FIRST E-Mail Blast Archive     Rumor Mill     Career     Robot Showcase     Moderated Discussion Technical     Technical Discussion     Robotics Education and Curriculum     Motors     Electrical         CAN     Programming         NI LabVIEW         WindRiver C++         Java         MATLAB & Simulink         Python     intelitek easyC/REC information         easyC v2 for Vex         easyC for FRC         REC/Learnmate         WPILib         easyC PRO     Control System         IFI Control System         FRC Control System         Sensors         USB Chicklet     Pneumatics     Kit & Additional Hardware     CAD         Inventor         SolidWorks         Pro ENGINEER / Creo     IT / Communications         3D Animation and Competition         Website Design/Showcase         Videography and Photography         Computer Graphics     National Instruments LabVIEW and Data Acquisition         LabView and Data Acquisition Competition     Awards         Chairman's Award     Rules/Strategy         Scouting         You Make The Call     Team Organization         Fundraising         Starting New Teams         Finding A Team         College Teams     Championship Event     Regional Competitions     FIRST in Michigan     Mid-Atlantic Robotics     Off-Season Events         Chief Delphi Invitational     Thanks and/or Congrats     FRC Game Design     OCCRA         OCCRA Q&A         OCCRA Programming Other     Unsung FIRST Heroes     Chit-Chat         Games/Trivia             Fantasy FIRST     Car Nack's Corner     College & University Education     Dean Kamen's Inventions     FIRST-related Organizations         Western Region Robotics Forum         Robot Chicks Union (RCU)         Southern California Regional Robotics Forum         Hearing-Impared Network         FIRST Interactive Rural SupporT         freelanceFIRST         FIRST Scouting Network         openFIRST         The Grand FIRST Team         FloridaFIRST         Inspiration Foundation Inc.         South Dakota FIRST         FIRST International Partners         FIRSTwiki         Ultimate Robot Challenge         East Coast Drivers Union         Team San Diego         CollegiateFIRST         Dream FIRST         FIRST Brasil         The Blue Alliance             Video Archives     FIRST Historians     FIRST-A-Holic's Anonymous Mailbox     FIRST In the News...     FIRST Lego League         Lego Mindstorm Discussion     FIRST Tech Challenge         FTC E-Mail Blast Archive     VEX         VEX Robotics Competition     Televised Robotics     Math and Science         NASA Discussion ChiefDelphi.com Website     CD Forum Support         Bulletins and Newsletters     Extra Discussion Old Forum Archives     1999     2000     2001

 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 03:02 PM.

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

 -- English (12 hour) -- English (24 hour) Contact Us - Chief Delphi - Rules - Team Site - Archive - Top