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,
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
52
53void
55 const float target_ori)
56{
57 setTarget(Target{target_pos, target_ori});
58}
59
60void
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
75void
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
105void
107 float waypoint_ori)
108{
109 addWaypoint(Target{waypoint_pos, waypoint_ori});
110}
111
112void
114{
115 m_waypoints = waypoints;
116 m_current_waypoint_index = 0;
117 m_waypoint_changed = true;
118}
119
120void
122{
123 m_waypoints.push_back(waypoint);
124}
125
126bool
128{
129 return m_current_waypoint_index + 1 >= m_waypoints.size();
130}
131
132bool
134{
135 return getPositionError() < m_cfg.pos_near_threshold and
136 getOrientationError() < m_cfg.ori_near_threshold;
137}
138
139bool
141{
142 return getPositionError() < m_cfg.pos_reached_threshold and
143 getOrientationError() < m_cfg.ori_reached_threshold;
144}
145
146bool
151
152bool
157
158void
160{
161 m_platform_unit->setMaxVelocities(max_vel, max_angular_vel);
162}
163
164float
166{
167 const Eigen::Vector2f agent_pos = m_robot->getGlobalPosition().head<2>();
168 return (getCurrentTarget().pos - agent_pos).norm();
169}
170
171float
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}
#define M_PI
Definition MathTools.h:17
void setTarget(const Eigen::Vector2f &target_pos, float target_ori)
void setMaxVelocities(float max_vel, float max_angular_vel)
ObstacleAvoidingPlatformUnitHelper(armarx::PlatformUnitInterfacePrx platform_unit, VirtualRobot::RobotPtr robot)
void setWaypoints(const std::vector< Target > &waypoints)
void addWaypoint(const Eigen::Vector2f &waypoint_pos, float waypoint_ori)
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19