CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

float32 typecasting

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








float32 typecasting
PostPosted: Tue Dec 16, 2008 9:40 pm     Reply with quote

dsPIC30F4011
PICC ver 4.079

Im using a lookup table to convert a sensor's response to a distance. element 0 holds the response at 0 inches, element 80 holds the response at 8.0 inches, and so on.

I've got the algorithm working for finding the closest value in the lookup table, and returning its element number. To add a little more pseudo-resolution, I'm trying to linearly interpolate between the two adjacent responses. basically my algorithm is this:

assuming lookup[n] holds the value closest to and less than in_val, and the array is sorted in ascending order:
Code:

int16 lookup[SIZE] = {100, 250, 600, 1000, 1300, ...........}

n=closest(response);
distance = n + (in_val-lookup[n])/(lookup[n+1]-lookup[n]);

Since the response is input through the ADC, in_val and lookup are int16.
but to reap any benefit from the interpolation, the division must be done with float32. I've tried all of the following, and each hit some reset causing error.


Code:

int8 n;
int16 in_val, lookup[80];
float32 numerator, denominator, distance;

//method 1
distance = (float32)(n + (in_val-lookup[n])/(lookup[n+1]-lookup[n]);

//method 2
distance = ((float32)n + ((float32)in_val-(float32)lookup[n])/((float32)lookup[n+1]-(float32)lookup[n]);

//method 3
numerator = (float32)in_val-(float32)lookup[n];
denominator = (float32)lookup[n+1]-(float32)lookup[n];
distance = n+numerator/denominator;


The third method resets on the first statement with the float32 cast.

I've been able to work out a program on a PC compiler, so I'm confident in the algorithm. But computing the algorithm creates an error.

Any idea why?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Dec 17, 2008 3:21 am     Reply with quote

Quote:
But computing the algorithm creates an error.
Which error?
Generally, C rules for implicite type conversion are pretty clear. An explicite type conversion of one division operand is necessary to perform a float operation, otherwise an integer division is taking place.
Ttelmah
Guest







PostPosted: Wed Dec 17, 2008 3:31 am     Reply with quote

The most likely cause of the problem, is that you are actually going outside the table. On the PC, there will be limit checking on the values allowed to address the array. This will not be present on the PIC.
Comments then:
There is no point in casting the values to float for a subtraction. in_val, and your lookup, both give integers, so do the subtraction with integers. However, make them _signed_, in case the second is larger than the first (you say not, but are you _sure_...).
Then you need to add testing, for being at the limit of the array. Your array has entries from 0 to SIZE-1. N, must not be larger than SIZE-2, or you will overflow the array in the lookup. You need to test for, and deal with this. With the example given, you will get problems at 7.8" because of this.
Remember that unlesss you have 'zero_ram', or initialise a variable, it will contain _unpredicatable_ values on the first pass through a function. What might 'n', and 'in_val' contain.
It is completely up to you to add bounds, and value checking to your arithmetic. On the PC, values default to being initialised to zero....

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group