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.

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.

The code is placed in the Process_Data_From_Master_uP() 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?

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.

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

Another possibility is that you have scope problems. Code like this could cause issues like you are seeing:


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

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.

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.

The code is:

printf(“Distance = %d”, DistanceCalc(TILT_SERVO));

The code for the DistanceCalc function is:

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

I coulda sworn TILT_SERVO was a byte…

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
    in your print statement

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).

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) ((xFIXP) / 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) = (1024x)(1024y) = 10242xy
but
fixedPoint(xy) = 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:


#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.

Thank you for all of your help. I will be trying this today.