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)
105 vis.layerCamera =
arviz.layer(
"In Camera");
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()
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);
185 Eigen::Matrix4f pose = framedPose->toEigen();
187 onLocalizationResult(pose);
191 vis.markerPoseInCamera.scale(0.5);
194 arviz.commit({vis.layerCamera, vis.layerRobotBase});
196 cycle.waitForCycleDuration();
201 ArMarkerExternalCameraCalibration::onLocalizationResult(
202 const Eigen::Matrix4f& markerPoseInCamera)
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());
216 Eigen::Matrix4f cameraPoseInRobotBase = framedCameraPose.toEigen();
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);
224 Eigen::Matrix4f::Identity(), arMarkerNode->getName(), robotArMarker->getName());
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())
void onInitComponent() override
void onDisconnectComponent() override
void RemoteGui_update() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onConnectComponent() override
void onExitComponent() override
void createRemoteGuiTab()
std::string getDefaultName() const override
armarx::viz::Client arviz
static std::string resolvePath(const std::string &path, bool verbose=true)
Resolves environment variables and home paths and tries to make path absolute.
Default component property definition container.
ProxyType getProxyFromProperty(const std::string &propertyName, bool addToDependencies=false, const std::string &endpoints="", bool throwOnProxyError=true)
Get a proxy whose name is specified by the given property.
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
This util class helps with keeping a cycle time during a control cycle.
std::string getName() const
Retrieve name of object.
bool synchronizeLocalClone(const VirtualRobot::RobotPtr &robot) const
bool isStopped()
Retrieve whether stop() has been called.
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
#define ARMARX_VERBOSE
The logging level for verbose information.
const VariantTypeId FramedPose
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceInternal::Handle< FramedPose > FramedPosePtr
SimpleRunningTask(Ts...) -> SimpleRunningTask< std::function< void(void)> >
void RemoteGui_startRunningTask()
void RemoteGui_createTab(std::string const &name, RemoteGui::Client::Widget const &rootWidget, RemoteGui::Client::Tab *tab)