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 namespace 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
61  {
62  }
63 
64  void
66  {
68 
69  objectProcessingTask =
70  new SimpleRunningTask<>([this]() { this->objectProcessingTaskRun(); });
71  objectProcessingTask->start();
72  }
73 
74  void
76  {
77  }
78 
79  void
81  {
82  }
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 
122  viz::Layer
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
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:85
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:89
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:187
armarx::objpose::ObjectPoseClient::fetchObjectPoses
ObjectPoseSeq fetchObjectPoses() const
Fetch all known object poses.
Definition: ObjectPoseClient.cpp:34
armarx::viz::Client::stage
StagedCommit stage()
Definition: Client.h:145
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:54
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:59
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:51
armarx::DebugObserverComponentPluginUser::setDebugObserverBatchModeEnabled
void setDebugObserverBatchModeEnabled(bool enable)
Definition: DebugObserverComponentPlugin.cpp:129
armarx::DebugObserverComponentPluginUser::setDebugObserverDatafield
void setDebugObserverDatafield(Ts &&... ts) const
Definition: DebugObserverComponentPlugin.h:86
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:196
ice_conversions.h
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:31
armarx::viz::StagedCommit
A staged commit prepares multiple layers to be committed.
Definition: Client.h:30
armarx::objpose::ObjectPoseClient
Provides access to the armarx::objpose::ObjectPoseStorageInterface (aka the object memory).
Definition: ObjectPoseClient.h:17
armarx::viz::Object::alpha
Object & alpha(float alpha)
Definition: Elements.cpp:69
armarx::ObjectPoseClientExample::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ObjectPoseClientExample.cpp:37
armarx::ObjectPoseClientExample::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ObjectPoseClientExample.cpp:80
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:72
armarx::viz::Object
Definition: Elements.h:332
armarx::ObjectPoseClientExample::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ObjectPoseClientExample.cpp:65
armarx::ObjectPoseClientExample::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: ObjectPoseClientExample.cpp:60
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
CycleUtil.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
ObjectPoseClientExample.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:176
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:42
armarx::DebugObserverComponentPluginUser::sendDebugObserverBatch
void sendDebugObserverBatch()
Definition: DebugObserverComponentPlugin.cpp:135
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::ObjectPoseClientExample::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ObjectPoseClientExample.cpp:75
ice_conversions_templates.h
armarx::viz::Color::green
static Color green(int g=255, int a=255)
Definition: Color.h:94
armarx::ObjectPoseClientPluginUser::getClient
objpose::ObjectPoseClient getClient() const
Definition: ObjectPoseClientPlugin.cpp:70
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:80
armarx::viz::Layer
Definition: Layer.h:12
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:48