Page 2 of 3

Re: Trigonometry Causing PWM to stop

Posted: Fri Aug 26, 2011 9:48 am
by Microman171
Hey Guys,

I have the results of my floating point test.

The function I use to print numbers to the serial port only works for integers, so I used conditional logic to determine if the answer was correct or not.

In the main thread, I had:

Code: Select all

// Floating point test
xvar = sinf(30);
if (xvar = 0.50) {
   debug_println("Float Success");
} else {
   debug_println("Float Fail");
}

Where xvar is of type 'float'.

Running the program yielded "Float Success".

However, when using sinf(60), I get a float fail when testing like this:

Code: Select all

if (xvar > 0.80)

I'm hoping this isn't because I suck at math (because I like to think I don't..!). Using my calculator, I've found that sin(60) = 0.86603 (5dp).

Is there something I did wrong with the test? If this proves that floating point actually does work, then I will do some more testing in the Controller thread...


Cheers,

MM

Re: Trigonometry Causing PWM to stop

Posted: Fri Aug 26, 2011 10:06 am
by Giovanni
When you invoke a math function with a constant parameter it is possible that the compiler does not really invokes the trigonometry function but just assigns directly the result because optimization.

If you want to be sure then you must pass as parameter a float variable defined externally (non static).

Giovanni

Re: Trigonometry Causing PWM to stop

Posted: Sat Aug 27, 2011 3:26 am
by Microman171
Hey,

I'm not sure what the problem is here. Using this method still results in "Float Fail"

Code: Select all

float xvar = 0.0; // Float output
float xin = 0.0; // Float input

xin += 60; // Set xin to 60
xvar = sinf(xin); // Floating point sine of xin
if (xvar > 0.80) { // Should be true for sin(60)
   debug_println("Float Success");
} else {
   debug_println("Float Fail");
}


I used xin+=60, as I assume this wont be optimised out? If that is the case, then I will try using the variable a couple of times (printing the value) before the floating point calculation.


Cheers,

MM

Re: Trigonometry Causing PWM to stop

Posted: Sat Aug 27, 2011 10:50 am
by mabl
Hey Microman,

Microman171 wrote:

Code: Select all

if (xvar = 0.50) {
   debug_println("Float Success");
} else {
   debug_println("Float Fail");
}


first, never compare floats like value1 == value2, this will eventually bite you.
EDIT: You use = to compare instead of ==, so no wonder your result is always true.

Microman171 wrote:I used xin+=60, as I assume this wont be optimised out?

If you don't want to optimize it out, define it as global volatile variable (for test purpose only)

Microman171 wrote:

Code: Select all

float xvar = 0.0; // Float output
float xin = 0.0; // Float input

xin += 60; // Set xin to 60
xvar = sinf(xin); // Floating point sine of xin
if (xvar > 0.80) { // Should be true for sin(60)
   debug_println("Float Success");
} else {
   debug_println("Float Fail");
}



The answer is correct. The computer will calculate in radian, where as you think in degrees. sin(60 rad) = -0.3048106211022167! You should do a conversion to Radian first, such as sin(60 * PI/180)=0.86

EDIT2: On a side note, you might want to think about using fixed point math if speed is an issue for you. I like libfixmath. It works beautifully with the Cortex M3 and includes some handcrafted ASM ;)

Re: Trigonometry Causing PWM to stop

Posted: Sat Aug 27, 2011 11:36 pm
by Microman171
Hey mabi,

Thanks for looking through this. The '=' vs '==' thing always bites me... I seem to forget about it the majority of times!

I'll implement one of the methods listed to compare floats, and see how it turns out... If it *doesn't* crash I will be confused, as that was the reason for this thread!

EDIT: Okay, I have used to following code to do my test:

Code: Select all

