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 
33 
34 namespace armarx
35 {
36 
38  {
40 
41  defs->optional(p.predictionsPerObject, "predictions.NumberPerObject",
42  "How many predictions with increasing time offsets to make per object.");
43 
44  defs->optional(p.millisecondPredictionIncrement, "predictions.TimeIncrement",
45  "The size of the prediction time offset increment in milliseconds.");
46 
47  return defs;
48  }
49 
51  {
52  return "ObjectPoseClientExample";
53  }
54 
56  {
57  }
58 
60  {
62 
63  objectProcessingTask = new SimpleRunningTask<>([this]()
64  {
65  this->objectProcessingTaskRun();
66  });
67  objectProcessingTask->start();
68  }
69 
71  {
72  }
73 
75  {
76  }
77 
78 
79  void ObjectPoseClientExample::objectProcessingTaskRun()
80  {
81  CycleUtil cycle(50);
82 
83  while (objectProcessingTask && !objectProcessingTask->isStopped())
84  {
85  // This client can be copied to other classes to give them access to the object pose storage.
87  const objpose::ObjectPoseSeq objectPoses = client.fetchObjectPoses();
88 
89  ARMARX_VERBOSE << "Received poses of " << objectPoses.size() << " objects.";
90 
91  {
92  setDebugObserverDatafield("NumObjectPoses", objectPoses.size());
94  }
95 
96  viz::StagedCommit stage = arviz.stage();
97  {
98  // Visualize the objects.
99  viz::Layer layer = arviz.layer("Objects");
100  for (const objpose::ObjectPose& objectPose : objectPoses)
101  {
102  layer.add(viz::Object(objectPose.objectID.str())
103  .pose(objectPose.objectPoseGlobal)
104  .fileByObjectFinder(objectPose.objectID)
105  .alpha(objectPose.confidence));
106  }
107  stage.add(layer);
108  }
109  stage.add(visualizePredictions(client, objectPoses));
110  arviz.commit(stage);
111 
112  cycle.waitForCycleDuration();
113  }
114  }
115 
116 
117  viz::Layer
118  ObjectPoseClientExample::visualizePredictions(const objpose::ObjectPoseClient& client,
119  const objpose::ObjectPoseSeq& objectPoses)
120  {
121  viz::Layer layer = arviz.layer("PredictionArray");
122 
123  objpose::ObjectPosePredictionRequestSeq requests;
124  for (const objpose::ObjectPose& objectPose : objectPoses)
125  {
126  for (int i = 0; i < p.predictionsPerObject; ++i)
127  {
128  objpose::ObjectPosePredictionRequest request;
129  toIce(request.objectID, objectPose.objectID);
130  request.settings.predictionEngineID = "Linear Position Regression";
131  toIce(request.timestamp,
132  DateTime::Now() +
133  Duration::MilliSeconds((i + 1) * p.millisecondPredictionIncrement));
134  toIce(request.timeWindow, Duration::Seconds(10));
135  requests.push_back(request);
136  }
137  }
138 
139  objpose::ObjectPosePredictionResultSeq results;
140  try
141  {
142  results = client.objectPoseStorage->predictObjectPoses(requests);
143  }
144  catch (const Ice::LocalException& e)
145  {
146  ARMARX_INFO << "Failed to get predictions for object poses: " << e.what();
147  }
148 
149  for (size_t i = 0; i < results.size(); ++i)
150  {
151  const objpose::ObjectPosePredictionResult& result = results.at(i);
152  if (result.success)
153  {
154  auto predictedPose = armarx::fromIce<objpose::ObjectPose>(result.prediction);
155  Eigen::Vector3f predictedPosition =
156  simox::math::position(predictedPose.objectPoseGlobal);
157  int alpha =
158  (p.predictionsPerObject - (static_cast<int>(i) % p.predictionsPerObject)) *
159  255 / p.predictionsPerObject; // NOLINT
160  layer.add(
161  viz::Arrow(predictedPose.objectID.str() + " Linear Prediction " +
162  std::to_string(i % p.predictionsPerObject))
163  .fromTo(simox::math::position(
164  objectPoses.at(i / p.predictionsPerObject).objectPoseGlobal),
165  predictedPosition)
166  .color(viz::Color::green(255, alpha))); // NOLINT
167  }
168  else
169  {
170  ARMARX_INFO << "Prediction for object '" << result.prediction.objectID
171  << "' failed: " << result.errorMessage;
172  }
173  }
174 
175  return layer;
176  }
177 } // namespace armarx
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:70
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
armarx::viz::StagedCommit::add
void add(Layer const &layer)
Stage a layer to be committed later via client.apply(*this)
Definition: Client.h:37
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::objpose::ObjectPoseClient::fetchObjectPoses
ObjectPoseSeq fetchObjectPoses() const
Fetch all known object poses.
Definition: ObjectPoseClient.cpp:39
armarx::viz::Client::stage
StagedCommit stage()
Definition: Client.h:135
ice_conversions.h
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
armarx::ObjectPoseClientExample::getDefaultName
std::string getDefaultName() const override
Definition: ObjectPoseClientExample.cpp:50
armarx::viz::Object::fileByObjectFinder
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:53
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::DebugObserverComponentPluginUser::setDebugObserverBatchModeEnabled
void setDebugObserverBatchModeEnabled(bool enable)
Definition: DebugObserverComponentPlugin.cpp:118
armarx::CycleUtil
This util class helps with keeping a cycle time during a control cycle.
Definition: CycleUtil.h:40
armarx::viz::Arrow
Definition: Elements.h:198
ice_conversions.h
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::viz::StagedCommit
A staged commit prepares multiple layers to be committed.
Definition: Client.h:31
armarx::objpose::ObjectPoseClient
Provides access to the armarx::objpose::ObjectPoseStorageInterface (aka the object memory).
Definition: ObjectPoseClient.h:18
armarx::viz::Object::alpha
Object & alpha(float alpha)
Definition: Elements.cpp:61
armarx::ObjectPoseClientExample::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ObjectPoseClientExample.cpp:37
armarx::ObjectPoseClientExample::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ObjectPoseClientExample.cpp:74
armarx::toIce
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
Definition: ice_conversions_boost_templates.h:15
armarx::core::time::Duration::Seconds
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:83
armarx::viz::Object
Definition: Elements.h:321
armarx::ObjectPoseClientExample::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ObjectPoseClientExample.cpp:59
armarx::ObjectPoseClientExample::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: ObjectPoseClientExample.cpp:55
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
CycleUtil.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
ObjectPoseClientExample.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:159
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:43
armarx::DebugObserverComponentPluginUser::sendDebugObserverBatch
void sendDebugObserverBatch()
Definition: DebugObserverComponentPlugin.cpp:122
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ObjectPoseClientExample::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ObjectPoseClientExample.cpp:70
ice_conversions_templates.h
armarx::viz::Color::green
static Color green(int g=255, int a=255)
Definition: Color.h:84
armarx::ObjectPoseClientPluginUser::getClient
objpose::ObjectPoseClient getClient() const
Definition: ObjectPoseClientPlugin.cpp:64
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:73
armarx::viz::Layer
Definition: Layer.h:12
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
armarx::DebugObserverComponentPluginUser::setDebugObserverDatafield
void setDebugObserverDatafield(Ts &&...ts) const
Definition: DebugObserverComponentPlugin.h:97