RobotWriter.cpp
Go to the documentation of this file.
1 #include "RobotWriter.h"
2 
3 #include <chrono>
4 #include <mutex>
5 #include <optional>
6 #include <thread>
7 
13 
22 #include <RobotAPI/libraries/armem_robot_state/aron/Proprioception.aron.generated.h>
23 #include <RobotAPI/libraries/armem_robot_state/aron/Robot.aron.generated.h>
24 #include <RobotAPI/libraries/armem_robot_state/aron/RobotState.aron.generated.h>
25 #include <RobotAPI/libraries/armem_robot_state/aron/Transform.aron.generated.h>
26 #include <RobotAPI/libraries/armem_robot_state/aron/TransformHeader.aron.generated.h>
30 
31 
32 namespace fs = ::std::filesystem;
33 
35 {
37  properties(r.properties), propertyPrefix(r.propertyPrefix), memoryWriter(r.memoryWriter)
38  {
39  }
40 
41  RobotWriter::~RobotWriter() = default;
42 
43  void
45  {
46  }
47 
48  void
50  {
51  // Wait for the memory to become available and add it as dependency.
52  ARMARX_IMPORTANT << "RobotWriter: Waiting for memory '" << constants::memoryName << "' ...";
53  try
54  {
55  memoryWriter = memoryNameSystem.useWriter(constants::memoryName);
56  ARMARX_IMPORTANT << "RobotWriter: Connected to memory '" << constants::memoryName
57  << "'";
58  }
60  {
61  ARMARX_ERROR << e.what();
62  return;
63  }
64  }
65 
66  bool
68  const armem::Time& timestamp)
69  {
70  const auto validTimestamp = timestamp.isInvalid() ? armarx::Clock::Now() : timestamp;
71 
72  const auto providerId = armem::MemoryID(
74  const auto entityID =
75  providerId.withEntityName(constants::descriptionEntityName).withTimestamp(timestamp);
76 
78  update.entityID = entityID;
79 
80  arondto::RobotDescription aronDescription;
81  toAron(aronDescription, description);
82 
83  update.instancesData = {aronDescription.toAron()};
84  update.referencedTime = timestamp;
85 
86  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
88 
89  ARMARX_DEBUG << updateResult;
90 
91  if (not updateResult.success)
92  {
93  ARMARX_ERROR << updateResult.errorMessage;
94  return false;
95  }
96 
97  return true;
98  }
99 
100  bool
102  const std::string& robotName,
103  const std::string& robotRootNodeName,
104  const armem::Time& timestamp)
105  {
106 
107  const auto providerId =
109  const auto entityID = providerId.withEntityName(GlobalFrame + "," + robotRootNodeName)
110  .withTimestamp(timestamp);
111 
113  update.entityID = entityID;
114 
115  arondto::Transform aronTransform;
116 
117  // populate aronTransform
118  {
119  aronTransform.header.agent = robotName;
120  aronTransform.header.frame = robotRootNodeName;
121  aronTransform.header.parentFrame = GlobalFrame;
122  aronTransform.header.timestamp = timestamp;
123 
124  aronTransform.transform = globalRootPose;
125  }
126 
127  update.instancesData = {aronTransform.toAron()};
128  update.referencedTime = timestamp;
129 
130  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
132 
133  ARMARX_DEBUG << updateResult;
134 
135  if (not updateResult.success)
136  {
137  ARMARX_ERROR << updateResult.errorMessage;
138  return false;
139  }
140 
141  return true;
142  }
143 
144  bool
145  RobotWriter::storeProprioception(const std::map<std::string, float>& jointMap,
146  const std::string& robotTypeName,
147  const std::string& robotName,
148  const armem::Time& timestamp)
149  {
150 
151  const auto providerId = armem::MemoryID(
153  const auto entityID = providerId.withEntityName(robotName).withTimestamp(timestamp);
154 
156  update.entityID = entityID;
157 
158  arondto::Proprioception aronProprioception;
159  aronProprioception.joints.position = jointMap;
160 
161  update.instancesData = {aronProprioception.toAron()};
162  update.referencedTime = timestamp;
163 
164  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
166 
167  ARMARX_DEBUG << updateResult;
168 
169  if (not updateResult.success)
170  {
171  ARMARX_ERROR << updateResult.errorMessage;
172  return false;
173  }
174 
175  return true;
176  }
177 
178  bool
180  const std::string& robotTypeName,
181  const std::string& robotName,
182  const std::string& robotRootNodeName)
183  {
184  const bool successLoc = storeLocalization(
185  state.globalPose.matrix(), robotName, robotRootNodeName, state.timestamp);
186  const bool successProp =
187  storeProprioception(state.jointMap, robotTypeName, robotName, state.timestamp);
188 
189  return successLoc and successProp;
190  }
191 
192 
193 } // namespace armarx::armem::robot_state::client::common
armarx::core::time::DateTime::isInvalid
bool isInvalid() const
Definition: DateTime.cpp:140
armarx::armem::robot_state::description::RobotDescription
Definition: types.h:44
RobotWriter.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
LocalException.h
armarx::armem::robot_state::RobotWriter
The RobotReader class.
Definition: RobotWriter.h:43
armarx::armem::EntityUpdateResult
Result of an EntityUpdate.
Definition: Commit.h:72
armarx::armem::robot_state::RobotState
Definition: types.h:113
armarx::armem::robot_state::RobotWriter::storeLocalization
bool storeLocalization(const Eigen::Matrix4f &globalRootPose, const std::string &robotName, const std::string &robotRootNodeName, const armem::Time &timestamp)
Definition: RobotWriter.cpp:101
armarx::armem::robot_state::constants::descriptionEntityName
const std::string descriptionEntityName
Definition: constants.h:33
forward_declarations.h
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::armem::robot_state::constants::memoryName
const std::string memoryName
Definition: constants.h:26
armarx::armem::client::MemoryNameSystem::useWriter
Writer useWriter(const MemoryID &memoryID)
Use a memory server and get a writer for it.
Definition: MemoryNameSystem.cpp:276
constants.h
armarx::armem::robot_state::constants::proprioceptionCoreSegment
const std::string proprioceptionCoreSegment
Definition: constants.h:30
armarx::armem::robot_state
Definition: RobotStatePredictionClient.cpp:42
types.h
armarx::armem::robot_state::RobotState::jointMap
JointMap jointMap
Definition: types.h:121
armarx::armem::robot_state::constants::robotRootNodeName
const std::string robotRootNodeName
Definition: constants.h:36
armarx::armem::robot_state::RobotState::timestamp
DateTime timestamp
Definition: types.h:118
Clock.h
armarx::armem::robot_state::RobotWriter::memoryWriter
armem::client::Writer memoryWriter
Definition: RobotWriter.h:83
armarx::armem::robot_state::RobotWriter::storeState
bool storeState(const RobotState &state, const std::string &robotTypeName, const std::string &robotName, const std::string &robotRootNodeName) override
Definition: RobotWriter.cpp:179
armarx::armem::robot_state::RobotWriter::storeProprioception
bool storeProprioception(const std::map< std::string, float > &jointMap, const std::string &robotTypeName, const std::string &robotName, const armem::Time &timestamp)
Definition: RobotWriter.cpp:145
armarx::armem::robot_state::RobotWriter::connect
void connect(armem::client::MemoryNameSystem &memoryNameSystem)
Definition: RobotWriter.cpp:49
FramedPose.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
error.h
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
aron_conversions.h
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition: Writer.cpp:59
armarx::armem::robot_state::RobotWriter::RobotWriter
RobotWriter()=default
armarx::armem::robot_state::constants::descriptionCoreSegment
const std::string descriptionCoreSegment
Definition: constants.h:28
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::armem::robot_state::RobotState::globalPose
Pose globalPose
Definition: types.h:120
armarx::armem::robot_state::RobotWriter::~RobotWriter
~RobotWriter() override
types.h
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
memory_definitions.h
ExpressionException.h
armarx::armem::robot_state::RobotWriter::storeDescription
bool storeDescription(const description::RobotDescription &description, const armem::Time &timestamp=armem::Time::Invalid()) override
Definition: RobotWriter.cpp:67
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::armem::robot_state::description::RobotDescription::name
std::string name
Definition: types.h:48
armarx::armem::robot_state::constants::localizationCoreSegment
const std::string localizationCoreSegment
Definition: constants.h:29
IceUtil::Handle< class PropertyDefinitionContainer >
Time.h
Builder.h
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
Logging.h
robot_conversions.h
armarx::armem::robot_state::RobotWriter::registerPropertyDefinitions
void registerPropertyDefinitions(::armarx::PropertyDefinitionsPtr &def)
Definition: RobotWriter.cpp:44
armarx::armem::robot_state::toAron
void toAron(arondto::ObjectInstance &dto, const RobotState &bo)
Definition: aron_conversions.cpp:33
PackagePath.h
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29