Wheel.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 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)
28 {
29  obj.clear();
30 
31  if (glCustomVisual_)
32  {
33  obj.insert(glCustomVisual_);
34  }
35  else
36  {
37  auto gl_wheel = mrpt::opengl::CCylinder::Create(
38  0.5 * diameter, 0.5 * diameter, this->width, 15);
39  gl_wheel->setColor_u8(color);
40  gl_wheel->setPose(
41  mrpt::poses::CPose3D(0, 0.5 * width, 0, 0, 0, mrpt::DEG2RAD(90)));
42 
43  auto gl_wheel_frame = mrpt::opengl::CSetOfObjects::Create();
44  gl_wheel_frame->setName("gl_wheel_frame");
45  gl_wheel_frame->insert(gl_wheel);
46  {
47  mrpt::opengl::CSetOfObjects::Ptr gl_xyz =
48  mrpt::opengl::stock_objects::CornerXYZSimple(
49  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  obj.setPose(mrpt::math::TPose3D(x, y, 0.5 * diameter, yaw, 0.0, 0.0));
59 }
60 
62 {
63  ASSERT_(xml_node);
64  // Parse attributes:
65  // <l_wheel pos="0.0 -0.5 [OPTIONAL_ANG]" mass="2.0" width="0.10"
66  // diameter="0.30" />
67  // pos:
68  if (const auto attr = xml_node->first_attribute("pos");
69  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>&
101  physical,
102  [[maybe_unused]] bool childrenOnly)
103 {
104  // nothing to do, already done in getAs3DObject()
105 }
106 
107 std::string Wheel::asString() const
108 {
109  std::stringstream ss;
110  ss << "Pose on vehicle: " << pose().asString() << "\n"
111  << "Diameter: " << diameter << "\n"
112  << "Width: " << width << "\n"
113  << "Mass: " << mass << "\n"
114  << "Iyy: " << Iyy << "\n"
115  << "Kinematic status: phi:" << mrpt::RAD2DEG(phi)
116  << " deg "
117  " w:"
118  << mrpt::RAD2DEG(w) << " deg/s\n";
119 
120  return ss.str();
121 }
This file contains rapidxml parser and DOM implementation.
double width
Definition: Wheel.h:44
void recalcInertia()
Recompute Iyy from mass, diameter and height.
Definition: Wheel.cpp:92
double w
Definition: Wheel.h:98
void loadFromXML(const rapidxml::xml_node< char > *xml_node)
Definition: Wheel.cpp:61
mrpt::img::TColor color
Definition: Wheel.h:52
void getAs3DObject(mrpt::opengl::CSetOfObjects &obj)
Definition: Wheel.cpp:27
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:184
mrpt::math::TPose3D pose() const
Definition: Wheel.h:41
double phi
Definition: Wheel.h:98
std::shared_ptr< mrpt::opengl::CSetOfObjects > glCustomVisual_
Definition: VisualObject.h:75
double x
Definition: Wheel.h:38
double y
Definition: Wheel.h:38
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
double diameter
Definition: Wheel.h:44
double yaw
Definition: Wheel.h:38
std::string asString() const
Definition: Wheel.cpp:107
double Iyy
Definition: Wheel.h:49
Ch * value() const
Definition: rapidxml.hpp:692
double mass
[kg]
Definition: Wheel.h:45
bool parseVisual(const rapidxml::xml_node< char > &rootNode)
Returns true if there is at least one <visual>...</visual> entry.
Wheel(World *world)
Definition: Wheel.cpp:25
const TParameterDefinitions params_
Definition: Wheel.h:61
xml_attribute< Ch > * first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:1025
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:245


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21