My brain farts a lot when it comes to hex. Some people are abidextrous and others are just plain right handed... well, kind of like that, I'm VERY base10 I guess.
Anyway... I'm trying to make some firmware more efficient. We have a function that's calculating a vehicle's speed based on some hex data it gets via the CANBUS. Right now I'm converting it to a float so I can wrap my head around it but I'm wondering if we'd use less ROM space if we left it in integer format? Can that be done without losing accuracy? Right now my floats are accurate to 1/16th of a kph. I know this function seems simple, but running hundreds of times per second it is bogging it down a little.
First, here is some sample data:
[06] [3c] ... [06] [3a] ... [06] [3b] ... [06] [46] ... [06] [3b] ...
I've left off the other 6 bytes as they don't relate to speed. The byte on the left we'll call speed_a and the byte on the right is speed_b. Here is the function to convert:
float calculateSpeed()
{
float speed;
speed = ( ( float )speed_a * 256.0 + speed_b ) / 16.0;
return speed;
}
So the data above would translate to:
99.7500 99.6250 99.6875 100.3750 99.6875
and that does accurately reflect the true speed of the vehicle in kph. For our application though, we don't really care what the true speed is because everything is relative. As long as we don't lose resolution we're happy. I thought about just keeping everything in INT form, but then when you divide by 16 it just truncates.
I'm not an idiot about most things... but I'm an idiot with base2.
Lil' help? Thanks.