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
107
108 void
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.
186 armem::robot_state::RobotState state{.timestamp = Clock::Now(),
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
static DateTime Now()
Current time on the virtual clock.
Definition Clock.cpp:93
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
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
std::string getName() const
Retrieve name of object.
The periodic task executes one thread method repeatedly using the time period specified in the constr...
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Creates the property definition container.
static DateTime Now()
Definition DateTime.cpp:51
Represents a duration.
Definition Duration.h:17
Measures the time this stop watch was inside the current scope.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
armarx::armem::robot_state::description::RobotDescription ArticulatedObjectDescription
Definition types.h:138
armarx::core::time::DateTime Time
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.