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 
26 
28 #include <armarx/control/deprecated_njoint_mp_controller/task_space/ControllerInterface.h>
30 
32 {
34 
38  remote(remote), properties(properties)
39  {
40  }
41 
42  void
43  RetrieveHand::_deleteTSVMPController()
44  {
45  NJointControllerInterfacePrx controller = remote.robotUnit->getNJointController("dmpController_RetrieveHand");
46  if (controller)
47  {
48  ARMARX_DEBUG << "dmpController_RetrieveHand exists, but need to be deleted.";
49  if (controller->isControllerActive())
50  {
51  controller->deactivateController();
52  const auto metronomeTargetPeriod = armarx::Duration::MilliSeconds(10);
53  armarx::Metronome loopScheduler(metronomeTargetPeriod);
54  while (controller->isControllerActive())
55  {
56  loopScheduler.waitForNextTick();
57  }
58  }
59  controller->deleteController();
61  ARMARX_DEBUG << "dmpController_RetrieveHand is deleted";
62  }
63  }
64 
65  bool
66  RetrieveHand::_runTSVMPController()
67  {
68  ARMARX_IMPORTANT << "Creating TSVMP Controller for Retrieve hand skill";
69  double phaseL = 100;
70  double phaseK = 1000;
71  double phaseDist0 = 50;
72  double phaseDist1 = 10;
73  double phaseKp = 2;
74  double phaseKd = 1;
75  double posToOriRatio = 10;
76  double amp = 1;
77  double maxLinearVel = 1000;
78  double maxAngularVel = 10;
79 
80  _deleteTSVMPController();
81 
82  armarx::NJointTaskSpaceDMPControllerConfigPtr tsConfig =
84  20,
85  1,
86  "Linear",
87  "Discrete",
88  amp,
89  phaseL,
90  phaseK,
91  phaseDist0,
92  phaseDist1,
93  phaseKp,
94  phaseKd,
96  posToOriRatio,
98  "",
99  "",
101  maxLinearVel,
102  maxAngularVel,
103  50.0,
104  "DMP",
109  1.0,
110  1.0);
111 
112  armarx::NJointTaskSpaceDMPControllerInterfacePrx dmpController =
113  armarx::NJointTaskSpaceDMPControllerInterfacePrx::checkedCast(
114  remote.robotUnit->createNJointController(
115  "NJointTSDMPController", "dmpController_RetrieveHand", tsConfig));
116 
117 
118  std::vector<std::string> fileNames = properties.fileNames;
119  dmpController->learnDMPFromFiles(fileNames);
120 
121  // load robot
123  robotReader.connect(remote.memoryNameSystem);
124 
126  auto r = robotReader.getSynchronizedRobot(properties.robotName, now);
127  ARMARX_CHECK(r.get());
128 
129  std::vector<double> goals;
130  Eigen::Matrix4f targetPoseInRoot = properties.targetPose.toRootFrame(r)->toEigen();
131  // Eigen::Matrix4f targetPoseInRoot = properties.targetPose.toEigen();
132  goals = armarx::control::common::mat4ToDVec(targetPoseInRoot);
133  ARMARX_INFO << "Running VMP with goal \n"
134  << VAROUT(targetPoseInRoot) << ", vec: \n"
136 
137  // if (in.isViaPoseListSet() && in.isViaPoseCanValSet() && in.getViaPoseList().size() == in.getViaPoseCanVal().size())
138  // {
139  // for (size_t i = 0; i < in.getViaPoseList().size(); ++i)
140  // {
141  // dmpController->setViaPoints(in.getViaPoseCanVal().at(i), Helpers::pose2dvec(in.getViaPoseList().at(i)->toEigen()));
142  // }
143  // }
144 
145  dmpController->activateController();
146 
147  dmpController->runDMP(goals, 1.0);
148 
149  const auto metronomeTargetPeriod = armarx::Duration::MilliSeconds(10);
150  armarx::Metronome loopScheduler(metronomeTargetPeriod);
151  ARMARX_INFO << "VMP controller started";
152  while (running.load() && not dmpController->isFinished())
153  {
154  // ARMARX_INFO << VAROUT(dmpController->getCanVal());
155  loopScheduler.waitForNextTick();
156  }
157 
158  ARMARX_DEBUG << "VMP Controller finished, try to delete controller";
159  dmpController->deactivateController();
160  while (dmpController->isControllerActive())
161  {
162  loopScheduler.waitForNextTick();
163  }
164  dmpController->deleteController();
165 
166  running.store(false);
167  ARMARX_INFO << "Retrieve hand skill finished";
168  return true;
169  }
170 
171 } // namespace armarx::control::retrieve_hand::core
armarx::control::retrieve_hand::core::RetrieveHand::Remote::robotUnit
armarx::RobotUnitInterfacePrx robotUnit
Definition: RetrieveHand.h:53
armarx::control::retrieve_hand::core
Definition: RetrieveHand.cpp:31
armarx::NJointTaskSpaceDMPControllerConfig
Definition: ControllerInterface.ice:43
armarx::control::retrieve_hand::core::RetrieveHand::Properties::timeDurationInSec
double timeDurationInSec
Definition: RetrieveHand.h:60
armarx::armem::robot_state::VirtualRobotReader
The VirtualRobotReader class.
Definition: VirtualRobotReader.h:40
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::control::retrieve_hand::core::RetrieveHand::Properties::kpAngular
float kpAngular
Definition: RetrieveHand.h:64
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::control::retrieve_hand::core::RetrieveHand::Remote
Definition: RetrieveHand.h:48
armarx::control::deprecated_njoint_mp_controller::task_space
Definition: DeprecatedNJointPeriodicTSDMPCompliantController.cpp:6
armarx::armem::robot_state::RobotReader::connect
virtual void connect(armem::client::MemoryNameSystem &memoryNameSystem)
Definition: RobotReader.cpp:48
RetrieveHand.h
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::control::retrieve_hand::core::RetrieveHand::Properties::robotNodeSet
std::string robotNodeSet
Definition: RetrieveHand.h:59
armarx::control::retrieve_hand::core::RetrieveHand::Properties::fileNames
std::vector< std::string > fileNames
Definition: RetrieveHand.h:68
armarx::control::common::dVecToString
std::string dVecToString(const mplib::core::DVec &dvec)
Definition: utils.cpp:289
armarx::control::retrieve_hand::core::RetrieveHand::Properties::kpLinear
float kpLinear
Definition: RetrieveHand.h:63
controller
Definition: AddOperation.h:39
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::control::retrieve_hand::core::RetrieveHand::properties
Properties properties
Definition: RetrieveHand.h:97
armarx::control::retrieve_hand::core::RetrieveHand::Remote::memoryNameSystem
armarx::armem::client::MemoryNameSystem memoryNameSystem
Definition: RetrieveHand.h:51
Metronome.h
armarx::control::retrieve_hand::core::RetrieveHand::Properties
Definition: RetrieveHand.h:56
armarx::control::retrieve_hand::core::RetrieveHand::Properties::kdLinear
float kdLinear
Definition: RetrieveHand.h:65
armarx::armem::robot_state::VirtualRobotReader::getSynchronizedRobot
VirtualRobot::RobotPtr getSynchronizedRobot(const std::string &name, const VirtualRobot::RobotIO::RobotDescription &loadMode=VirtualRobot::RobotIO::RobotDescription::eStructure, bool blocking=true)
armarx::control::retrieve_hand::core::RetrieveHand::remote
Remote remote
Definition: RetrieveHand.h:96
armarx::control::retrieve_hand::core::RetrieveHand::RetrieveHand
RetrieveHand(const Remote &, const Properties &)
Definition: RetrieveHand.cpp:35
utils.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::TimeUtil::SleepMS
static void SleepMS(float milliseconds)
Definition: TimeUtil.h:195
armarx::NJointTaskSpaceDMPControllerMode::eAll
@ eAll
Definition: ControllerInterface.ice:38
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:35
armarx::control::retrieve_hand::core::RetrieveHand::Properties::robotName
std::string robotName
Definition: RetrieveHand.h:58
armarx::control::common::mat4ToDVec
mplib::core::DVec mat4ToDVec(const Eigen::Matrix4f &mat)
convert Eigen:Matrix4f to 7D double vector.
Definition: utils.cpp:240
armarx::control::retrieve_hand::core::RetrieveHand::Properties::targetPose
armarx::FramedPose targetPose
Definition: RetrieveHand.h:62
armarx::FramedPose::toRootFrame
FramedPosePtr toRootFrame(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:534
NJointTSDMPController.h
armarx::control::retrieve_hand::core::RetrieveHand::Properties::kdAngular
float kdAngular
Definition: RetrieveHand.h:66
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55