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
80  {
81  }
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
96  {
97  task->stop();
98  }
99 
100  void
102  {
103  }
104 
106  VirtualRobotWriterExample::loadRobot() const
107  {
108  auto robot =
109  VirtualRobot::RobotIO::loadRobot(PackagePath(p.robot.package, p.robot.path).toSystemPath(),
110  VirtualRobot::RobotIO::eStructure);
111  return robot;
112  }
113 
114 
115  void
117  {
118  if (robot == nullptr)
119  {
120  robot = loadRobot();
121 
122  if (robot == nullptr) // still
123  {
124  ARMARX_ERROR << "Loading robot failed";
125  return;
126  }
127  }
128 
129  ARMARX_DEBUG << "Reporting robot";
130  ARMARX_CHECK(virtualRobotWriterPlugin->get().storeDescription(*robot));
131 
132  const armem::Time now = armem::Time::Now();
133  const float t = float((now - start).toSecondsDouble());
134 
135  // move joints at certain frequency
136  const float m = (1 + std::sin(t / (M_2_PI * 10))) / 2; // in [0,1]
137 
138  auto jointValues = robot->getJointValues();
139  for (auto& [k, v] : jointValues)
140  {
141  const auto node = robot->getRobotNode(k);
142 
143  if (node->isLimitless())
144  {
145  continue;
146  }
147 
148  v = node->getJointLimitLow() +
149  (node->getJointLimitHigh() - node->getJointLimitLow()) * m;
150  }
151 
152  robot->setGlobalPose(simox::math::pose(Eigen::Vector3f(1000, 0, 0)));
153  robot->setJointValues(jointValues);
154 
155  ARMARX_CHECK(virtualRobotWriterPlugin->get().storeState(*robot, armarx::Clock::Now()));
156  }
157 
158 } // namespace armarx::virtual_robot_writer_example
armarx::virtual_robot_writer_example
Definition: VirtualRobotWriterExample.cpp:50
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::virtual_robot_writer_example::VirtualRobotWriterExample::run
void run()
Definition: VirtualRobotWriterExample.cpp:116
PeriodicTask.h
query_fns.h
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
armarx::virtual_robot_writer_example::VirtualRobotWriterExample::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: VirtualRobotWriterExample.cpp:101
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Clock.h
armarx::virtual_robot_writer_example::VirtualRobotWriterExample::VirtualRobotWriterExample
VirtualRobotWriterExample()
Definition: VirtualRobotWriterExample.cpp:52
armarx::virtual_robot_writer_example::VirtualRobotWriterExample::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: VirtualRobotWriterExample.cpp:58
armarx::PackagePath::toSystemPath
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
Definition: PackagePath.cpp:54
types.h
MemoryRemoteGui.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::virtual_robot_writer_example::VirtualRobotWriterExample::getDefaultName
std::string getDefaultName() const override
Definition: VirtualRobotWriterExample.cpp:73
armarx::virtual_robot_writer_example::VirtualRobotWriterExample::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: VirtualRobotWriterExample.cpp:79
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
VirtualRobotWriterExample.h
armarx::virtual_robot_writer_example::VirtualRobotWriterExample::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: VirtualRobotWriterExample.cpp:95
CycleUtil.h
ExpressionException.h
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::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
float
#define float
Definition: 16_Level.h:22
armarx::virtual_robot_writer_example::VirtualRobotWriterExample::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: VirtualRobotWriterExample.cpp:84
IceUtil::Handle< class PropertyDefinitionContainer >
Time.h
Builder.h
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
armarx::PeriodicTask
Definition: ArmarXManager.h:70
Logging.h
ArmarXDataPath.h
armarx::PackagePath
Definition: PackagePath.h:55
ice_conversions.h
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
PackagePath.h