Go to Post Profesional Gracism - n. the blessing said before Robotics competitions begin. - Cynette [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-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 17-02-2007, 16:49
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Looping a function

For the IFI controller, it supposedly re-executes the user_routines.c file every 20 some-odd ms. I have a printf statement that prints the distance from the target using a custom function. However, when the tilt angle changes, the distance stays the same. It apparently does not keep re-evaluating the function and printing out the new value. Thanks for the help.
  #2   Spotlight this post!  
Unread 17-02-2007, 17:39
Shinigami2057 Shinigami2057 is offline
Slackware Is Your New God (Mentor)
AKA: Harry Bock
FRC #1350 (Rambots)
Team Role: Programmer
 
Join Date: Oct 2006
Rookie Year: 2006
Location: Johnston, RI
Posts: 106
Shinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really nice
Re: Looping a function

The code in user_routines.c is NOT executed every 26.2ms; the function Process_Data_From_Master_uP() does. Any code you want looped at that rate must be called through that function, or another function called within Process_Data_From_Master_uP(). Just because it is contained in that file does not mean it will be called at the same time as the 26.2ms "loop". They are contained there for organization.
__________________
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
  #3   Spotlight this post!  
Unread 17-02-2007, 21:23
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Looping a function

The code is placed in the Process_Data_From_Master_uP() function.

Last edited by Tz0m : 18-02-2007 at 09:45.
  #4   Spotlight this post!  
Unread 17-02-2007, 21:31
paulcd2000's Avatar
paulcd2000 paulcd2000 is offline
Accidentally speaks in C
AKA: Paul Dagnelie
FRC #1719 (The Umbrella Corp.)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Baltimore
Posts: 368
paulcd2000 is a jewel in the roughpaulcd2000 is a jewel in the roughpaulcd2000 is a jewel in the rough
Send a message via AIM to paulcd2000
Re: Looping a function

how do you know that the tilt angle changes? i'm assuming you mean the camera moves. Could you maybe post how you're calculating the angle?
__________________
"People don't say 'It's just a game' when their team is winning!" -- Scott Adams

5.5 students (on average)* $7/h *210 hours/student= $8085 of labor, all volunteered (not counting mentors', who are each that much)

We have blades on our robot?! ***sweeeeeet***

There are 11 types of people in the world. Those who can read binary, those who can't, and those who say this joke is supposed to be, "There are 10 types of people in the world. Those who can read binary and those who have a life."
  #5   Spotlight this post!  
Unread 18-02-2007, 09:31
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Looping a function

I'm using the already defined variable TILT_SERVO to calculate distance. First I convert that number into degrees and then into radians so it can be passed to tangent. The tilt angle does change. I have it print out the servo values and degree values. The servo values have to be correct. I have also manually calculated angle values for servo values and they match up in the terminal window.
  #6   Spotlight this post!  
Unread 18-02-2007, 11:50
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Looping a function

Are you absolutely sure that the function is working properly? If it was executed once from Process_Data_From_Master_uP, and isn't in an if statement or other structure, it is probably getting executed again (especially if you are printing results from it).

Check the function to make sure that it is using the right inputs, and that it is definetely USING them. I've had this problem before, and without fail it is because I used the wrong variable, or in one of the lines of the function, I didn't use the result from the previous line. You could also post the code and let us look at it. I wouldn't worry about people stealing it. After all, it is broken

Another possibility is that you have scope problems. Code like this could cause issues like you are seeing:
Code:
// Bongle's error in scoping example
int degrees = 5;
printf("%d,%d,%d",servo,tilt,other) // verifies all the inputs are changing
if(someCondition)
{
   int degrees; // creates a variable that shares name with another variable in the code
   degrees = myCustomFunction(); // calls the function, assigns it to the most-local variable (the one we just declared)
}                   // at this closing brace, the degrees variable that stored our result is destroyed
printf("%d",degrees) // always prints out a constant value, despite inputs always changing
Your output will never change (it will always print 5) because the assignment statement in the if statement assigns the return value to a variable that shortly afterwards is destroyed at the end of the if statement.

Last edited by Bongle : 18-02-2007 at 11:54.
  #7   Spotlight this post!  
Unread 19-02-2007, 00:32
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Looping a function

The function is working properly. I do not have my function setup as an if statement or anything like that. The function is also being called from within a printf.
  #8   Spotlight this post!  
Unread 19-02-2007, 06:41
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: 9,112
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
Re: Looping a function

Could you at least show us the line of code that isn't working the way you want it to? We can't help you much if we can't see what's wrong.
  #9   Spotlight this post!  
Unread 19-02-2007, 12:37
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Looping a function

The code is:
Quote:
printf("Distance = %d", DistanceCalc(TILT_SERVO));
The code for the DistanceCalc function is:
Quote:
int DistanceCalc(int tiltS)
{
int tiltD, tiltR, dist;
tiltD = ((tiltS - 124) * 65) / 124;
tiltR = (tiltD * 3.14159265) / 180.0;
dist = 80.0 / tan(tiltR);
return dist;
}
  #10   Spotlight this post!  
Unread 19-02-2007, 13:40
robind robind is offline
Registered User
FRC #0675
 
Join Date: Dec 2006
Location: Rohnert Park
Posts: 31
robind is an unknown quantity at this point
Re: Looping a function

I coulda sworn TILT_SERVO was a byte...
  #11   Spotlight this post!  
Unread 19-02-2007, 14:43
jdejoannis jdejoannis is offline
Registered User
FRC #1845
 
Join Date: Feb 2006
Location: Atlanta,GA
Posts: 48
jdejoannis will become famous soon enoughjdejoannis will become famous soon enough
Re: Looping a function

Code looks alright to me. Try different things, eg.
  • Print TILT_SERVO itself - does it change?
  • Create a new variable to hold the distance and then print it
  • Use the (int) mask on the function and its argument
  • Make sure you are downloading the newly compiled hex and not some old version
  • Be nice to your eyes and include a \r\n in your print statement
  #12   Spotlight this post!  
Unread 19-02-2007, 15:04
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Looping a function

The problem is that you are doing a bunch of floating point calculations, then truncating them to integers.

This is the biggie:
tiltR = (tiltD * 3.14159265) / 180.0;

For tiltD values between 0 and 57, you're going to get a value of 0 in tiltR. For 58-114, you're going to get 1. All three of your main calculating lines will all round into integers. The combination of all that rounding will probably result in you seeing what you are.

Recomendation:
1) Use fixed-point math (multiply everything by 1000 and be careful with multiplications/divisions),
-or-
2) declare everything as a float and make sure you don't call this function often (floats are very expensive).

