Calculating the volume of liquid in a horizontal cylinder?

Calculating the volume of a square tank or vertical cylinder is a about the limit of my Math skills. embarrassingly it's been 25 years since i used cos or square root.

Could someone offer some insight on how to translate this equation into C++?

This is referenced via this page: http://mathworld.wolfram.com/HorizontalCylindricalSegment.html

In case someone is interested in understanding why.. i have an unusual shape domestic heating oil tank and wish to use an HC-SR4 to measure the quantity of oil.

Thanks to all involved in supporting arduino.

cos-1(x) is written acos(x) in C/C++
use sqrt(x) for square root.

All variables should be double or float, which are treated the same on an Arduino.

That equation won’t work for your tank without modifications, because of the flattened side (h is measured from one side of the intact cylinder).

I'd probably cheat - put four load cells underneath and weigh it. :) Edit: No matter what, you'll have an inaccuracy due to the ribs in the container, and it's a pretty complex shape overall. Weighing is relatively easy.

Thanks for the quick reply.

I'd planned on calculating the volume of liquid by subtracting the volume of the piece of cylinder that has been removed from the measured level calculation of a complete cylinder.

I'll have a go at re-writing the formula and post here for you to laugh at or make suggestions!

OldSteve Nice idea. thanks for that. It's a possibility although the tank will never be empty enough to lift for sometime. it's getting colder and wet over here in the UK.

Different way:

You can use an approximation:

Approach the cylinder as a cube with its current footprint as one side. As this cube is smaller than the real volume you are always on the safe side. (otherwise take the footprint a bit smaller)

float volume = floor * height.

a more accurate approximation would be an "hexagon-cylinder". Calculating the volume could be done in 2 steps. Volume upper halve + volume lower halve.

check - http://hubpages.com/education/Volume-of-a-Trapezoidal-Prism-Formula-and-Examples to get started

Formula would include square and triangle math, no gonio.


The formula: volume = L * (R * R * acos((R-h)/R) - (R-h) * sqrt(2*R*h - h*h));

You can calculate the volume of the missing part with the same volume formula. This will be a constant.

Then the formula becomes

volume = L * (R*R * acos((R-h)/R) - (R-h) * sqrt(2*R*h - h*h)) - constant;


Another way could be to add a flow control and measure how much liquid leaves the cylinder?


another ref to the original formula http://jwilson.coe.uga.edu/emt668/EMAT6680.2002.Fall/Wright/6690/essay%201/essay1.html

All you young ones should pay attention in school and college.. my brain hurts.. and i've got the syntax wrong somewhere!

double radius = 700;
double height = 350;
double length = 1400;
double volume;

void setup() {
Serial.begin(9600);
}

void loop() {
volume = (((radius*radius)*acos((radius - height)/radius)-(radius - height) sqrt((2*radius)* height)-(height*height))*length)
 Serial.println(volume);
}

You're missing an operator of some sort before sqrt. You need a + or - or * or / there. I haven't really looked at the formula so I'll leave it to you which is right.

too many () to be readable. * and ; missing

volume = (radius * radius * acos((radius - height)/radius) - (radius - height) * sqrt( 2 * radius * height - height * height )) * length;

I would cheat a different way. Since this program will be pretty small there will be lots of room left over on the chip for a lookup table. Fill the tank once taking readings as you do and noting the number of gallons put in there vs the reading from your sensor. Stick that in a lookup table and be done.

Ah Rob, thanks that's super. looks as though i tweaked your interest too. i think i will ignore the tanks ribs and slightly domed ends as it's really only an approximation of the oil volume i'm after. I agree a flow meter would show consumption with the best accuracy. something to add when the aga gets turned off in the spring.

There is a graph on the webpage I posted, seems you would be quite safe with a linear interpolation.