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 
30 
32  armarx::PlatformUnitInterfacePrx platform_unit,
33  VirtualRobot::RobotPtr robot) :
34  ObstacleAvoidingPlatformUnitHelper{platform_unit, robot, Config{}}
35 {
36  // pass
37 }
38 
39 
41  armarx::PlatformUnitInterfacePrx platform_unit,
43  const Config& cfg) :
44  m_platform_unit{platform_unit},
45  m_robot{robot},
46  m_cfg{cfg}
47 {
48  // pass
49 }
50 
51 
53 {
54  m_platform_unit->stopPlatform();
55 }
56 
57 
58 void
60  const Eigen::Vector2f& target_pos,
61  const float target_ori)
62 {
63  setTarget(Target{target_pos, target_ori});
64 }
65 
66 
67 void
69 {
70  m_waypoints.clear();
71  m_waypoints.push_back(target);
72  m_current_waypoint_index = 0;
73  m_waypoint_changed = true;
74 }
75 
76 
79 const
80 {
81  return m_waypoints.at(m_current_waypoint_index);
82 }
83 
84 
85 void
87 {
88  // Skip to next waypoint if in proximity.
89  if (isCurrentTargetNear() and not isLastWaypoint())
90  {
91  m_current_waypoint_index++;
92  m_waypoint_changed = true;
93  }
94 
95  // Only call `moveTo` on platform unit if the target actually changed, as `update` is designed
96  // to be called within a high-frequency loop. Prevents constantly invalidating the buffers in
97  // platform unit.
98  if (m_waypoint_changed)
99  {
100  // Use reached-thresholds regardless of whether this is the final target or just a waypoint.
101  // The near-thresholds are more unconstrained as the reached-thresholds, so this helper will
102  // change the position target before the unit will actually reach it, preventing slow downs
103  // or stops.
104  const float pos_thresh = m_cfg.pos_reached_threshold;
105  const float ori_thresh = m_cfg.ori_reached_threshold;
106 
107  // Control.
108  const Target target = getCurrentTarget();
109  m_platform_unit->moveTo(target.pos.x(), target.pos.y(), target.ori, pos_thresh, ori_thresh);
110 
111  m_waypoint_changed = false;
112  }
113 }
114 
115 
116 void
117 armarx::ObstacleAvoidingPlatformUnitHelper::addWaypoint(const Eigen::Vector2f& waypoint_pos, float waypoint_ori)
118 {
119  addWaypoint(Target{waypoint_pos, waypoint_ori});
120 }
121 
122 
123 void
124 armarx::ObstacleAvoidingPlatformUnitHelper::setWaypoints(const std::vector<Target>& waypoints)
125 {
126  m_waypoints = waypoints;
127  m_current_waypoint_index = 0;
128  m_waypoint_changed = true;
129 }
130 
131 
132 void
134 {
135  m_waypoints.push_back(waypoint);
136 }
137 
138 
139 bool
141 const
142 {
143  return m_current_waypoint_index + 1 >= m_waypoints.size();
144 }
145 
146 
147 bool
149 const
150 {
151  return getPositionError() < m_cfg.pos_near_threshold and getOrientationError() < m_cfg.ori_near_threshold;
152 }
153 
154 
155 bool
157 const
158 {
159  return getPositionError() < m_cfg.pos_reached_threshold and getOrientationError() < m_cfg.ori_reached_threshold;
160 }
161 
162 
163 bool
165 const
166 {
167  return isLastWaypoint() and isCurrentTargetNear();
168 }
169 
170 
171 bool
173 const
174 {
175  return isLastWaypoint() and isCurrentTargetReached();
176 }
177 
178 
179 void
181 {
182  m_platform_unit->setMaxVelocities(max_vel, max_angular_vel);
183 }
184 
185 
186 float
188 const
189 {
190  const Eigen::Vector2f agent_pos = m_robot->getGlobalPosition().head<2>();
191  return (getCurrentTarget().pos - agent_pos).norm();
192 }
193 
194 
195 float
197 const
198 {
199  using namespace simox::math;
200 
201  const float agent_ori =
202  periodic_clamp<float>(mat4f_to_rpy(m_robot->getGlobalPose()).z(), -M_PI, M_PI);
203  return std::fabs(periodic_clamp<float>(getCurrentTarget().ori - agent_ori, -M_PI, M_PI));
204 }
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::ObstacleAvoidingPlatformUnitHelper::setTarget
void setTarget(const Eigen::Vector2f &target_pos, float target_ori)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:59
armarx::ObstacleAvoidingPlatformUnitHelper::isCurrentTargetReached
bool isCurrentTargetReached() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:156
armarx::ObstacleAvoidingPlatformUnitHelper::setWaypoints
void setWaypoints(const std::vector< Target > &waypoints)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:124
armarx::ObstacleAvoidingPlatformUnitHelper::isLastWaypoint
bool isLastWaypoint() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:140
armarx::ObstacleAvoidingPlatformUnitHelper::Config
Definition: ObstacleAvoidingPlatformUnitHelper.h:45
armarx::ObstacleAvoidingPlatformUnitHelper::getOrientationError
float getOrientationError() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:196
armarx::ObstacleAvoidingPlatformUnitHelper::Target
Definition: ObstacleAvoidingPlatformUnitHelper.h:53
armarx::ObstacleAvoidingPlatformUnitHelper::isCurrentTargetNear
bool isCurrentTargetNear() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:148
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::ObstacleAvoidingPlatformUnitHelper::~ObstacleAvoidingPlatformUnitHelper
virtual ~ObstacleAvoidingPlatformUnitHelper()
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:52
armarx::ObstacleAvoidingPlatformUnitHelper
Definition: ObstacleAvoidingPlatformUnitHelper.h:40
armarx::ObstacleAvoidingPlatformUnitHelper::ObstacleAvoidingPlatformUnitHelper
ObstacleAvoidingPlatformUnitHelper(armarx::PlatformUnitInterfacePrx platform_unit, VirtualRobot::RobotPtr robot)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:31
armarx::ObstacleAvoidingPlatformUnitHelper::isFinalTargetReached
bool isFinalTargetReached() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:172
armarx::ObstacleAvoidingPlatformUnitHelper::isFinalTargetNear
bool isFinalTargetNear() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:164
armarx::ObstacleAvoidingPlatformUnitHelper::getPositionError
float getPositionError() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:187
armarx::ObstacleAvoidingPlatformUnitHelper::addWaypoint
void addWaypoint(const Eigen::Vector2f &waypoint_pos, float waypoint_ori)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:117
ObstacleAvoidingPlatformUnitHelper.h
armarx::ObstacleAvoidingPlatformUnitHelper::update
void update()
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:86
armarx::ObstacleAvoidingPlatformUnitHelper::getCurrentTarget
Target getCurrentTarget() const
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:78
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::ObstacleAvoidingPlatformUnitHelper::setMaxVelocities
void setMaxVelocities(float max_vel, float max_angular_vel)
Definition: ObstacleAvoidingPlatformUnitHelper.cpp:180
norm
double norm(const Point &a)
Definition: point.hpp:94