%rinomina il file pwmMauro.m
function t_off = pwmMauro(Tdem,men_theo_T,Gamma,mot,scale)
%Tdem=T required
%ta=Thrust request updating period
%output=thrusters off time
ta=1;
mof=.1;
MOF=1-(1-mof)*scale;
Tpwm=[men_theo_T(1,1)+men_theo_T(1,4);men_theo_T(2,2)+men_theo_T(2,4);men_theo_T(3,3)+men_theo_T(3,4)];
t_axis_off = Tdem .* (ta./Tpwm);

t_off(4)=max([-(t_axis_off(2)+t_axis_off(1))+MOF, -(t_axis_off(2)+t_axis_off(3))+MOF , -(t_axis_off(1)+t_axis_off(3))+MOF , +MOF]);
t_off(3)=t_off(4)+(t_axis_off(1)+t_axis_off(2));
t_off(1)=t_off(4)+(t_axis_off(2)+t_axis_off(3));
t_off(2)=t_off(4)+(t_axis_off(1)+t_axis_off(3));

% if( min(t_off) ) <.1,
%     t_off = t_off +(.1-min(t_off)) ;
% end

tmax = max(abs(t_off));

if(tmax > ta)
    %t_off = t_off * (ta/tmax);
    t_off=min(t_off)+(t_off-min(t_off))*(ta-min(t_off))/(tmax-min(t_off));
end


for ii=1:4
   if t_off(ii) <= 2*Gamma*mot %rimetti 2
       t_off(ii) = 0.0;
   elseif  t_off(ii) <= 2*mot 
       t_off(ii)=mot;
   end
end

 t_off=round(t_off/.001)*.001;
 t_off=t_off';

