RetrieveHand.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 * @package control::ArmarXObjects::retrieve_hand
17 * @author Jianfeng Gao ( jianfeng dot gao at kit dot edu )
18 * @date 2023
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "RetrieveHand.h"
24
25#include <VirtualRobot/VirtualRobot.h>
28#include <armarx/control/deprecated_njoint_mp_controller/task_space/ControllerInterface.h>
30
32{
34
41
42 void
43 RetrieveHand::_deleteTSVMPController()
44 {
45 NJointControllerInterfacePrx controller =
46 remote.robotUnit->getNJointController("dmpController_RetrieveHand");
47 if (controller)
48 {
49 ARMARX_DEBUG << "dmpController_RetrieveHand exists, but need to be deleted.";
50 if (controller->isControllerActive())
51 {
52 controller->deactivateController();
53 const auto metronomeTargetPeriod = armarx::Duration::MilliSeconds(10);
54 armarx::Metronome loopScheduler(metronomeTargetPeriod);
55 while (controller->isControllerActive())
56 {
57 loopScheduler.waitForNextTick();
58 }
59 }
60 controller->deleteController();
62 ARMARX_DEBUG << "dmpController_RetrieveHand is deleted";
63 }
64 }
65
66 bool
67 RetrieveHand::_runTSVMPController()
68 {
69 ARMARX_IMPORTANT << "Creating TSVMP Controller for Retrieve hand skill";
70 double phaseL = 100;
71 double phaseK = 1000;
72 double phaseDist0 = 50;
73 double phaseDist1 = 10;
74 double phaseKp = 2;
75 double phaseKd = 1;
76 double posToOriRatio = 10;
77 double amp = 1;
78 double maxLinearVel = 1000;
79 double maxAngularVel = 10;
80
81 _deleteTSVMPController();
82
83 armarx::NJointTaskSpaceDMPControllerConfigPtr tsConfig =
85 20,
86 1,
87 "Linear",
88 "Discrete",
89 amp,
90 phaseL,
91 phaseK,
92 phaseDist0,
93 phaseDist1,
94 phaseKp,
95 phaseKd,
97 posToOriRatio,
99 "",
100 "",
102 maxLinearVel,
103 maxAngularVel,
104 50.0,
105 "DMP",
110 1.0,
111 1.0);
112
113 armarx::NJointTaskSpaceDMPControllerInterfacePrx dmpController =
114 armarx::NJointTaskSpaceDMPControllerInterfacePrx::checkedCast(
115 remote.robotUnit->createNJointController(
116 "NJointTSDMPController", "dmpController_RetrieveHand", tsConfig));
117
118
119 std::vector<std::string> fileNames = properties.fileNames;
120 dmpController->learnDMPFromFiles(fileNames);
121
122 // load robot
124 robotReader.connect(remote.memoryNameSystem);
125
128 robotReader.synchronizeRobotJoints(*r, now);
129 // auto r = robotReader.getSynchronizedRobot(properties.robotName, now);
130 ARMARX_CHECK(r.get());
131
132 std::vector<double> goals;
133 Eigen::Matrix4f targetPoseInRoot = properties.targetPose.toRootFrame(r)->toEigen();
134 // Eigen::Matrix4f targetPoseInRoot = properties.targetPose.toEigen();
135 goals = armarx::control::common::mat4ToDVec(targetPoseInRoot);
136 ARMARX_INFO << "Running VMP with goal \n"
137 << VAROUT(targetPoseInRoot) << ", vec: \n"
139
140 // if (in.isViaPoseListSet() && in.isViaPoseCanValSet() && in.getViaPoseList().size() == in.getViaPoseCanVal().size())
141 // {
142 // for (size_t i = 0; i < in.getViaPoseList().size(); ++i)
143 // {
144 // dmpController->setViaPoints(in.getViaPoseCanVal().at(i), Helpers::pose2dvec(in.getViaPoseList().at(i)->toEigen()));
145 // }
146 // }
147
148 dmpController->activateController();
149
150 dmpController->runDMP(goals, 1.0);
151
152 const auto metronomeTargetPeriod = armarx::Duration::MilliSeconds(10);
153 armarx::Metronome loopScheduler(metronomeTargetPeriod);
154 ARMARX_INFO << "VMP controller started";
155 while (running.load() && not dmpController->isFinished())
156 {
157 // ARMARX_INFO << VAROUT(dmpController->getCanVal());
158 loopScheduler.waitForNextTick();
159 }
160
161 ARMARX_DEBUG << "VMP Controller finished, try to delete controller";
162 dmpController->deactivateController();
163 while (dmpController->isControllerActive())
164 {
165 loopScheduler.waitForNextTick();
166 }
167 dmpController->deleteController();
168
169 running.store(false);
170 ARMARX_INFO << "Retrieve hand skill finished";
171 return true;
172 }
173
174} // namespace armarx::control::retrieve_hand::core
#define VAROUT(x)
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition Duration.cpp:48
FramedPosePtr toRootFrame(const SharedRobotInterfacePrx &referenceRobot) const
static void SleepMS(float milliseconds)
Definition TimeUtil.h:203
virtual void connect(armem::client::MemoryNameSystem &memoryNameSystem)
VirtualRobot::RobotPtr getRobot(const std::string &name, const armem::Time &timestamp=armem::Time::Invalid(), const VirtualRobot::RobotIO::RobotDescription &loadMode=VirtualRobot::RobotIO::RobotDescription::eStructure)
bool synchronizeRobotJoints(VirtualRobot::Robot &robot, const armem::Time &timestamp) const
Synchronize only the joint values of a virtual robot, according to the robot state memory for a certa...
RetrieveHand(const Remote &, const Properties &)
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
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#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
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
DVec mat4ToDVec(const Eigen::Matrix4f &mat)
convert Eigen:Matrix4f to 7D double vector.
Definition utils.cpp:359
std::string dVecToString(const DVec &dvec)
Definition utils.cpp:408
armarx::armem::client::MemoryNameSystem & memoryNameSystem