Wheel.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 
10 #include <mrpt/img/TColor.h>
11 #include <mrpt/opengl/CCylinder.h>
12 #include <mrpt/opengl/CSetOfObjects.h>
13 #include <mrpt/opengl/stock_objects.h>
14 #include <mrpt/version.h>
15 #include <mvsim/Wheel.h>
16 #include <mvsim/World.h>
17 
18 #include <rapidxml.hpp>
19 
20 #include "xml_utils.h"
21 
22 using namespace mvsim;
23 using namespace std;
24 
26 
27 void Wheel::getAs3DObject(mrpt::opengl::CSetOfObjects& obj, bool isPhysicalScene)
28 {
29  obj.clear();
30 
31  if (glCustomVisual_)
32  {
33  obj.insert(glCustomVisual_);
34  }
35  else
36  {
37  auto gl_wheel =
38  mrpt::opengl::CCylinder::Create(0.5 * diameter, 0.5 * diameter, this->width, 15);
39  gl_wheel->setColor_u8(color);
40  gl_wheel->setPose(mrpt::poses::CPose3D(0, 0.5 * width, 0, 0, 0, mrpt::DEG2RAD(90)));
41 
42  if (!isPhysicalScene)
43  {
44  auto gl_wheel_frame = mrpt::opengl::CSetOfObjects::Create();
45  gl_wheel_frame->setName("gl_wheel_frame");
46  gl_wheel_frame->insert(gl_wheel);
47  {
48  mrpt::opengl::CSetOfObjects::Ptr gl_xyz =
49  mrpt::opengl::stock_objects::CornerXYZSimple(0.9 * diameter, 2.0);
50 #if MRPT_VERSION >= 0x270
51  gl_xyz->castShadows(false);
52 #endif
53  gl_wheel_frame->insert(gl_xyz);
54  }
55  obj.insert(gl_wheel_frame);
56  }
57  }
58 
59  obj.setPose(mrpt::math::TPose3D(x, y, 0.5 * diameter, yaw, 0.0, 0.0));
60 }
61 
63 {
64  ASSERT_(xml_node);
65  // Parse attributes:
66  // <l_wheel pos="0.0 -0.5 [OPTIONAL_ANG]" mass="2.0" width="0.10"
67  // diameter="0.30" />
68  // pos:
69  if (const auto attr = xml_node->first_attribute("pos"); attr && attr->value())
70  {
71  const std::string sAttr = attr->value();
72  mrpt::math::TPose2D v = parseXYPHI(sAttr, true);
73  this->x = v.x;
74  this->y = v.y;
75  this->yaw = v.phi;
76  }
77 
78  // Detect if inertia is manually set:
79  const double INERTIA_NOT_SET = -1.;
80  this->Iyy = INERTIA_NOT_SET;
81 
82  parse_xmlnode_attribs(*xml_node, params_, {}, "[Wheel]");
83 
84  // If not manually overrided, calc automatically:
85  if (Iyy == INERTIA_NOT_SET) this->recalcInertia();
86 
87  // parse custom visual stuff:
88  this->parseVisual(*xml_node);
89 }
90 
91 // Recompute Iyy from mass, diameter and height.
93 {
94  // Iyy = m*r^2 / 2
95  Iyy = mass * (0.25 * diameter * diameter) * 0.5;
96 }
97 
99  [[maybe_unused]] const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& viz,
100  [[maybe_unused]] const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& physical,
101  [[maybe_unused]] bool childrenOnly)
102 {
103  // nothing to do, already done in getAs3DObject()
104 }
105 
106 std::string Wheel::asString() const
107 {
108  std::stringstream ss;
109  ss << "Pose on vehicle: " << pose().asString() << "\n"
110  << "Diameter: " << diameter << "\n"
111  << "Width: " << width << "\n"
112  << "Mass: " << mass << "\n"
113  << "Iyy: " << Iyy << "\n"
114  << "Kinematic status: phi:" << mrpt::RAD2DEG(phi)
115  << " deg "
116  " w:"
117  << mrpt::RAD2DEG(w) << " deg/s\n";
118 
119  return ss.str();
120 }
mvsim
Definition: Client.h:21
mvsim::Wheel::yaw
double yaw
Definition: Wheel.h:40
World.h
mvsim::Wheel::w
double w
Definition: Wheel.h:93
mvsim::Wheel::internalGuiUpdate
void internalGuiUpdate(const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &viz, const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &physical, bool childrenOnly) override
Definition: Wheel.cpp:98
mvsim::Wheel::x
double x
Definition: Wheel.h:40
xml_utils.h
mvsim::Wheel::params_
const TParameterDefinitions params_
Definition: Wheel.h:63
mvsim::Wheel::recalcInertia
void recalcInertia()
Recompute Iyy from mass, diameter and height.
Definition: Wheel.cpp:92
mvsim::Wheel::diameter
double diameter
Definition: Wheel.h:46
mvsim::Wheel::Iyy
double Iyy
Definition: Wheel.h:51
mvsim::Wheel::color
mrpt::img::TColor color
Definition: Wheel.h:54
mvsim::Wheel::getAs3DObject
void getAs3DObject(mrpt::opengl::CSetOfObjects &obj, bool isPhysicalScene)
Definition: Wheel.cpp:27
mvsim::Wheel::Wheel
Wheel(World *world)
Definition: Wheel.cpp:25
mvsim::Wheel::width
double width
Definition: Wheel.h:46
mvsim::Wheel::loadFromXML
void loadFromXML(const rapidxml::xml_node< char > *xml_node)
Definition: Wheel.cpp:62
mvsim::World
Definition: World.h:82
rapidxml::xml_node< char >
mvsim::Wheel::pose
mrpt::math::TPose3D pose() const
Definition: Wheel.h:43
mvsim::Wheel::mass
double mass
[kg]
Definition: Wheel.h:47
std
Wheel.h
mvsim::parseXYPHI
mrpt::math::TPose2D parseXYPHI(const std::string &s, bool allow_missing_angle=false, double default_angle_radians=0.0, const std::map< std::string, std::string > &variableNamesValues={})
Definition: xml_utils.cpp:235
mvsim::Wheel::y
double y
Definition: Wheel.h:40
mvsim::Wheel::phi
double phi
Definition: Wheel.h:93
mvsim::VisualObject::parseVisual
bool parseVisual(const rapidxml::xml_node< char > &rootNode)
Returns true if there is at least one <visual>...</visual> entry.
Definition: VisualObject.cpp:155
mvsim::VisualObject::glCustomVisual_
std::shared_ptr< mrpt::opengl::CSetOfObjects > glCustomVisual_
Definition: VisualObject.h:77
rapidxml.hpp
mvsim::parse_xmlnode_attribs
void parse_xmlnode_attribs(const rapidxml::xml_node< char > &xml_node, const TParameterDefinitions &params, const std::map< std::string, std::string > &variableNamesValues={}, const char *functionNameContext="")
Definition: xml_utils.cpp:182
rapidxml::xml_node::first_attribute
xml_attribute< Ch > * first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:1025
mvsim::Wheel::asString
std::string asString() const
Definition: Wheel.cpp:106
mvsim::VisualObject
Definition: VisualObject.h:35


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