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
34
36 "SimpleVirtualRobot");
37
39{
41 {
42 addPlugin(virtualRobotWriterPlugin);
43 }
44
47 {
50
51 // defs->topic(debugObserver);
52
53 defs->optional(
54 properties.oneShot, "p.oneShot", "If true, commit once after connecting, then stop.");
55 defs->optional(
56 properties.updateFrequency, "p.updateFrequency", "Memory update frequency (write).");
57
58 defs->optional(properties.robot.name,
59 "p.robot.name",
60 "Optional override for the robot name. If not set, the default name from "
61 "the robot model is used.");
62 defs->optional(
63 properties.robot.package, "p.robot.package", "Package of the Simox robot XML.");
64 defs->optional(properties.robot.path, "p.robot.path", "Local path of the Simox robot XML.");
65
66 defs->optional(properties.robot.jointValues,
67 "p.robot.jointValues",
68 "Specify a certain joint configuration.");
69
70 defs->optional(properties.robot.globalPositionX, "p.robot.globalPositionX", "");
71 defs->optional(properties.robot.globalPositionY, "p.robot.globalPositionY", "");
72 defs->optional(properties.robot.globalPositionYaw, "p.robot.globalPositionYaw", "");
73
74 return defs;
75 }
76
77 std::string
79 {
80 return "SimpleVirtualRobot";
81 }
82
83 void
87
88 void
90 {
91 task = new armarx::SimpleRunningTask<>([this]() { this->run(); });
92 task->start();
93 }
94
95 void
97 {
98 task->stop();
99 }
100
101 void
105
107 SimpleVirtualRobot::loadRobot(const Properties::Robot& p) const
108 {
109 if (p.package.empty() or p.path.empty())
110 {
111 ARMARX_IMPORTANT << "Robot model not specified. "
112 << "Please specify the Simox robot XMl file in the properties.";
113 return nullptr;
114 }
115
116 PackagePath path(p.package, p.path);
117 ARMARX_INFO << "Load robot from " << path << ".";
118
119 VirtualRobot::RobotPtr robot = VirtualRobot::RobotIO::loadRobot(
120 path.toSystemPath(), VirtualRobot::RobotIO::eStructure);
121
122 ARMARX_CHECK_NOT_NULL(robot) << "Failed to load robot `" << path << "`.";
123
124 if (not p.jointValues.empty())
125 {
126 ARMARX_DEBUG << "Parsing: " << p.jointValues;
127
128 const nlohmann::json j = nlohmann::json::parse(p.jointValues);
129 ARMARX_DEBUG << "JSON parsed as: " << j;
130
131 std::map<std::string, float> jointValues;
132 nlohmann::from_json(j, jointValues);
133
134 ARMARX_VERBOSE << "The following joint values are given by the user: " << jointValues;
135 robot->setJointValues(jointValues);
136 }
137
138 const Eigen::Isometry3f global_T_robot =
139 Eigen::Translation3f{
140 properties.robot.globalPositionX, properties.robot.globalPositionY, 0} *
141 Eigen::AngleAxisf{properties.robot.globalPositionYaw, Eigen::Vector3f::UnitZ()};
142 robot->setGlobalPose(global_T_robot.matrix());
143
144 if (not p.name.empty())
145 {
146 robot->setName(p.name);
147 }
148
149 return robot;
150 }
151
152 void
154 {
155 armarx::Metronome metronome(armarx::Frequency::HertzDouble(properties.updateFrequency));
156
157 while (task and not task->isStopped())
158 {
159 if (robot == nullptr)
160 {
161 robot = loadRobot(properties.robot);
162 if (robot)
163 {
164 ARMARX_INFO << "Start to commit robot '" << robot->getName()
165 << "' to the robot state memory.";
166 }
167 else
168 {
169 ARMARX_INFO << "Loading robot failed.";
170 return;
171 }
172 }
173
174 const armem::Time now = armem::Time::Now();
175 ARMARX_DEBUG << "Report robot.";
176
177 bool success = virtualRobotWriterPlugin->get().storeDescription(*robot, now);
179
180 success = virtualRobotWriterPlugin->get().storeState(*robot, now);
182
183 metronome.waitForNextTick();
184
185 if (properties.oneShot)
186 {
187 // Done.
188 return;
189 }
190 }
191 }
192
193} // namespace armarx::simple_virtual_robot
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
static Frequency HertzDouble(double hertz)
Definition Frequency.cpp:30
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
static DateTime Now()
Definition DateTime.cpp:51
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition Metronome.h:57
Duration waitForNextTick() const
Wait and block until the target period is met.
Definition Metronome.cpp:27
Brief description of class SimpleVirtualRobot.
void onInitComponent() override
Pure virtual hook for the subclass.
void onDisconnectComponent() override
Hook for subclass.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onConnectComponent() override
Pure virtual hook for the subclass.
std::string getDefaultName() const override
Retrieve default name of component.
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#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...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
armarx::core::time::DateTime Time
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
SimpleRunningTask(Ts...) -> SimpleRunningTask< std::function< void(void)> >