Calculate volume of cylindric tank

Hi all,
I'm trying to build a script/function that will help me calculate the volume of a cylindrical fuel tank, and translate about 10 (every 10%) points of the tank to 4-20mA output.
I'm using these tanks and as part of my work. The measurment is via a device that can read 4-20mA but it must be calibrated according to the calcultation above.
Basicaly what I'm looking for is to build a function that receives the diamater and length of the tank as an input and output 10 points of fuel level translated into 4-20 mA (where 4 mA means 0 liters of fuel and 20mA is the maximum the fuel tank can store)
Also plotting the fuel tank somehow and viewing it would be a great addition.
Any suggestions where and how to start building this??
I'm kind of new around matlab so any help would be more than appreciated.
Thanks ahead,

 Respuesta aceptada

Sudheer Bhimireddy
Sudheer Bhimireddy el 7 de Ag. de 2020
Editada: Sudheer Bhimireddy el 7 de Ag. de 2020
Try this:
function [tank_volume mA_points fuel_level mA_reading] = get_tank_volume(tank_diameter, tank_length, sensor_read)
% Calculate the volume of the tank
tank_volume = pi * (tank_diameter/2)^2 * tank_length;
vol_temp = 0:0.1:1;
vol_temp = vol_temp .* tank_volume; % Now your volume is divided into 10% increments
mA_points = linspace(4,20,numel(vol_temp)); % your mA points divided in to same number of volume increments
% Now fit a straight line through the volume and mA points to get the line-equation
coeffs = polyfit(vol_temp, mA_points, 1);
mA_slope = coeffs (1);
mA_intercept = coeffs (2);
% Now if you have the mA reading from the sensors
% then you can get the current fuel level by:
fuel_level = (sensor_read - mA_intercept)/mA_slope;
mA_reading = sensor_read;
% Instead if you have the fuel level reading and would like to
% get the equivalent mA reading, uncomment the below 2 expression
% fuel_level = sensor_read;
% mA_reading = (mA_slope * sensor_read) + mA_intercept
end
Once you get the required variable, you can plot the level as a rectangle with curved edges to resemble the tank.
tank_diameter=10;tank_length = 100; fuel_level = 45;
h=figure(1);
clf;
hold on;
% The first rectangle will draw the tank
rectangle('Position',[1 0 tank_diameter tank_length],'Curvature',0.5);
% The second rectangle will fill the tank based on the reading
% make sure to keep the curvature value same between the two rectangles for it to look good
rectangle('Position',[1 0 tank_diameter fuel_level],'Curvature',0.5,'FaceColor','b');
ylabel('Fuel level (L)');
ax=gca;ax.XTick=[];ax.XColor='none';
daspect([1 1 1]);
Hope this helps.

5 comentarios

Shaked Bar
Shaked Bar el 8 de Ag. de 2020
WOW Thanks so much I will definately try it.
Also, in case my fuel tank would be horizontal, should anything in the calculation change?
If the tank is horizontal, the only that changes is the way you plot those rectangles. Below code will plot the % of tank filled after getting the fuel volume.
tank_diameter = 10;
tank_length = 100;
fuel_vol = get_tank_volume(tank_diameter, tank_length, sensor_read);
% Vertical tank plot - so X-axis would be tank diamater
% fuel_x = tank_diameter;
% tank_y = tank_length;
% fuel_y = 100*fuel_vol/(fuel_x*tank_y); % Get the y-axis in % of tank filled
% Horizontal tank plot - so X-axis would be tank length
fuel_x = tank_length;
tank_y = tank_diameter;
fuel_y = 100*fuel_vol/(fuel_x*tank_y); % Get the y-axis in % of tank filled
% Plot tank and % volume filled in the tank
h = figure(1);
clf;clc;
hold on;
% No matter horizontal or vertical the y-axis is 100 as it reads (0-100)% fill
rectangle('Position',[1 0 fuel_x 100],'Curvature',0.5);
rectangle('Position',[1 0 fuel_x fuel_y],'Curvature',0.5,'FaceColor','b');
ylabel('Fuel level ($\%$)','interpreter','latex');
ax=gca;ax.XTick=[];ax.XColor='none';
% For horizontal tank change the value in the second index to make it good looking
% For vertical tank change the first value
daspect([1 5 1]);
John D'Errico
John D'Errico el 8 de Ag. de 2020
Please try not to do homework assignments for people on this site. That only convinces them they can get their homework done for them, with no effort expended. It does not help them or the site. If someone shows an effort, and asks for help in fixing what they have done, then it is reasonable to help them. Otherwise, the most you should offer is general guidance rather than complete code.
Sudheer Bhimireddy
Sudheer Bhimireddy el 8 de Ag. de 2020
Got it! :)
Shaked Bar
Shaked Bar el 12 de Ag. de 2020
Hi John,
This is not homework, this is actually for my work.
I'm trying to learn Matlab online from Youtube and from this respectful forum.
Thanks again Sudheer, I will definately try your suggestion.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 7 de Ag. de 2020

Comentada:

el 12 de Ag. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by