VirtualRobotWriterExample.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
23
24#include <memory>
25
26#include <Eigen/Geometry>
27
28#include <IceUtil/Time.h>
29
30#include <SimoxUtility/math/pose/pose.h>
31#include <VirtualRobot/Robot.h>
32#include <VirtualRobot/VirtualRobot.h>
33#include <VirtualRobot/XML/RobotIO.h>
34
42
49
51{
53 {
54 addPlugin(virtualRobotWriterPlugin);
55 }
56
59 {
62
63 // defs->topic(debugObserver);
64
65 defs->optional(p.updateFrequency, "updateFrequency", "Memory update frequency (write).");
66 defs->optional(p.robot.package, "robot.package", "");
67 defs->optional(p.robot.path, "robot.path", "");
68
69 return defs;
70 }
71
72 std::string
74 {
75 return "VirtualRobotWriterExample";
76 }
77
78 void
82
83 void
85 {
86 ARMARX_IMPORTANT << "Running example.";
87 start = armem::Time::Now();
88
90 this, &VirtualRobotWriterExample::run, static_cast<int>(1000.f / p.updateFrequency));
91 task->start();
92 }
93
94 void
99
100 void
104
106 VirtualRobotWriterExample::loadRobot() const
107 {
108 auto robot = VirtualRobot::RobotIO::loadRobot(
109 PackagePath(p.robot.package, p.robot.path).toSystemPath(),
110 VirtualRobot::RobotIO::eStructure);
111 return robot;
112 }
113
114 void
116 {
117 if (robot == nullptr)
118 {
119 robot = loadRobot();
120
121 if (robot == nullptr) // still
122 {
123 ARMARX_ERROR << "Loading robot failed";
124 return;
125 }
126 }
127
128 ARMARX_DEBUG << "Reporting robot";
129 ARMARX_CHECK(virtualRobotWriterPlugin->get().storeDescription(*robot));
130
131 const armem::Time now = armem::Time::Now();
132 const float t = float((now - start).toSecondsDouble());
133
134 // move joints at certain frequency
135 const float m = (1 + std::sin(t / (M_2_PI * 10))) / 2; // in [0,1]
136
137 auto jointValues = robot->getJointValues();
138 for (auto& [k, v] : jointValues)
139 {
140 const auto node = robot->getRobotNode(k);
141
142 if (node->isLimitless())
143 {
144 continue;
145 }
146
147 v = node->getJointLimitLow() +
148 (node->getJointLimitHigh() - node->getJointLimitLow()) * m;
149 }
150
151 robot->setGlobalPose(simox::math::pose(Eigen::Vector3f(1000, 0, 0)));
152 robot->setJointValues(jointValues);
153
154 ARMARX_CHECK(virtualRobotWriterPlugin->get().storeState(*robot, armarx::Clock::Now()));
155 }
156
157} // namespace armarx::virtual_robot_writer_example
#define float
Definition 16_Level.h:22
static DateTime Now()
Current time on the virtual clock.
Definition Clock.cpp:93
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
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
The periodic task executes one thread method repeatedly using the time period specified in the constr...
static DateTime Now()
Definition DateTime.cpp:51
void onInitComponent() override
Pure virtual hook for the subclass.
void onConnectComponent() override
Pure virtual hook for the subclass.
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
armarx::core::time::DateTime Time
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.