ism_helper.cpp
Go to the documentation of this file.
1 
18 //Global includes
19 #include <Eigen/Geometry>
20 
21 //Pkg includes
22 #include <tf/transform_datatypes.h>
23 #include <boost/filesystem.hpp>
24 
25 //ISM includes
26 #include "ISM/utility/GeometryHelper.hpp"
27 
28 //local includes
29 #include "ism_helper.hpp"
30 
31 
32 IH::ObjectConverter::ObjectConverter(const std::string& pBaseFrame, const bool pIgnoreTypes, const bool pIgnoreIds, const int pEnableRotationMode, const std::string pRotationFrame, const std::string pRotationObjType, const std::string pRotationObjId) : mBaseFrame(pBaseFrame),
33  mIgnoreTypes(pIgnoreTypes), mIgnoreIds(pIgnoreIds), mEnableRotationMode(pEnableRotationMode), mRotationFrame(pRotationFrame), mRotationObjType(pRotationObjType), mRotationObjId(pRotationObjId), mUseConfidenceFromMsg(false),
34  nodehandle(ros::this_node::getName())
35 {
36  ROS_INFO("Waiting for service \"/asr_object_database/object_meta_data\" to become available.");
37  ros::service::waitForService("/asr_object_database/object_meta_data");
38  objectMetaDataClient = nodehandle.serviceClient<asr_object_database::ObjectMetaData>("/asr_object_database/object_meta_data");
39 }
40 
41 IH::ObjectConverter::ObjectConverter(const std::string& pBaseFrame, const bool pIgnoreTypes, const bool pIgnoreIds, const int pEnableRotationMode, const std::string pRotationFrame, const std::string pRotationObjType, const std::string pRotationObjId, const bool pUseConfidenceFromMsg)
42  : mBaseFrame(pBaseFrame), mIgnoreTypes(pIgnoreTypes), mIgnoreIds(pIgnoreIds), mEnableRotationMode(pEnableRotationMode), mRotationFrame(pRotationFrame), mRotationObjType(pRotationObjType), mRotationObjId(pRotationObjId), mUseConfidenceFromMsg(pUseConfidenceFromMsg),
43  nodehandle(ros::this_node::getName())
44 {
45  objectMetaDataClient = nodehandle.serviceClient<asr_object_database::ObjectMetaData>("/asr_object_database/object_meta_data");
46 }
47 
48 
49 //How long to block in waitForTransform(), before failing.
50 #define KINEMATICCHAINTIMEOUT 0.1
51 
52 ISM::PosePtr
53 IH::rosPoseToISMPose(const geometry_msgs::Pose p) {
54  return ISM::PosePtr(
55  new ISM::Pose(new ISM::Point(p.position.x, p.position.y, p.position.z),
56  new ISM::Quaternion(p.orientation.w, p.orientation.x, p.orientation.y, p.orientation.z)));
57 }
58 
59 ISM::ObjectPtr IH::ObjectConverter::pbdObjectToISMObject(const asr_msgs::AsrObject& o)
60 {
61  ROS_DEBUG("Received asr message");
62  geometry_msgs::PoseStamped input, output;
63  input.header = o.header;
64 
65  if(!o.sampledPoses.size()){
66  std::cerr << "Got a AsrObject without poses." << std::endl;
67  std::exit(1);
68  }
69 
70  input.pose = o.sampledPoses.front().pose;
72  mTfListener.transformPose(mBaseFrame, input, output);
73  ROS_DEBUG("Converted object message");
74  ISM::ObjectPtr obj = ISM::ObjectPtr( new ISM::Object( mIgnoreTypes ? "" : o.type,
75  IH::rosPoseToISMPose(output.pose),
76  mIgnoreIds ? "" : o.identifier));
77  obj->ressourcePath = boost::filesystem::path(o.meshResourcePath).replace_extension(".dae");
78  obj->confidence = mUseConfidenceFromMsg ? o.typeConfidence : 1.0;
79  obj->providedBy = o.providedBy;
80 
81  return obj;
82 }
83 
84 void IH::ObjectConverter::normalizeRotationInvariantObjects(ISM::ObjectSetPtr objectSet) {
86  Eigen::Quaterniond orientationRef;
87  Eigen::Vector3d yUnitVector; //
88  // Search reference object in set
90  for (ISM::ObjectPtr object : objectSet->objects) {
91  if (object->type == mRotationObjType && object->observedId == mRotationObjId) {
92  mRotationRefObject = object;
93  break;
94  }
95  }
96  // return if object not found
97  if (!mRotationRefObject) {
98  ROS_INFO_STREAM("Orientation reference object: [" << mRotationObjType << ", " << mRotationObjId << "] is not set.");
99  return;
100  }
101  orientationRef = ISM::GeometryHelper::quatToEigenQuat(mRotationRefObject->pose->quat);
102  yUnitVector = Eigen::Vector3d::UnitY();
104  if (mRotationFrame != mBaseFrame) {
105  ROS_INFO_STREAM("Base frame: " << mBaseFrame << " is not equal to rotation frame: " << mRotationFrame << ". Normalization not possible.");
106  return;
107  }
108  orientationRef = Eigen::Quaterniond(1.0, 0, 0, 0);
109  yUnitVector = -Eigen::Vector3d::UnitZ(); // NOTE: in world coordinates system is the -Z-Axis equivalent to the Y-Axis in objects coordinates system.
110  }
111  orientationRef.normalize();
112 
113  for (ISM::ObjectPtr object : objectSet->objects) {
114  asr_object_database::ObjectMetaData objMetaData;
115  objMetaData.request.object_type = object->type;
116  objMetaData.request.recognizer = object->providedBy == "fake_data_publisher" || "fake_object_recognition" ? "segmentable" : object->providedBy;
117  if (objectMetaDataClient.call(objMetaData)) {
118  if (objMetaData.response.is_rotation_invariant)
119  {
120  Eigen::Quaterniond orientation = ISM::GeometryHelper::quatToEigenQuat(object->pose->quat);
121  orientation.normalize();
122  Eigen::Vector3d objYAxis = ISM::GeometryHelper::getAxisFromQuat(ISM::GeometryHelper::eigenQuatToQuat(orientation), Eigen::Vector3d::UnitY());
123  Eigen::Vector3d refYAxis = ISM::GeometryHelper::getAxisFromQuat(ISM::GeometryHelper::eigenQuatToQuat(orientationRef), yUnitVector);
124  Eigen::AngleAxisd yTransform = calculateTransform(objYAxis, refYAxis, Eigen::Vector3d::UnitX());
125  Eigen::Quaterniond yAlignedOrientation = (yTransform * orientation).normalized();
126  Eigen::Vector3d objXAxis = ISM::GeometryHelper::getAxisFromQuat(ISM::GeometryHelper::eigenQuatToQuat(yAlignedOrientation), Eigen::Vector3d::UnitX());
127  Eigen::Vector3d refXAxis = ISM::GeometryHelper::getAxisFromQuat(ISM::GeometryHelper::eigenQuatToQuat(orientationRef), Eigen::Vector3d::UnitX());
128  object->pose->quat->eigen = (yTransform.inverse() * calculateTransform(objXAxis, refXAxis, Eigen::Vector3d::UnitY())).normalized() * yAlignedOrientation;
129  }
130  } else {
131  ROS_ERROR("Could not call the asr_object_database::ObjectMetaData service call");
132  }
133  }
134 }
135 
136 Eigen::AngleAxisd IH::ObjectConverter::calculateTransform(const Eigen::Vector3d& objectAxis, const Eigen::Vector3d& referenceAxis, const Eigen::Vector3d& unitAxis) {
137  double angle = ISM::GeometryHelper::getAngleBetweenAxes(referenceAxis, objectAxis);
138  Eigen::Vector3d rotationAxis = objectAxis.cross(referenceAxis);
139  rotationAxis.normalize();
140 
141  angle *= M_PI/180; // Convert to radians
142  if (angle < 0.001) {
143  return Eigen::AngleAxisd(0, unitAxis);
144  } else if (fabs(angle - M_PI) < 0.001) {
145  return Eigen::AngleAxisd(M_PI, unitAxis);
146  } else {
147  return Eigen::AngleAxisd(angle, rotationAxis);
148  }
149 }
150 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
tf::TransformListener mTfListener
Needed for transform to base_frame coordinate system.
Definition: ism_helper.hpp:92
ISM::PosePtr rosPoseToISMPose(const geometry_msgs::Pose p)
Definition: ism_helper.cpp:53
ISM::ObjectPtr mRotationRefObject
Current object pose, to which rotation invariant objects are normalized.
Definition: ism_helper.hpp:112
const bool mIgnoreTypes
Whether to take over object types from AsrObjects.
Definition: ism_helper.hpp:98
const std::string mRotationObjType
If normalizing to object: Which type of object to normalize to.
Definition: ism_helper.hpp:108
bool call(MReq &req, MRes &res)
std::string getName(void *handle)
const bool mUseConfidenceFromMsg
Whether to take over recognition confidences (into ism rating) from AsrObjects.
Definition: ism_helper.hpp:115
const int mEnableRotationMode
Normalize orientations of rotation invariant objects to either a coordinate frame or an not-rotation ...
Definition: ism_helper.hpp:103
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
bool waitForTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration &timeout, const ros::Duration &polling_sleep_duration=ros::Duration(0.01), std::string *error_msg=NULL) const
Eigen::AngleAxisd calculateTransform(const Eigen::Vector3d &objectAxis, const Eigen::Vector3d &referenceAxis, const Eigen::Vector3d &unitAxis)
calculateTransform Caculate the transformation between two vectors.
Definition: ism_helper.cpp:136
#define KINEMATICCHAINTIMEOUT
Definition: ism_helper.cpp:50
TFSIMD_FORCE_INLINE Vector3 normalized() const
void normalizeRotationInvariantObjects(ISM::ObjectSetPtr objectSet)
normalizeOrientation Normalizes the orientation of rotation invariance objects accroding to: ...
Definition: ism_helper.cpp:84
#define ROS_INFO(...)
ISM::ObjectPtr pbdObjectToISMObject(const asr_msgs::AsrObject &o)
Definition: ism_helper.cpp:59
ros::ServiceClient objectMetaDataClient
Definition: ism_helper.hpp:122
ObjectConverter(const std::string &pBaseFrame, const bool pIgnoreTypes, const bool pIgnoreIds, const int pEnableRotationMode, const std::string pRotationFrame, const std::string pRotationObjType, const std::string pRotationObjId)
Definition: ism_helper.cpp:32
const std::string mRotationFrame
If normalizing to frame: Which frame to normalize to.
Definition: ism_helper.hpp:106
const std::string mBaseFrame
Frame to which incoming object messages are transformed.
Definition: ism_helper.hpp:95
#define ROS_INFO_STREAM(args)
void transformPose(const std::string &target_frame, const geometry_msgs::PoseStamped &stamped_in, geometry_msgs::PoseStamped &stamped_out) const
ros::NodeHandle nodehandle
Definition: ism_helper.hpp:120
const int ROTATION_MODE_FRAME
Definition: ism_helper.hpp:88
static Time now()
const bool mIgnoreIds
Whether to take over object ids from AsrObjects.
Definition: ism_helper.hpp:100
#define ROS_ERROR(...)
const std::string mRotationObjId
If normalizing to object: Which id of object to normalize to.
Definition: ism_helper.hpp:110
const int ROTATION_MODE_OBJECT
Definition: ism_helper.hpp:89
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)
const int ROTATION_MODE_DEACTIVATED
Definition: ism_helper.hpp:87
#define ROS_DEBUG(...)


asr_ism
Author(s): Borella Jocelyn, Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Thu Jan 9 2020 07:20:58