DefaultFriction.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2024 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
11 #include <mvsim/VehicleBase.h>
12 #include <mvsim/World.h>
13 
14 #include <rapidxml.hpp>
15 
16 #include "xml_utils.h"
17 
18 using namespace mvsim;
19 
21  : FrictionBase(my_vehicle), mu_(0.8), C_damping_(1.0)
22 {
23  // Sanity: we can tolerate node==nullptr (=> means use default params).
24  if (node && 0 != strcmp(node->name(), "friction"))
25  throw std::runtime_error("<friction>...</friction> XML node was expected!!");
26 
27  // Parse XML params:
29 }
30 
31 // See docs in base class.
32 mrpt::math::TVector2D DefaultFriction::evaluate_friction(
33  const FrictionBase::TFrictionInput& input) const
34 {
35  // Rotate wheel velocity vector from veh. frame => wheel frame
36  const mrpt::poses::CPose2D wRot(0, 0, input.wheel.yaw);
37 
38  // Velocity of the wheel cog in the frame of the wheel itself:
39  const mrpt::math::TVector2D vel_w = wRot.inverseComposePoint(input.wheelCogLocalVel);
40 
41  // Action/Reaction, slippage, etc:
42  // --------------------------------------
43  const double mu = mu_;
44  const double gravity = myVehicle_.parent()->get_gravity();
45  const double partial_mass = input.weight / gravity + input.wheel.mass;
46  const double max_friction = mu * partial_mass * gravity;
47 
48  // 1) Lateral friction (decoupled sub-problem)
49  // --------------------------------------------
50  double wheel_lat_friction = 0.0; // direction: +y local wrt the wheel
51  {
52  // Impulse required to step the lateral slippage:
53  wheel_lat_friction = -vel_w.y * partial_mass / input.context.dt;
54 
55  wheel_lat_friction = b2Clamp(wheel_lat_friction, -max_friction, max_friction);
56  }
57 
58  // 2) Longitudinal friction (decoupled sub-problem)
59  // -------------------------------------------------
60  double wheel_long_friction = 0.0; // direction: +x local wrt the wheel
61 
62  // (eq. 1)==> desired impulse in wheel spinning speed.
63  // wheel_C_lon_vel = vel_w.x - input.wheel.w * 0.5*input.wheel.diameter
64 
65  // It should be = 0 for no slippage (nonholonomic constraint): find out
66  // required wheel \omega:case '4':
67  const double R = 0.5 * input.wheel.diameter; // Wheel radius
68  const double lon_constraint_desired_wheel_w = vel_w.x / R;
69 
70  const double desired_wheel_w_impulse = (lon_constraint_desired_wheel_w - input.wheel.getW());
71 
72  const double desired_wheel_alpha = desired_wheel_w_impulse / input.context.dt;
73 
74  // (eq. 3)==> Find out F_r
75  // Iyy_w * \Delta\omega_w = dt*\tau- R*dt*Fri -C_damp * \omega_w * dt
76  // "Damping" / internal friction of the wheel's shaft, etc.
77  const double C_damping = C_damping_;
78  // const mrpt::math::TPoint2D wheel_damping(- C_damping *
79  // input.wheel_speed.x, 0.0);
80 
81  const double I_yy = input.wheel.Iyy;
82  double F_friction_lon =
83  (input.motorTorque - I_yy * desired_wheel_alpha - C_damping * input.wheel.getW()) / R;
84 
85  // Slippage: The friction with the ground is not infinite:
86  F_friction_lon = b2Clamp(F_friction_lon, -max_friction, max_friction);
87 
88  // Recalc wheel ang. velocity impulse with this reduced force:
89  const double actual_wheel_alpha =
90  (input.motorTorque - R * F_friction_lon - C_damping * input.wheel.getW()) / I_yy;
91 
92  // Apply impulse to wheel's spinning:
93  input.wheel.setW(input.wheel.getW() + actual_wheel_alpha * input.context.dt);
94 
95  wheel_long_friction = F_friction_lon;
96 
97  // Resultant force: In local (x,y) coordinates (Newtons) wrt the Wheel
98  // -----------------------------------------------------------------------
99  const mrpt::math::TPoint2D result_force_wrt_wheel(wheel_long_friction, wheel_lat_friction);
100 
101  // Rotate to put: Wheel frame ==> vehicle local framework:
102  mrpt::math::TVector2D res;
103  wRot.composePoint(result_force_wrt_wheel, res);
104  return res;
105 }
move-object-example.R
int R
Definition: move-object-example.py:41
mvsim::VisualObject::parent
World * parent()
Definition: VisualObject.h:51
mvsim::DefaultFriction::mu_
double mu_
friction coeficient (non-dimensional)
Definition: DefaultFriction.h:35
mvsim
Definition: Client.h:21
mvsim::FrictionBase::TFrictionInput::context
const TSimulContext & context
Definition: FrictionBase.h:40
mvsim::FrictionBase
Definition: FrictionBase.h:26
mvsim::Wheel::yaw
double yaw
Definition: Wheel.h:40
mvsim::parse_xmlnode_children_as_param
void parse_xmlnode_children_as_param(const rapidxml::xml_node< char > &xml_node, const TParameterDefinitions &params, const std::map< std::string, std::string > &variableNamesValues={}, const char *functionNameContext="", mrpt::system::COutputLogger *logger=nullptr)
Definition: xml_utils.cpp:215
World.h
mvsim::World::get_gravity
double get_gravity() const
Definition: World.h:154
mvsim::FrictionBase::TFrictionInput
Definition: FrictionBase.h:38
mvsim::FrictionBase::myVehicle_
VehicleBase & myVehicle_
Definition: FrictionBase.h:71
xml_utils.h
mvsim::TSimulContext::dt
double dt
timestep
Definition: basic_types.h:63
VehicleBase.h
mvsim::Wheel::diameter
double diameter
Definition: Wheel.h:46
mvsim::Wheel::Iyy
double Iyy
Definition: Wheel.h:51
b2Clamp
T b2Clamp(T a, T low, T high)
Definition: b2_math.h:648
mvsim::DefaultFriction::C_damping_
double C_damping_
For wheels "internal friction" (N*m*s/rad)
Definition: DefaultFriction.h:36
mvsim::FrictionBase::world_
World * world_
Definition: FrictionBase.h:70
mvsim::DefaultFriction::DefaultFriction
DefaultFriction(VehicleBase &my_vehicle, const rapidxml::xml_node< char > *node)
Definition: DefaultFriction.cpp:20
mvsim::DefaultFriction::evaluate_friction
virtual mrpt::math::TVector2D evaluate_friction(const FrictionBase::TFrictionInput &input) const override
Definition: DefaultFriction.cpp:32
mvsim::FrictionBase::TFrictionInput::wheel
Wheel & wheel
Definition: FrictionBase.h:41
DefaultFriction.h
mvsim::VehicleBase
Definition: VehicleBase.h:44
rapidxml::xml_node< char >
mvsim::World::user_defined_variables
const std::map< std::string, std::string > & user_defined_variables() const
Definition: World.h:390
mvsim::Wheel::mass
double mass
[kg]
Definition: Wheel.h:47
mvsim::FrictionBase::TFrictionInput::weight
double weight
Definition: FrictionBase.h:46
mvsim::FrictionBase::TFrictionInput::wheelCogLocalVel
mrpt::math::TVector2D wheelCogLocalVel
Definition: FrictionBase.h:54
rapidxml.hpp
mvsim::FrictionBase::TFrictionInput::motorTorque
double motorTorque
Definition: FrictionBase.h:50
mvsim::DefaultFriction::params_
const TParameterDefinitions params_
Definition: DefaultFriction.h:39


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:07