%power_calcs
clear all
close all


%buoyancy force per airlock cycle
wpc=.042;
%starting negaitve buoyancy/weight of the robot
sw=1.6;
%volume of gas per inflation in m^3
Vg=1.591*10^-5;
%Pressure of gas
Pg=12000;
R=8.314472; % ideal gas constant
mmco=44; %molar mass of CO2in grams
Tg=273+15; %gas temperature

%P*V=m*R*T/M find m, m=P*V*M/(R*T)
%Mass/weight of CO2 used per airlock cycle (in pounds)
gm=Pg*Vg*mmco/(R*Tg)*2.205*10^-3;
msa=2900; %max amount of mAh for solenoids
mra=1200; %max amount of mAh for relays and microcontroller


%Determine how many days the air supply will last and how much battery
%power will be required 
%set the current weight and other counter variables
cw=sw;
i=0; %number of inflation cycles
cc=0; %number of inflate-deflate cycles
sa=0; %number of solenoid activations
sp=0; %amount of power for solenoids
rp=0; %amount of power used for relays


while cw>(sw-.75) %run until the current weight is less than the starting weight less the CO2 (until the CO2 is gone)
    cc=cc+1;
    bf=0; %preset buoyancy force
    j=0;
    while bf<(cw+.5)
        bf=bf+wpc;
        cw=cw-gm;
        i=i+1;
        sa=sa+2*1;
        j=j+1;
    end
    sa=sa+3;
    count(cc,:)=[i j sa cw];
    sp=sa*(1/(60*60))*400;
    rp=sa*(1/(60*60))*5/700+3*cc*2;
    if sp<msa
        tsa=cc; %record the number of cycles possible before exhausting solenoid battery
    end
    if rp<mra
        tra=cc; %record number of cycles possible before exhausting microcontroller battery
    end
end

%determine how many days it will take to exhaust the robots onboard
%resources
s.cnt=size(count);
daysGas=s.cnt(1)/12;
daysSol=tsa/12;
daysRel=tra/12;
days=[daysGas daysSol daysRel];
maxdays=min(days);

%determine the stages at which to changes thenumber of airlock cycles
i=0;
j=1;
h=1;
for k=1:s.cnt(1)
    if i~=count(h,2)
        i=count(h,2);
        act(j,:)=count(h,1:2);
        j=j+1;
    end
    h=h+1;
end




'Number of days possible for one mission'
maxdays

'Number of days possible based on Gas, Solenoid Battery, and Microcontroller Battery'
days

'i and j values to program the microcontroler with.'
'These determine how many times the airlock is cycled per inflate-deflate cycle'
act

