Shop OBEX P1 Docs P2 Docs Learn Events
Float Math - Addition to Previous Value Problem — Parallax Forums

Float Math - Addition to Previous Value Problem

I am very fond of the built in floating point functions of the P2, though I keep running into a random problem whenever I try to add a new value to a previous one. It seems very random and some of the solutions don't seem right even though it worked. The same problem exists when using BinFloat to perform as the built in P2 float functions. This code works 100% in VB6 and on the P1 chip.

Working through my PID function at the moment and cannot add the output to my current position value. Whenever I do, every value that uses the data turns to NAN. With the final step disabled, the rest of the function works as expected. It is only when I add the final output to the current position does it crash. If I use a hard coded value in place of "Output" then it works fine also.

I ran into the same issue with a complementary filter function, though what seemed to fix it was changing the stored dT value to an actively calculated dT. This should not have made any difference. The current issue works fine with a stored dT and active dT loop time with no issue either way. So it makes no sense why this fixed the complementary filter issue of adding the current gyro/accel to the previous gyro/accel value.

Is there something obvious I am simply overlooking here? In some places it works fine (I-Term) but in others (PVout) it randomly causes this NAN problem.

In the code attached is a PID function where the final step has been disabled due to an unknown NAN issue. Everything works fine until that line is un-commented, then everything that uses Pv to create a new value crashes.

Comments

  • ersmithersmith Posts: 6,068
    edited 2021-12-31 17:57

    dT is initially 0 (it comes from EndTime1, which is not set until after the first call to PIDtest()), and D is calculated as (Error -. LastError) /. dT, so your code divides by 0. This should actually give infinity rather than NaN (and does so in flexspin), but PNut and PropTool incorrectly give NaN for the answer.

  • @ersmith said:
    dT is initially 0 (it comes from EndTime1, which is not set until after the first call to PIDtest()), and D is calculated as (Error -. LastError) /. dT, so your code divides by 0. This should actually give infinity rather than NaN (and does so in flexspin), but PNut and PropTool incorrectly give NaN for the answer.

    Thats it... I guess the P2 is just extra anal about everything being pre-defined. The function should still work out with 0 as its initial value though. That value would simply = 0 in the PID loop because there was no time to compare against. Simply setting EndTime1 := GETCT() before repeat is called corrected the problem. Funny quirks of the language. VB6 assumes 0 * N or 0 / N always = 0 and any non-defined values are also = 0 at start.

    Thanks for the help! It is greatly appreciated!

Sign In or Register to comment.