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 
33 
36 
37 namespace armarx
38 {
39  NJointControllerRegistration<NJointHolonomicPlatformGlobalPositionController>
41  "NJointHolonomicPlatformGlobalPositionController");
42 
45  RobotUnit*,
46  const NJointHolonomicPlatformGlobalPositionControllerConfigPtr& cfg,
47  const VirtualRobot::RobotPtr&) :
48  pid(cfg->p, cfg->i, cfg->d, cfg->maxVelocity, cfg->maxAcceleration),
49  opid(cfg->p_rot,
50  cfg->i_rot,
51  cfg->d_rot,
52  cfg->maxRotationVelocity,
53  cfg->maxRotationAcceleration,
54  true)
55 
56  {
57  const SensorValueBase* sv =
60  target = useControlTarget(cfg->platformName, ControlModes::HolonomicPlatformVelocity)
62 
63 
64  pid.threadSafe = false;
65  pid.preallocate(2);
66 
67  opid.threadSafe = false;
68  }
69 
70  void
72  const IceUtil::Time& currentTime,
73  const IceUtil::Time& timeSinceLastIteration)
74  {
75  const auto global_T_robot = sv->global_T_root;
76 
77  const Eigen::Vector3f rpy = simox::math::mat4f_to_rpy(global_T_robot);
78  const float global_orientation = rpy.z();
79  const Eigen::Vector2f global_P_robot = global_T_robot.block<2, 1>(0, 3);
80 
82  {
83  pid.reset();
84  opid.reset();
85 
88 
89  isTargetSet = true;
90  }
91 
92 
93  // if ((sv->lastUpdate + IceUtil::Time::seconds(2)) < currentTime)
94  // {
95  // ARMARX_RT_LOGF_WARNING << deactivateSpam(0.5) << "Waiting for global pos";
96 
97  // target->velocityX = 0;
98  // target->velocityY = 0;
99 
100  // target->velocityRotation = 0;
101  // isAborted = true;
102 
103  // return;
104  // }
105 
106  if (not isTargetSet)
107  {
108  target->velocityX = 0;
109  target->velocityY = 0;
111 
112  return;
113  }
114 
115  if (not sv->isAvailable())
116  {
117  // ARMARX_RT_LOGF_INFO << deactivateSpam(1) << "global pose not available";
118  target->velocityX = 0;
119  target->velocityY = 0;
121 
122  return;
123  }
124 
125  const float measuredOrientation = global_orientation;
126 
127  pid.update(
128  timeSinceLastIteration.toSecondsDouble(), global_P_robot, rtGetControlStruct().target);
129  opid.update(timeSinceLastIteration.toSecondsDouble(),
130  static_cast<double>(measuredOrientation),
131  rtGetControlStruct().targetOrientation);
132 
133  const Eigen::Rotation2Df global_R_local(-measuredOrientation);
134  const Eigen::Vector2f velocities = global_R_local * pid.getControlValue();
135 
136  target->velocityX = velocities.x();
137  target->velocityY = velocities.y();
138  target->velocityRotation = static_cast<float>(opid.getControlValue());
139  }
140 
141  void
143  {
144  target->velocityX = 0;
145  target->velocityY = 0;
147  }
148 
149  void
151  float y,
152  float yaw,
153  float translationAccuracy,
154  float rotationAccuracy)
155  {
156  // todo do we really need a recursive mutex?
157 
158  std::lock_guard<std::recursive_mutex> lock(controlDataMutex);
159 
160  getWriterControlStruct().target << x, y;
162  simox::math::periodic_clamp(static_cast<double>(yaw), -M_PI, M_PI);
163 
164  getWriterControlStruct().translationAccuracy = translationAccuracy;
165  getWriterControlStruct().rotationAccuracy = rotationAccuracy;
166 
169  }
170 
171 } // namespace armarx
armarx::SensorValueGlobalRobotPose::isAvailable
bool isAvailable() const
Definition: GlobalRobotPoseSensorDevice.cpp:34
armarx::SensorValueGlobalRobotPose
Definition: GlobalRobotPoseSensorDevice.h:41
armarx::ControlTargetHolonomicPlatformVelocity
Brief description of class ControlTargetHolonomicPlatformVelocity.
Definition: ControlTargetHolonomicPlatformVelocity.h:38
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
armarx::NJointHolonomicPlatformGlobalPositionControllerTarget::newTargetSet
bool newTargetSet
Definition: NJointHolonomicPlatformGlobalPositionController.h:73
armarx::NJointControllerWithTripleBuffer< NJointHolonomicPlatformGlobalPositionControllerTarget >::rtGetControlStruct
const NJointHolonomicPlatformGlobalPositionControllerTarget & rtGetControlStruct() const
Definition: NJointControllerWithTripleBuffer.h:32
armarx::SensorValueBase::asA
const T * asA() const
Definition: SensorValueBase.h:82
armarx::SensorValueBase
The SensorValueBase class.
Definition: SensorValueBase.h:40
armarx::MultiDimPIDControllerTemplate::reset
void reset()
Definition: MultiDimPIDController.h:196
armarx::NJointHolonomicPlatformGlobalPositionController::isTargetSet
bool isTargetSet
Definition: NJointHolonomicPlatformGlobalPositionController.h:123
armarx::GlobalRobotLocalizationSensorDevice::DeviceName
static std::string DeviceName()
Definition: GlobalRobotPoseSensorDevice.cpp:109
armarx::NJointControllerBase::useControlTarget
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...
Definition: NJointController.cpp:410
armarx::MultiDimPIDControllerTemplate::update
void update(const double deltaSec, const PIDVectorX &measuredValue, const PIDVectorX &targetValue)
Definition: MultiDimPIDController.h:75
armarx::MultiDimPIDControllerTemplate::preallocate
void preallocate(size_t size)
Definition: MultiDimPIDController.h:66
NJointHolonomicPlatformGlobalPositionController.h
armarx::NJointHolonomicPlatformGlobalPositionController::setTarget
void setTarget(float x, float y, float yaw, float translationAccuracy, float rotationAccuracy)
Definition: NJointHolonomicPlatformGlobalPositionController.cpp:150
armarx::NJointHolonomicPlatformGlobalPositionControllerTarget::targetOrientation
float targetOrientation
Definition: NJointHolonomicPlatformGlobalPositionController.h:70
armarx::NJointControllerWithTripleBuffer< NJointHolonomicPlatformGlobalPositionControllerTarget >::getWriterControlStruct
NJointHolonomicPlatformGlobalPositionControllerTarget & getWriterControlStruct()
Definition: NJointControllerWithTripleBuffer.h:54
armarx::PIDController::getControlValue
double getControlValue() const
Definition: PIDController.cpp:273
armarx::MultiDimPIDControllerTemplate::threadSafe
bool threadSafe
Definition: MultiDimPIDController.h:224
armarx::NJointHolonomicPlatformGlobalPositionControllerTarget::rotationAccuracy
float rotationAccuracy
Definition: NJointHolonomicPlatformGlobalPositionController.h:72
armarx::ControlTargetHolonomicPlatformVelocity::velocityRotation
float velocityRotation
Definition: ControlTargetHolonomicPlatformVelocity.h:43
armarx::SensorValueGlobalRobotPose::global_T_root
Transformation global_T_root
Definition: GlobalRobotPoseSensorDevice.h:45
armarx::PIDController::update
void update(double deltaSec, double measuredValue, double targetValue)
Definition: PIDController.cpp:211
armarx::NJointHolonomicPlatformGlobalPositionController::opid
PIDController opid
Definition: NJointHolonomicPlatformGlobalPositionController.h:119
armarx::PIDController::reset
void reset()
Definition: PIDController.cpp:148
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::NJointControllerWithTripleBuffer< NJointHolonomicPlatformGlobalPositionControllerTarget >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
armarx::NJointHolonomicPlatformGlobalPositionController::sv
const GlobalRobotLocalizationSensorDevice::SensorValueType * sv
Definition: NJointHolonomicPlatformGlobalPositionController.h:117
armarx::NJointControllerWithTripleBuffer< NJointHolonomicPlatformGlobalPositionControllerTarget >::controlDataMutex
MutexType controlDataMutex
Definition: NJointControllerWithTripleBuffer.h:73
armarx::ControlTargetHolonomicPlatformVelocity::velocityX
float velocityX
Definition: ControlTargetHolonomicPlatformVelocity.h:41
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::PIDController::threadSafe
bool threadSafe
Definition: PIDController.h:83
armarx::ControlTargetHolonomicPlatformVelocity::velocityY
float velocityY
Definition: ControlTargetHolonomicPlatformVelocity.h:42
armarx::NJointHolonomicPlatformGlobalPositionController::target
ControlTargetHolonomicPlatformVelocity * target
Definition: NJointHolonomicPlatformGlobalPositionController.h:121
armarx::NJointHolonomicPlatformGlobalPositionController::NJointHolonomicPlatformGlobalPositionController
NJointHolonomicPlatformGlobalPositionController(RobotUnit *robotUnit, const NJointHolonomicPlatformGlobalPositionControllerConfigPtr &cfg, const VirtualRobot::RobotPtr &)
Definition: NJointHolonomicPlatformGlobalPositionController.cpp:44
armarx::registrationNJointHolonomicPlatformGlobalPositionController
NJointControllerRegistration< NJointHolonomicPlatformGlobalPositionController > registrationNJointHolonomicPlatformGlobalPositionController("NJointHolonomicPlatformGlobalPositionController")
armarx::MultiDimPIDControllerTemplate::getControlValue
const PIDVectorX & getControlValue() const
Definition: MultiDimPIDController.h:186
armarx::RobotUnit
The RobotUnit class manages a robot and its controllers.
Definition: RobotUnit.h:180
armarx::NJointHolonomicPlatformGlobalPositionControllerTarget::target
Eigen::Vector2f target
Definition: NJointHolonomicPlatformGlobalPositionController.h:69
ControlThreadOutputBuffer.h
Logging.h
armarx::NJointHolonomicPlatformGlobalPositionController::pid
MultiDimPIDController pid
Definition: NJointHolonomicPlatformGlobalPositionController.h:118
armarx::NJointHolonomicPlatformGlobalPositionController::rtRun
virtual void rtRun(const IceUtil::Time &, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: NJointHolonomicPlatformGlobalPositionController.cpp:71
armarx::NJointHolonomicPlatformGlobalPositionController::rtPreActivateController
virtual void rtPreActivateController() override
This function is called before the controller is activated.
Definition: NJointHolonomicPlatformGlobalPositionController.cpp:142
NJointControllerRegistry.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::NJointHolonomicPlatformGlobalPositionControllerTarget::translationAccuracy
float translationAccuracy
Definition: NJointHolonomicPlatformGlobalPositionController.h:71
armarx::NJointControllerBase::useSensorValue
const SensorValueBase * useSensorValue(const std::string &sensorDeviceName) const
Get a const ptr to the given SensorDevice's SensorValue.
Definition: NJointController.cpp:383
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18