GazeController.cpp
Go to the documentation of this file.
1#include "GazeController.h"
2
3#include <algorithm>
4#include <cstddef>
5#include <map>
6#include <memory>
7#include <string>
8#include <vector>
9
10#include <Ice/Current.h>
11#include <IceUtil/Time.h>
12
13#include <range/v3/range/conversion.hpp>
14#include <range/v3/view/zip.hpp>
15
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>
22
28#include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
29#include <ArmarXCore/interface/observers/ObserverInterface.h>
30#include <ArmarXCore/interface/observers/VariantBase.h>
32
36#include <RobotAPI/components/units/RobotUnit/RobotUnit.h> // FIXME avoid thi>
38#include <RobotAPI/interface/aron/Aron.h>
39#include <RobotAPI/interface/units/RobotUnit/NJointController.h>
40#include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
42
45#include <armarx/view_selection/gaze_controller/gaze_ik/aron/ControllerConfig.aron.generated.h>
48
50{
51
54
56 const NJointControllerConfigPtr& config,
58 {
59 ARMARX_RT_LOGF("Creating gaze controller");
60 // config
61 ConfigPtrT cfg = ConfigPtrT::dynamicCast(config);
63 // ARMARX_CHECK_EXPRESSION(!cfg->nodeSetName.empty());
64
65 ARMARX_CHECK_EXPRESSION(robotUnit);
66
67 const auto configData = arondto::Config::FromAron(cfg->config);
68
69 // robot
70 ARMARX_RT_LOGF("Setting up nodes");
71 {
72 _rtRobot = useSynchronizedRtRobot();
73 ARMARX_CHECK_NOT_NULL(_rtRobot);
74 _rtRobot->setThreadsafe(false);
75
76 _robotAdditionalTask = _rtRobot->clone();
77 _robotAdditionalTask->setThreadsafe(false);
78 RobotState initialState;
79 initialState.globalRobotPose = _rtRobot->getGlobalPose();
80
81 ARMARX_INFO << "Using nodeset `" << configData.params.nodeSetName << "`.";
82 _headNodeSet = _robotAdditionalTask->getRobotNodeSet(configData.params.nodeSetName);
83 ARMARX_CHECK_NOT_NULL(_headNodeSet);
84 ARMARX_INFO << VAROUT(_headNodeSet->size());
85
86 for (const auto& [name, value] : _robotAdditionalTask->getJointValues())
87 {
88 initialState.jointValues.push_back(value);
89
90 const auto node = _rtRobot->getRobotNode(name);
91 ARMARX_CHECK_NOT_NULL(node) << name;
92 _rtRobotNodes.push_back(node);
93
94 _additionalTaskRobotNodeNames.push_back(name);
95 }
96
97 ARMARX_CHECK_EQUAL(initialState.jointValues.size(), _rtRobotNodes.size());
98
99 _robotStateBuffer_rtToAdditionalTask.reinitAllBuffers(initialState);
100 Config initialConfig;
101 fromAron(configData, initialConfig);
102
103 ARMARX_INFO << VAROUT(initialConfig.target.position);
104
105 _configBuffer_updateConfigToAdditionalTask.reinitAllBuffers(initialConfig);
106 ARMARX_CHECK_NOT_NULL(_headNodeSet);
107
108 const auto tcpNode = _headNodeSet->getTCP();
109 _virtualPrismaticJoint =
110 std::dynamic_pointer_cast<VirtualRobot::RobotNodePrismatic>(tcpNode);
111 ARMARX_CHECK_NOT_NULL(_virtualPrismaticJoint);
112
113 ARMARX_INFO << "Setting up GazeIK.";
114 _gazeIK = std::make_unique<VirtualRobot::GazeIK>(_headNodeSet, _virtualPrismaticJoint);
115 _gazeIK->enableJointLimitAvoidance(configData.params.enableJointLimitAvoidance);
116 _gazeIK->setup(configData.params.maxPositionError,
117 configData.params.maxLoops,
118 configData.params.maxGradientDecentSteps);
119 _gazeIK->setVerbose(false);
120
121 // ARMARX_CHECK_NOT_NULL(_rtTorsoNode);
122 // joint positions to be controlled
123
124 ARMARX_INFO << "Requesting control targets";
125 std::vector<float> controlTargets;
126
127 for (const auto& nodeName : _headNodeSet->getNodeNames())
128 {
129 if (nodeName == _virtualPrismaticJoint->getName())
130 {
131 continue;
132 }
133
134 ARMARX_INFO << "Using control target `" << nodeName << "`.";
135 auto& target = _rtCtrlTargets.emplace_back(
137 nodeName, ControlModes::Position1DoF));
138 ARMARX_CHECK_NOT_NULL(target);
139 target->position = 0;
140
141 const auto node = _headNodeSet->getNode(nodeName);
142 ARMARX_CHECK_NOT_NULL(node) << nodeName;
143 // controlTargets.emplace_back(node->getJointValue());
144 controlTargets.emplace_back(0);
145 }
146 reinitTripleBuffer(controlTargets);
147 }
148
149 ARMARX_RT_LOGF("Nodes set up successfully");
150 }
151
153
154 void
155 GazeController::updateConfig(const ::armarx::aron::data::dto::DictPtr& dto,
156 const Ice::Current& /*iceCurrent*/)
157 {
158 ARMARX_INFO << "Controller::updateConfig";
159
160 auto updateConfigDto = arondto::Config::FromAron(dto);
161
162 Config config;
163 fromAron(updateConfigDto, config);
164
165 ARMARX_INFO << VAROUT(config.target.position.frame);
166
167 _configBuffer_updateConfigToAdditionalTask.getWriteBuffer() = config;
168 _configBuffer_updateConfigToAdditionalTask.commitWrite();
169 }
170
171 void
173 {
174 runTask("GazeControllerAdditionalTask",
175 [&]
176 {
177 CycleUtil c(10);
178 getObjectScheduler()->waitForObjectStateMinimum(eManagedIceObjectStarted);
179 ARMARX_IMPORTANT << "Create a new thread alone PlatformTrajectory controller";
180 while (getState() == eManagedIceObjectStarted)
181 {
182 if (isControllerActive() and rtReady.load())
183 {
184 ARMARX_VERBOSE << "additional task";
185 additionalTask();
186 }
187 c.waitForCycleDuration();
188 }
189 });
190 }
191
192 void
196
197 std::string
202
203 void
204 GazeController::rtRun(const IceUtil::Time& /*sensorValuesTimestamp*/,
205 const IceUtil::Time& /*timeSinceLastIteration*/)
206 {
208
209 // update control devices
210 ARMARX_CHECK_EQUAL(_rtCtrlTargets.size(), rtGetControlStruct().size());
211 for (std::size_t i = 0; i < _rtCtrlTargets.size(); i++)
212 {
213 _rtCtrlTargets.at(i)->position = rtGetControlStruct().at(i);
214 }
215
216 // read data (for non-rt)
217 _robotStateBuffer_rtToAdditionalTask.getWriteBuffer().globalRobotPose =
218 rtGetRobot()->getGlobalPose();
219
220 ARMARX_CHECK_EQUAL(_robotStateBuffer_rtToAdditionalTask.getWriteBuffer().jointValues.size(),
221 _rtRobotNodes.size());
222 for (std::size_t i = 0; i < _rtRobotNodes.size(); i++)
223 {
224 ARMARX_CHECK_NOT_NULL(_rtRobotNodes.at(i)) << i;
225 _robotStateBuffer_rtToAdditionalTask.getWriteBuffer().jointValues.at(i) =
226 _rtRobotNodes.at(i)->getJointValue();
227 }
228
229 _robotStateBuffer_rtToAdditionalTask.commitWrite();
230 }
231
232 void
234 {
235 rtReady.store(true);
236 }
237
238 void
240 {
241 rtReady.store(false);
242 }
243
244 void
247 const DebugObserverInterfacePrx& debugObserver)
248 {
249
250 const float currentPitchAngle = _publishCurrentPitchAngle;
251 const float currentYawAngle = _publishCurrentYawAngle;
252 const float targetPitchAngle = _publishTargetPitchAngle;
253 const float targetYawAngle = _publishTargetYawAngle;
254
255 StringVariantBaseMap datafields;
256 datafields["currentPitchAngle"] = new Variant(currentPitchAngle);
257 datafields["currentYawAngle"] = new Variant(currentYawAngle);
258 datafields["targetPitchAngle"] = new Variant(targetPitchAngle);
259 datafields["targetYawAngle"] = new Variant(targetYawAngle);
260 if (_gazeTarget_rtRunToPublishing.updateReadBuffer())
261 {
262 gaze_targets::GazeTarget target = _gazeTarget_rtRunToPublishing.getReadBuffer();
263 // this->publishTarget(target);
264 }
265 debugObserver->setDebugChannel(getInstanceName(), datafields);
266 }
267
268 void
269 GazeController::additionalTask()
270 {
271 const auto& configBuffer =
272 _configBuffer_updateConfigToAdditionalTask.getUpToDateReadBuffer();
273 const auto& robotStateBuffer = _robotStateBuffer_rtToAdditionalTask.getUpToDateReadBuffer();
274
275 // update robot
276 _robotAdditionalTask->setGlobalPose(robotStateBuffer.globalRobotPose);
277
278 ARMARX_CHECK_EQUAL(_additionalTaskRobotNodeNames.size(),
279 robotStateBuffer.jointValues.size());
280 const std::map<std::string, float> jointPositions =
281 ranges::views::zip(_additionalTaskRobotNodeNames, robotStateBuffer.jointValues) |
282 ranges::to<std::map>();
283
284 _robotAdditionalTask->setJointValues(jointPositions);
285
286 ARMARX_CHECK_NOT_NULL(_gazeIK);
287 ARMARX_CHECK_NOT_EMPTY(configBuffer.target.position.frame);
288
289 const Eigen::Vector3f globalPosition =
290 configBuffer.target.position.toGlobalEigen(_robotAdditionalTask);
291 ARMARX_VERBOSE << deactivateSpam(5) << "Trying to solve IK for target global position "
292 << globalPosition;
293 const bool solutionFound = _gazeIK->solve(globalPosition);
294
295 // store result
296 if (solutionFound)
297 {
298 for (std::size_t i = 0; i < _headNodeSet->getSize(); i++)
299 {
300 if (auto node = _headNodeSet->getNode(i); node != _headNodeSet->getTCP())
301 {
302 getWriterControlStruct().at(i) = node->getJointValue();
303 }
304
305 ARMARX_DEBUG << _headNodeSet->getNode(i)->getName() << ": "
306 << _headNodeSet->getNode(i)->getJointValue();
307 }
308 }
309 else
310 {
312 << "No IK solution found for Gaze IK to global target "
313 << globalPosition;
314 }
316
317 // store results (onPublish)
318 }
319
321 GazeController::getConfig(const ::Ice::Current&)
322 {
323 ARMARX_ERROR << "NYI";
324 return nullptr;
325 }
326} // namespace armarx::view_selection::gaze_controller::gaze_ik
#define ARMARX_RT_LOGF(...)
#define ARMARX_CHECK_NOT_EMPTY(c)
#define VAROUT(x)
constexpr T c
This util class helps with keeping a cycle time during a control cycle.
Definition CycleUtil.h:41
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.
Definition Logging.cpp:99
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 T & getUpToDateReadBuffer() const
The Variant class is described here: Variants.
Definition Variant.h:224
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 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 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.
Definition GazeTarget.h:23
#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.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
::IceInternal::Handle< Dict > DictPtr
const simox::meta::EnumNames< ControllerType > ControllerTypeNames
const armarx::NJointControllerRegistration< GazeController > RegistrationControllerGazeController(common::ControllerTypeNames.to_name(common::ControllerType::GazeControllerGazeIK))
void fromAron(const arondto::Config &dto, Config &bo)
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
std::map< std::string, VariantBasePtr > StringVariantBaseMap
IceUtil::Handle< class RobotUnit > RobotUnitPtr
Definition FTSensor.h:34
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
detail::ControlThreadOutputBufferEntry SensorAndControl
armarx::view_selection::gaze_controller::Target target