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
36
37namespace armarx
38{
39
42 {
45
46 defs->topic(debugObserver);
47
48 defs->optional(initialObjectIDs, "Objects", "Object IDs of objects to be tracked.")
49 .map(simox::alg::join(initialObjectIDs, ", "), initialObjectIDs);
50
51 defs->optional(singleShot, "SingleShot", "If true, publishes only one snapshot.");
52
53 return defs;
54 }
55
56 std::string
58 {
59 return "ObjectPoseProviderExample";
60 }
61
62 void
64 {
65 for (const auto& initial : initialObjectIDs)
66 {
67 // -1 -> infinitely
68 requestedObjects.requestObjects({ObjectID(initial)}, -1);
69 }
70
71 {
72 providerInfo.objectType = objpose::ObjectType::KnownObject;
73 std::vector<ObjectInfo> objects = objectFinder.findAllObjectsOfDataset("KIT");
74 for (const auto& obj : objects)
75 {
76 providerInfo.supportedObjects.push_back(armarx::toIce(obj.id()));
77 }
78 }
79 }
80
81 void
83 {
84 poseEstimationTask = new SimpleRunningTask<>([this]() { this->poseEstimationTaskRun(); });
85 poseEstimationTask->start();
86 }
87
88 void
92
93 void
97
98 objpose::ProviderInfo
100 {
101 return providerInfo;
102 }
103
104 objpose::provider::RequestObjectsOutput
105 ObjectPoseProviderExample::requestObjects(const objpose::provider::RequestObjectsInput& input,
106 const Ice::Current&)
107 {
108 ARMARX_INFO << "Requested object IDs for " << input.relativeTimeoutMS
109 << " ms: " << input.objectIDs;
110 {
111 std::scoped_lock lock(requestedObjectsMutex);
112 requestedObjects.requestObjects(input.objectIDs, input.relativeTimeoutMS);
113 }
114
115 objpose::provider::RequestObjectsOutput output;
116 // All requests are successful.
117 for (const auto& id : input.objectIDs)
118 {
119 output.results[id].success = true;
120 }
121 return output;
122 }
123
124 void
125 ObjectPoseProviderExample::poseEstimationTaskRun()
126 {
127 CycleUtil cycle(50);
128 IceUtil::Time start = TimeUtil::GetTime();
129
130 std::map<ObjectID, ObjectInfo> objectInfos;
131
132 while (poseEstimationTask and not poseEstimationTask->isStopped())
133 {
134 IceUtil::Time now = TimeUtil::GetTime();
135 float t = float((now - start).toSecondsDouble());
136
138 {
139 std::scoped_lock lock(requestedObjectsMutex);
140 update = requestedObjects.updateRequestedObjects(now);
141 }
142
143 if (update.added.size() > 0 || update.removed.size() > 0)
144 {
145 ARMARX_INFO << "Added: " << update.added << "Removed: " << update.removed;
146 }
147
148 int i = 0;
150 for (const ObjectID& id : update.current)
151 {
152 if (objectInfos.count(id) == 0)
153 {
154 if (std::optional<ObjectInfo> info = objectFinder.findObject(id))
155 {
156 objectInfos.emplace(id, *info);
157 }
158 else
159 {
160 ARMARX_WARNING << "Could not find object class '" << id << "'.";
161 }
162 }
163 const ObjectInfo& info = objectInfos.at(id);
164
165 armarx::objpose::ProvidedObjectPose& pose = poses.emplace_back();
166 pose.providerName = getName();
167 pose.objectType = objpose::ObjectType::KnownObject;
168
169 pose.objectID = info.id();
170
171 Eigen::Vector3f pos =
172 200 * Eigen::Vector3f(std::sin(t - i), std::cos(t - i), 1 + i);
173 Eigen::AngleAxisf ori((t - i) / M_PI_2, Eigen::Vector3f::UnitZ());
174 pose.objectPose = simox::math::pose(pos, ori);
175 // pose.objectPoseFrame = armarx::GlobalFrame;
176 pose.objectPoseFrame = "DepthCamera";
177
178 pose.objectPoseGaussian = objpose::PoseManifoldGaussian();
179 pose.objectPoseGaussian->mean = pose.objectPose;
180 pose.objectPoseGaussian->covariance = Eigen::Matrix6f::Identity();
181 // Translational (co)variance.
182 const float posVar = 10 + 10 * std::sin(t - i);
183 pose.objectPoseGaussian->covariance.diagonal()(0) = 1.0 * posVar;
184 pose.objectPoseGaussian->covariance.diagonal()(1) = 3.0 * posVar;
185 pose.objectPoseGaussian->covariance.diagonal()(2) = 2.0 * posVar;
186 if (i % 2 == 1)
187 {
188 Eigen::Matrix3f rot =
189 Eigen::AngleAxisf(0.25 * (t - i), Eigen::Vector3f::UnitZ())
190 .toRotationMatrix();
191 pose.objectPoseGaussian->positionCovariance() =
192 rot * pose.objectPoseGaussian->positionCovariance() * rot.transpose();
193 }
194
195 // Rotational (co)variance.
196 const float oriVar = (M_PI_4) + (M_PI_4)*std::sin(t - i);
197 pose.objectPoseGaussian->covariance.diagonal()(3) = 1.0 * oriVar;
198 pose.objectPoseGaussian->covariance.diagonal()(4) = 4.0 * oriVar;
199 pose.objectPoseGaussian->covariance.diagonal()(5) = 2.0 * oriVar;
200 if (i % 2 == 1)
201 {
202 Eigen::Matrix3f rot =
203 Eigen::AngleAxisf(0.25 * (t - i), Eigen::Vector3f::UnitZ())
204 .toRotationMatrix();
205 pose.objectPoseGaussian->orientationCovariance() =
206 rot * pose.objectPoseGaussian->orientationCovariance() * rot.transpose();
207 }
208
209 pose.confidence = 0.75 + 0.25 * std::sin(t - i);
210 pose.timestamp = DateTime::Now();
211
212 i++;
213 }
214
215 ARMARX_VERBOSE << "Reporting " << poses.size() << " object poses";
216 objectPoseTopic->reportObjectPoses(getName(), objpose::toIce(poses));
217
218 if (singleShot)
219 {
220 break;
221 }
222
223 cycle.waitForCycleDuration();
224 }
225 }
226
228} // namespace armarx
#define float
Definition 16_Level.h:22
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
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
Brief description of class ObjectPoseProviderExample.
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)> >