VirtualRobotReader.cpp
Go to the documentation of this file.
1 #include "VirtualRobotReader.h"
2 
3 #include <optional>
4 #include <thread>
5 
6 #include <VirtualRobot/Robot.h>
7 #include <VirtualRobot/VirtualRobot.h>
8 #include <VirtualRobot/XML/RobotIO.h>
9 
15 
17 {
18 
19  bool
21  const armem::Time& timestamp) const
22  {
23  const auto robotState = queryState(robot.getName(), timestamp);
24  if (not robotState)
25  {
26  ARMARX_VERBOSE << deactivateSpam(5) << "Querying robot state failed for robot `"
27  << robot.getName() << "` "
28  << "(type `" << robot.getType() << "`)!";
29  return false;
30  }
31 
32  robot.setJointValues(robotState->jointMap);
33  robot.setGlobalPose(robotState->globalPose.matrix());
34 
35  return true;
36  }
37 
38  bool
40  const armem::Time& timestamp) const
41  {
42  const auto robotState = queryJointState(robot.getName(), timestamp);
43  if (not robotState)
44  {
45  ARMARX_VERBOSE << deactivateSpam(5) << "Querying robot state failed for robot `"
46  << robot.getName() << "` "
47  << "(type `" << robot.getType() << "`)!";
48  return false;
49  }
50 
51  robot.setJointValues(robotState->jointMap);
52 
53  return true;
54  }
55 
57  VirtualRobotReader::getRobot(const std::string& name,
58  const armem::Time& timestamp,
59  const VirtualRobot::RobotIO::RobotDescription& loadMode)
60  {
61  ARMARX_VERBOSE << deactivateSpam(60) << "Querying robot description for robot '" << name
62  << "'";
63  const auto description = queryDescription(name, timestamp);
64 
65  if (not description)
66  {
67  ARMARX_VERBOSE << deactivateSpam(5) << "The description of robot `" << name
68  << "` is not a available!";
69 
70  return nullptr;
71  }
72 
73  const std::string xmlFilename = description->xml.toSystemPath();
74  ARMARX_CHECK(std::filesystem::exists(xmlFilename)) << xmlFilename;
75 
76  ARMARX_VERBOSE << deactivateSpam(5) << "Loading (virtual) robot '" << description->name
77  << "' from XML file '" << xmlFilename << "'";
78 
79  auto robot = VirtualRobot::RobotIO::loadRobot(xmlFilename, loadMode);
80  ARMARX_CHECK_NOT_NULL(robot) << "Could not load robot from file `" << xmlFilename << "`";
81 
82  robot->setName(name);
83 
84  return robot;
85  }
86 
88  VirtualRobotReader::getSynchronizedRobot(const std::string& name,
89  const VirtualRobot::BaseIO::RobotDescription& loadMode,
90  bool blocking)
91  {
92  return _getSynchronizedRobot(name, armem::Time::Invalid(), loadMode, blocking);
93  }
94 
97  const std::string& name,
98  const armem::Time& timestamp,
99  const VirtualRobot::RobotIO::RobotDescription& loadMode,
100  const bool blocking)
101  {
102  return _getSynchronizedRobot(name, timestamp, loadMode, blocking);
103  }
104 
106  VirtualRobotReader::_getSynchronizedRobot(
107  const std::string& name,
108  const Time& timestamp,
109  const VirtualRobot::BaseIO::RobotDescription& loadMode,
110  bool blocking)
111  {
112  while (blocking)
113  {
114  const auto ts = timestamp.isInvalid() ? armarx::Clock::Now() : timestamp;
115 
116  VirtualRobot::RobotPtr robot = getRobot(name, ts, loadMode);
117  if (robot and synchronizeRobot(*robot, ts))
118  {
119  return robot;
120  }
121 
122  ARMARX_VERBOSE << deactivateSpam(5) << "Retrying to query robot after failure";
124  }
125 
126  ARMARX_VERBOSE << deactivateSpam(5) << "Failed to get synchronized robot `" << name << "`";
127  return nullptr;
128  }
129 
130 
131 } // namespace armarx::armem::robot_state::client::common
armarx::armem::robot_state::RobotReader::queryJointState
std::optional< RobotState > queryJointState(const std::string &robotName, const armem::Time &timestamp) const
Definition: RobotReader.cpp:206
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::core::time::DateTime::isInvalid
bool isInvalid() const
Definition: DateTime.cpp:140
armarx::armem::robot_state::VirtualRobotReader::getRobot
VirtualRobot::RobotPtr getRobot(const std::string &name, const armem::Time &timestamp=armem::Time::Invalid(), const VirtualRobot::RobotIO::RobotDescription &loadMode=VirtualRobot::RobotIO::RobotDescription::eStructure)
Definition: VirtualRobotReader.cpp:57
armarx::core::time::Clock::WaitFor
static void WaitFor(const Duration &duration)
Wait for a certain duration on the virtual clock.
Definition: Clock.cpp:104
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::robot_state
Definition: RobotStatePredictionClient.cpp:42
armarx::armem::robot_state::VirtualRobotReader::synchronizeRobot
bool synchronizeRobot(VirtualRobot::Robot &robot, const armem::Time &timestamp) const
Definition: VirtualRobotReader.cpp:20
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Clock.h
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::armem::human::Robot
@ Robot
Definition: util.h:14
armarx::armem::robot_state::VirtualRobotReader::synchronizeRobotJoints
bool synchronizeRobotJoints(VirtualRobot::Robot &robot, const armem::Time &timestamp) const
Definition: VirtualRobotReader.cpp:39
VirtualRobotReader.h
armarx::armem::robot_state::RobotReader::queryDescription
std::optional< description::RobotDescription > queryDescription(const std::string &name, const armem::Time &timestamp) const
Definition: RobotReader.cpp:138
armarx::armem::robot_state::VirtualRobotReader::getSynchronizedRobot
VirtualRobot::RobotPtr getSynchronizedRobot(const std::string &name, const VirtualRobot::RobotIO::RobotDescription &loadMode=VirtualRobot::RobotIO::RobotDescription::eStructure, bool blocking=true)
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
CMakePackageFinder.h
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
Logging.h
armarx::armem::robot_state::RobotReader::queryState
std::optional< RobotState > queryState(const std::string &robotName, const armem::Time &timestamp) const
Definition: RobotReader.cpp:186
ArmarXDataPath.h
armarx::armem::robot_state::RobotReader::sleepAfterFailure
armem::Duration sleepAfterFailure
Definition: RobotReader.h:125
armarx::core::time::DateTime::Invalid
static DateTime Invalid()
Definition: DateTime.cpp:60
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
PackagePath.h