10#include <Ice/Current.h>
11#include <IceUtil/Time.h>
13#include <range/v3/range/conversion.hpp>
14#include <range/v3/view/zip.hpp>
16#include <VirtualRobot/IK/GazeIK.h>
17#include <VirtualRobot/Nodes/RobotNode.h>
18#include <VirtualRobot/Nodes/RobotNodePrismatic.h>
19#include <VirtualRobot/Robot.h>
20#include <VirtualRobot/RobotNodeSet.h>
21#include <VirtualRobot/VirtualRobot.h>
28#include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
29#include <ArmarXCore/interface/observers/ObserverInterface.h>
30#include <ArmarXCore/interface/observers/VariantBase.h>
38#include <RobotAPI/interface/aron/Aron.h>
39#include <RobotAPI/interface/units/RobotUnit/NJointController.h>
40#include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
45#include <armarx/view_selection/gaze_controller/gaze_ik/aron/ControllerConfig.aron.generated.h>
58 const NJointControllerConfigPtr& config,
63 ConfigPtrT cfg = ConfigPtrT::dynamicCast(config);
69 const auto configData = arondto::Config::FromAron(cfg->config);
76 _rtRobot->setThreadsafe(
false);
78 _robotAdditionalTask = _rtRobot->clone();
79 _robotAdditionalTask->setThreadsafe(
false);
80 RobotState initialState;
81 initialState.globalRobotPose = _rtRobot->getGlobalPose();
83 ARMARX_INFO <<
"Using nodeset `" << configData.params.nodeSetName <<
"`.";
84 _headNodeSet = _robotAdditionalTask->getRobotNodeSet(configData.params.nodeSetName);
88 for (
const auto& [name, value] : _robotAdditionalTask->getJointValues())
90 initialState.jointValues.push_back(value);
92 const auto node = _rtRobot->getRobotNode(name);
94 _rtRobotNodes.push_back(node);
96 _additionalTaskRobotNodeNames.push_back(name);
101 _robotStateBuffer_rtToAdditionalTask.reinitAllBuffers(initialState);
103 fromAron(configData, initialConfig);
107 _configBuffer_updateConfigToAdditionalTask.reinitAllBuffers(initialConfig);
110 const auto tcpNode = _headNodeSet->getTCP();
111 _virtualPrismaticJoint =
112 std::dynamic_pointer_cast<VirtualRobot::RobotNodePrismatic>(tcpNode);
116 _gazeIK = std::make_unique<VirtualRobot::GazeIK>(_headNodeSet, _virtualPrismaticJoint);
117 _gazeIK->enableJointLimitAvoidance(configData.params.enableJointLimitAvoidance);
118 _gazeIK->setup(configData.params.maxPositionError,
119 configData.params.maxLoops,
120 configData.params.maxGradientDecentSteps);
121 _gazeIK->setVerbose(
false);
127 std::vector<float> controlTargets;
129 for (
const auto& nodeName : _headNodeSet->getNodeNames())
131 if (nodeName == _virtualPrismaticJoint->getName())
136 ARMARX_INFO <<
"Using control target `" << nodeName <<
"`.";
137 auto& target = _rtCtrlTargets.emplace_back(
139 nodeName, ControlModes::Position1DoF));
141 target->position = 0;
143 const auto node = _headNodeSet->getNode(nodeName);
146 controlTargets.emplace_back(0);
158 const Ice::Current& )
162 auto updateConfigDto = arondto::Config::FromAron(dto);
171 _configBuffer_updateConfigToAdditionalTask.getWriteBuffer() = config;
172 _configBuffer_updateConfigToAdditionalTask.commitWrite();
180 runTask(
"GazeControllerAdditionalTask",
185 ARMARX_IMPORTANT <<
"Create a new thread alone PlatformTrajectory controller";
186 while (
getState() == eManagedIceObjectStarted)
193 c.waitForCycleDuration();
213 const IceUtil::Time& )
219 for (std::size_t i = 0; i < _rtCtrlTargets.size(); i++)
225 _robotStateBuffer_rtToAdditionalTask.getWriteBuffer().globalRobotPose =
228 ARMARX_CHECK_EQUAL(_robotStateBuffer_rtToAdditionalTask.getWriteBuffer().jointValues.size(),
229 _rtRobotNodes.size());
230 for (std::size_t i = 0; i < _rtRobotNodes.size(); i++)
233 _robotStateBuffer_rtToAdditionalTask.getWriteBuffer().jointValues.at(i) =
234 _rtRobotNodes.at(i)->getJointValue();
237 _robotStateBuffer_rtToAdditionalTask.commitWrite();
249 rtReady.store(
false);
258 const float currentPitchAngle = _publishCurrentPitchAngle;
259 const float currentYawAngle = _publishCurrentYawAngle;
260 const float targetPitchAngle = _publishTargetPitchAngle;
261 const float targetYawAngle = _publishTargetYawAngle;
264 datafields[
"currentPitchAngle"] =
new Variant(currentPitchAngle);
265 datafields[
"currentYawAngle"] =
new Variant(currentYawAngle);
266 datafields[
"targetPitchAngle"] =
new Variant(targetPitchAngle);
267 datafields[
"targetYawAngle"] =
new Variant(targetYawAngle);
268 datafields[
"angularError"] =
new Variant(_publishAngularError.load());
269 datafields[
"lateralError"] =
new Variant(_publishLateralError.load());
270 if (_gazeTarget_rtRunToPublishing.updateReadBuffer())
277 _statusReporter.report(_statusTopic);
281 GazeController::additionalTask()
283 const auto& configBuffer =
288 _robotAdditionalTask->setGlobalPose(robotStateBuffer.globalRobotPose);
291 robotStateBuffer.jointValues.size());
292 const std::map<std::string, float> jointPositions =
293 ranges::views::zip(_additionalTaskRobotNodeNames, robotStateBuffer.jointValues) |
294 ranges::to<std::map>();
296 _robotAdditionalTask->setJointValues(jointPositions);
301 const Eigen::Vector3f globalPosition =
302 configBuffer.target.position.toGlobalEigen(_robotAdditionalTask);
306 const Eigen::Vector3f gazeOrigin = _virtualPrismaticJoint->getGlobalPosition();
309 globalPosition - gazeOrigin,
310 (globalPosition - gazeOrigin).norm());
314 const bool solutionFound = _gazeIK->solve(globalPosition);
318 _statusReporter.
update(configBuffer.targetId, residual, solutionFound);
323 for (std::size_t i = 0; i < _headNodeSet->getSize(); i++)
325 if (
auto node = _headNodeSet->getNode(i); node != _headNodeSet->getTCP())
330 ARMARX_DEBUG << _headNodeSet->getNode(i)->getName() <<
": "
331 << _headNodeSet->getNode(i)->getJointValue();
337 <<
"No IK solution found for Gaze IK to global target "
#define ARMARX_RT_LOGF(...)
#define ARMARX_CHECK_NOT_EMPTY(c)
This util class helps with keeping a cycle time during a control cycle.
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
ArmarXObjectSchedulerPtr getObjectScheduler() const
int getState() const
Retrieve current state of the ManagedIceObject.
bool isControllerActive(const Ice::Current &=Ice::emptyCurrent) const final override
void runTask(const std::string &taskName, Task &&task)
Executes a given task in a separate thread from the Application ThreadPool.
const VirtualRobot::RobotPtr & useSynchronizedRtRobot(bool updateCollisionModel=false)
Requests a VirtualRobot for use in rtRun *.
std::string getInstanceName(const Ice::Current &=Ice::emptyCurrent) const final override
const VirtualRobot::RobotPtr & rtGetRobot()
TODO make protected and use attorneys.
ControlTargetBase * useControlTarget(const std::string &deviceName, const std::string &controlMode)
Declares to calculate the ControlTarget for the given ControlDevice in the given ControlMode when rtR...
const ControlTarget & rtGetControlStruct() const
void writeControlStruct()
bool rtUpdateControlStruct()
ControlTarget & getWriterControlStruct()
void reinitTripleBuffer(const ControlTarget &initial)
const T & getUpToDateReadBuffer() const
The Variant class is described here: Variants.
void update(std::int64_t targetId, const GazeResidual &residual, bool targetReachable)
Called from the control side, at whatever rate it computes the residual.
std::string getClassName(const Ice::Current &=Ice::emptyCurrent) const override
::armarx::aron::data::dto::DictPtr getConfig(const ::Ice::Current &=::Ice::emptyCurrent) override
GazeController(RobotUnitPtr robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &robot)
void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &) override
void onInitNJointController() override
~GazeController() override
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
void onConnectNJointController() override
void updateConfig(const ::armarx::aron::data::dto::DictPtr &dto, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
void rtPreActivateController() override
This function is called before the controller is activated.
control::ConfigurableNJointControllerConfigPtr ConfigPtrT
Business Object (BO) class of GazeTarget.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
#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_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
#define ARMARX_VERBOSE
The logging level for verbose information.
std::shared_ptr< class Robot > RobotPtr
::IceInternal::Handle< Dict > DictPtr
const simox::meta::EnumNames< ControllerType > ControllerTypeNames
const std::string GazeControllerStatusTopicName
Topic the low-level gaze controllers report their residual on.
This file is part of ArmarX.
const armarx::NJointControllerRegistration< GazeController > RegistrationControllerGazeController(common::ControllerTypeNames.to_name(common::ControllerType::GazeControllerGazeIK))
void fromAron(const arondto::Config &dto, Config &bo)
GazeResidual computeGazeResidual(const Eigen::Vector3f ¤tGazeDirection, const Eigen::Vector3f &targetDirection, const float targetDistance)
The gaze residual, in a form that means the same thing for every controller variant.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
std::map< std::string, VariantBasePtr > StringVariantBaseMap
IceUtil::Handle< class RobotUnit > RobotUnitPtr
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
detail::ControlThreadOutputBufferEntry SensorAndControl
How far the gaze currently is from a target.
float lateralError
Lateral miss distance at target range [mm].
float angularError
Angle between the gaze ray and the direction to the target [rad].
armarx::view_selection::gaze_controller::Target target
std::int64_t targetId
Identifies this control target; echoed back in the status reports.