27 #include <SimoxUtility/json.h>
28 #include <SimoxUtility/math/pose/invert.h>
29 #include <SimoxUtility/math/pose/pose.h>
30 #include <VirtualRobot/XML/RobotIO.h>
46 defs->component(localizer);
47 defs->defineOptionalProperty<std::string>(
"cmp.PointCloudToArViz",
49 "Name of the PointCloudToArViz component.");
51 defs->optional(localizerMarkerID,
"loc.MarkerID",
"The marker ID to use (-1 for any ID).");
53 defs->optional(robotArMarkerFileName,
54 "robot.ArMarkerRobotFileName",
55 "File name of robot model with ArMarker marker.");
56 defs->optional(robotBaseNodeName,
"robot.BaseNodeName",
"Name of the base robot node.");
57 defs->optional(robotArMarkerMarkerNodeName,
58 "robot.ArMarkerMarkerNodeName",
59 "Name of the ArMarker marker robot node.");
61 defs->optional(calibrationFilePath,
"calibrationFilePath",
"Path to the calibration file.");
69 return "ArMarkerExternalCameraCalibration";
75 namespace fs = std::filesystem;
79 ARMARX_INFO <<
"Loading robot with ArMarker marker from: " << robotArMarkerFileName;
80 robotArMarker = VirtualRobot::RobotIO::loadRobot(
81 robotArMarkerFileName, VirtualRobot::RobotIO::RobotDescription::eStructure);
85 fs::path path = calibrationFilePath;
87 this->calibrationFilePath = parent_path / path.filename();
88 ARMARX_INFO <<
"Calibration file: " << calibrationFilePath;
96 if (vis.pointCloudToArViz)
106 vis.layerCamera.add(
viz::Pose(
"Origin"));
107 vis.layerCamera.add(vis.markerPoseInCamera);
109 vis.layerRobotBase =
arviz.
layer(
"In RobotBase");
110 vis.layerRobotBase.add(vis.markerPoseInRobotBase);
111 vis.layerRobotBase.add(vis.markerModelInRobotBase);
112 vis.layerRobotBase.add(vis.cameraPoseInRobotBase);
139 ArMarkerExternalCameraCalibration::run()
142 while (!task->isStopped())
149 visionx::ArMarkerLocalizationResultList localizationResults;
152 localizationResults = localizer->GetLatestLocalizationResult();
154 catch (
const Ice::ConnectionRefusedException&)
159 ARMARX_VERBOSE <<
"Got " << localizationResults.size() <<
" localization results.";
161 std::optional<visionx::ArMarkerLocalizationResult> result;
162 if (localizationResults.size() > 0)
164 if (localizerMarkerID < 0)
166 result = localizationResults.front();
170 for (
const auto& r : localizationResults)
172 if (r.id == localizerMarkerID)
184 FramedPosePtr framedPose = FramedPosePtr::dynamicCast(result->pose);
187 onLocalizationResult(pose);
191 vis.markerPoseInCamera.scale(0.5);
194 arviz.
commit({vis.layerCamera, vis.layerRobotBase});
196 cycle.waitForCycleDuration();
201 ArMarkerExternalCameraCalibration::onLocalizationResult(
206 Eigen::Matrix4f cameraPoseMarker = simox::math::inverted_pose(markerPoseInCamera);
208 VirtualRobot::RobotNodePtr arMarkerNode =
209 robotArMarker->getRobotNode(robotArMarkerMarkerNodeName);
210 VirtualRobot::RobotNodePtr baseNode = robotArMarker->getRobotNode(robotBaseNodeName);
213 cameraPoseMarker, arMarkerNode->getName(), robotArMarker->getName());
214 framedCameraPose.changeFrame(robotArMarker, baseNode->getName());
217 Eigen::Matrix4f markerInRobotBase = cameraPoseInRobotBase * markerPoseInCamera;
219 vis.markerPoseInCamera.pose(markerPoseInCamera).scale(1.0);
220 vis.markerPoseInRobotBase.pose(baseNode->getPoseInRootFrame() * markerInRobotBase);
221 vis.cameraPoseInRobotBase.pose(baseNode->getPoseInRootFrame() * cameraPoseInRobotBase);
225 framedMarkerModelPose.changeFrame(robotArMarker, baseNode->getName());
226 vis.markerModelInRobotBase.pose(baseNode->getPoseInRootFrame() *
227 framedMarkerModelPose.toEigen());
231 std::scoped_lock lock(markerPoseMutex);
232 this->markerPoseInCamera = markerPoseInCamera;
233 this->cameraPoseInRobotBase = cameraPoseInRobotBase;
235 if (vis.pointCloudToArViz)
237 vis.pointCloudToArViz->setCustomTransform(
238 new Pose(baseNode->getPoseInRootFrame() * cameraPoseInRobotBase));
243 ArMarkerExternalCameraCalibration::storeCalibration()
245 ExternalCameraCalibration calibration;
247 std::scoped_lock lock(markerPoseMutex);
248 calibration.cameraPose = cameraPoseInRobotBase;
249 calibration.cameraPoseFrame = robotBaseNodeName;
253 const nlohmann::json j = calibration;
256 << calibrationFilePath <<
"\n\n"
258 nlohmann::write_json(calibrationFilePath, j, 2);
266 tab.storeButton.setLabel(
"Store calibration");
275 if (tab.storeButton.wasClicked())