ObjectPoseProviderExample.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::ObjectPoseProviderExample
17 * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18 * @date 2020
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
25#include <SimoxUtility/algorithm/string/string_tools.h>
26#include <SimoxUtility/math/pose/pose.h>
27
29
34
35namespace armarx
36{
37
40 {
43
44 defs->topic(debugObserver);
45
46 defs->optional(initialObjectIDs, "Objects", "Object IDs of objects to be tracked.")
47 .map(simox::alg::join(initialObjectIDs, ", "), initialObjectIDs);
48
49 defs->optional(singleShot, "SingleShot", "If true, publishes only one snapshot.");
50
51 return defs;
52 }
53
54 std::string
56 {
57 return "ObjectPoseProviderExample";
58 }
59
60 void
62 {
63 for (const auto& initial : initialObjectIDs)
64 {
65 // -1 -> infinitely
66 requestedObjects.requestObjects({ObjectID(initial)}, -1);
67 }
68
69 {
70 providerInfo.objectType = objpose::ObjectType::KnownObject;
71 std::vector<ObjectInfo> objects = objectFinder.findAllObjectsOfDataset("KIT");
72 for (const auto& obj : objects)
73 {
74 providerInfo.supportedObjects.push_back(armarx::toIce(obj.id()));
75 }
76 }
77 }
78
79 void
81 {
82 poseEstimationTask = new SimpleRunningTask<>([this]() { this->poseEstimationTaskRun(); });
83 poseEstimationTask->start();
84 }
85
86 void
90
91 void
95
96 objpose::ProviderInfo
98 {
99 return providerInfo;
100 }
101
102 objpose::provider::RequestObjectsOutput
103 ObjectPoseProviderExample::requestObjects(const objpose::provider::RequestObjectsInput& input,
104 const Ice::Current&)
105 {
106 ARMARX_INFO << "Requested object IDs for " << input.relativeTimeoutMS
107 << " ms: " << input.objectIDs;
108 {
109 std::scoped_lock lock(requestedObjectsMutex);
110 requestedObjects.requestObjects(input.objectIDs, input.relativeTimeoutMS);
111 }
112
113 objpose::provider::RequestObjectsOutput output;
114 // All requests are successful.
115 for (const auto& id : input.objectIDs)
116 {
117 output.results[id].success = true;
118 }
119 return output;
120 }
121
122 void
123 ObjectPoseProviderExample::poseEstimationTaskRun()
124 {
125 CycleUtil cycle(50);
126 IceUtil::Time start = TimeUtil::GetTime();
127
128 std::map<ObjectID, ObjectInfo> objectInfos;
129
130 while (poseEstimationTask and not poseEstimationTask->isStopped())
131 {
132 IceUtil::Time now = TimeUtil::GetTime();
133 float t = float((now - start).toSecondsDouble());
134
136 {
137 std::scoped_lock lock(requestedObjectsMutex);
138 update = requestedObjects.updateRequestedObjects(now);
139 }
140
141 if (update.added.size() > 0 || update.removed.size() > 0)
142 {
143 ARMARX_INFO << "Added: " << update.added << "Removed: " << update.removed;
144 }
145
146 int i = 0;
148 for (const ObjectID& id : update.current)
149 {
150 if (objectInfos.count(id) == 0)
151 {
152 if (std::optional<ObjectInfo> info = objectFinder.findObject(id))
153 {
154 objectInfos.emplace(id, *info);
155 }
156 else
157 {
158 ARMARX_WARNING << "Could not find object class '" << id << "'.";
159 }
160 }
161 const ObjectInfo& info = objectInfos.at(id);
162
163 armarx::objpose::ProvidedObjectPose& pose = poses.emplace_back();
164 pose.providerName = getName();
165 pose.objectType = objpose::ObjectType::KnownObject;
166
167 pose.objectID = info.id();
168
169 Eigen::Vector3f pos =
170 200 * Eigen::Vector3f(std::sin(t - i), std::cos(t - i), 1 + i);
171 Eigen::AngleAxisf ori((t - i) / M_PI_2, Eigen::Vector3f::UnitZ());
172 pose.objectPose = simox::math::pose(pos, ori);
173 // pose.objectPoseFrame = armarx::GlobalFrame;
174 pose.objectPoseFrame = "DepthCamera";
175
176 pose.objectPoseGaussian = objpose::PoseManifoldGaussian();
177 pose.objectPoseGaussian->mean = pose.objectPose;
178 pose.objectPoseGaussian->covariance = Eigen::Matrix6f::Identity();
179 // Translational (co)variance.
180 const float posVar = 10 + 10 * std::sin(t - i);
181 pose.objectPoseGaussian->covariance.diagonal()(0) = 1.0 * posVar;
182 pose.objectPoseGaussian->covariance.diagonal()(1) = 3.0 * posVar;
183 pose.objectPoseGaussian->covariance.diagonal()(2) = 2.0 * posVar;
184 if (i % 2 == 1)
185 {
186 Eigen::Matrix3f rot =
187 Eigen::AngleAxisf(0.25 * (t - i), Eigen::Vector3f::UnitZ())
188 .toRotationMatrix();
189 pose.objectPoseGaussian->positionCovariance() =
190 rot * pose.objectPoseGaussian->positionCovariance() * rot.transpose();
191 }
192
193 // Rotational (co)variance.
194 const float oriVar = (M_PI_4) + (M_PI_4)*std::sin(t - i);
195 pose.objectPoseGaussian->covariance.diagonal()(3) = 1.0 * oriVar;
196 pose.objectPoseGaussian->covariance.diagonal()(4) = 4.0 * oriVar;
197 pose.objectPoseGaussian->covariance.diagonal()(5) = 2.0 * oriVar;
198 if (i % 2 == 1)
199 {
200 Eigen::Matrix3f rot =
201 Eigen::AngleAxisf(0.25 * (t - i), Eigen::Vector3f::UnitZ())
202 .toRotationMatrix();
203 pose.objectPoseGaussian->orientationCovariance() =
204 rot * pose.objectPoseGaussian->orientationCovariance() * rot.transpose();
205 }
206
207 pose.confidence = 0.75 + 0.25 * std::sin(t - i);
208 pose.timestamp = DateTime::Now();
209
210 i++;
211 }
212
213 ARMARX_VERBOSE << "Reporting " << poses.size() << " object poses";
214 objectPoseTopic->reportObjectPoses(getName(), objpose::toIce(poses));
215
216 if (singleShot)
217 {
218 break;
219 }
220
221 cycle.waitForCycleDuration();
222 }
223 }
224} // namespace armarx
#define float
Definition 16_Level.h:22
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
This util class helps with keeping a cycle time during a control cycle.
Definition CycleUtil.h:41
static DateTime Now()
Definition DateTime.cpp:51
std::string getName() const
Retrieve name of object.
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
objpose::provider::RequestObjectsOutput requestObjects(const objpose::provider::RequestObjectsInput &input, const Ice::Current &) override
std::string getDefaultName() const override
objpose::ProviderInfo getProviderInfo(const Ice::Current &=Ice::emptyCurrent) override
objpose::ObjectPoseStorageInterfacePrx objectPoseTopic
bool isStopped()
Retrieve whether stop() has been called.
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
float confidence
Confidence in [0, 1] (1 = full, 0 = none).
armarx::ObjectID objectID
The object ID, i.e. dataset, class name and instance name.
std::string providerName
Name of the providing component.
DateTime timestamp
Source timestamp.
ObjectType objectType
Known or unknown object.
std::optional< PoseManifoldGaussian > objectPoseGaussian
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition mongodb.cpp:68
std::vector< ProvidedObjectPose > ProvidedObjectPoseSeq
objpose::AABB toIce(const simox::AxisAlignedBoundingBox &aabb)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
SimpleRunningTask(Ts...) -> SimpleRunningTask< std::function< void(void)> >