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