function [Rc, flag, Rb, Rr, Rg, Ro, Rall] = Rall_fun(Type, bw, p, zw, Ww, teta, dw, soil, fr, n)
% RALL_FUN Compute total motion resistance for a wheel on deformable soil.
%
% Inputs
%   Type  - wheel type (kept for interface compatibility)
%   bw    - wheel width [m]
%   p     - average ground pressure [Pa] (kept for interface compatibility)
%   zw    - sinkage [m]
%   Ww    - wheel load [N]
%   teta  - terrain inclination angle [rad for sin, deg for cosd-based terms as in original code]
%   dw    - wheel diameter [m]
%   soil  - structure containing soil parameters
%   fr    - rolling resistance coefficient [-]
%   n     - sinkage exponent (kept for interface compatibility)
%
% Outputs
%   Rc    - soil compaction resistance [N]
%   flag  - status flag
%   Rb    - bulldozing resistance [N]
%   Rr    - rolling resistance [N]
%   Rg    - gravitational resistance [N]
%   Ro    - obstacle resistance [N]
%   Rall  - total motion resistance [N]

% Soil compaction resistance [Bekker]
Csig = soil.kc ./ bw + soil.k_phi; %#ok<NASGU>
flag = false;

% Soil compaction resistance for rigid wheel
Rc_rw = ((3 .* Ww .* cosd(teta) ./ sqrt(dw)) .^ ((2 .* soil.n + 2) ...
    ./ (2 .* soil.n + 1))) ./ ((3 - soil.n) .^ ((2 .* soil.n + 2) ...
    ./ (2 .* soil.n + 1)) .* (soil.n + 1) .* (soil.kc + bw .* soil.k_phi) ...
    .^ (1 ./ (2 * soil.n + 1)));
Rc = Rc_rw;

% Bulldozing resistance [Bekker]
alfa = acos(1 - 2 * zw / dw);            % angle of approach [rad]
lr = zw * tan(pi / 4 - soil.phi / 2)^2;  % rupture distance [m]

Rb = ((bw * sin(alfa + soil.phi)) / (2 * sin(alfa) * cos(soil.phi))) * ...
    (2 * soil.c * soil.Kc * zw + soil.gamma * soil.Kgamma * zw^2) + ...
    pi * soil.gamma * lr^3 * (90 - soil.phi * 180 / pi) / 540 + ...
    pi * soil.c * lr^2 / 180 + ...
    soil.c * lr^2 * tan(pi / 4 + soil.phi / 2);

% Rolling resistance
Rr = fr .* Ww;

% Gravitational resistance
Rg = Ww .* sin(teta);

% Obstacle resistance [Wong]
Ro = 0;

% Total motion resistance
Rall = Rc + Rb + Rr + Rg + Ro;

end
