Invalid float/TimeSpan calculation

  • I have problems with one of our applications written in C#:


    In our main application we have a function which does some calculations every second:



    From time to time, we have the problem that this calculation fails because of
    - The value of the variable actValue jumps to 8.64E+7
    - From one second to the next, the value of elapsedTime has 1 day higher.


    I programmed another test application which does similar things like the function described above and have a test where both,
    the test application and our main application run parallel on the same PicoMOD6.
    -> The result was, that both, the test application and the main application have errors in the calculation funkcion.


    The next test was to deactivate the calculation function in the main application.
    -> The were still errors in the test application.


    The next test was that I habve deactivated the main application the only let the test application run.
    -> In this test I found no errors in the test application.


    After some more tests, I found out that the problem seems to be located in the CAN-Bus operation of the main application:


    When I start the main application with deactivated CAN-Bus communication (No CanPort opened), neither the main application nor the test application
    have any errors.


    Actually I have no ideas how to fix the problem or how the CAN-Bus operation in one application can cause problems in another application.
    Have anyone ever had similar problems or has anyone an idea what can cause the problem?
    And if yes, how to fix it?

  • Hello,


    i can't imaging that CAN drivers causes such an error!!! Did you do some floating point caculations in your CAN thread too?
    Why do you use double and float? Timespan contains only integer values. What happens if you replace this values by interger values. Is it possible?


    Note:
    Timespan resolution is 1s
    GetTickCount() resolution is 1ms

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • I also can't imagine why this happens.


    I'm using float and double because of:


    • The TotalSeconds Property of timespan is double


    • The calculated value is a floating point value


    My CAN receive handling is running as an own thread and the received messages are forwarded to another thread where the messages are handled.
    But even if comment out the method where the message is received (ReadEventData), I get this error. Actually, it seems that the only way where I do not get any error is when I do noch open the CanPort.


    Some more detailed information about my can-bus handling:
    I open 3 virtual CAN-ports:
    2 for receiving
    1 for sending


    Both receive ports receive all messages. In an earlier version of the program, the both receive ports where configured so that each port received 1/2 of the received message, but this was deactivated since I had problems that some messages where never received.

  • Hello,
    ok thanks for your explanation. But i still think CAN is not to blame. It may be a raise condition because ot the processor load.
    In a first and i think quick step, can you please replace "Timespan" by difference of two "GetTickCount"?

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • Hello,


    any new about this subject? Does it work without "Timespan" and floating point calculation?
    I assume complier or FPU causes this failure.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • No news.


    I cannot replace the float with another datatype.
    I will run a test over the weekend with the timespan replaced by a integer value which will be incremented every second.


    But I do not think that this will help resolving the issue because I do not think that the problem is located in the calculations.
    Even sometime simply reading out a value out of a float variable and put it to the debug output using ToString() will cause a erroneous output.
    (sometimes 8.64E7, sometimes NaN)...


    Do you have any ideas?
    Btw as I wrote earlier:
    The value of the timespan variable jumps to 1 day higher and 8.64E7 are the amount of milliseconds per day...
    But I had also tests where the timespan value has jumped to 46000 day higher or the float value is not 8.64E7...

  • Hello,

    Quote

    Do you have any ideas?

    See post above "I assume complier or FPU causes this failure".


    What i do not understand is, resolution of "TimeSpan" is 1s in WCE. So why you use floating point values, "GetTickCount()" and integers would be more accurate! Or i am wrong?


    I think more test are not required, i agree with you that someting is wrong. So i am looking for a workaround.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • The function does something like:


    "Go from 0 to 10 in one hour".


    This means that the function calculates a new value every second to go from 0 to 10 in 3600s.


    In this example, the function adds every second 1/360 to the actual value. How can this be done without using floating point values?

  • For example go from 0...3600000 in 3600000 ms and add ervery second 3600 for output purposes (display, file, ...) you may cast and divide, this should work.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • I tested a slighty changed function over the weekend:


    I changed the TimeSpan datatype with an integer datatype but let the float datatype unchanged.
    -> I got still errors with the float datatype


    But what is much more alarming:
    I added a small function with a float value and in this function I also get errors.


    The full function is:



    The added part is fTest and fTest2. And although I only put the value of fTest into the variable fTest2, sometimes the debug output shows a value of 8.64E7 (or sometimes -1.088841E+10).


    This would mean, that I cannot use any floating point value in the whole program. Is this possible???

  • Hello,


    did the problem persist if you do all floting point calculation in one thread? Then FPU has a problem! Sorry, I saw that we had already checked this problem under WCE6. Under EC7 it works (but PM6 i not avail with EC7)
    For "resolution" you may wrap floting point calculation in critical sections:-(. Or I provide you a kernel without FPU (takes a few days).

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • What do you mean by "calculation"?
    Does "calculation" include also reading a float value from a variable and write it to another variable like in my last test?


    Can you give me an example what you mean by wrapinng in critical section?


    The problem with the kernel is, that we have several units shipped to customers and it will be very difficult to exchange the kernel in these units.

  • Hello,
    by calculation i meant +, - , *, /, etc ... (all mathematical operations).
    In .NET a critical section is entered by the "lock" keyword. You will find samples in the MSDN documentation.


    We can also update the kernel in the field. I can send you the tool and documentation for free. The update itself is very easy but it should be tested extensive in the office before.


    Nevertheless, you should do a short and quik test either by do all fp calculations in one thead or by lock them. When it works we can be sure that we have located the bug.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • Hello,


    I willl do a test with the lock keyword. I think it would be harder to switch all operations in one thread.


    What do you think how long it takes until we can have the update tool and the kernel?

  • Hello,


    i can send you update tool and documentation immediately by mail.
    I can provide you kernel without FPU within this week.


    You are interested?

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.