Calculation goes wrong (Float32)
PlaneTeeR
Posts: 100
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)
I've attached my java code...
Johnny
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)
Also there are sometimes strange symbols.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
I've attached my java code...
Johnny
Comments
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
With your corrected code I get a error:
[noparse][[/noparse]Error IDE-0024] Unknown bytecode in the JEM file (dup2_x2)
Johnny
That sometimes·resolves the Unknown bytecode error.
regards peter
I want to display the speed in this format 00.0 Km/h.
How do i get this done?
Johnny
·
Then print speed/10, the decimal point, speed%10
See the attachement
regards 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
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
Johnny
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
·
Johnny
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
with the integer value obtained from Float32,
that is with the Format class and a StringBuffer.
regards peter
(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
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
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
I defined·buf0 to buf3 as static global buffers.
That should resolve the outofmemory problem.
regards peter
With this code it writes the values all over the LCD and misses very much readings!
I don't get this!
Johnny
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
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
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:
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
results that are printed to the jide message window.
regards 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.
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
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
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
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
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
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