Segment.cpp
Go to the documentation of this file.
1 #include "Segment.h"
2 #include <filesystem>
3 #include <SimoxUtility/algorithm/string/string_tools.h>
4 
10 
13 
20 
21 #include <RobotAPI/libraries/armem_robot/aron/Robot.aron.generated.h>
24 #include <RobotAPI/libraries/armem_robot_state/aron/Proprioception.aron.generated.h>
26 
27 
29 {
30 
32  Base(memoryToIceAdapter, armem::robot_state::descriptionSegmentID.coreSegmentName, arondto::RobotDescription::ToAronType())
33  {
34  }
35 
36  Segment::~Segment() = default;
37 
38 
39  void Segment::onConnect(const RobotUnitInterfacePrx& robotUnitPrx)
40  {
41  robotUnit = robotUnitPrx;
42 
43  // store the robot description linked to the robot unit in this segment
44  updateRobotDescription();
45  }
46 
47 
48  void Segment::commitRobotDescription(const robot::RobotDescription& robotDescription)
49  {
50  const Time now = Time::Now();
51 
52  const MemoryID providerID = segmentPtr->id().withProviderSegmentName(robotDescription.name);
54  {
55  segmentPtr->addProviderSegment(providerID.providerSegmentName, arondto::RobotDescription::ToAronType());
56  }
57 
58 
60  update.entityID = providerID.withEntityName("description");
61  update.arrivedTime = update.referencedTime = update.sentTime = now;
62 
63  arondto::RobotDescription dto;
64  robot::toAron(dto, robotDescription);
65  update.instancesData =
66  {
67  dto.toAron()
68  };
69 
70  Commit commit;
71  commit.updates.push_back(update);
72  iceMemory.commitLocking(commit);
73  }
74 
75 
76  void Segment::updateRobotDescription()
77  {
78  ARMARX_CHECK_NOT_NULL(robotUnit);
79  KinematicUnitInterfacePrx kinematicUnit = robotUnit->getKinematicUnit();
80  if (kinematicUnit)
81  {
82  const std::string robotName = kinematicUnit->getRobotName();
83  const std::string robotFilename = kinematicUnit->getRobotFilename();
84 
85  const std::vector<std::string> packages = armarx::CMakePackageFinder::FindAllArmarXSourcePackages();
86  const std::string package = armarx::ArmarXDataPath::getProject(packages, robotFilename);
87 
88  // make sure that the relative path is without the 'package/' prefix
89  const std::string robotFileRelPath = [&robotFilename, &package]()-> std::string {
90 
91  if(simox::alg::starts_with(robotFilename, package))
92  {
93  // remove "package" + "/"
94  const std::string fixedRobotFilename = robotFilename.substr(package.size() + 1, -1);
95  return fixedRobotFilename;
96  }
97 
98  return robotFilename;
99  }();
100 
101  ARMARX_INFO << "Robot description '" << VAROUT(robotFileRelPath) << "' found in package " << package;
102 
103  const robot::RobotDescription robotDescription
104  {
105  .name = kinematicUnit->getRobotName(),
106  .xml = {package, robotFileRelPath}
107  };
108 
109  // make sure that the package path is valid
110  ARMARX_CHECK(std::filesystem::exists(robotDescription.xml.toSystemPath()));
111 
112  commitRobotDescription(robotDescription);
113  }
114  else
115  {
116  ARMARX_WARNING << "Robot unit '" << robotUnit->ice_getIdentity().name << "' does not have a kinematic unit."
117  << "\n Cannot commit robot description.";
118  }
119  }
120 
121 
124  {
125  return doLocked([this, &timestamp]()
126  {
127  return getRobotDescriptions(timestamp);
128  });
129  }
130 
131 
134  {
136  (void) timestamp; // Unused
137 
138  RobotDescriptionMap robotDescriptions;
139  segmentPtr->forEachEntity([this, &robotDescriptions](const wm::Entity & entity)
140  {
141  const wm::EntityInstance& entityInstance = entity.getLatestSnapshot().getInstance(0);
142  const auto description = robot::convertRobotDescription(entityInstance);
143  if (description)
144  {
145  ARMARX_DEBUG << "Key is " << armem::MemoryID(entity.id());
146  robotDescriptions.emplace(description->name, *description);
147  }
148  else
149  {
150  ARMARX_WARNING << "Could not convert entity instance to 'RobotDescription'";
151  }
152  });
153 
154  ARMARX_VERBOSE << deactivateSpam(60) << "Number of known robot descriptions: " << robotDescriptions.size();
155  return robotDescriptions;
156  }
157 
158 } // namespace armarx::armem::server::robot_state::description
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
Writer.h
armarx::armem::MemoryID::providerSegmentName
std::string providerSegmentName
Definition: MemoryID.h:52
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::server::MemoryToIceAdapter
Helps connecting a Memory server to the Ice interface.
Definition: MemoryToIceAdapter.h:19
armarx::armem::base::CoreSegmentBase::hasProviderSegment
bool hasProviderSegment(const std::string &name) const
Definition: CoreSegmentBase.h:116
armarx::armem::base::detail::GetLatestSnapshotMixin::getLatestSnapshot
auto & getLatestSnapshot(int snapshotIndex=0)
Retrieve the latest entity snapshot.
Definition: lookup_mixins.h:199
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::CMakePackageFinder::FindAllArmarXSourcePackages
static std::vector< std::string > FindAllArmarXSourcePackages()
Definition: CMakePackageFinder.cpp:438
MemoryID.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::robot_state::description::Segment::onConnect
void onConnect(const RobotUnitInterfacePrx &robotUnitPrx)
Definition: Segment.cpp:39
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
query_fns.h
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:43
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:30
armarx::armem::base::detail::ForEachEntityMixin::forEachEntity
bool forEachEntity(FunctionT &&func)
Definition: iteration_mixins.h:258
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::armem::Commit::updates
std::vector< EntityUpdate > updates
The entity updates.
Definition: Commit.h:97
armarx::armem::robot::RobotDescription
Definition: types.h:17
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::segmentPtr
server::wm::CoreSegment * segmentPtr
Definition: SpecializedSegment.h:54
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::server::robot_state::description
Definition: Segment.cpp:28
armarx::PackagePath::toSystemPath
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
Definition: PackagePath.cpp:54
armarx::armem::server::robot_state::description::Segment::~Segment
virtual ~Segment() override
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::server::segment::SpecializedCoreSegment::doLocked
auto doLocked(FunctionT &&function) const
Definition: SpecializedCoreSegment.h:39
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::server::robot_state::description::Segment::getRobotDescriptions
RobotDescriptionMap getRobotDescriptions(const armem::Time &timestamp) const
Definition: Segment.cpp:133
PluginAll.h
aron_conversions.h
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
aron_conversions.h
MemoryToIceAdapter.h
armarx::armem::server::robot_state::description::RobotDescriptionMap
std::unordered_map< std::string, robot::RobotDescription > RobotDescriptionMap
Definition: forward_declarations.h:64
aron_conversions.h
ExpressionException.h
armarx::armem::robot::RobotDescription::xml
PackagePath xml
Definition: types.h:22
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
CMakePackageFinder.h
armarx::armem::robot::RobotDescription::name
std::string name
Definition: types.h:21
armarx::armem::robot::convertRobotDescription
std::optional< RobotDescription > convertRobotDescription(const armem::wm::EntityInstance &instance)
Definition: robot_conversions.cpp:21
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::iceMemory
MemoryToIceAdapter & iceMemory
Definition: SpecializedSegment.h:60
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::armem::server::robot_state::description::Segment::Segment
Segment(server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:31
Segment.h
armarx::armem::server::robot_state::description::Segment::getRobotDescriptionsLocking
RobotDescriptionMap getRobotDescriptionsLocking(const armem::Time &timestamp) const
Definition: Segment.cpp:123
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
types.h
IceInternal::ProxyHandle<::IceProxy::armarx::RobotUnitInterface >
Builder.h
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
armarx::armem::robot_state::descriptionSegmentID
const MemoryID descriptionSegmentID
Definition: memory_ids.cpp:31
robot_conversions.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::server::wm::CoreSegment::addProviderSegment
ProviderSegment & addProviderSegment(const std::string &name, Args... args)
Definition: memory_definitions.h:101
ArmarXDataPath.h
armarx::armem::robot::toAron
void toAron(arondto::Robot &dto, const Robot &bo)
Definition: aron_conversions.cpp:21
armarx::armem::server::MemoryToIceAdapter::commitLocking
data::CommitResult commitLocking(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:163
memory_ids.h
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29