Shop OBEX P1 Docs P2 Docs Learn Events
Calculation goes wrong (Float32) — Parallax Forums

Calculation goes wrong (Float32)

PlaneTeeRPlaneTeeR Posts: 100
edited 2006-05-24 13:34 in General Discussion
Hello everyone,

I've got this program on mij BOE with a LCD and a Hall-sensor.

It meassures the time between pulses and then it calculates the speed.

I use the Float32 klasse also so mayby this is a good question for you peter.

The First reading is good, but then it went wrong! Check the result:

(ms = time between pulses in ms)
output javelin

Km/h 3.7716„
ms 1909
Km/h 4.9422
ms 2747
Km/h 7.546
········· 4
ms 2358
Km/h 13.832165
ms 1964
Km/h 15.788139
ms 3154
Km/h 24.56236
ms 2314
Km/h 28.03g
ms 3154
Km/h 47.607
ms 2120
Km/h 69.89ü8
ms 2452
Km/h 167.19589
ms 1505
Km/h 304.1ý4
ms 1979
Also there are sometimes strange symbols.

I've attached my java code...

Johnny
«1

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-17 20:02
    You must reset o to "2.0" for each speed calculation.
    I corrected your code in the attachement.
    Also, do not use + to concatenate strings.
    The javelin has no garbarge collection, so the use of +
    leads to OutOfMemory errors.

    regards peter

    Post Edited (Peter Verkaik) : 5/17/2006 8:05:41 PM GMT
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-18 11:50
    Dear Peter,

    With your corrected code I get a error:

    [noparse][[/noparse]Error IDE-0024] Unknown bytecode in the JEM file (dup2_x2)

    Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-18 12:20
    I put a try/catch block in the code.
    That sometimes·resolves the Unknown bytecode error.
    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-18 12:51
    Yes i saw it, i made the code to difficult at first and this works.

    I want to display the speed in this format 00.0 Km/h.

    How do i get this done?

    Johnny

    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-18 13:01
    You calculate the 10x speed and takes only the integer part.
    Then print speed/10, the decimal point, speed%10
    See the attachement
    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-18 17:11
    Dear Peter,

    I only can send Strings to my LCD so it does work on PC println but the not on my LCD (BPI-216).

    So is there another method?

    Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-18 17:52
    The idea is to send speed/10 to your lcd, then "." and then speed%10.
    I always use the Format class to assemble strings in memory
    char[noparse]/noparse buf2 = new char[noparse][[/noparse]6]; //allow xxx.x plus closing 0
    int k=0;
    k = Format.bprintf(buf2,k,"%d.",speed/10);
    k = Format.bprintf(buf2,k,"%d",speed%10);
    buf2[noparse][[/noparse]k]=0; //now buf2 holds nullterminated string
    myLCD.write(buf2.toString()); //print string to LCD

    I am not sure about toString for a nullterminated char array.
    Basically, all buf2[noparse][[/noparse]i] that are not zero, starting at i=0, must be
    appended to a StringBuffer, which may be converted to String.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-18 19:35
    Peter, i have an error that says: No method named toString() was found in type Char[noparse]/noparse

    Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-18 19:39
    That's what I feared.
    add this to the code:
    StringBuffer buf3 = new StringBuffer();
    int j=0;
    while (buf2[noparse][[/noparse]j]!=0) {
    · buf3.append(buf2[noparse][[/noparse]j]);
    · j++;
    }
    myLCD.write(buf3.toString());

    regards peter

    ·
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-18 19:54
    This works, thanks another problem solved!

    Johnny
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-18 21:22
    One last question, can i do it completely without the Float32? And for the print to the LCD use the format class?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-18 22:04
    The resulting formula for 10x speed [noparse][[/noparse]km/h] = 72,000 / time[noparse][[/noparse]ms]
    You can use the UnsignedIntMath class to calculate 2/time
    Then use umulf() to calculate integer * fraction (eg. speed = 36000 * (2/time))

    int speed =·UnsignedIntMath.umulf((short)36000,UnsignedIntMath.ufrac(2,time));

    then you don't need Float32.
    The UnsignedIntMath class is much smaller than Float32
    and is located here:
    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/math/

    The results should be about the same (I expect differences +-0.1 or so compared to Float32)



    regards peter




    Post Edited (Peter Verkaik) : 5/19/2006 9:36:11 AM GMT
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-19 09:26
    oke i have an Integer value, but i need a string to send to my LCD
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-19 09:35
    You do that the same way how you did it
    with the integer value obtained from Float32,
    that is with the Format class and a StringBuffer.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-19 22:53
    Also i have to display the distance, can i do this the same way.
    (I use an this way to count the distance after every pulse dst=dst+2[noparse];)[/noparse]
    The distance must be showed as 0000.00 KM but as you can see 2 is in meters so its of the scale.

    Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-19 23:21
    If you want to keep distance up to 9999.99 then I
    suggest to use 2 integers:
    int dstKm that holds the integral Km
    int dstm that holds the fractional Km in meters
    For updating your distance
    · dstm = dstm + 2;
    · if (dstm == 1000) {
    ··· dstKm++;
    ··· dstm = 0;
    ····if (dstKm == 10000) dstKm = 0; //wrap from 9999 to 0000
    · }

    For printing distance:
    · k = Format.bprintf(buf2,0,"%04d",dstKm);· //print 0000 to 9999
    · k = Format.bprintf(buf2,k,".%02d",dstm/10);· //print .00 to .99
    · buf2[noparse][[/noparse]k]=0;

    Then convert to String as in previous code.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-21 13:39
    Peter,

    The code works good, but after a while it gives an error see picture.

    Tis is weird, why out of memory? I don't storage information or something!

    Also added the code.

    Johnny
    1280 x 770 - 95K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-21 13:52
    You used new char[noparse]/noparse and new StringBuffer inside while (true) loop.
    I defined·buf0 to buf3 as static global buffers.
    That should resolve the outofmemory problem.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-21 13:55
    Why does that give a out of memory?
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-21 14:08
    Hmm,

    With this code it writes the values all over the LCD and misses very much readings!

    I don't get this!

    Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-21 14:35
    One problem with the setup is that you increment the distance by a fixed amount
    everytime through the while (true) loop. If pulses are missed (and I think that is likely
    because calculation and printing takes some time) the calculated distance is lower
    than the actual distance.
    In attached program I calculate the travelled distance for the time it takes to run
    a single loop. The travelled distance is in 0.1 m units.

    Let me know the results.
    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-21 15:23
    I checked the BPI216 class, and besides the write(String s) method that you use,
    it also has a write(StringBuffer s) method, so the conversion from StringBuffer to String
    is not required. Both StringBuffer and String variants call write(int CG) for each
    character in the StringBuffer or String. So you can write directly from the buffers.

    I did this in the attached file. This reduces the loopTime.

    regards peter

    Post Edited (Peter Verkaik) : 5/21/2006 3:35:01 PM GMT
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-21 16:40
    I don't think it's that, this code works but not alwayss accurate.

    To calculate the distance you simply add the circumference each time there's a pulse.

    I looked at what it printed and this is the result:
    output
    1
    19.2
    2
    19.2 26.4
    3
    19.2 26.4 28.1
    4
    19.2 26.4 28.1 4.5
    5
    19.2 26.4 28.1 4.5 31.8
    6
    19.2 26.4 28.1 4.5 31.8 56.7
    7
    19.2 26.4 28.1 4.5 31.8 56.7 20.2
    8
    19.2 26.4 28.1 4.5 31.8 56.7 20.2 72.0
    9
    19.2 26.4 28.1 4.5 31.8 56.7 20.2 72.0 57.6
    As you can see the results are printed after each other in the char.

    This is started when we made them global.

    I tried to make the buf null but that doesn't work here?

    Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-21 17:41
    Try the attached program and tell me the
    results that are printed to the jide message window.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-22 10:00
    Hey Peter,

    The program works fine, but i'm curious.

    When we made the chars and stringbuffers globally it went wrong.

    As you can see the value's of the chars and stringbuffers were added after each other.
    output km/h javelin

    19.2
    19.2 26.4
    19.2 26.4 28.1
    19.2 26.4 28.1 4.5
    19.2 26.4 28.1 4.5 31.8
    19.2 26.4 28.1 4.5 31.8 56.7
    19.2 26.4 28.1 4.5 31.8 56.7 20.2
    19.2 26.4 28.1 4.5 31.8 56.7 20.2 72.0
    19.2 26.4 28.1 4.5 31.8 56.7 20.2 72.0 57.6
    I think this has to do with making them global and the javelin not clearing them. How do i clear these values? I tried char = null; but then the program doesn't run. That's what i did in pc java you could set a object to null.

    What can i do about this?

    Thanks Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-22 10:20
    They shouldn't be placed after each other. Each loop a=0 before using bprintf()
    so the char arrays are filled from position 0.
    That should be visual if you run the V3 version I posted.
    Here is my Jide Message output (I set passed=100 and disabled LCD output as I have no sensor)
    using the attached program.

    count: 1
    ·km/h: 72.0
    ·· km: 0000.000
    count: 2
    ·km/h: 72.0
    ·· km: 0000.002
    count: 3
    ·km/h: 72.0
    ·· km: 0000.005
    count: 4
    ·km/h: 72.0
    ·· km: 0000.008
    count: 5
    ·km/h: 72.0
    ·· km: 0000.011
    count: 6
    ·km/h: 72.0
    ·· km: 0000.014
    count: 7
    ·km/h: 72.0
    ·· km: 0000.017
    count: 8
    ·km/h: 72.0
    ·· km: 0000.020
    count: 9
    ·km/h: 72.0
    ·· km: 0000.023
    count: 10
    ·km/h: 72.0
    ·· km: 0000.026
    count: 11
    ·km/h: 72.0
    ·· km: 0000.029
    count: 12
    ·km/h: 72.0
    ·· km: 0000.032
    count: 13
    ·km/h: 72.0
    ·· km: 0000.035
    count: 14
    ·km/h: 72.0
    ·· km: 0000.038
    count: 15
    ·km/h: 72.0
    ·· km: 0000.041
    count: 16
    ·km/h: 72.0
    ·· km: 0000.044
    count: 17
    ·km/h: 72.0
    ·· km: 0000.047
    count: 18
    ·km/h: 72.0
    ·· km: 0000.050
    count: 19
    ·km/h: 72.0
    ·· km: 0000.053
    count: 20
    ·km/h: 72.0
    ·· km: 0000.056
    count: 21
    ·km/h: 72.0
    ·· km: 0000.059
    count: 22
    ·km/h: 72.0
    ·· km: 0000.062
    count: 23
    ·km/h: 72.0
    ·· km: 0000.065
    count: 24
    ·km/h: 72.0
    ·· km: 0000.068
    count: 25
    ·km/h: 72.0
    ·· km: 0000.071
    count: 26
    ·km/h: 72.0
    ·· km: 0000.074
    count: 27
    ·km/h: 72.0
    ·· km: 0000.077
    count: 28
    ·km/h: 72.0
    ·· km: 0000.080
    count: 29
    ·km/h: 72.0
    ·· km: 0000.083
    count: 30
    ·km/h: 72.0
    ·· km: 0000.086
    count: 31
    ·km/h: 72.0
    ·· km: 0000.089
    count: 32
    ·km/h: 72.0
    ·· km: 0000.092
    count: 33
    ·km/h: 72.0
    ·· km: 0000.095
    count: 34
    ·km/h: 72.0
    ·· km: 0000.098
    count: 35
    ·km/h: 72.0
    ·· km: 0000.101
    count: 36
    ·km/h: 72.0
    ·· km: 0000.104
    count: 37
    ·km/h: 72.0
    ·· km: 0000.107
    count: 38
    ·km/h: 72.0
    ·· km: 0000.110
    count: 39
    ·km/h: 72.0
    ·· km: 0000.113
    count: 40
    ·km/h: 72.0
    ·· km: 0000.116
    count: 41
    ·km/h: 72.0
    ·· km: 0000.119
    count: 42
    ·km/h: 72.0
    ·· km: 0000.122
    count: 43
    ·km/h: 72.0
    ·· km: 0000.125
    count: 44
    ·km/h: 72.0
    ·· km: 0000.128
    count: 45
    ·km/h: 72.0
    ·· km: 0000.131
    count: 46
    ·km/h: 72.0
    ·· km: 0000.134
    count: 47
    ·km/h: 72.0
    ·· km: 0000.137
    count: 48
    ·km/h: 72.0
    ·· km: 0000.140
    count: 49
    ·km/h: 72.0
    ·· km: 0000.143
    count: 50
    ·km/h: 72.0
    ·· km: 0000.146
    count: 51
    ·km/h: 72.0
    ·· km: 0000.149
    count: 52
    ·km/h: 72.0
    ·· km: 0000.152
    count: 53
    ·km/h: 72.0
    ·· km: 0000.155
    count: 54
    ·km/h: 72.0
    ·· km: 0000.158
    count: 55
    ·km/h: 72.0
    ·· km: 0000.161
    count: 56
    ·km/h: 72.0
    ·· km: 0000.164
    count: 57
    ·km/h: 72.0
    ·· km: 0000.167
    count: 58
    ·km/h: 72.0
    ·· km: 0000.170
    count: 59
    ·km/h: 72.0
    ·· km: 0000.173

    As you can see, the values are not stored after each other.
    That's why I asked to post your Jide Message output.
    To pinpoint where it goes wrong, either in the char arrays
    or the bprintf() methods.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-22 10:55
    I think you forgot to print a newline after printing a value.
    My debug code does print a newline and then values are
    not printed after each other, which proves the char arrays
    only hold one value each loop.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-22 11:03
    This is the jide message output from v3

    count: 1
    km/h: 9.1
    km: 0000.002
    count: 2
    km/h: 26.4
    km: 0000.005
    count: 3
    km/h: 7.4
    km: 0000.008
    count: 4
    km/h: 3.5
    km: 0000.010
    count: 5
    km/h: 10.6
    km: 0000.012
    count: 6
    km/h: 12.0
    km: 0000.015
    count: 7
    km/h: 14.7
    km: 0000.018
    count: 8
    km/h: 18.0
    km: 0000.021
    count: 9
    km/h: 16.1
    km: 0000.024
    count: 10
    km/h: 23.1
    km: 0000.028
    count: 11
    km/h: 18.8
    km: 0000.031
    count: 12
    km/h: 18.2
    km: 0000.034
    count: 13
    km/h: 18.0
    km: 0000.037
    count: 14
    km/h: 26.1
    km: 0000.040
    count: 15
    km/h: 23.1
    km: 0000.044
    count: 16
    km/h: 21.0
    km: 0000.047
    count: 17
    km/h: 21.9
    km: 0000.051
    count: 18
    km/h: 26.9
    km: 0000.054
    count: 19
    km/h: 21.7
    km: 0000.057
    count: 20
    km/h: 39.3
    km: 0000.062
    count: 21
    km/h: 36.9
    km: 0000.066
    count: 22
    km/h: 33.5
    km: 0000.070
    count: 23
    km/h: 6.0
    km: 0000.073
    count: 24
    km/h: 1800.0
    km: 0000.182
    count: 25
    km/h: 78.2
    km: 0000.189
    count: 26
    km/h: 24.1
    km: 0000.193
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-22 11:20
    output looks ok.
    I attached V5, which has an delay of 10 sec after LCD init
    so you can make speed. Then a single loop is executed and
    then the program halts after message "program finished".
    Show me the jide message output and your lcd output.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-22 12:03
    The jide output:

    count: 1
    time: 880
    km/h: 8.1
    km: 0000.002
    program finished
    count: 1
    time: 34
    km/h: 211.8
    km: 0000.002
    program finished
    count: 1
    time: 98
    km/h: 73.4
    km: 0000.002
    program finished
    count: 1
    time: 805
    km/h: 8.9
    km: 0000.002
    program finished
    count: 1
    time: 306
    km/h: 23.5
    km: 0000.002
    program finished
    count: 1
    time: 252
    km/h: 28.6
    km: 0000.002
    program finished
    count: 1
    time: 390
    km/h: 18.5
    km: 0000.003
    program finished

    LCD all the same
Sign In or Register to comment.