int AlmostEqual2sComplement(float A, float B, int maxUlps) {
   // Make sure maxUlps is non-negative and small enough that the
   // default NAN won't compare as equal to anything.
   int aInt = *(int*)&A;
   // Make aInt lexicographically ordered as a twos-complement int
   if (aInt < 0)
      aInt = 0x80000000 - aInt;
   // Make bInt lexicographically ordered as a twos-complement int
   int bInt = *(int*)&B;
   if (bInt < 0)
      bInt = 0x80000000 - bInt;
   int intDiff = abs(aInt - bInt);
   if (intDiff <= maxUlps)
      return 1;
   return intDiff;
}

/* Floating point globals */
float xvar = 0.0;

volatile float xin = 60.0;

// Floating point test

xvar = sinf(xin);
diff = AlmostEqual2sComplement(xvar, -0.304810621, 1000);
if (diff == 1) {
   debug_println("Float Success");
} else {
   debug_print("Float Fail: ");
   debug_printn(diff);
   debug_println("");
}


This results in success! Which means that the motors are still working after those calculations. This leaves me to believe that maybe the Controller thread causing the issues is still doing something while asleep? I put a blinky LED into the loop, so I can see the iteration time, and the LED reflects the 100mS sleep time suggesting that if the controller thread does indeed run longer that the 100mS, it is creating other threads of some kind...

As another test, I'll try to continuously do floating point trigonometry in the main thread.


Cheers,

MM

Re: Trigonometry Causing PWM to stop

Posted: Sun Aug 28, 2011 4:48 am
by Microman171
I have found the bug!

Thanks to all this testing, I have found that the problem was not floating point or trigonometry. The problem is with the sqrt(); function.

Code: Select all

void float_test(void) {
   int diff = 0;
   xin = 60.0;
   xvar = sinf(xin);
   diff = AlmostEqual2sComplement(xvar, -0.304810621, 1000);
   if (diff == 1) {
      debug_println("Sine Success");
   } else {
      debug_print("Sine Fail: ");
      debug_printn(diff);
      debug_println("");
   }
   xin = 60.0;

   xvar = sqrt(xin);
   diff = AlmostEqual2sComplement(xvar, 7.745966692, 1000);
   if (diff == 1) {
      debug_println("Square Root Success");
   } else {
      debug_print("Square Root Fail: ");
      debug_printn(diff);
      debug_println("");
   }
}


Results in success for both Sine and Square Root. However, with the square root code running, the motors do now work. By disabling the square root calculations (commenting the block out), the motors become fully functional.

Can anyone advise how to go from here? I'm not sure what would cause the sqrt(); function to disable PWM / interrupts?


Cheers,

MM

Re: Trigonometry Causing PWM to stop

Posted: Thu Sep 01, 2011 8:09 pm
by Microman171
Is it possible that the sqrt(); is causing a break in the state machine of the motors somehow? Disabling interrupts, or modifying a variable?

If I have to, I'll write a square root function..!


Cheers,

MM

Re: Trigonometry Causing PWM to stop

Posted: Fri Sep 02, 2011 9:57 am
by mabl
Sqrt should not have any effects on other threads. What I'd do in your place is to build a minimal application which still shows the behaviour. Enable all debug checks in chibios, get yourself a good JTAG debugger and see what it brings. Good tools will save you a lot of trouble.

In my personal tool collection I have the "Open Bench Logic Sniffer", great piece of hardware, cheap and ultrafast. And a JLINK EDU, which is a great deal if you are still a student.

Re: Trigonometry Causing PWM to stop

Posted: Wed Sep 14, 2011 3:29 am
by Xamusk
About JTAGs, the Open Logic Sniffer's builder also has a "generic" JTAG, the Bus Blaster. It's very cheap too.
If you need cheaper, there's the CoLink, but last time I tried, I had to patch OpenOCD to make it work properly.
I've seen some cheap JTAGs from India too, but don't have much info on them.

Re: Trigonometry Causing PWM to stop

Posted: Wed Sep 14, 2011 7:31 am
by Giovanni
Olimex and Amontec have cheap JTAGs too. I would not buy one that requires patches on OpenOCD, best have it supported in the mainline code or you could find yourself stuck with an old version.

Giovanni