NJointHolonomicPlatformRelativePositionController.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 Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2017
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
25
26#include <Eigen/Geometry>
27
28#include <VirtualRobot/Robot.h>
29
31
32namespace armarx
33{
36 "NJointHolonomicPlatformRelativePositionController");
37
40 RobotUnit*,
41 const NJointHolonomicPlatformRelativePositionControllerConfigPtr& cfg,
43 pid(cfg->p, cfg->i, cfg->d, cfg->maxVelocity, cfg->maxAcceleration)
44 {
45 const SensorValueBase* sv = useSensorValue(cfg->platformName);
46 this->sv = sv->asA<SensorValueHolonomicPlatform>();
47 target = useControlTarget(cfg->platformName, ControlModes::HolonomicPlatformVelocity)
50 << "The actuator '" << cfg->platformName << "' has no control mode "
51 << ControlModes::HolonomicPlatformVelocity;
52
53 pid.threadSafe = false;
54
55 oriCtrl.maxV = cfg->maxRotationVelocity;
56 oriCtrl.acceleration = cfg->maxRotationAcceleration;
57 oriCtrl.deceleration = cfg->maxRotationAcceleration;
58 oriCtrl.maxDt = 0.1;
59 oriCtrl.pid->Kp = cfg->p;
60 oriCtrl.positionPeriodLo = -M_PI;
61 oriCtrl.positionPeriodHi = M_PI;
62 pid.preallocate(2);
63 }
64
65 void
67 const IceUtil::Time&,
68 const IceUtil::Time& timeSinceLastIteration)
69 {
70 currentPosition << sv->relativePositionX, sv->relativePositionY;
71 currentOrientation = sv->relativePositionRotation;
72 if (rtGetControlStruct().newTargetSet)
73 {
76 pid.reset();
79 }
80
81 //position pid
82 Eigen::Vector2f relativeCurrentPosition = currentPosition - startPosition;
83 pid.update(timeSinceLastIteration.toSecondsDouble(),
84 relativeCurrentPosition,
86
87 float relativeOrientation = currentOrientation - startOrientation;
88 //rotation pid
89 // Revert the rotation by rotating by the negative angle
90 Eigen::Vector2f localTargetVelocity =
91 Eigen::Rotation2Df(-currentOrientation) *
92 Eigen::Vector2f(pid.getControlValue()[0], pid.getControlValue()[1]);
93 //ARMARX_RT_LOGF_INFO("global target vel x: %.2f y: %2.f, local target vel x: %.2f y: %2.f rotation: %2.f", pid.getControlValue()[0], pid.getControlValue()[1], localTargetVelocity[0], localTargetVelocity[1], sv->relativePositionRotation).deactivateSpam(0.1);
94
95 target->velocityX = localTargetVelocity[0];
96 target->velocityY = localTargetVelocity[1];
97 // target->velocityRotation = pid.getControlValue()[2] / rad2MMFactor;
98 oriCtrl.dt = timeSinceLastIteration.toSecondsDouble();
100 oriCtrl.currentPosition = relativeOrientation;
102 oriCtrl.currentV = sv->velocityRotation;
103 target->velocityRotation = oriCtrl.run();
104 Eigen::Vector2f posError = pid.target.head(2) - pid.processValue.head(2);
105 if (posError.norm() < rtGetControlStruct().translationAccuracy)
106 {
107 target->velocityX = 0;
108 target->velocityY = 0;
109 }
110 //float orientationError = std::abs(oriCtrl.currentPosition - oriCtrl.targetPosition);
111 // if (orientationError < rtGetControlStruct().rotationAccuracy)
112 // {
113 // target->velocityRotation = 0;
114 // }
115 // ARMARX_INFO << deactivateSpam(0.1) << VAROUT(oriCtrl.currentPosition) << VAROUT(orientationError);
116 // ARMARX_INFO << deactivateSpam(0.1) << VAROUT(target->velocityRotation) << VAROUT(sv->velocityRotation);
117 // ARMARX_RT_LOGF_INFO("current pose x: %.2f y: %2.f, error x: %.2f y: %2.f error: %2.f ori error: %2.f", currentPose[0], currentPose[1], posError[0], posError[1], posError.norm(), orientationError).deactivateSpam(0.1);
118 // ARMARX_RT_LOGF_INFO("new target vel: %2.f, %2.f current vel: %2.f, %2.f", target->velocityX, target->velocityY, sv->velocityX, sv->velocityY).deactivateSpam(0.1);
119 }
120
121 void
123 {
124 startPosition[0] = sv->relativePositionX;
125 startPosition[1] = sv->relativePositionY;
126 startOrientation = sv->relativePositionRotation;
127 }
128
129 void
131 float y,
132 float yaw,
133 float translationAccuracy,
134 float rotationAccuracy)
135 {
139 getWriterControlStruct().translationAccuracy = translationAccuracy;
140 getWriterControlStruct().rotationAccuracy = rotationAccuracy;
143 // rtUpdateControlStruct();
144 }
145
146
147} // 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...
NJointHolonomicPlatformRelativePositionController(RobotUnit *robotUnit, const NJointHolonomicPlatformRelativePositionControllerConfigPtr &cfg, const VirtualRobot::RobotPtr &)
PositionThroughVelocityControllerWithAccelerationBoundsAndPeriodicPosition oriCtrl
void setTarget(float x, float y, float yaw, float translationAccuracy, float rotationAccuracy)
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.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
NJointControllerRegistration< NJointHolonomicPlatformRelativePositionController > registrationNJointHolonomicPlatformRelativePositionController("NJointHolonomicPlatformRelativePositionController")