SegmentAdapter.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::armem_objects::SegmentAdapter
17  * @author Fabian Reister ( fabian dot reister at kit dot edu )
18  * @date 2024
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "SegmentAdapter.h"
24 
25 #include <iterator>
26 #include <memory>
27 #include <optional>
28 
29 #include <SimoxUtility/algorithm/get_map_keys_values.h>
30 #include <VirtualRobot/Robot.h>
31 
43 
49 #include <RobotAPI/libraries/armem_objects/aron/FamiliarObjectInstance.aron.generated.h>
50 #include <RobotAPI/libraries/armem_objects/aron/ObjectInstance.aron.generated.h>
54 
56 {
57 
58  SegmentAdapter::SegmentAdapter(MemoryToIceAdapter& iceMemory) : segment(iceMemory)
59  {
60  }
61 
62  std::string
64  {
65  return Logging::tag.tagName;
66  }
67 
68  void
70  {
71  segment.defineProperties(defs, prefix);
72  visu.defineProperties(defs, prefix + "visu.");
73  }
74 
75  void
77  {
78  segment.setTag(getName());
79  segment.decay.setTag(getName());
80  // segment.setPredictionEngines(predictionEngines);
81  visu.setTag(getName());
82 
83  segment.init();
84  }
85 
86  void
89  viz::Client arviz,
90  DebugObserverInterfacePrx debugObserver)
91  {
92  this->debugObserver = debugObserver;
93  this->arviz = arviz;
94 
95  ARMARX_CHECK_NOT_NULL(virtualRobotReader);
96  segment.robots.reader = virtualRobotReader;
97 
98  visu.arviz = arviz;
99 
100  if (!visu.updateTask)
101  {
102  ARMARX_IMPORTANT << "Enabled visu";
103  visu.updateTask = new SimpleRunningTask<>([this]() { this->visualizeRun(); });
104  visu.updateTask->start();
105  }
106 
107  segment.connect(arviz);
108  }
109 
110  void
112  const std::string& providerName,
113  const ::armarx::objpose::ProvidedFamiliarObjectPoseSeq& data,
114  const Ice::Current&)
115  {
116 
117  if (data.empty())
118  {
119  return;
120  }
121 
122  // Note, that in general, timestamps in `data` can differ. Therefore, we maintain the sync timestamp
123  // and check whether we have to update the robot. Otherwise, we can avoid these Ice calls.
124  std::optional<armarx::DateTime> syncTimestamp;
125 
126 
127  const auto convertAndFillFamiliarObjectInstances =
128  [this, providerName, &syncTimestamp](const ::armarx::aron::data::dto::DictPtr& data)
129  -> armarx::armem::arondto::FamiliarObjectInstance
130  {
131  auto famObjInstance = armarx::armem::arondto::FamiliarObjectInstance::FromAron(data);
132 
133 
134  armarx::FramedPose framedPose(famObjInstance.poseSensFrame.pose,
135  famObjInstance.poseSensFrame.header.frame,
136  famObjInstance.poseSensFrame.header.agent);
137 
138  ARMARX_CHECK_NOT_EMPTY(famObjInstance.poseSensFrame.header.agent);
139  ARMARX_CHECK_NOT_EMPTY(famObjInstance.poseSensFrame.header.frame);
140 
141  VirtualRobot::RobotPtr robot =
142  segment.robots.get(famObjInstance.poseSensFrame.header.agent, providerName);
143  ARMARX_CHECK_NOT_NULL(robot) << famObjInstance.poseSensFrame.header.agent;
144 
145 
146  if (not syncTimestamp.has_value() or syncTimestamp.value() != famObjInstance.timestamp)
147  {
148  ARMARX_CHECK(
149  segment.robots.reader->synchronizeRobot(*robot, famObjInstance.timestamp));
150  syncTimestamp = famObjInstance.timestamp;
151  }
152 
153 
154  // fill global pose info if not set yet
155  if (not famObjInstance.poseGlobal.has_value())
156  {
157  famObjInstance.poseGlobal.emplace();
158  famObjInstance.poseGlobal->pose = framedPose.toGlobalEigen(robot);
159  famObjInstance.poseGlobal->header.agent = famObjInstance.poseSensFrame.header.agent;
160  famObjInstance.poseGlobal->header.frame = armarx::GlobalFrame;
161  }
162  else
163  {
164  ARMARX_CHECK_EQUAL(famObjInstance.poseGlobal->header.frame, armarx::GlobalFrame);
165  }
166 
167 
168  return famObjInstance;
169  };
170 
171  std::vector<armarx::armem::arondto::FamiliarObjectInstance> familiarObjectInstances;
172  familiarObjectInstances.reserve(data.size());
173 
174  std::transform(std::begin(data),
175  std::end(data),
176  std::back_inserter(familiarObjectInstances),
177  convertAndFillFamiliarObjectInstances);
178 
179  segment.commit(familiarObjectInstances, providerName);
180  }
181 
182  void
183  SegmentAdapter::handleProviderUpdate(const std::string& providerName)
184  {
185  // Initialized to 0 on first access.
186  if (segment.providers.count(providerName) == 0)
187  {
188  segment.providers[providerName] = objpose::ProviderInfo();
189  }
190  }
191 
192  void
193  SegmentAdapter::visualizeRun()
194  {
195  if (not visu.enabled)
196  {
197  ARMARX_INFO << "Visu disabled";
198  return;
199  }
200 
202 
203  while (visu.updateTask && !visu.updateTask->isStopped())
204  {
205  {
206  std::scoped_lock lock(visuMutex);
207 
208  const std::map<std::string,
209  std::vector<armarx::armem::arondto::FamiliarObjectInstance>>
210  familiarObjectsByProvider = segment.doLocked(
211  [this]()
212  { return segment.getFamiliarObjectsByProvider(armarx::Clock::Now()); });
213 
214  ARMARX_VERBOSE << "Visualizing " << familiarObjectsByProvider.size()
215  << " providers.";
216  visu.visualizeFamiliarObjectsByProvider(familiarObjectsByProvider);
217  }
218 
219  metronome.waitForNextTick();
220  }
221  }
222 
223 
224 } // namespace armarx::armem::server::obj::familiar_object_instance
armarx::armem::server::obj::familiar_object_instance::SegmentAdapter::connect
void connect(std::experimental::observer_ptr< robot_state::VirtualRobotReader > virtualRobotReader, viz::Client arviz, DebugObserverInterfacePrx debugObserver)
Definition: SegmentAdapter.cpp:87
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:85
RemoteRobot.h
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::armem::server::obj::familiar_object_instance::Segment::getFamiliarObjectsByProvider
std::map< std::string, std::vector< armarx::armem::arondto::FamiliarObjectInstance > > getFamiliarObjectsByProvider(const DateTime &now)
Definition: Segment.cpp:170
armarx::armem::server::obj::familiar_object_instance::Segment::init
void init() override
Definition: Segment.cpp:93
ice_conversions.h
armarx::armem::server::obj::familiar_object_instance::SegmentAdapter::getName
std::string getName() const
Definition: SegmentAdapter.cpp:63
armarx::armem::server::obj::familiar_object_instance::Visu::updateTask
SimpleRunningTask ::pointer_type updateTask
Definition: Visu.h:64
predictions.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::LogTag::tagName
std::string tagName
Definition: LoggingUtil.h:75
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:280
armarx::armem::server::MemoryToIceAdapter
Helps connecting a Memory server to the Ice interface.
Definition: MemoryToIceAdapter.h:19
DateTime.h
Frequency.h
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::armem::server::obj::familiar_object_instance::Segment::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:78
armarx::armem::server::obj::familiar_object_instance::Visu::enabled
bool enabled
Definition: Visu.h:42
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
Duration.h
armarx::armem::server::obj::familiar_object_instance::Segment::connect
void connect(viz::Client arviz)
Definition: Segment.cpp:101
SegmentAdapter.h
armarx::armem::server::obj::familiar_object_instance::SegmentAdapter::reportFamiliarObjectPoses
void reportFamiliarObjectPoses(const std::string &providerName, const ::armarx::objpose::ProvidedFamiliarObjectPoseSeq &data, ICE_CURRENT_ARG) override
Definition: SegmentAdapter.cpp:111
ARMARX_CHECK_NOT_EMPTY
#define ARMARX_CHECK_NOT_EMPTY(c)
Definition: ExpressionException.h:224
ice_conversions.h
aron_conversions.h
std::experimental::fundamentals_v2::observer_ptr
Definition: ManagedIceObject.h:53
armarx::armem::server::obj::familiar_object_instance::Segment::providers
objpose::ProviderInfoMap providers
Definition: Segment.h:99
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Clock.h
armarx::armem::server::obj::familiar_object_instance::Segment::RobotsCache::reader
std::experimental::observer_ptr< robot_state::VirtualRobotReader > reader
Definition: Segment.h:87
armarx::FramedPose::toGlobalEigen
Eigen::Matrix4f toGlobalEigen(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:598
FramedPose.h
armarx::armem::server::obj::familiar_object_instance::Visu::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="visu.")
Definition: Visu.cpp:28
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::armem::server::segment::SpecializedCoreSegment::doLocked
auto doLocked(FunctionT &&function) const
Definition: SpecializedCoreSegment.h:38
ObjectID.h
armarx::core::time::Frequency::HertzDouble
static Frequency HertzDouble(double hertz)
Definition: Frequency.cpp:30
armarx::armem::server::obj::familiar_object_instance::SegmentAdapter::init
void init()
Definition: SegmentAdapter.cpp:76
Metronome.h
armarx::armem::server::obj::familiar_object_instance::Segment::robots
RobotsCache robots
Definition: Segment.h:96
armarx::armem::server::obj::familiar_object_instance::SegmentAdapter::SegmentAdapter
SegmentAdapter(MemoryToIceAdapter &iceMemory)
Definition: SegmentAdapter.cpp:58
armarx::armem::server::obj::familiar_object_instance::Visu::visualizeFamiliarObjectsByProvider
void visualizeFamiliarObjectsByProvider(const std::map< std::string, std::vector< armarx::armem::arondto::FamiliarObjectInstance >> &familiarObjectsByProvider)
Definition: Visu.cpp:56
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT >>
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:351
CycleUtil.h
ExpressionException.h
armarx::armem::server::obj::familiar_object_instance::Visu::frequencyHz
float frequencyHz
Definition: Visu.h:43
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:34
IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface >
armarx::Logging::tag
LogTag tag
Definition: Logging.h:278
armarx::armem::server::obj::familiar_object_instance::Segment::decay
Decay decay
Decay model.
Definition: Segment.h:105
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:93
ice_conversions_templates.h
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:54
Logging.h
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::armem::server::obj::familiar_object_instance
Definition: Decay.cpp:8
aron_conversions.h
armarx::armem::server::obj::familiar_object_instance::Segment::commit
void commit(const std::vector< armarx::armem::arondto::FamiliarObjectInstance > &familiarObjectInstances, const std::string &providerName)
Definition: Segment.cpp:110
armarx::viz::Client
Definition: Client.h:117
armarx::armem::server::obj::familiar_object_instance::SegmentAdapter::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="")
Definition: SegmentAdapter.cpp:69
Variant.h
aron_conversions.h
armarx::armem::server::obj::familiar_object_instance::Visu::arviz
viz::Client arviz
Definition: Visu.h:40
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::armem::server::obj::familiar_object_instance::Segment::RobotsCache::get
VirtualRobot::RobotPtr get(const std::string &robotName, const std::string &providerName="")
Definition: Segment.cpp:203