NJointHolonomicPlatformGlobalPositionController.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX
19 * @author Markus Grotz (markus.grotz at kit dot edu)
20 * @date 2019
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
25
26#include <cmath>
27
28#include <SimoxUtility/math/convert/mat3f_to_rpy.h>
29#include <SimoxUtility/math/convert/mat4f_to_rpy.h>
30#include <SimoxUtility/math/periodic/periodic_clamp.h>
31#include <VirtualRobot/Robot.h>
32
34
37
38namespace armarx
39{
42 "NJointHolonomicPlatformGlobalPositionController");
43
46 RobotUnit*,
47 const NJointHolonomicPlatformGlobalPositionControllerConfigPtr& cfg,
49 pid(cfg->p, cfg->i, cfg->d, cfg->maxVelocity, cfg->maxAcceleration),
50 opid(cfg->p_rot,
51 cfg->i_rot,
52 cfg->d_rot,
53 cfg->maxRotationVelocity,
54 cfg->maxRotationAcceleration,
55 true)
56
57 {
58 const SensorValueBase* sv =
61 target = useControlTarget(cfg->platformName, ControlModes::HolonomicPlatformVelocity)
63
64
65 pid.threadSafe = false;
66 pid.preallocate(2);
67
68 opid.threadSafe = false;
69 }
70
71 void
73 const IceUtil::Time& currentTime,
74 const IceUtil::Time& timeSinceLastIteration)
75 {
76 const auto global_T_robot = sv->global_T_root;
77
78 const Eigen::Vector3f rpy = simox::math::mat4f_to_rpy(global_T_robot);
79 const float global_orientation = rpy.z();
80 const Eigen::Vector2f global_P_robot = global_T_robot.block<2, 1>(0, 3);
81
82 if (rtGetControlStruct().newTargetSet)
83 {
84 pid.reset();
85 opid.reset();
86
89
90 isTargetSet = true;
91 }
92
93
94 // if ((sv->lastUpdate + IceUtil::Time::seconds(2)) < currentTime)
95 // {
96 // ARMARX_RT_LOGF_WARNING << deactivateSpam(0.5) << "Waiting for global pos";
97
98 // target->velocityX = 0;
99 // target->velocityY = 0;
100
101 // target->velocityRotation = 0;
102 // isAborted = true;
103
104 // return;
105 // }
106
107 if (not isTargetSet)
108 {
109 target->velocityX = 0;
110 target->velocityY = 0;
111 target->velocityRotation = 0;
112
113 return;
114 }
115
116 if (not sv->isAvailable())
117 {
118 // ARMARX_RT_LOGF_INFO << deactivateSpam(1) << "global pose not available";
119 target->velocityX = 0;
120 target->velocityY = 0;
121 target->velocityRotation = 0;
122
123 return;
124 }
125
126 const float measuredOrientation = global_orientation;
127
128 pid.update(
129 timeSinceLastIteration.toSecondsDouble(), global_P_robot, rtGetControlStruct().target);
130 opid.update(timeSinceLastIteration.toSecondsDouble(),
131 static_cast<double>(measuredOrientation),
132 rtGetControlStruct().targetOrientation);
133
134 const Eigen::Rotation2Df global_R_local(-measuredOrientation);
135 const Eigen::Vector2f velocities = global_R_local * pid.getControlValue();
136
137 target->velocityX = velocities.x();
138 target->velocityY = velocities.y();
139 target->velocityRotation = static_cast<float>(opid.getControlValue());
140 }
141
142 void
144 {
145 target->velocityX = 0;
146 target->velocityY = 0;
147 target->velocityRotation = 0;
148 }
149
150 void
152 float y,
153 float yaw,
154 float translationAccuracy,
155 float rotationAccuracy)
156 {
157 // todo do we really need a recursive mutex?
158
159 std::lock_guard<std::recursive_mutex> lock(controlDataMutex);
160
163 simox::math::periodic_clamp(static_cast<double>(yaw), -M_PI, M_PI);
164
165 getWriterControlStruct().translationAccuracy = translationAccuracy;
166 getWriterControlStruct().rotationAccuracy = rotationAccuracy;
167
170 }
171
172} // namespace armarx
#define M_PI
Definition MathTools.h:17
Brief description of class ControlTargetHolonomicPlatformVelocity.
const SensorValueBase * useSensorValue(const std::string &sensorDeviceName) const
Get a const ptr to the given SensorDevice's SensorValue.
ControlTargetBase * useControlTarget(const std::string &deviceName, const std::string &controlMode)
Declares to calculate the ControlTarget for the given ControlDevice in the given ControlMode when rtR...
void setTarget(float x, float y, float yaw, float translationAccuracy, float rotationAccuracy)
NJointHolonomicPlatformGlobalPositionController(RobotUnit *robotUnit, const NJointHolonomicPlatformGlobalPositionControllerConfigPtr &cfg, const VirtualRobot::RobotPtr &)
virtual void rtPreActivateController() override
This function is called before the controller is activated.
virtual void rtRun(const IceUtil::Time &, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
The RobotUnit class manages a robot and its controllers.
Definition RobotUnit.h:192
The SensorValueBase class.
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
NJointControllerRegistration< NJointHolonomicPlatformGlobalPositionController > registrationNJointHolonomicPlatformGlobalPositionController("NJointHolonomicPlatformGlobalPositionController")