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{
54 {
55 addPlugin(articulatedObjectReaderPlugin);
56 addPlugin(articulatedObjectWriterPlugin);
57 }
58
61 {
64
65 defs->topic(debugObserver);
66
67 defs->optional(p.updateFrequency, "updateFrequency", "Memory update frequency (write).");
68
69 defs->optional(p.obj.dataset, "p.obj.dataset", "");
70 defs->optional(p.obj.className, "p.obj.class", "");
71
72
73 return defs;
74 }
75
76 std::string
78 {
79 return "ArticulatedObjectLocalizerExample";
80 }
81
82 void
84 {
85 // auto& articulatedObjectReader = articulatedObjectReaderPlugin->get();
86 auto& articulatedObjectWriter = articulatedObjectWriterPlugin->get();
87
88 articulatedObjectWriter.setProviderName(getName());
89 }
90
91 void
93 {
94 ARMARX_IMPORTANT << "Running example.";
95 start = armem::Time::Now();
96
98 this,
100 static_cast<int>(1000.f / p.updateFrequency));
101 task->start();
102 }
103
104 void
109
110 void
114
116 ArticulatedObjectLocalizerExample::createArticulatedObject()
117 {
118 auto& articulatedObjectReader = articulatedObjectReaderPlugin->get();
119
120 const std::string dishwasherName = p.obj.dataset + "/" + p.obj.className;
121
122 const auto descriptions =
123 articulatedObjectReader.queryDescriptions(armem::Time::Now(), std::nullopt);
124
125 ARMARX_INFO << "Found " << descriptions.size() << " articulated object descriptions";
126
127 for (const auto& description : descriptions)
128 {
129 ARMARX_INFO << "- " << description.name;
130 }
131
132 const auto it = std::find_if(
133 descriptions.begin(),
134 descriptions.end(),
136 { return desc.name == dishwasherName; });
137
138 if (it == descriptions.end())
139 {
140 ARMARX_WARNING << "Articulated object " << dishwasherName << " not (yet) available";
141 return nullptr;
142 }
143
144 auto obj = VirtualRobot::RobotIO::loadRobot(it->xml.toSystemPath(),
145 VirtualRobot::RobotIO::eStructure);
146
147 if (not obj)
148 {
149 return nullptr;
150 }
151
152 obj->setName("0"); // aka instance name
153 obj->setType(it->name); // aka dataset/class name
154
155 return obj;
156 }
157
158 void
160 {
161 if (articulatedObject == nullptr)
162 {
163 articulatedObject = createArticulatedObject();
164
165 if (articulatedObject == nullptr) // still
166 {
167 return;
168 }
169 }
170
171 const armem::robot_state::RobotState state = [this]()
172 {
173 // Query state from memory
174 {
175 const std::optional<armem::robot_state::RobotState> state =
176 articulatedObjectReaderPlugin->get().queryState(
177 articulatedObject->getType() + "/" + articulatedObject->getName(),
178 Clock::Now(),
179 p.obj.readProviderName);
180
181 if (state)
182 {
183 return state.value();
184 }
185 }
186
187 // The object does not exist in the memory (yet). Therefore, we create an initial state.
188 armem::robot_state::RobotState state{.timestamp = Clock::Now(),
189 .globalPose = Eigen::Isometry3f::Identity(),
190 .jointMap = articulatedObject->getJointValues(),
191 .proprioception = std::nullopt};
192
193 return state;
194 }();
195
196 articulatedObject->setGlobalPose(state.globalPose.matrix());
197
198 ARMARX_DEBUG << "Reporting articulated objects";
199
200 const armem::Time now = armem::Time::Now();
201 const float t = static_cast<float>((now - start).toSecondsDouble());
202
203 // move joints at certain frequency
204 const float k = (1 + std::sin(t / (M_2_PI))) / 2; // in [0,1]
205
206 auto jointValues = articulatedObject->getJointValues();
207
208 for (auto& [name, jointValue] : jointValues)
209 {
210 const auto node = articulatedObject->getRobotNode(name);
211 jointValue = node->unscaleJointValue(k, 0, 1);
212 }
213
214 // articulatedObject->setGlobalPose(simox::math::pose(Eigen::Vector3f(1000, 0, 0)));
215 articulatedObject->setJointValues(jointValues);
216
217 {
219 [this](const armarx::Duration& dur)
220 { ARMARX_INFO << "Storing object took " << dur << "."; });
221
222 auto& articulatedObjectWriter = articulatedObjectWriterPlugin->get();
223 articulatedObjectWriter.storeArticulatedObject(articulatedObject, now);
224 }
225 }
226
228 "ArticulatedObjectLocalizerExample");
229} // namespace armarx::articulated_object
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
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.