SimpleVirtualRobot.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  * @author Fabian Reister ( fabian dot reister at kit dot edu )
17  * @date 2021
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #include "SimpleVirtualRobot.h"
23 
24 #include <SimoxUtility/json/json.hpp>
25 #include <VirtualRobot/Robot.h>
26 #include <VirtualRobot/XML/RobotIO.h>
27 
33 
35 {
37  {
38  addPlugin(virtualRobotWriterPlugin);
39  }
40 
43  {
46 
47  // defs->topic(debugObserver);
48 
49  defs->optional(
50  properties.oneShot, "p.oneShot", "If true, commit once after connecting, then stop.");
51  defs->optional(
52  properties.updateFrequency, "p.updateFrequency", "Memory update frequency (write).");
53 
54  defs->optional(properties.robot.name,
55  "p.robot.name",
56  "Optional override for the robot name. If not set, the default name from "
57  "the robot model is used.");
58  defs->optional(
59  properties.robot.package, "p.robot.package", "Package of the Simox robot XML.");
60  defs->optional(properties.robot.path, "p.robot.path", "Local path of the Simox robot XML.");
61 
62  defs->optional(properties.robot.jointValues,
63  "p.robot.jointValues",
64  "Specify a certain joint configuration.");
65 
66  defs->optional(properties.robot.globalPositionX, "p.robot.globalPositionX", "");
67  defs->optional(properties.robot.globalPositionY, "p.robot.globalPositionY", "");
68  defs->optional(properties.robot.globalPositionYaw, "p.robot.globalPositionYaw", "");
69 
70  return defs;
71  }
72 
73  std::string
75  {
76  return "SimpleVirtualRobot";
77  }
78 
79  void
81  {
82  }
83 
84  void
86  {
87  task = new armarx::SimpleRunningTask<>([this]() { this->run(); });
88  task->start();
89  }
90 
91  void
93  {
94  task->stop();
95  }
96 
97  void
99  {
100  }
101 
103  SimpleVirtualRobot::loadRobot(const Properties::Robot& p) const
104  {
105  if (p.package.empty() or p.path.empty())
106  {
107  ARMARX_IMPORTANT << "Robot model not specified. "
108  << "Please specify the Simox robot XMl file in the properties.";
109  return nullptr;
110  }
111 
112  PackagePath path(p.package, p.path);
113  ARMARX_INFO << "Load robot from " << path << ".";
114 
115  VirtualRobot::RobotPtr robot = VirtualRobot::RobotIO::loadRobot(
116  path.toSystemPath(), VirtualRobot::RobotIO::eStructure);
117 
118  ARMARX_CHECK_NOT_NULL(robot) << "Failed to load robot `" << path << "`.";
119 
120  if (not p.jointValues.empty())
121  {
122  ARMARX_DEBUG << "Parsing: " << p.jointValues;
123 
124  const nlohmann::json j = nlohmann::json::parse(p.jointValues);
125  ARMARX_DEBUG << "JSON parsed as: " << j;
126 
127  std::map<std::string, float> jointValues;
128  nlohmann::from_json(j, jointValues);
129 
130  ARMARX_VERBOSE << "The following joint values are given by the user: " << jointValues;
131  robot->setJointValues(jointValues);
132  }
133 
134  const Eigen::Isometry3f global_T_robot =
135  Eigen::Translation3f{
136  properties.robot.globalPositionX, properties.robot.globalPositionY, 0} *
137  Eigen::AngleAxisf{properties.robot.globalPositionYaw, Eigen::Vector3f::UnitZ()};
138  robot->setGlobalPose(global_T_robot.matrix());
139 
140  if (not p.name.empty())
141  {
142  robot->setName(p.name);
143  }
144 
145  return robot;
146  }
147 
148  void
150  {
151  armarx::Metronome metronome(armarx::Frequency::HertzDouble(properties.updateFrequency));
152 
153  while (task and not task->isStopped())
154  {
155  if (robot == nullptr)
156  {
157  robot = loadRobot(properties.robot);
158  if (robot)
159  {
160  ARMARX_INFO << "Start to commit robot '" << robot->getName()
161  << "' to the robot state memory.";
162  }
163  else
164  {
165  ARMARX_INFO << "Loading robot failed.";
166  return;
167  }
168  }
169 
170  const armem::Time now = armem::Time::Now();
171  ARMARX_DEBUG << "Report robot.";
172 
173  bool success = virtualRobotWriterPlugin->get().storeDescription(*robot, now);
175 
176  success = virtualRobotWriterPlugin->get().storeState(*robot, now);
178 
179  metronome.waitForNextTick();
180 
181  if (properties.oneShot)
182  {
183  // Done.
184  return;
185  }
186  }
187  }
188 
189 } // namespace armarx::simple_virtual_robot
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:85
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::simple_virtual_robot::SimpleVirtualRobot::SimpleVirtualRobot
SimpleVirtualRobot()
Definition: SimpleVirtualRobot.cpp:36
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::data::from_json
void from_json(const nlohmann::json &j, PackagePath &pp)
Definition: json_conversions.h:42
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:51
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::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
Definition: ManagedIceObject.h:186
SimpleVirtualRobot.h
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::simple_virtual_robot
Definition: SimpleVirtualRobot.cpp:34
armarx::armem::human::Robot
@ Robot
Definition: util.h:17
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::core::time::Frequency::HertzDouble
static Frequency HertzDouble(double hertz)
Definition: Frequency.cpp:30
armarx::simple_virtual_robot::SimpleVirtualRobot::run
void run()
Definition: SimpleVirtualRobot.cpp:149
Metronome.h
armarx::simple_virtual_robot::SimpleVirtualRobot::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: SimpleVirtualRobot.cpp:85
armarx::simple_virtual_robot::SimpleVirtualRobot::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: SimpleVirtualRobot.cpp:80
ExpressionException.h
armarx::core::time::Metronome::waitForNextTick
Duration waitForNextTick()
Wait and block until the target period is met.
Definition: Metronome.cpp:27
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
armarx::simple_virtual_robot::SimpleVirtualRobot::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: SimpleVirtualRobot.cpp:42
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::simple_virtual_robot::SimpleVirtualRobot::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: SimpleVirtualRobot.cpp:92
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
Logging.h
armarx::simple_virtual_robot::SimpleVirtualRobot::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: SimpleVirtualRobot.cpp:98
armarx::PackagePath
Definition: PackagePath.h:52
armarx::simple_virtual_robot::SimpleVirtualRobot::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: SimpleVirtualRobot.cpp:74
armarx::status::success
@ success
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
PackagePath.h