Main MRPT website > C++ reference for MRPT 1.9.9
CHolonomicVFF.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "nav-precomp.h" // Precomp header
11 
13 #include <mrpt/utils/CStream.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::utils;
17 using namespace std;
18 
19 using namespace mrpt::nav;
20 
24 
25 /*---------------------------------------------------------------
26  initialize
27  ---------------------------------------------------------------*/
28 CHolonomicVFF::CHolonomicVFF(const mrpt::utils::CConfigFileBase* INI_FILE)
30 {
31  if (INI_FILE != nullptr) initialize(*INI_FILE);
32 }
33 
34 void CHolonomicVFF::initialize(const mrpt::utils::CConfigFileBase& INI_FILE)
35 {
36  options.loadFromConfigFile(INI_FILE, getConfigFileSectionName());
37 }
38 void CHolonomicVFF::saveConfigFile(mrpt::utils::CConfigFileBase& c) const
39 {
40  options.saveToConfigFile(c, getConfigFileSectionName());
41 }
42 
43 /*---------------------------------------------------------------
44  navigate
45  ---------------------------------------------------------------*/
46 void CHolonomicVFF::navigate(const NavInput& ni, NavOutput& no)
47 {
48  const auto ptg = getAssociatedPTG();
49  const double ptg_ref_dist = ptg ? ptg->getRefDistance() : 1.0;
50 
51  // Create a log record for returning data.
52  no.logRecord = mrpt::make_aligned_shared<CLogFileRecord_VFF>();
53 
54  // Forces vector:
55  mrpt::math::TPoint2D resultantForce(0, 0), instantaneousForce(0, 0);
56 
57  // Obstacles:
58  {
59  const size_t n = ni.obstacles.size();
60  const double inc_ang = 2 * M_PI / n;
61  double ang = -M_PI + 0.5 * inc_ang;
62  for (size_t i = 0; i < n; i++, ang += inc_ang)
63  {
64  // Compute force strength:
65  // const double mod = exp(- obstacles[i] );
66  const double mod = std::min(1e6, 1.0 / ni.obstacles[i]);
67 
68  // Add repulsive force:
69  instantaneousForce.x = -cos(ang) * mod;
70  instantaneousForce.y = -sin(ang) * mod;
71  resultantForce += instantaneousForce;
72  }
73  }
74 
75  const double obstcl_weight = 20.0 / ni.obstacles.size();
76  resultantForce *= obstcl_weight;
77 
78  const double obstacleNearnessFactor =
79  std::min(1.0, 6.0 / resultantForce.norm());
80 
81  // Target:
82  ASSERT_(!ni.targets.empty());
83  const auto trg = *ni.targets.rbegin();
84 
85  const double ang = atan2(trg.y, trg.x);
86  const double mod = options.TARGET_ATTRACTIVE_FORCE;
87  resultantForce += mrpt::math::TPoint2D(cos(ang) * mod, sin(ang) * mod);
88 
89  // Result:
90  no.desiredDirection = (resultantForce.y == 0 && resultantForce.x == 0)
91  ? 0
92  : atan2(resultantForce.y, resultantForce.x);
93 
94  // Speed control: Reduction factors
95  // ---------------------------------------------
96  if (m_enableApproachTargetSlowDown)
97  {
98  const double targetNearnessFactor = std::min(
99  1.0, trg.norm() /
100  (options.TARGET_SLOW_APPROACHING_DISTANCE / ptg_ref_dist));
101  no.desiredSpeed =
102  ni.maxRobotSpeed *
103  std::min(obstacleNearnessFactor, targetNearnessFactor);
104  }
105 }
106 
107 void CHolonomicVFF::writeToStream(mrpt::utils::CStream& out, int* version) const
108 {
109  if (version)
110  *version = 0;
111  else
112  {
113  out << options.TARGET_ATTRACTIVE_FORCE
114  << options.TARGET_SLOW_APPROACHING_DISTANCE;
115  }
116 }
117 void CHolonomicVFF::readFromStream(mrpt::utils::CStream& in, int version)
118 {
119  switch (version)
120  {
121  case 0:
122  {
123  in >> options.TARGET_ATTRACTIVE_FORCE >>
124  options.TARGET_SLOW_APPROACHING_DISTANCE;
125  }
126  break;
127  default:
129  };
130 }
131 
132 /*---------------------------------------------------------------
133  writeToStream
134  Implements the writing to a CStream capability of
135  CSerializable objects
136  ---------------------------------------------------------------*/
137 void CLogFileRecord_VFF::writeToStream(
138  mrpt::utils::CStream& out, int* version) const
139 {
140  MRPT_UNUSED_PARAM(out);
141  if (version)
142  *version = 0;
143  else
144  {
145  }
146 }
147 
148 /*---------------------------------------------------------------
149  readFromStream
150  ---------------------------------------------------------------*/
151 void CLogFileRecord_VFF::readFromStream(mrpt::utils::CStream& in, int version)
152 {
154  switch (version)
155  {
156  case 0:
157  {
158  }
159  break;
160  default:
162  };
163 }
164 
165 /*---------------------------------------------------------------
166  TOptions
167  ---------------------------------------------------------------*/
168 CHolonomicVFF::TOptions::TOptions()
169  : TARGET_SLOW_APPROACHING_DISTANCE(0.10), TARGET_ATTRACTIVE_FORCE(20.0)
170 {
171 }
172 
174  const mrpt::utils::CConfigFileBase& source, const std::string& section)
175 {
176  MRPT_START
177 
178  // Load from config text:
180  TARGET_SLOW_APPROACHING_DISTANCE, double, source, section);
181  MRPT_LOAD_CONFIG_VAR(TARGET_ATTRACTIVE_FORCE, double, source, section);
182 
183  MRPT_END
184 }
185 
188 {
189  MRPT_START;
190 
192  TARGET_SLOW_APPROACHING_DISTANCE, "For stopping gradually");
194  TARGET_ATTRACTIVE_FORCE,
195  "Dimension-less (may have to be tuned depending on the density of "
196  "obstacle sampling)");
197 
198  MRPT_END;
199 }
A holonomic reactive navigation method, based on Virtual Force Fields (VFF).
Definition: CHolonomicVFF.h:50
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define min(a, b)
void saveToConfigFile(mrpt::utils::CConfigFileBase &cfg, const std::string &section) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
A class for storing extra information about the execution of CHolonomicVFF navigation.
Definition: CHolonomicVFF.h:28
std::vector< double > obstacles
Distance to obstacles in polar coordinates, relative to the robot.
GLenum GLsizei n
Definition: glext.h:5074
A base class for holonomic reactive navigation methods.
STL namespace.
#define M_PI
Definition: bits.h:92
GLdouble s
Definition: glext.h:3676
std::vector< mrpt::math::TPoint2D > targets
Relative location (x,y) of target point(s).
This class allows loading and storing values and vectors of different types from a configuration text...
double maxRobotSpeed
Maximum robot speed, in the same units than obstacles, per second.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
#define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment)
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
const GLubyte * c
Definition: glext.h:6313
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
A base class for log records for different holonomic navigation methods.
double desiredDirection
The desired motion direction, in the range [-PI, PI].
CHolonomicLogFileRecord::Ptr logRecord
The navigation method will create a log record and store it here via a smart pointer.
GLsizei const GLchar ** string
Definition: glext.h:4101
IMPLEMENTS_SERIALIZABLE(CHolonomicVFF, CAbstractHolonomicReactiveMethod, mrpt::nav) CHolonomicVFF
double desiredSpeed
The desired motion speed in that direction, from 0 up to NavInput::maxRobotSpeed. ...
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLuint in
Definition: glext.h:7274
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
#define ASSERT_(f)
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
Input parameters for CAbstractHolonomicReactiveMethod::navigate()
#define MRPT_LOAD_CONFIG_VAR( variableName, variableType, configFileObject, sectionNameStr)
An useful macro for loading variables stored in a INI-like file under a key with the same name that t...
Lightweight 2D point.
Output for CAbstractHolonomicReactiveMethod::navigate()



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019