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
34
35namespace armarx
36{
37
40 {
43
44 defs->optional(p.predictionsPerObject,
45 "predictions.NumberPerObject",
46 "How many predictions with increasing time offsets to make per object.");
47
48 defs->optional(p.millisecondPredictionIncrement,
49 "predictions.TimeIncrement",
50 "The size of the prediction time offset increment in milliseconds.");
51
52 return defs;
53 }
54
55 std::string
57 {
58 return "ObjectPoseClientExample";
59 }
60
61 void
65
66 void
68 {
70
71 objectProcessingTask =
72 new SimpleRunningTask<>([this]() { this->objectProcessingTaskRun(); });
73 objectProcessingTask->start();
74 }
75
76 void
80
81 void
85
86 void
87 ObjectPoseClientExample::objectProcessingTaskRun()
88 {
89 CycleUtil cycle(50);
90
91 while (objectProcessingTask && !objectProcessingTask->isStopped())
92 {
93 // This client can be copied to other classes to give them access to the object pose storage.
95 const objpose::ObjectPoseSeq objectPoses = client.fetchObjectPoses();
96
97 ARMARX_VERBOSE << "Received poses of " << objectPoses.size() << " objects.";
98
99 {
100 setDebugObserverDatafield("NumObjectPoses", objectPoses.size());
102 }
103
104 viz::StagedCommit stage = arviz.stage();
105 {
106 // Visualize the objects.
107 viz::Layer layer = arviz.layer("Objects");
108 for (const objpose::ObjectPose& objectPose : objectPoses)
109 {
110 layer.add(viz::Object(objectPose.objectID.str())
111 .pose(objectPose.objectPoseGlobal)
112 .fileByObjectFinder(objectPose.objectID)
113 .alpha(objectPose.confidence));
114 }
115 stage.add(layer);
116 }
117 stage.add(visualizePredictions(client, objectPoses));
118 arviz.commit(stage);
119
120 cycle.waitForCycleDuration();
121 }
122 }
123
125 ObjectPoseClientExample::visualizePredictions(const objpose::ObjectPoseClient& client,
126 const objpose::ObjectPoseSeq& objectPoses)
127 {
128 viz::Layer layer = arviz.layer("PredictionArray");
129
130 objpose::ObjectPosePredictionRequestSeq requests;
131 for (const objpose::ObjectPose& objectPose : objectPoses)
132 {
133 for (int i = 0; i < p.predictionsPerObject; ++i)
134 {
135 objpose::ObjectPosePredictionRequest request;
136 toIce(request.objectID, objectPose.objectID);
137 request.settings.predictionEngineID = "Linear Position Regression";
138 toIce(request.timestamp,
139 DateTime::Now() +
140 Duration::MilliSeconds((i + 1) * p.millisecondPredictionIncrement));
141 toIce(request.timeWindow, Duration::Seconds(10));
142 requests.push_back(request);
143 }
144 }
145
146 objpose::ObjectPosePredictionResultSeq results;
147 try
148 {
149 results = client.objectPoseStorage->predictObjectPoses(requests);
150 }
151 catch (const Ice::LocalException& e)
152 {
153 ARMARX_INFO << "Failed to get predictions for object poses: " << e.what();
154 }
155
156 for (size_t i = 0; i < results.size(); ++i)
157 {
158 const objpose::ObjectPosePredictionResult& result = results.at(i);
159 if (result.success)
160 {
161 auto predictedPose = armarx::fromIce<objpose::ObjectPose>(result.prediction);
162 Eigen::Vector3f predictedPosition =
163 simox::math::position(predictedPose.objectPoseGlobal);
164 int alpha =
165 (p.predictionsPerObject - (static_cast<int>(i) % p.predictionsPerObject)) *
166 255 / p.predictionsPerObject; // NOLINT
167 layer.add(
168 viz::Arrow(predictedPose.objectID.str() + " Linear Prediction " +
169 std::to_string(i % p.predictionsPerObject))
170 .fromTo(simox::math::position(
171 objectPoses.at(i / p.predictionsPerObject).objectPoseGlobal),
172 predictedPosition)
173 .color(viz::Color::green(255, alpha))); // NOLINT
174 }
175 else
176 {
177 ARMARX_INFO << "Prediction for object '" << result.prediction.objectID
178 << "' failed: " << result.errorMessage;
179 }
180 }
181
182 return layer;
183 }
184
186} // namespace armarx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
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
Brief description of class ObjectPoseClientExample.
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