27 #include <rtabmap/core/odometry/OdometryF2M.h>
30 #include <rtabmap/core/util3d.h>
31 #include <rtabmap/core/Transform.h>
35 #include <VisionX/interface/components/PointCloudAndImageAndCalibrationProviderInterface.h>
38 using namespace rtabmap;
44 getConfigIdentifier()));
49 agentName = getProperty<std::string>(
"agentName").getValue();
50 provideGlobalPointCloud = getProperty<bool>(
"provideGlobalPointCloud").getValue();
52 usingProxy(getProperty<std::string>(
"RobotStateComponentName").getValue());
53 usingProxy(getProperty<std::string>(
"WorkingMemoryName").getValue());
55 passThroughFilter.setFilterFieldName(
"z");
56 passThroughFilter.setFilterLimits(getProperty<float>(
"MinDistance").getValue(), getProperty<float>(
"MaxDistance").getValue());
60 ParametersMap parameters;
62 parameters.insert(rtabmap::ParametersPair(rtabmap::Parameters::kMemImageKept(),
"true"));
64 ULogger::setType(ULogger::kTypeConsole);
66 ULogger::setLevel(ULogger::kWarning);
68 rtabmap =
new Rtabmap();
69 rtabmap->init(parameters);
70 rtabmapThread =
new RtabmapThread(rtabmap);
72 odomThread =
new OdometryThread(
new OdometryF2M());
74 odomThread->registerToEventsManager();
75 rtabmapThread->registerToEventsManager();
77 this->registerToEventsManager();
79 UEventsManager::createPipe(
this, odomThread,
"CameraEvent");
84 this->unregisterFromEventsManager();
86 resultCondition.notify_one();
87 dataCondition.notify_one();
89 rtabmapThread->unregisterFromEventsManager();
90 odomThread->unregisterFromEventsManager();
92 odomThread->join(
true);
93 rtabmapThread->join(
true);
104 robotStateComponent = getProxy<RobotStateComponentInterfacePrx>(getProperty<std::string>(
"RobotStateComponentName").getValue());
105 workingMemory = getProxy<memoryx::WorkingMemoryInterfacePrx>(getProperty<std::string>(
"WorkingMemoryName").getValue());
106 agentInstancesSegment = workingMemory->getAgentInstancesSegment();
118 resultPointCloudPtr.reset(
new pcl::PointCloud<pcl::PointXYZRGBA>);
119 hasNewResultPointCloud =
false;
122 FramedPoseBasePtr poseInGlobalFrame = synchronizedRobot->getRobotNode(sourceFrameName)->getGlobalPose();
123 initialCameraPose = armarx::FramedPosePtr::dynamicCast(poseInGlobalFrame)->toEigen();
125 rtabmapThread->start();
131 while (captureEnabled && (!rtabmapThread->isRunning() || !odomThread->isRunning()))
140 if (provideGlobalPointCloud)
142 requestGlobalPointCloud();
151 rtabmapThread->kill();
154 void RTABMapRegistration::requestGlobalPointCloud()
156 UEventsManager::post(
new RtabmapEventCmd(RtabmapEventCmd::kCmdPublish3DMap,
true,
true,
false));
162 boost::mutex::scoped_lock lock(resultPointCloudMutex);
164 while (captureEnabled && !hasNewResultPointCloud)
166 resultCondition.wait(lock);
174 hasNewResultPointCloud =
false;
176 if (resultPointCloudPtr->points.size())
178 providePointCloud(resultPointCloudPtr);
193 if (event->getClassName().compare(
"RtabmapEvent") == 0)
195 RtabmapEvent* rtabmapEvent =
static_cast<RtabmapEvent*
>(event);
197 boost::mutex::scoped_lock lock(RTABDataMutex);
213 if (!provideGlobalPointCloud)
215 signatures = rtabmapEvent->getStats().getSignatures();
218 dataCondition.notify_one();
222 else if (event->getClassName().compare(
"OdometryEvent") == 0)
224 OdometryEvent* rtabmapEvent =
static_cast<OdometryEvent*
>(event);
226 if (rtabmapEvent->pose().isNull())
231 else if (provideGlobalPointCloud && event->getClassName().compare(
"RtabmapEvent3DMap") == 0)
233 RtabmapEvent3DMap* rtabmapEvent =
static_cast<RtabmapEvent3DMap*
>(event);
235 boost::mutex::scoped_lock lock(RTABDataMutex);
237 signatures = rtabmapEvent->getSignatures();
238 poses = rtabmapEvent->getPoses();
241 dataCondition.notify_one();
251 bool RTABMapRegistration::extractPointCloud(SensorData sensorData,
Eigen::Matrix4f transform, pcl::PointCloud<pcl::PointXYZRGBA>::Ptr& tempCloudPtr)
253 std::vector<CameraModel> cameraModels = sensorData.cameraModels();
255 if (cameraModels.size() != 1)
260 CameraModel m = cameraModels[0];
265 int decimation = (provideGlobalPointCloud) ? 4 : 1;
267 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPtr = util3d::cloudFromDepthRGB(sensorData.imageRaw(), sensorData.depthOrRightRaw(), m, decimation);
269 pcl::copyPointCloud(*cloudPtr, *tempCloudPtr);
270 passThroughFilter.setInputCloud(tempCloudPtr);
271 passThroughFilter.filter(*tempCloudPtr);
273 Eigen::Affine3f convertUnitToMM = Eigen::Affine3f(Eigen::Scaling(1000.0f));
274 pcl::transformPointCloud(*tempCloudPtr, *tempCloudPtr, convertUnitToMM);
276 pcl::transformPointCloud(*tempCloudPtr, *tempCloudPtr,
transform);
281 void RTABMapRegistration::processData()
283 boost::mutex::scoped_lock lock(RTABDataMutex);
285 while (captureEnabled && !hasNewData)
287 dataCondition.wait(lock);
297 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr nextCloudPtr(
new pcl::PointCloud<pcl::PointXYZRGBA>());
299 for (
auto& items : signatures)
301 const int id = items.first;
302 const Signature signature = items.second;
304 if (signature.isBadSignature())
309 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr tempCloudPtr(
new pcl::PointCloud<pcl::PointXYZRGBA>());
313 if (signature.isEnabled())
315 transform = signature.getPose().toEigen4f();
329 extractPointCloud(signature.sensorData(),
transform, tempCloudPtr);
331 *nextCloudPtr += *tempCloudPtr;
340 if (provideGlobalPointCloud)
342 requestGlobalPointCloud();
346 boost::mutex::scoped_lock resultLock(resultPointCloudMutex);
350 pcl::copyPointCloud(*nextCloudPtr, *resultPointCloudPtr);
352 hasNewResultPointCloud =
true;
353 resultCondition.notify_one();
360 Eigen::Vector3f position = pose.block<3, 1>(0, 3);
362 agentInstance->setOrientation(orientationPtr);
363 agentInstance->setPosition(positionPtr);
365 if (agentInstanceId.empty() || !agentInstancesSegment->hasEntityById(agentInstanceId))
367 agentInstanceId = agentInstancesSegment->addEntity(agentInstance);
371 agentInstancesSegment->updateEntity(agentInstanceId, agentInstance);
378 providerName = getProperty<std::string>(
"providerName").getValue();
379 usingImageProvider(providerName);
397 images =
new CByteImage*[numImages];
399 for (
int i = 0 ; i < numImages ; i++)
404 visionx::StereoCalibrationInterfacePrx calibrationProvider = visionx::StereoCalibrationInterfacePrx::checkedCast(imageProviderInfo.
proxy);
405 if (!calibrationProvider)
407 ARMARX_FATAL <<
"unable to retrieve calibration information. aborting.";
411 sourceFrameName = calibrationProvider->getReferenceFrame();
413 visionx::StereoCalibration calibration = calibrationProvider->getStereoCalibration();
415 float fx = calibration.calibrationRight.cameraParam.focalLength[0];
416 float fy = calibration.calibrationRight.cameraParam.focalLength[1];
417 float cx = calibration.calibrationRight.cameraParam.principalPoint[0];
418 float cy = calibration.calibrationRight.cameraParam.principalPoint[1];
424 cameraModel = CameraModel(fx, fy, cx, cy, Transform::getIdentity(), tx);
429 post(
new CameraEvent());
439 if (!waitForImages(1000))
441 ARMARX_WARNING <<
"Timeout or error while waiting for image data";
444 long lastStamp = imageMetaInfo ? imageMetaInfo->timeProvided : 0.0;
445 if (getImages(providerName, images, imageMetaInfo) != 2)
450 if (lastStamp == imageMetaInfo->timeProvided)
452 ARMARX_INFO <<
"Last timestamp as before - skipping image";
457 const cv::Mat tempRGBImage = cv::cvarrToMat(IplImageAdaptor::Adapt(images[0]));
459 cv::cvtColor(tempRGBImage, RGBImage, CV_RGB2BGR);
464 cv::Mat depthImage = cv::Mat(RGBImage.rows, RGBImage.cols, CV_16UC1);
466 for (
int j = 0; j < depthImage.rows; j++)
468 for (
int i = 0; i < depthImage.cols; i++)
470 unsigned short value = images[1]->pixels[3 * (j * images[1]->width + i) + 0]
471 + (images[1]->pixels[3 * (j * images[1]->width + i) + 1] << 8);
473 depthImage.at<
unsigned short>(j, i) =
value;
494 double time = imageMetaInfo->timeProvided / 1000.0;
495 SensorData sensorData(RGBImage, depthImage, cameraModel, ++imageId, time);
497 post(
new CameraEvent(sensorData, providerName));
502 visionx::MetaPointCloudFormatPtr info =
new visionx::MetaPointCloudFormat();
503 info->size = 640 * 480 *
sizeof(visionx::ColoredPoint3D);
505 if (provideGlobalPointCloud)
507 info->capacity = info->size * 50;
511 info->capacity = info->size;
514 info->type = visionx::PointContentType::eColoredPoints;