PDA

View Full Version : speed Calculations



longpole001
- 3rd November 2014, 03:11
Hi guys ,

i need to do some simple speed calculations in PBP 3 on 18f67k22 pic , that is very very full PIC already.

i have not had to do floating point calculations before and not sure if i need to now for this but i suspect i do, hope i dont cos i believe the overheads can be high .

i have options / variables that select the following for the displayed / recorded results

Display Distance base measurement
00 = Feet
01 = meters
02 = miles
03 = KM

Display Time base measurement
00 = time in sec
01 = time in minutes
02 = Time in Hours
03 = LAP SPEED

Distance input values
0 - 4000

Time input values

days, hours , min, sec , 1/100th


i would like to display the speed for the selected option , with a result to 2 decimal places

can you advise the best way to get this started for the least likely overhead in code space


Cheers

Sheldon

pedja089
- 3rd November 2014, 07:41
You didn't give range of variables...
One way is just multiply distance with 100, and put decimal separator fixed on display in result, if long variable have range to do that...

longpole001
- 3rd November 2014, 10:23
i cant see multiy by 100 is going to help
speed = distance / time

also cant see how i can avoid floating points and longs again ;(


distance = distance base measurement ( feet or meters or km or miles ) x distance input value
time = time base measurement ( sec or mins or hours or lap count ) x time measured value

for the caclulation of distance i though convert all values to meters as common base value


if meters option then Distbase_value = 1 ' = 1 meter
if feet option then Distbase_value = 3.28 ' = feet to meters
if Km option then Distbase_value = 1000 ' = 1 km in meters
if Miles option then Distbase_value = 1609.34 ' = 1 mile in meters

Dist_input = 0 - 4000
Distbase_value x Dist_input = Distance ' 24 bit number max ???



Calculation for Time base - make a sec the common base to work to

if sec option then Timebase_value = 1 ' = 1 sec base
if min option then Timebase_value = 60 ' = 60 sec in min
if hour option then Timebase_value = 3600 ' = 3600 sec in hour

TimeValue = days/hours/min/sec /1_100th - convert time to secs base

timevalue = days x 24 x 60 x 60 ' days
timevalue = timevalue + (hours x 60 x 60) ' hours
timevalue = timevalue + ( mins x 60) ' mins
timevalue = timevalue + sec ' sec
timevalue = timevalue + (100th / 100) ' 1_100th

timevalue eg 12456.78

timebase x timevalue = Time


speed = distance / time ' speed is a max 32bit positive number

is this seem right or am i doing the long and hard way , with larger number than need to

i am just starting to read up on Floating point , with the include of "FP1832.bas" and what i think is required as well , but not sure where to put them yet in the program of Fp32.A18 and math18.inc files

sheldon

pedja089
- 3rd November 2014, 10:54
By multiply your number with 100 you shift result for 2 decimal places.
Eg:
10/3=3.333
If you are using integer, result is 3.
If you multiply 10 with 100, (10*100)/3=333.333, using integer's result is 333. Put dot on in front of second digit, and displayed result is 3.33. And you get your result with 2 decimal places.
This is explained too many times when using ADC to display voltage on input...

richard
- 3rd November 2014, 11:03
would it not be easier to decide on a basic speed unit first ie miles/hour , kph or meters/sec.
even yards per month or whatever then just convert it to the desired display format

for my money i'd pick meters/second

longpole001
- 3rd November 2014, 11:25
id like to use sec/meters as the only option but i dont know what option is to be used on the day , and must allow for each possible one described

richard
- 3rd November 2014, 19:36
and

1 m/s = 2.236936292054402 mph =3.3 kph =1.943844492440605 knots=3.280839895013123 ft/s =0.0029386414601757 mach ................................


id like to use sec/meters as the only option

as I said

or whatever then just convert it to the desired display format

to have any chance of doing this efficiently we need to know
min and maximum for each of these quantities
1 velocity
2 distance
3 time
otherwise its just pissing in the wind

Acetronics2
- 3rd November 2014, 19:40
as a surprise ...

1 foot = 78/256 meter

1 mile = 412/256 kilometer

Which are more than easy to calculate in PBP ...

others are obvious ...

Alain

longpole001
- 3rd November 2014, 22:40
have any chance of doing this efficiently we need to know
min and maximum for each of these quantities
1 velocity
2 distance
3 time
otherwise its just pissing in the wind


velocity - max = 300km /hour
distance input max = 4000 ,
options of distance base is miles/km/meters/feets
timer allows for 7days 23hours ,59mins, 59,sec,. 99 1/100th - before rolling to 0.00
timer base for speed measurments is sec, min, hour,

richard
- 4th November 2014, 03:24
so worst case


max distance 4000 miles = 6437376 m
max time = 8 days = 69120000 1/ 100's of a second

v=(d/time)/100 (d in meters ,time in 1/100's of a second) = v in m/s
velocity= (6437376/69120000) /100 (to two decimals) as m/s
velocity= (6437376/69120000)*36 /1000 (to two decimals) as kph

and using Alain's figures
velocity= (6437376/69120000)*256 /(100*78) (to two decimals) as ft/s

etc.....................
just keep within the range of unsigned longs


a better strategy would be to examine the magnitude of the time and switch to hours at a suitable size and ditto for distance switch to kilometres for larger distances. you may be able to avoid longs
that way

longpole001
- 4th November 2014, 05:10
yes like to avoid longs if i can for code space

longpole001
- 4th November 2014, 05:11
btw u pic a good horse today richard

richard
- 4th November 2014, 05:23
could not even get last in the sweep

longpole001
- 4th November 2014, 22:01
in a starting point i am trying to "include FP1832.bas" , into the program with fp32.a18 , math18.inc in the programs working directory , but getting lots of asm errors symbol not prev defined 113 as a result , what am i forgetting ?

Acetronics2
- 6th November 2014, 07:27
a better strategy would be to examine the magnitude of the time and switch to hours at a suitable size and ditto for distance switch to kilometres for larger distances. you may be able to avoid longs
that way

+1000

and moreover if only two decimals are needed for the result ...

might be the " Arduino syndrom " ( extensive use of floats :D:D:D ) ???

Alain

longpole001
- 6th November 2014, 22:54
i would love to avoid floats or long at all , which means a lot more if/then for ranging of the inputs for the distance and time options for a given time / distance input

the result display would be returned to 1 byte for decimal display use , and a word value for the above decimal use ,
i am bit lost on this one on how to get it going atm would not be too bad normally but i have only small amount of code space left , i am still trying to claw back some code space elsewhere

varables i have so far

Display Distance base measurement selection option

00 = Feet
01 = meters
02 = miles
03 = KM

Display Time base measurement selection option
00 = time in sec
01 = time in minutes
02 = Time in Hours
03 = LAP SPEED

Distance input values
0 - 4000 - 2 bytes

Time input values

days, hours , min, sec , 1/100th - all 1 bytes

richard
- 6th November 2014, 23:48
one of the first rules in mathematical calculations is that 2000/2.xx is accurate to 0 decimal points
2000.0 / 2.xx is accurate to 1 decimal point. to be accurate to 2 decimals all!! input figures must be measured to two decimals , otherwise its fantasy

richard
- 7th November 2014, 00:12
actually its worse than I made out
see this
http://courses.chem.indiana.edu/s117/documents/Precision-SigFigs.pdf

longpole001
- 7th November 2014, 00:55
that was interesting read and a worry in how calculations can go astray , i guess i am looking for no better than if i put distance / time into online calc i get it to 2.dec places , as best as possible