ObjectPoseClientExample.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::ArmarXObjects::ObjectPoseClientExample
17 * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18 * @date 2021
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
25#include <SimoxUtility/math/pose.h>
26
30
32
33namespace armarx
34{
35
38 {
41
42 defs->optional(p.predictionsPerObject,
43 "predictions.NumberPerObject",
44 "How many predictions with increasing time offsets to make per object.");
45
46 defs->optional(p.millisecondPredictionIncrement,
47 "predictions.TimeIncrement",
48 "The size of the prediction time offset increment in milliseconds.");
49
50 return defs;
51 }
52
53 std::string
55 {
56 return "ObjectPoseClientExample";
57 }
58
59 void
63
64 void
66 {
68
69 objectProcessingTask =
70 new SimpleRunningTask<>([this]() { this->objectProcessingTaskRun(); });
71 objectProcessingTask->start();
72 }
73
74 void
78
79 void
83
84 void
85 ObjectPoseClientExample::objectProcessingTaskRun()
86 {
87 CycleUtil cycle(50);
88
89 while (objectProcessingTask && !objectProcessingTask->isStopped())
90 {
91 // This client can be copied to other classes to give them access to the object pose storage.
93 const objpose::ObjectPoseSeq objectPoses = client.fetchObjectPoses();
94
95 ARMARX_VERBOSE << "Received poses of " << objectPoses.size() << " objects.";
96
97 {
98 setDebugObserverDatafield("NumObjectPoses", objectPoses.size());
100 }
101
102 viz::StagedCommit stage = arviz.stage();
103 {
104 // Visualize the objects.
105 viz::Layer layer = arviz.layer("Objects");
106 for (const objpose::ObjectPose& objectPose : objectPoses)
107 {
108 layer.add(viz::Object(objectPose.objectID.str())
109 .pose(objectPose.objectPoseGlobal)
110 .fileByObjectFinder(objectPose.objectID)
111 .alpha(objectPose.confidence));
112 }
113 stage.add(layer);
114 }
115 stage.add(visualizePredictions(client, objectPoses));
116 arviz.commit(stage);
117
118 cycle.waitForCycleDuration();
119 }
120 }
121
123 ObjectPoseClientExample::visualizePredictions(const objpose::ObjectPoseClient& client,
124 const objpose::ObjectPoseSeq& objectPoses)
125 {
126 viz::Layer layer = arviz.layer("PredictionArray");
127
128 objpose::ObjectPosePredictionRequestSeq requests;
129 for (const objpose::ObjectPose& objectPose : objectPoses)
130 {
131 for (int i = 0; i < p.predictionsPerObject; ++i)
132 {
133 objpose::ObjectPosePredictionRequest request;
134 toIce(request.objectID, objectPose.objectID);
135 request.settings.predictionEngineID = "Linear Position Regression";
136 toIce(request.timestamp,
137 DateTime::Now() +
138 Duration::MilliSeconds((i + 1) * p.millisecondPredictionIncrement));
139 toIce(request.timeWindow, Duration::Seconds(10));
140 requests.push_back(request);
141 }
142 }
143
144 objpose::ObjectPosePredictionResultSeq results;
145 try
146 {
147 results = client.objectPoseStorage->predictObjectPoses(requests);
148 }
149 catch (const Ice::LocalException& e)
150 {
151 ARMARX_INFO << "Failed to get predictions for object poses: " << e.what();
152 }
153
154 for (size_t i = 0; i < results.size(); ++i)
155 {
156 const objpose::ObjectPosePredictionResult& result = results.at(i);
157 if (result.success)
158 {
159 auto predictedPose = armarx::fromIce<objpose::ObjectPose>(result.prediction);
160 Eigen::Vector3f predictedPosition =
161 simox::math::position(predictedPose.objectPoseGlobal);
162 int alpha =
163 (p.predictionsPerObject - (static_cast<int>(i) % p.predictionsPerObject)) *
164 255 / p.predictionsPerObject; // NOLINT
165 layer.add(
166 viz::Arrow(predictedPose.objectID.str() + " Linear Prediction " +
167 std::to_string(i % p.predictionsPerObject))
168 .fromTo(simox::math::position(
169 objectPoses.at(i / p.predictionsPerObject).objectPoseGlobal),
170 predictedPosition)
171 .color(viz::Color::green(255, alpha))); // NOLINT
172 }
173 else
174 {
175 ARMARX_INFO << "Prediction for object '" << result.prediction.objectID
176 << "' failed: " << result.errorMessage;
177 }
178 }
179
180 return layer;
181 }
182} // namespace armarx
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
This util class helps with keeping a cycle time during a control cycle.
Definition CycleUtil.h:41
static DateTime Now()
Definition DateTime.cpp:51
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition Duration.cpp:72
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition Duration.cpp:48
void onInitComponent() override
Pure virtual hook for the subclass.
void onDisconnectComponent() override
Hook for subclass.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onConnectComponent() override
Pure virtual hook for the subclass.
void onExitComponent() override
Hook for subclass.
std::string getDefaultName() const override
objpose::ObjectPoseClient getClient() const
bool isStopped()
Retrieve whether stop() has been called.
Provides access to the armarx::objpose::ObjectPoseStorageInterface (aka the object memory).
ObjectPoseSeq fetchObjectPoses() const
Fetch all known object poses.
virtual Layer layer(std::string const &name) const
Definition Client.cpp:80
StagedCommit stage()
Definition Client.h:146
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition ElementOps.h:176
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::vector< ObjectPose > ObjectPoseSeq
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void fromIce(const std::map< IceKeyT, IceValueT > &iceMap, boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
SimpleRunningTask(Ts...) -> SimpleRunningTask< std::function< void(void)> >
void add(ElementT const &element)
Definition Layer.h:31
Object & fileByObjectFinder(const armarx::ObjectID &objectID, const std::string &objectsPackage=DefaultObjectsPackage, const std::string &relativeObjectsDirectory=DefaultRelativeObjectsDirectory)
Set the file so it could be found using armarx::ObjectFinder (also on remote machine).
Definition Elements.cpp:59
Object & alpha(float alpha)
Definition Elements.cpp:69
A staged commit prepares multiple layers to be committed.
Definition Client.h:30
void add(Layer const &layer)
Stage a layer to be committed later via client.apply(*this)
Definition Client.h:36