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 
26 #include <Eigen/Geometry>
27 
28 #include <IceUtil/Time.h>
29 
30 #include <SimoxUtility/math/pose/pose.h>
31 #include <VirtualRobot/Robot.h>
32 #include <VirtualRobot/VirtualRobot.h>
33 #include <VirtualRobot/XML/RobotIO.h>
34 
41 
44 
46 {
48  {
49  addPlugin(articulatedObjectReaderPlugin);
50  addPlugin(articulatedObjectWriterPlugin);
51  }
52 
55  {
58 
59  defs->topic(debugObserver);
60 
61  defs->optional(p.updateFrequency, "updateFrequency", "Memory update frequency (write).");
62 
63  defs->optional(p.obj.dataset, "p.obj.dataset", "");
64  defs->optional(p.obj.className, "p.obj.class", "");
65 
66 
67  return defs;
68  }
69 
70  std::string
72  {
73  return "ArticulatedObjectLocalizerExample";
74  }
75 
76  void
78  {
79  // auto& articulatedObjectReader = articulatedObjectReaderPlugin->get();
80  auto& articulatedObjectWriter = articulatedObjectWriterPlugin->get();
81 
82  articulatedObjectWriter.setProviderName(getName());
83  }
84 
85  void
87  {
88  ARMARX_IMPORTANT << "Running example.";
89  start = armem::Time::Now();
90 
92  this,
94  static_cast<int>(1000.f / p.updateFrequency));
95  task->start();
96  }
97 
98  void
100  {
101  task->stop();
102  }
103 
104  void
106  {
107  }
108 
110  ArticulatedObjectLocalizerExample::createArticulatedObject()
111  {
112  auto& articulatedObjectReader = articulatedObjectReaderPlugin->get();
113 
114  const std::string dishwasherName = p.obj.dataset + "/" + p.obj.className;
115 
116  const auto descriptions =
117  articulatedObjectReader.queryDescriptions(armem::Time::Now(), std::nullopt);
118 
119  ARMARX_INFO << "Found " << descriptions.size() << " articulated object descriptions";
120 
121  for (const auto& description : descriptions)
122  {
123  ARMARX_INFO << "- " << description.name;
124  }
125 
126  const auto it = std::find_if(
127  descriptions.begin(),
128  descriptions.end(),
130  { return desc.name == dishwasherName; });
131 
132  if (it == descriptions.end())
133  {
134  ARMARX_WARNING << "Articulated object " << dishwasherName << " not (yet) available";
135  return nullptr;
136  }
137 
138  auto obj = VirtualRobot::RobotIO::loadRobot(it->xml.toSystemPath(),
139  VirtualRobot::RobotIO::eStructure);
140 
141  if (not obj)
142  {
143  return nullptr;
144  }
145 
146  obj->setName("0"); // aka instance name
147  obj->setType(it->name); // aka dataset/class name
148 
149  return obj;
150  }
151 
152  void
154  {
155  if (articulatedObject == nullptr)
156  {
157  articulatedObject = createArticulatedObject();
158 
159  if (articulatedObject == nullptr) // still
160  {
161  return;
162  }
163  }
164 
165  const auto state = articulatedObjectReaderPlugin->get().queryState(
166  articulatedObject->getType() + "/" + articulatedObject->getName(),
167  Clock::Now(),
168  p.obj.readProviderName);
169 
170  ARMARX_CHECK(state.has_value());
171  articulatedObject->setGlobalPose(state->globalPose.matrix());
172 
173  ARMARX_DEBUG << "Reporting articulated objects";
174 
175  const armem::Time now = armem::Time::Now();
176  const float t = static_cast<float>((now - start).toSecondsDouble());
177 
178  // move joints at certain frequency
179  const float k = (1 + std::sin(t / (M_2_PI))) / 2; // in [0,1]
180 
181  auto jointValues = articulatedObject->getJointValues();
182 
183  for (auto& [name, jointValue] : jointValues)
184  {
185  const auto node = articulatedObject->getRobotNode(name);
186  jointValue = node->unscaleJointValue(k, 0, 1);
187  }
188 
189  // articulatedObject->setGlobalPose(simox::math::pose(Eigen::Vector3f(1000, 0, 0)));
190  articulatedObject->setJointValues(jointValues);
191 
192  auto& articulatedObjectWriter = articulatedObjectWriterPlugin->get();
193  articulatedObjectWriter.storeArticulatedObject(articulatedObject, now);
194  }
195 
196 } // namespace armarx::articulated_object
armarx::armem::robot_state::description::RobotDescription
Definition: types.h:44
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
PeriodicTask.h
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
armarx::articulated_object::ArticulatedObjectLocalizerExample::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ArticulatedObjectLocalizerExample.cpp:54
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Clock.h
armarx::articulated_object::ArticulatedObjectLocalizerExample::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ArticulatedObjectLocalizerExample.cpp:86
ArticulatedObjectLocalizerExample.h
armarx::articulated_object::ArticulatedObjectLocalizerExample::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ArticulatedObjectLocalizerExample.cpp:105
armarx::articulated_object::ArticulatedObjectLocalizerExample::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: ArticulatedObjectLocalizerExample.cpp:77
types.h
armarx::articulated_object::ArticulatedObjectLocalizerExample::run
void run()
Definition: ArticulatedObjectLocalizerExample.cpp:153
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::articulated_object
Definition: ArticulatedObjectLocalizerExample.cpp:45
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:74
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::articulated_object::ArticulatedObjectLocalizerExample::getDefaultName
std::string getDefaultName() const override
Definition: ArticulatedObjectLocalizerExample.cpp:71
IceUtil::Handle< class PropertyDefinitionContainer >
Time.h
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::PeriodicTask
Definition: ArmarXManager.h:70
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::articulated_object::ArticulatedObjectLocalizerExample::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ArticulatedObjectLocalizerExample.cpp:99
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::articulated_object::ArticulatedObjectLocalizerExample::ArticulatedObjectLocalizerExample
ArticulatedObjectLocalizerExample()
Definition: ArticulatedObjectLocalizerExample.cpp:47
PackagePath.h