ObstacleAvoidingPlatformUnitHelper.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ArmarXObjects::ObstacleAvoidingPlatformUnit
17  * @author Christian R. G. Dreher <c.dreher@kit.edu>
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
25 
26 
27 // Simox
28 #include <SimoxUtility/math.h>
29 #include <VirtualRobot/Nodes/RobotNode.h>
30 
32  armarx::PlatformUnitInterfacePrx platform_unit,
33  VirtualRobot::RobotPtr robot) :
34  ObstacleAvoidingPlatformUnitHelper{platform_unit, robot, Config{}}
35 {
36  // pass
37 }
38 
40  armarx::PlatformUnitInterfacePrx platform_unit,
42  const Config& cfg) :
43  m_platform_unit{platform_unit}, m_robot{robot}, m_cfg{cfg}
44 {
45  // pass
46 }
47 
49 {
50  m_platform_unit->stopPlatform();
51 }
52 
53 void
55  const float target_ori)
56 {
57  setTarget(Target{target_pos, target_ori});
58 }
59 
60 void
62 {
63  m_waypoints.clear();
64  m_waypoints.push_back(target);
65  m_current_waypoint_index = 0;
66  m_waypoint_changed = true;
67 }
68 
71 {
72  return m_waypoints.at(m_current_waypoint_index);
73 }
74 
75 void
77 {
78  // Skip to next waypoint if in proximity.
79  if (isCurrentTargetNear() and not isLastWaypoint())
80  {
81  m_current_waypoint_index++;
82  m_waypoint_changed = true;
83  }
84 
85  // Only call `moveTo` on platform unit if the target actually changed, as `update` is designed
86  // to be called within a high-frequency loop. Prevents constantly invalidating the buffers in
87  // platform unit.
88  if (m_waypoint_changed)
89  {
90  // Use reached-thresholds regardless of whether this is the final target or just a waypoint.
91  // The near-thresholds are more unconstrained as the reached-thresholds, so this helper will
92  // change the position target before the unit will actually reach it, preventing slow downs
93  // or stops.
94  const float pos_thresh = m_cfg.pos_reached_threshold;
95  const float ori_thresh = m_cfg.ori_reached_threshold;
96 
97  // Control.
98  const Target target = getCurrentTarget();
99  m_platform_unit->moveTo(target.pos.x(), target.pos.y(), target.ori, pos_thresh, ori_thresh);
100 
101  m_waypoint_changed = false;
102  }
103 }
104 
105 void
107  float waypoint_ori)
108 {
109  addWaypoint(Target{waypoint_pos, waypoint_ori});
110 }
111 
112 void
113 armarx::ObstacleAvoidingPlatformUnitHelper::setWaypoints(const std::vector<Target>& waypoints)
114 {
115  m_waypoints = waypoints;
116  m_current_waypoint_index = 0;
117  m_waypoint_changed = true;
118 }
119 
120 void
122 {
123  m_waypoints.push_back(waypoint);
124 }
125 
126 bool
128 {
129  return m_current_waypoint_index + 1 >= m_waypoints.size();
130 }
131 
132 bool
134 {
135  return getPositionError() < m_cfg.pos_near_threshold and
136  getOrientationError() < m_cfg.ori_near_threshold;
137 }
138 
139 bool
141 {
142  return getPositionError() < m_cfg.pos_reached_threshold and
143  getOrientationError() < m_cfg.ori_reached_threshold;
144 }
145 
146 bool
148 {
149  return isLastWaypoint() and isCurrentTargetNear();
150 }
151 
152 bool
154 {
155  return isLastWaypoint() and isCurrentTargetReached();
156 }
157 
158 void
160 {
161  m_platform_unit->setMaxVelocities(max_vel, max_angular_vel);
162 }
163 
164 float
166 {
167  const Eigen::Vector2f agent_pos = m_robot->getGlobalPosition().head<2>();
168  return (getCurrentTarget().pos - agent_pos).norm();
169 }
170 
171 float
173 {
174  using namespace simox::math;
175 
176  const float agent_ori =
177  periodic_clamp<float>(mat4f_to_rpy(m_robot->getGlobalPose()).z(), -M_PI, M_PI);
178  return std::fabs(periodic_clamp<float>(getCurrentTarget().ori - agent_ori, -M_PI, M_PI));
179 }
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:668
armarx::ObstacleAvoidingPlatformUnitHelper::setTarget
void setTarget(const Eigen::Vector2f &target_pos, float target_ori)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:54
armarx::ObstacleAvoidingPlatformUnitHelper::isCurrentTargetReached
bool isCurrentTargetReached() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:140
armarx::ObstacleAvoidingPlatformUnitHelper::setWaypoints
void setWaypoints(const std::vector< Target > &waypoints)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:113
armarx::ObstacleAvoidingPlatformUnitHelper::isLastWaypoint
bool isLastWaypoint() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:127
armarx::ObstacleAvoidingPlatformUnitHelper::Config
Definition: ObstacleAvoidingPlatformUnitHelper.h:43
armarx::ObstacleAvoidingPlatformUnitHelper::getOrientationError
float getOrientationError() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:172
armarx::ObstacleAvoidingPlatformUnitHelper::Target
Definition: ObstacleAvoidingPlatformUnitHelper.h:51
armarx::ObstacleAvoidingPlatformUnitHelper::isCurrentTargetNear
bool isCurrentTargetNear() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:133
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::ObstacleAvoidingPlatformUnitHelper::~ObstacleAvoidingPlatformUnitHelper
virtual ~ObstacleAvoidingPlatformUnitHelper()
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:48
armarx::ObstacleAvoidingPlatformUnitHelper
Definition: ObstacleAvoidingPlatformUnitHelper.h:39
armarx::ObstacleAvoidingPlatformUnitHelper::ObstacleAvoidingPlatformUnitHelper
ObstacleAvoidingPlatformUnitHelper(armarx::PlatformUnitInterfacePrx platform_unit, VirtualRobot::RobotPtr robot)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:31
armarx::ObstacleAvoidingPlatformUnitHelper::isFinalTargetReached
bool isFinalTargetReached() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:153
armarx::ObstacleAvoidingPlatformUnitHelper::isFinalTargetNear
bool isFinalTargetNear() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:147
armarx::ObstacleAvoidingPlatformUnitHelper::getPositionError
float getPositionError() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:165
armarx::ObstacleAvoidingPlatformUnitHelper::addWaypoint
void addWaypoint(const Eigen::Vector2f &waypoint_pos, float waypoint_ori)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:106
ObstacleAvoidingPlatformUnitHelper.h
armarx::ObstacleAvoidingPlatformUnitHelper::update
void update()
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:76
armarx::ObstacleAvoidingPlatformUnitHelper::getCurrentTarget
Target getCurrentTarget() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:70
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::ObstacleAvoidingPlatformUnitHelper::setMaxVelocities
void setMaxVelocities(float max_vel, float max_angular_vel)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:159
norm
double norm(const Point &a)
Definition: point.hpp:102