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
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
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
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
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.
void onExitComponent() override
Hook for 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)> >