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 "VirtualRobotWriterExample");
54
56{
58 {
59 addPlugin(virtualRobotWriterPlugin);
60 }
61
64 {
67
68 // defs->topic(debugObserver);
69
70 defs->optional(p.updateFrequency, "updateFrequency", "Memory update frequency (write).");
71 defs->optional(p.robot.package, "robot.package", "");
72 defs->optional(p.robot.path, "robot.path", "");
73
74 return defs;
75 }
76
77 std::string
79 {
80 return "VirtualRobotWriterExample";
81 }
82
83 void
87
88 void
90 {
91 ARMARX_IMPORTANT << "Running example.";
92 start = armem::Time::Now();
93
95 this, &VirtualRobotWriterExample::run, static_cast<int>(1000.f / p.updateFrequency));
96 task->start();
97 }
98
99 void
104
105 void
109
111 VirtualRobotWriterExample::loadRobot() const
112 {
113 auto robot = VirtualRobot::RobotIO::loadRobot(
114 PackagePath(p.robot.package, p.robot.path).toSystemPath(),
115 VirtualRobot::RobotIO::eStructure);
116 return robot;
117 }
118
119 void
121 {
122 if (robot == nullptr)
123 {
124 robot = loadRobot();
125
126 if (robot == nullptr) // still
127 {
128 ARMARX_ERROR << "Loading robot failed";
129 return;
130 }
131 }
132
133 ARMARX_DEBUG << "Reporting robot";
134 ARMARX_CHECK(virtualRobotWriterPlugin->get().storeDescription(*robot));
135
136 const armem::Time now = armem::Time::Now();
137 const float t = float((now - start).toSecondsDouble());
138
139 // move joints at certain frequency
140 const float m = (1 + std::sin(t / (M_2_PI * 10))) / 2; // in [0,1]
141
142 auto jointValues = robot->getJointValues();
143 for (auto& [k, v] : jointValues)
144 {
145 const auto node = robot->getRobotNode(k);
146
147 if (node->isLimitless())
148 {
149 continue;
150 }
151
152 v = node->getJointLimitLow() +
153 (node->getJointLimitHigh() - node->getJointLimitLow()) * m;
154 }
155
156 robot->setGlobalPose(simox::math::pose(Eigen::Vector3f(1000, 0, 0)));
157 robot->setJointValues(jointValues);
158
159 ARMARX_CHECK(virtualRobotWriterPlugin->get().storeState(*robot, armarx::Clock::Now()));
160 }
161
162} // namespace armarx::virtual_robot_writer_example
#define float
Definition 16_Level.h:22
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
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
Brief description of class VirtualRobotWriterExample.
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.