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 
30 #include <RobotAPI/interface/core/GeometryBase.h>
31 #include <RobotAPI/interface/core/PoseBase.h>
33 
34 namespace armarx
35 {
36 
37  void
40  {
41  if (!getProxy())
42  {
43  //this unit is not initialized yet
44  ARMARX_IMPORTANT << deactivateSpam(1) << "not initialized yet - skipping this update";
45  return;
46  }
47  if (!listenerPrx)
48  {
49  ARMARX_INFO << deactivateSpam(1) << "listener is not set - skipping this update";
50  return;
51  }
52  const SensorValueBase* sensorValue = sc.sensors.at(platformSensorIndex).get();
53  ARMARX_CHECK_EXPRESSION(sensorValue);
54  std::lock_guard<std::mutex> guard{dataMutex};
55 
56  const auto timestamp = sc.sensorValuesTimestamp.toMicroSeconds();
57  ;
58 
59  // odom velocity is in local robot frame
60  FrameHeader odomVelocityHeader;
61  odomVelocityHeader.parentFrame = robotRootFrame;
62  odomVelocityHeader.frame = robotRootFrame;
63  odomVelocityHeader.agent = agentName;
64  odomVelocityHeader.timestampInMicroSeconds = timestamp;
65 
66  // odom pose is in odom frame
67  FrameHeader odomPoseHeader;
68  odomPoseHeader.parentFrame = OdometryFrame;
69  odomPoseHeader.frame = robotRootFrame;
70  odomPoseHeader.agent = agentName;
71  odomPoseHeader.timestampInMicroSeconds = timestamp;
72 
73  // odom velocity is in local robot frame
74  FrameHeader globalPoseHeader;
75  globalPoseHeader.parentFrame = GlobalFrame;
76  globalPoseHeader.frame = robotRootFrame;
77  globalPoseHeader.agent = agentName;
78  globalPoseHeader.timestampInMicroSeconds = timestamp;
79 
80 
81  //pos
82  {
87 
88  const auto odom_T_robot = VirtualRobot::MathTools::posrpy2eigen4f(
89  s->relativePositionX, s->relativePositionY, 0, 0, 0, s->relativePositionRotation);
90 
91  // @@@ CHECK BELOW:
92  // if (s->isA<SensorValueHolonomicPlatformAbsolutePosition>())
93  // {
94  // const SensorValueHolonomicPlatformAbsolutePosition* sabs = s->asA<SensorValueHolonomicPlatformAbsolutePosition>();
95  // abs = positionCorrection * VirtualRobot::MathTools::posrpy2eigen4f(sabs->absolutePositionX, sabs->absolutePositionY, 0, 0, 0, sabs->absolutePositionRotation);
96 
97  // TransformStamped currentPose;
98  // currentPose.header = globalPoseHeader;
99  // currentPose.transform = abs;
100 
101  // globalPosePrx->reportGlobalRobotPose(currentPose);
102 
103  // // legacy interfaces
104  // PlatformPose platformPose = toPlatformPose(currentPose);
105  // listenerPrx->reportPlatformPose(platformPose);
106  // globalPosCtrl->setGlobalPos(platformPose);
107  // }
108  // else
109  // {
110  // global_T_robot = global_T_odom * odom_T_robot;
111  // }
112 
113  // const auto global_T_robot = global_T_odom * odom_T_robot;
114 
115 
116  const TransformStamped odomPose{.header = odomPoseHeader, .transform = odom_T_robot};
117  odometryPrx->reportOdometryPose(odomPose);
118 
119  // legacy interface
120  const auto odomLegacyPose = toPlatformPose(odomPose);
121  listenerPrx->reportPlatformOdometryPose(
122  odomLegacyPose.x, odomLegacyPose.y, odomLegacyPose.rotationAroundZ);
123  }
124 
125  //vel
126  {
130 
131  TwistStamped odomVelocity;
132  odomVelocity.header = odomVelocityHeader;
133  odomVelocity.twist.linear << s->velocityX, s->velocityY, 0;
134  odomVelocity.twist.angular << 0, 0, s->velocityRotation;
135 
136  odometryPrx->reportOdometryVelocity(odomVelocity);
137 
138  // legacy interface
139  const auto& vel = odomVelocity.twist;
140  listenerPrx->reportPlatformVelocity(vel.linear.x(), vel.linear.y(), vel.angular.z());
141  }
142  }
143 
144  void
146  {
147  //holding the mutex here could deadlock
148  if (!pt->isControllerActive())
149  {
150  pt->activateController();
151  }
152  std::lock_guard<std::mutex> guard{dataMutex};
153  pt->setVelocites(std::clamp(vx, -maxVLin, maxVLin),
154  std::clamp(vy, -maxVLin, maxVLin),
155  std::clamp(vr, -maxVAng, maxVAng));
156  }
157 
158  void
160  Ice::Float ry,
161  Ice::Float rr,
162  Ice::Float lac,
163  Ice::Float rac,
164  const Ice::Current&)
165  {
166  globalPosCtrl->setTarget(rx, ry, rr, lac, rac);
167  if (!globalPosCtrl->isControllerActive())
168  {
169  globalPosCtrl->activateController();
170  };
171  }
172 
173  void
175  Ice::Float ry,
176  Ice::Float rr,
177  Ice::Float lac,
178  Ice::Float rac,
179  const Ice::Current&)
180  {
181  relativePosCtrl->setTarget(rx, ry, rr, lac, rac);
182  if (!relativePosCtrl->isControllerActive())
183  {
184  relativePosCtrl->activateController();
185  }
186  //holding the mutex here could deadlock
187  // std::lock_guard<std::mutex> guard {dataMutex};
188  ARMARX_INFO << "target orientation: " << rr;
189  }
190 
191  void
193  Ice::Float mxVAng,
194  const Ice::Current&)
195  {
196  std::lock_guard<std::mutex> guard{dataMutex};
197  maxVLin = std::abs(mxVLin);
198  maxVAng = std::abs(mxVAng);
199  }
200 
201  // void armarx::PlatformSubUnit::setGlobalPose(armarx::PoseBasePtr globalPose, const Ice::Current&)
202  // {
203  // std::lock_guard<std::mutex> guard {dataMutex};
204  // PosePtr p = PosePtr::dynamicCast(globalPose);
205  // global_T_odom = p->toEigen() * global_T_robot.inverse();
206  // }
207 
208  // armarx::PoseBasePtr armarx::PlatformSubUnit::getGlobalPose(const Ice::Current&)
209  // {
210  // std::lock_guard<std::mutex> guard {dataMutex};
211  // return new Pose {global_T_robot};
212  // }
213 
214  void
216  {
217  move(0, 0, 0);
218  }
219 
220 
221 } // namespace armarx
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
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:183
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:174
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::detail::ControlThreadOutputBufferEntry::sensors
std::vector< PropagateConst< SensorValueBase * > > sensors
Definition: ControlThreadOutputBuffer.h:203
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
clamp
double clamp(double x, double a, double b)
Definition: point.hpp:125
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:177
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::PlatformSubUnit::setMaxVelocities
void setMaxVelocities(Ice::Float mxVLin, Ice::Float mxVAng, const Ice::Current &=Ice::emptyCurrent) override
Definition: PlatformSubUnit.cpp:192
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::PlatformSubUnit::update
void update(const SensorAndControl &sc, const JointAndNJointControllers &c) override
Definition: PlatformSubUnit.cpp:38
FramedPose.h
armarx::PlatformSubUnit::stopPlatform
void stopPlatform(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PlatformSubUnit.cpp:215
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:159
armarx::SensorValueHolonomicPlatformVelocity
Definition: SensorValueHolonomicPlatform.h:30
armarx::OdometryFrame
const std::string OdometryFrame
Definition: FramedPose.h:63
armarx::PlatformSubUnit::move
void move(Ice::Float vx, Ice::Float vy, Ice::Float vr, const Ice::Current &=Ice::emptyCurrent) override
Definition: PlatformSubUnit.cpp:145
armarx::detail::ControlThreadOutputBufferEntry::sensorValuesTimestamp
IceUtil::Time sensorValuesTimestamp
Definition: ControlThreadOutputBuffer.h:201
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:174
GlobalRobotPoseSensorDevice.h
armarx::toPlatformPose
PlatformPose toPlatformPose(const TransformStamped &transformStamped)
Definition: PlatformUnit.cpp:79
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:28
armarx::SensorValueBase::isA
bool isA() const
Definition: SensorValueBase.h:75