Last edited by Bongle : 19-02-2007 at 15:07.
  #13   Spotlight this post!  
Unread 19-02-2007, 16:37
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Looping a function

Even if I make everything a float the terminal prints blank spaces for the results. Also, do I multiply the final answer by 1000 or do I multiply before hand?
  #14   Spotlight this post!  
Unread 19-02-2007, 21:25
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Looping a function

Quote:
Originally Posted by Tz0m View Post
Even if I make everything a float the terminal prints blank spaces for the results. Also, do I multiply the final answer by 1000 or do I multiply before hand?
Apparently printf has issues with floating-point output. To fix that, do this:

// suppose your result is in variable myFloat
int iTemp = (int)(myFloat*1000.0f)
printf("result is %d divided by 1000",iTemp);


If you want to go the faster fixed-point method, then I'd suggest writing a bunch of defines to make sure that you don't screw up a multiply or divide.

#define FIXP 1024
#define FIXP_MULTIPLY(x,y) ( (x * y)/FIXP )
#define FIXP_DIVIDE(x,y) ((x*FIXP) / y)
#define FIXP_ADD(x,y) (x+y)
#define FIXP_SUBTRACT(x,y) (x-y)
#define FIXP_TO_FLOAT(x) ( (float)x/FIXP )
#define TO_FIXED_POINT(x) (x*1024)
It looks uglier, but it'll keep you from forgetting normalizing a value.

Intro to fixed-point numbers:
Basically, what you're doing is saying: 1024 in my fixed-point system equals 1. So an integer holding 512 REALLY means 0.5, one holding 768 means 0.75, etc. This way, you can simulate decimal points without having to do all the complicated (and very computationally expensive) floating-point calculations.

The reason that multiply and divide are different is because you're system is based on this idea:
fixedPoint(x) = 1024*x

fixedPoint(x)*fixedPoint(y) = (1024*x)*(1024*y) = 10242xy
but
fixedPoint(x*y) = 1024*(x*y) = 1024xy

So we need to divided the multiply result by 1024 in order to get the correct result.

Anyway, your function changed to use these fixed-point #defines would look like:
Code:
#define FIXP 1024
#define FIXP_MULTIPLY(x,y) ( (x * y)/FIXP )
#define FIXP_DIVIDE(x,y) ((x*FIXP) / y)
#define FIXP_ADD(x,y) (x+y)
#define FIXP_SUBTRACT(x,y) (x-y)
#define FIXP_TO_FLOAT(x) ( (float)x/FIXP )
#define TO_FIXED_POINT(x) (x*1024)

float DistanceCalc(int tiltS)
{
// tiltS is between 0 and 255, let's make it fixed-point
int fixp_tiltS = TO_FIXED_POINT(tiltS);

int tiltD, tiltR, dist;
tiltD = ((fixp_tiltS - TO_FIXED_POINT(124)) * 65) / 124;
// tiltD is fixed-point because divides/multiplies by non-fixed-point numbers don't affect that
tiltR = (tiltD * 3.14159265) / 180.0;
// tiltR is fixed-point for the same results tiltD was
dist = (float)(TO_FIXED_POINT(80.0)) / float(tan(tiltR));
// dist is NOT fixed point because it got divided out
return dist;
}
If you have a programming mentor, ask them about the basics of fixed-point math.

A good way to think about it is to just imagine doing all your math with the decimal place moved to the right, and if you ever want to find out your 'real' number, you just have to move the decimal back to the left.
Example:
You have 3.14 and 2.71. You want to multiply them and find the result.
1) Represent them as 3140 and 2710.
2) Multiply them to get a result of 8509400, but that's HUGE! If we move the decimal place to the left again, then we'd be saying 3.14*2.71 is 8509! That's because of the multiplication issue I discussed above.
3) If we move the decimal place back to the left 3 places first, we get 8509.
4) Then if we want to know the result in real numbers, we move it again, and get 8.509, which is actually (close to) correct.

This was kind of a stream-of-thought post, I hope it helped. Do a google search for it. A basic fixed-point math system is really quite simple to implement.
  #15   Spotlight this post!  
Unread 20-02-2007, 11:22
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Looping a function

Thank you for all of your help. I will be trying this today.
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
Arcade Function gabrielse Programming 1 08-02-2006 00:49
Autocalibrate function Validius Programming 2 29-03-2005 21:59
Looping Atonomous BobcatProgramer Programming 2 24-02-2004 14:06
FreeLibrary() Function Raven_Writer Programming 0 09-08-2003 15:39
control program looping??? ctartist236 Programming 1 08-02-2002 10:09


All times are GMT -5. The time now is 10:59.

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


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