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 support@ccsinfo.com

volume computation with cosine

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



Joined: 23 Dec 2005
Posts: 23

View user's profile Send private message

volume computation with cosine
PostPosted: Fri Dec 23, 2005 3:52 am     Reply with quote

guys, i want to use a formula,through which i would get the volume of a tank,the parameters of which will be defined by the user,except the height,which will be computed using adc through a pressure sensor.I am using pic 16f689.The formula looks like this:
Code:
#ifdef __pcm__
#include <16F689.h>

#device *=16 ADC=10
#include<stdio.h>

#endif



#FUSES NOWDT, INTRC_IO, NOPROTECT,NOBROWNOUT,NOMCLR, NOCPD,NOPUT, NOIESO, NOFCMEN
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5,ERRORS)

#include <math.h>


void volume1(void);
void volume2(void);
long r,t1,t2,volume;
long result;


///formula for volume
///
///Volume=L*{[r^2 *  acos ((r-h)/r) ] - [sqrt(2 r h -h^2) * (r-h)]}


void main()
{
h=1000;
r=1500  ;
L=4000;


t1= acos((r-h)/h)*pow(r,2);
t2=sqrt(2*r*h -pow(h,2))*(r-h) ;

volume=(t1-t2)*L;

printf("result %4ld \n\r", volume);

}








i was trying to use user-defined h,r,L values just to c if its workin properly.
i am only getting volume as 0000,wonder why.
.also,is it possible to have these complex math operations,when u have only 30% OF ROM and 16% ram free?as u might see ,this code itself eats up around 80% ROM and 40% ram?need some help here,guys.would be really cool,if u do.
Guest








PostPosted: Fri Dec 23, 2005 5:11 am     Reply with quote

Start by using floats.
If you look at the first line for example, you calculate:
acos((r-h)/r)
Now in int16 (which is a 'long'), the value r-h will work out as 500, then 500/1500, in integer maths, will equal _zero_....
You can work right up to the limit of the memory size, _unless_ you are using ICD, which requires some ROM. Many people here, have programs using ROM/RAM, up in the 98+% area.
Using floats will consume another 10 bytes of RAM (a float is 4bytes/value, against a long at 2 bytes per value), but is necessary for the arithmetic and the intermediate results.
On values that start as integers (like your height, and radius), you can convert them when used. So:

acos((float)(r-h)/r);

Will calculate r-h as integer, convert this to a 'float', then do the division using float arithmetic, and pass the result to the acos function. This way, these values can stay as integers.

The code size will increase using float arithmetic, but since acos (the largest function) is already using float (it always does, since it makes no sense in integer), this will not be too bad.

Best Wishes
codewrecker2



Joined: 23 Dec 2005
Posts: 23

View user's profile Send private message

PostPosted: Sat Dec 24, 2005 1:31 pm     Reply with quote

thanks Ttelmah 4 ur gd advice,i worked out the best way to compute volume for this horizontal cylindrical cylinder,and this is how it goes:

Code:

#ifdef __pcm__
#include <16F689.h>

#device *=16 ADC=10
#include<stdio.h>

#endif



#FUSES NOWDT, INTRC_IO, NOPROTECT,NOBROWNOUT,NOMCLR, NOCPD,NOPUT, NOIESO, NOFCMEN
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5,ERRORS)

#include <math.h>

long r,h,L,volume;

int32 t1;

///formula for volume
///
///Volume=L*{[r^2 *  acos ((r-h)/r) ] - [sqrt(2 r h -h^2) * (r-h)]}


void main()
{
h=1000;
r=1500  ;
L=4000;


t1= acos((float)(r-h)/h)*(r*r)-sqrt(2*r*h -(h*h))*(r-h);

volume=(t1*L)/1000;

printf("volume is %ld litres \n\r", volume);

}





The results are pretty accurate ,if i use this.
The problem is ,i want to include this routine in my main program,which has all ADC,SERIAL COMM and LCD functions.without this routine,the used rom is around 74%(around 2.8K) and the used ram is around 210(76%) worst case & 63% at main level,woteva it means..
The acos ,being a big function eats a lot of ram ,i guess.is ther any way around this?

Anonymous wrote:


You can work right up to the limit of the memory size, _unless_ you are using ICD, which requires some ROM. Many people here, have programs using ROM/RAM, up in the 98+% area.


I don't use ICD ,so how can i use maximum rom and ram?need to work this out quick.

i followed a few earlier threads,and used #separate for the functions that i am calling only once.is ther nething else i could do?

season's greetings to all

Very Happy
Ttelmah
Guest







PostPosted: Sat Dec 24, 2005 3:39 pm     Reply with quote

Just use the memory. There are no 'tricks' here.
On the acos, you could probably generate a smaller, less accurate version, using a simple lookup table, and linear interpolation. I have done this in the past, for generating coordinates from a digital compass module, and if only a couple of digits accuracy is needed, it can be significantly smaller.

Best Wishes, and 'Merry Christmas' everybody. Shutting down for a few days. :-)
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