ArticulatedObjectLocalizerExample.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  * @author Fabian Reister ( fabian dot reister at kit dot edu )
17  * @date 2021
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
23 
24 #include <memory>
25 #include <optional>
26 
27 #include <Eigen/Geometry>
28 
29 #include <IceUtil/Time.h>
30 
31 #include <SimoxUtility/math/pose/pose.h>
32 #include <VirtualRobot/Robot.h>
33 #include <VirtualRobot/VirtualRobot.h>
34 #include <VirtualRobot/XML/RobotIO.h>
35 
45 
48 
50 {
52  {
53  addPlugin(articulatedObjectReaderPlugin);
54  addPlugin(articulatedObjectWriterPlugin);
55  }
56 
59  {
62 
63  defs->topic(debugObserver);
64 
65  defs->optional(p.updateFrequency, "updateFrequency", "Memory update frequency (write).");
66 
67  defs->optional(p.obj.dataset, "p.obj.dataset", "");
68  defs->optional(p.obj.className, "p.obj.class", "");
69 
70 
71  return defs;
72  }
73 
74  std::string
76  {
77  return "ArticulatedObjectLocalizerExample";
78  }
79 
80  void
82  {
83  // auto& articulatedObjectReader = articulatedObjectReaderPlugin->get();
84  auto& articulatedObjectWriter = articulatedObjectWriterPlugin->get();
85 
86  articulatedObjectWriter.setProviderName(getName());
87  }
88 
89  void
91  {
92  ARMARX_IMPORTANT << "Running example.";
93  start = armem::Time::Now();
94 
96  this,
98  static_cast<int>(1000.f / p.updateFrequency));
99  task->start();
100  }
101 
102  void
104  {
105  task->stop();
106  }
107 
108  void
110  {
111  }
112 
114  ArticulatedObjectLocalizerExample::createArticulatedObject()
115  {
116  auto& articulatedObjectReader = articulatedObjectReaderPlugin->get();
117 
118  const std::string dishwasherName = p.obj.dataset + "/" + p.obj.className;
119 
120  const auto descriptions =
121  articulatedObjectReader.queryDescriptions(armem::Time::Now(), std::nullopt);
122 
123  ARMARX_INFO << "Found " << descriptions.size() << " articulated object descriptions";
124 
125  for (const auto& description : descriptions)
126  {
127  ARMARX_INFO << "- " << description.name;
128  }
129 
130  const auto it = std::find_if(
131  descriptions.begin(),
132  descriptions.end(),
134  { return desc.name == dishwasherName; });
135 
136  if (it == descriptions.end())
137  {
138  ARMARX_WARNING << "Articulated object " << dishwasherName << " not (yet) available";
139  return nullptr;
140  }
141 
142  auto obj = VirtualRobot::RobotIO::loadRobot(it->xml.toSystemPath(),
143  VirtualRobot::RobotIO::eStructure);
144 
145  if (not obj)
146  {
147  return nullptr;
148  }
149 
150  obj->setName("0"); // aka instance name
151  obj->setType(it->name); // aka dataset/class name
152 
153  return obj;
154  }
155 
156  void
158  {
159  if (articulatedObject == nullptr)
160  {
161  articulatedObject = createArticulatedObject();
162 
163  if (articulatedObject == nullptr) // still
164  {
165  return;
166  }
167  }
168 
169  const armem::robot_state::RobotState state = [this]()
170  {
171  // Query state from memory
172  {
173  const std::optional<armem::robot_state::RobotState> state =
174  articulatedObjectReaderPlugin->get().queryState(
175  articulatedObject->getType() + "/" + articulatedObject->getName(),
176  Clock::Now(),
177  p.obj.readProviderName);
178 
179  if (state)
180  {
181  return state.value();
182  }
183  }
184 
185  // The object does not exist in the memory (yet). Therefore, we create an initial state.
187  .globalPose = Eigen::Isometry3f::Identity(),
188  .jointMap = articulatedObject->getJointValues(),
189  .proprioception = std::nullopt};
190 
191  return state;
192  }();
193 
194  articulatedObject->setGlobalPose(state.globalPose.matrix());
195 
196  ARMARX_DEBUG << "Reporting articulated objects";
197 
198  const armem::Time now = armem::Time::Now();
199  const float t = static_cast<float>((now - start).toSecondsDouble());
200 
201  // move joints at certain frequency
202  const float k = (1 + std::sin(t / (M_2_PI))) / 2; // in [0,1]
203 
204  auto jointValues = articulatedObject->getJointValues();
205 
206  for (auto& [name, jointValue] : jointValues)
207  {
208  const auto node = articulatedObject->getRobotNode(name);
209  jointValue = node->unscaleJointValue(k, 0, 1);
210  }
211 
212  // articulatedObject->setGlobalPose(simox::math::pose(Eigen::Vector3f(1000, 0, 0)));
213  articulatedObject->setJointValues(jointValues);
214 
215  {
217  [this](const armarx::Duration& dur)
218  { ARMARX_INFO << "Storing object took " << dur << "."; });
219 
220  auto& articulatedObjectWriter = articulatedObjectWriterPlugin->get();
221  articulatedObjectWriter.storeArticulatedObject(articulatedObject, now);
222  }
223  }
224 
225 } // namespace armarx::articulated_object
armarx::armem::robot_state::description::RobotDescription
Definition: types.h:44
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
StopWatch.h
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:51
armarx::armem::robot_state::RobotState
Definition: types.h:113
Duration.h
PeriodicTask.h
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
Definition: ManagedIceObject.h:186
armarx::articulated_object::ArticulatedObjectLocalizerExample::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ArticulatedObjectLocalizerExample.cpp:58
armarx::armem::robot_state::RobotState::timestamp
DateTime timestamp
Definition: types.h:118
Clock.h
armarx::articulated_object::ArticulatedObjectLocalizerExample::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ArticulatedObjectLocalizerExample.cpp:90
ArticulatedObjectLocalizerExample.h
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::articulated_object::ArticulatedObjectLocalizerExample::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ArticulatedObjectLocalizerExample.cpp:109
armarx::articulated_object::ArticulatedObjectLocalizerExample::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: ArticulatedObjectLocalizerExample.cpp:81
types.h
armarx::articulated_object::ArticulatedObjectLocalizerExample::run
void run()
Definition: ArticulatedObjectLocalizerExample.cpp:157
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::articulated_object
Definition: ArticulatedObjectLocalizerExample.cpp:49
armarx::armem::robot_state::RobotState::globalPose
Pose globalPose
Definition: types.h:120
CycleUtil.h
ExpressionException.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::articulated_object::ArticulatedObjectLocalizerExample::getDefaultName
std::string getDefaultName() const override
Definition: ArticulatedObjectLocalizerExample.cpp:75
armarx::core::time::ScopedStopWatch
Measures the time this stop watch was inside the current scope.
Definition: ScopedStopWatch.h:31
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:16
Time.h
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:93
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::PeriodicTask
Definition: ArmarXManager.h:70
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
ScopedStopWatch.h
armarx::articulated_object::ArticulatedObjectLocalizerExample::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ArticulatedObjectLocalizerExample.cpp:103
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::articulated_object::ArticulatedObjectLocalizerExample::ArticulatedObjectLocalizerExample
ArticulatedObjectLocalizerExample()
Definition: ArticulatedObjectLocalizerExample.cpp:51
PackagePath.h