function [necessary_gear]=necessary_gear_calculator_2(curr_speed,percent_throttle,curr_gear)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

% UP shifts:
curve_throttle_1_2=[0,30,65,100];
curve_throttle_2_3=[0,35,70,100];
curve_throttle_3_4=[0,30,70,100];
curve_throttle_4_5=[0,30,75,100];

curve_speed_1_2=[20,20,50,50];
curve_speed_2_3=[40,40,65,65];
curve_speed_3_4=[60,60,75,75];
curve_speed_4_5=[75,75,100,100];

% DOWN shifts:

curve_throttle_2_1=[0,30,65,100];
curve_throttle_3_2=[0,35,70,100];
curve_throttle_4_3=[0,30,70,100];
curve_throttle_5_4=[0,30,75,100];

curve_speed_2_1=[15,15,40,40];
curve_speed_3_2=[35,35,60,60];
curve_speed_4_3=[55,55,70,70];
curve_speed_5_4=[70,70,90,90];


k_UP=[interp1(curve_throttle_1_2,curve_speed_1_2,percent_throttle),...
      interp1(curve_throttle_2_3,curve_speed_2_3,percent_throttle),...
      interp1(curve_throttle_3_4,curve_speed_3_4,percent_throttle),...
      interp1(curve_throttle_4_5,curve_speed_4_5,percent_throttle)];

k_DN=[interp1(curve_throttle_2_1,curve_speed_2_1,percent_throttle),...
      interp1(curve_throttle_3_2,curve_speed_3_2,percent_throttle),...
      interp1(curve_throttle_4_3,curve_speed_4_3,percent_throttle),...
      interp1(curve_throttle_5_4,curve_speed_5_4,percent_throttle)];

% Condição de passagem 2->1:
if curr_speed<=k_DN(1)
    necessary_gear=1;
elseif (curr_speed>k_DN(1))&&(curr_speed<=k_UP(1))
    if curr_gear>=2
        necessary_gear=2;
    elseif curr_gear<=1
        necessary_gear=1;
    end
elseif (curr_speed>k_UP(1))&&(curr_speed<=k_DN(2))
    necessary_gear=2;
elseif (curr_speed>k_DN(2))&&(curr_speed<=k_UP(2))
    if curr_gear>=3
        necessary_gear=3;
    elseif curr_gear<=2
        necessary_gear=2;
    end
elseif (curr_speed>k_UP(2))&&(curr_speed<=k_DN(3))
    necessary_gear=3;
elseif (curr_speed>k_DN(3))&&(curr_speed<=k_UP(3))
    if curr_gear>=4
        necessary_gear=4;
    elseif curr_gear<=3
        necessary_gear=3;
    end
elseif (curr_speed>k_UP(3))&&(curr_speed<=k_DN(4))
    necessary_gear=4;
elseif (curr_speed>k_DN(4))&&(curr_speed<=k_UP(4))
    if curr_gear>=5
        necessary_gear=5;
    elseif curr_gear<=4
        necessary_gear=4;
    end
elseif curr_speed>k_UP(4)
    necessary_gear=5;
end

end