PlatformSubUnit.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::PlatformSubUnit
17  * @author Raphael ( raphael dot grimm at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 #include "PlatformSubUnit.h"
23 
24 #include <Eigen/Core>
25 #include <Eigen/Geometry>
26 
27 #include <SimoxUtility/math/convert/mat4f_to_rpy.h>
28 #include <VirtualRobot/MathTools.h>
29 
31 #include <RobotAPI/interface/core/GeometryBase.h>
32 #include <RobotAPI/interface/core/PoseBase.h>
34 
35 namespace armarx
36 {
37 
38  void
41  {
42  if (!getProxy())
43  {
44  //this unit is not initialized yet
45  ARMARX_IMPORTANT << deactivateSpam(1) << "not initialized yet - skipping this update";
46  return;
47  }
48  if (!listenerPrx)
49  {
50  ARMARX_INFO << deactivateSpam(1) << "listener is not set - skipping this update";
51  return;
52  }
53  const SensorValueBase* sensorValue = sc.sensors.at(platformSensorIndex).get();
54  ARMARX_CHECK_EXPRESSION(sensorValue);
55  std::lock_guard<std::mutex> guard{dataMutex};
56 
57  const auto timestamp = sc.sensorValuesTimestamp.toMicroSeconds();
58  ;
59 
60  // odom velocity is in local robot frame
61  FrameHeader odomVelocityHeader;
62  odomVelocityHeader.parentFrame = robotRootFrame;
63  odomVelocityHeader.frame = robotRootFrame;
64  odomVelocityHeader.agent = agentName;
65  odomVelocityHeader.timestampInMicroSeconds = timestamp;
66 
67  // odom pose is in odom frame
68  FrameHeader odomPoseHeader;
69  odomPoseHeader.parentFrame = OdometryFrame;
70  odomPoseHeader.frame = robotRootFrame;
71  odomPoseHeader.agent = agentName;
72  odomPoseHeader.timestampInMicroSeconds = timestamp;
73 
74  // odom velocity is in local robot frame
75  FrameHeader globalPoseHeader;
76  globalPoseHeader.parentFrame = GlobalFrame;
77  globalPoseHeader.frame = robotRootFrame;
78  globalPoseHeader.agent = agentName;
79  globalPoseHeader.timestampInMicroSeconds = timestamp;
80 
81 
82  //pos
83  {
88 
89  const auto odom_T_robot = VirtualRobot::MathTools::posrpy2eigen4f(
90  s->relativePositionX, s->relativePositionY, 0, 0, 0, s->relativePositionRotation);
91 
92  // @@@ CHECK BELOW:
93  // if (s->isA<SensorValueHolonomicPlatformAbsolutePosition>())
94  // {
95  // const SensorValueHolonomicPlatformAbsolutePosition* sabs = s->asA<SensorValueHolonomicPlatformAbsolutePosition>();
96  // abs = positionCorrection * VirtualRobot::MathTools::posrpy2eigen4f(sabs->absolutePositionX, sabs->absolutePositionY, 0, 0, 0, sabs->absolutePositionRotation);
97 
98  // TransformStamped currentPose;
99  // currentPose.header = globalPoseHeader;
100  // currentPose.transform = abs;
101 
102  // globalPosePrx->reportGlobalRobotPose(currentPose);
103 
104  // // legacy interfaces
105  // PlatformPose platformPose = toPlatformPose(currentPose);
106  // listenerPrx->reportPlatformPose(platformPose);
107  // globalPosCtrl->setGlobalPos(platformPose);
108  // }
109  // else
110  // {
111  // global_T_robot = global_T_odom * odom_T_robot;
112  // }
113 
114  // const auto global_T_robot = global_T_odom * odom_T_robot;
115 
116 
117  const TransformStamped odomPose{.header = odomPoseHeader, .transform = odom_T_robot};
118  odometryPrx->reportOdometryPose(odomPose);
119 
120  // legacy interface
121  const auto odomLegacyPose = toPlatformPose(odomPose);
122  listenerPrx->reportPlatformOdometryPose(
123  odomLegacyPose.x, odomLegacyPose.y, odomLegacyPose.rotationAroundZ);
124  }
125 
126  //vel
127  {
131 
132  TwistStamped odomVelocity;
133  odomVelocity.header = odomVelocityHeader;
134  odomVelocity.twist.linear << s->velocityX, s->velocityY, 0;
135  odomVelocity.twist.angular << 0, 0, s->velocityRotation;
136 
137  odometryPrx->reportOdometryVelocity(odomVelocity);
138 
139  // legacy interface
140  const auto& vel = odomVelocity.twist;
141  listenerPrx->reportPlatformVelocity(vel.linear.x(), vel.linear.y(), vel.angular.z());
142  }
143  }
144 
145  void
147  {
148  //holding the mutex here could deadlock
149  if (!pt->isControllerActive())
150  {
151  pt->activateController();
152  }
153  std::lock_guard<std::mutex> guard{dataMutex};
154  pt->setVelocites(std::clamp(vx, -maxVLin, maxVLin),
155  std::clamp(vy, -maxVLin, maxVLin),
156  std::clamp(vr, -maxVAng, maxVAng));
157  }
158 
159  void
161  Ice::Float ry,
162  Ice::Float rr,
163  Ice::Float lac,
164  Ice::Float rac,
165  const Ice::Current&)
166  {
167  globalPosCtrl->setTarget(rx, ry, rr, lac, rac);
168  if (!globalPosCtrl->isControllerActive())
169  {
170  globalPosCtrl->activateController();
171  };
172  }
173 
174  void
176  Ice::Float ry,
177  Ice::Float rr,
178  Ice::Float lac,
179  Ice::Float rac,
180  const Ice::Current&)
181  {
182  relativePosCtrl->setTarget(rx, ry, rr, lac, rac);
183  if (!relativePosCtrl->isControllerActive())
184  {
185  relativePosCtrl->activateController();
186  }
187  //holding the mutex here could deadlock
188  // std::lock_guard<std::mutex> guard {dataMutex};
189  ARMARX_INFO << "target orientation: " << rr;
190  }
191 
192  void
194  Ice::Float mxVAng,
195  const Ice::Current&)
196  {
197  std::lock_guard<std::mutex> guard{dataMutex};
198  maxVLin = std::abs(mxVLin);
199  maxVAng = std::abs(mxVAng);
200  }
201 
202  // void armarx::PlatformSubUnit::setGlobalPose(armarx::PoseBasePtr globalPose, const Ice::Current&)
203  // {
204  // std::lock_guard<std::mutex> guard {dataMutex};
205  // PosePtr p = PosePtr::dynamicCast(globalPose);
206  // global_T_odom = p->toEigen() * global_T_robot.inverse();
207  // }
208 
209  // armarx::PoseBasePtr armarx::PlatformSubUnit::getGlobalPose(const Ice::Current&)
210  // {
211  // std::lock_guard<std::mutex> guard {dataMutex};
212  // return new Pose {global_T_robot};
213  // }
214 
215  void
217  {
218  move(0, 0, 0);
219  }
220 
221 
222 } // namespace armarx
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:919
armarx::JointAndNJointControllers
Structure used by the RobotUnit to swap lists of Joint and NJoint controllers.
Definition: JointAndNJointControllers.h:32
armarx::SensorValueHolonomicPlatformRelativePosition
The robot's position relative to its initial pose when starting the robot unit based on odometry info...
Definition: SensorValueHolonomicPlatform.h:83
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::SensorValueBase::asA
const T * asA() const
Definition: SensorValueBase.h:82
armarx::SensorValueBase
The SensorValueBase class.
Definition: SensorValueBase.h:40
armarx::PlatformSubUnit::moveRelative
void moveRelative(Ice::Float rx, Ice::Float ry, Ice::Float rr, Ice::Float lac, Ice::Float rac, const Ice::Current &=Ice::emptyCurrent) override
Definition: PlatformSubUnit.cpp:175
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
armarx::detail::ControlThreadOutputBufferEntry::sensors
std::vector< PropagateConst< SensorValueBase * > > sensors
Definition: ControlThreadOutputBuffer.h:208
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
clamp
double clamp(double x, double a, double b)
Definition: point.hpp:136
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:182
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::PlatformSubUnit::setMaxVelocities
void setMaxVelocities(Ice::Float mxVLin, Ice::Float mxVAng, const Ice::Current &=Ice::emptyCurrent) override
Definition: PlatformSubUnit.cpp:193
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:281
armarx::PlatformSubUnit::update
void update(const SensorAndControl &sc, const JointAndNJointControllers &c) override
Definition: PlatformSubUnit.cpp:39
FramedPose.h
armarx::PlatformSubUnit::stopPlatform
void stopPlatform(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PlatformSubUnit.cpp:216
armarx::PlatformSubUnit::moveTo
void moveTo(Ice::Float rx, Ice::Float ry, Ice::Float rr, Ice::Float lac, Ice::Float rac, const Ice::Current &=Ice::emptyCurrent) override
Definition: PlatformSubUnit.cpp:160
armarx::SensorValueHolonomicPlatformVelocity
Definition: SensorValueHolonomicPlatform.h:30
armarx::OdometryFrame
const std::string OdometryFrame
Definition: FramedPose.h:66
armarx::PlatformSubUnit::move
void move(Ice::Float vx, Ice::Float vy, Ice::Float vr, const Ice::Current &=Ice::emptyCurrent) override
Definition: PlatformSubUnit.cpp:146
armarx::detail::ControlThreadOutputBufferEntry::sensorValuesTimestamp
IceUtil::Time sensorValuesTimestamp
Definition: ControlThreadOutputBuffer.h:206
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
GlobalRobotPoseSensorDevice.h
armarx::toPlatformPose
PlatformPose toPlatformPose(const TransformStamped &transformStamped)
Definition: PlatformUnit.cpp:86
PlatformSubUnit.h
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::SensorValueBase::isA
bool isA() const
Definition: SensorValueBase.h:75