CartesianImpedanceControllerWidgetController.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 RobotAPI::gui-plugins::CartesianImpedanceControllerWidgetController
17  * \author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * \date 2020
19  * \copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 #include <string>
26 
27 #include <SimoxUtility/math/convert/mat4f_to_pos.h>
28 #include <SimoxUtility/math/convert/mat4f_to_rpy.h>
29 #include <SimoxUtility/math/convert/rpy_to_quat.h>
30 
32 
33 namespace armarx
34 {
36  NJointControllerGuiPluginBase("NJointTaskSpaceImpedanceController")
37  {
38  _ui.setupUi(getWidget());
39  connectCreateAcivateDeactivateDelete(_ui.pushButtonCreate,
40  _ui.pushButtonActivate,
41  _ui.pushButtonDeactivate,
42  _ui.pushButtonDelete);
44  _ui.gridLayoutSettings->addWidget(_settings);
45 
46  _targ.setXYZWidgets(
47  _ui.doubleSpinBoxTargTX, _ui.doubleSpinBoxTargTY, _ui.doubleSpinBoxTargTZ);
48 
49  _targ.setRPYWidgets(
50  _ui.doubleSpinBoxTargRX, _ui.doubleSpinBoxTargRY, _ui.doubleSpinBoxTargRZ);
51 
52  _targ.setDefaultLimits();
53  _timer = startTimer(10);
54  }
56  {
57  if (_timer)
58  {
59  killTimer(_timer);
60  }
61  }
62 
63  void
65  {
67  std::lock_guard g{_allMutex};
69  ARMARX_INFO << "setup comboBoxRNS";
70  for (const auto& rnsName : _robot->getRobotNodeSetNames())
71  {
72  _ui.comboBoxRNS->addItem(QString::fromStdString(rnsName));
73  }
75  for (const auto& nodeName : _robot->getRobotNodeNames())
76  {
77  _ui.comboBoxLockY->addItem(QString::fromStdString(nodeName));
78  _ui.comboBoxLockZ->addItem(QString::fromStdString(nodeName));
79  }
80 
82  connect(
83  _ui.pushButtonTargGet, &QPushButton::clicked, this, &T::on_pushButtonTargGet_clicked);
84  connect(
85  _ui.pushButtonTargAdd, &QPushButton::clicked, this, &T::on_pushButtonTargAdd_clicked);
86  connect(
87  _ui.pushButtonTargSet, &QPushButton::clicked, this, &T::on_pushButtonTargSet_clicked);
88  connect(_ui.comboBoxRNS,
89  SIGNAL(currentIndexChanged(const QString&)),
90  this,
91  SLOT(on_comboBoxRNS_currentIndexChanged(const QString&)));
92  connect(_ui.radioButtonSelectLeft,
93  &QRadioButton::clicked,
94  this,
95  &T::on_radioButtonSelect_clicked);
96  connect(_ui.radioButtonSelectRight,
97  &QRadioButton::clicked,
98  this,
99  &T::on_radioButtonSelect_clicked);
100  connect(_ui.radioButtonSelectRNS,
101  &QRadioButton::clicked,
102  this,
103  &T::on_radioButtonSelect_clicked);
104 
105  _ui.groupBoxAdvanced->setChecked(false);
106  _ui.radioButtonSelectLeft->setChecked(true);
107  }
108 
109  void
111  {
112  ARMARX_TRACE;
113  std::lock_guard g{_allMutex};
114  _left = getRobotNameHelper().getArm("Left");
115  _right = getRobotNameHelper().getArm("Right");
116  }
117 
118  void
120  {
121  ARMARX_TRACE;
122  std::lock_guard g{_allMutex};
123  if (_robot)
124  {
126  }
127  if (_ui.checkBoxAutoUpdatePose->isChecked())
128  {
129  ARMARX_TRACE;
130  on_pushButtonTargSet_clicked();
131  }
132  const bool lockY = _ui.checkBoxLockY->isChecked();
133  const bool lockZ = _ui.checkBoxLockZ->isChecked();
134  bool anyTracking = lockY || lockZ;
135  _ui.widgetTarget->setEnabled(!anyTracking);
136  if (_rns && (lockY || lockZ))
137  {
138  ARMARX_TRACE;
139  Eigen::Matrix4f pose = _rns->getTCP()->getPoseInRootFrame();
140  if (lockY)
141  {
142  pose(1, 3) = _robot->getRobotNode(_ui.comboBoxLockY->currentText().toStdString())
143  ->getPositionInRootFrame()(1);
144  }
145  if (lockZ)
146  {
147  pose(2, 3) = _robot->getRobotNode(_ui.comboBoxLockZ->currentText().toStdString())
148  ->getPositionInRootFrame()(2);
149  }
150  if (_controller)
151  {
152  _controller->setPosition(pose.topRightCorner<3, 1>());
153  }
154  visuHandAtPose(pose);
155  ARMARX_INFO << "set Target pose to\n" << pose;
156  }
157  else if (!(lockY || lockZ))
158  {
159  const Eigen::Matrix4f rootTcpTarg =
160  _ui.radioButtonVisuSet ? _targ.getMat4()
161  : _rns->getTCP()->getPoseInRootFrame() * _targ.getMat4();
162  visuHandAtPose(rootTcpTarg);
163  }
164  }
165 
166  void
168  {
169  ARMARX_TRACE;
170  std::lock_guard g{_allMutex};
171  if (_visuHandRobotFile.empty() || !_robot || !arviz.topic())
172  {
173  if (arviz.topic())
174  {
176  }
177  return;
178  }
179 
181  viz::Robot{"Hand"}
182  .file(_visuHandRobotProject, _visuHandRobotFile)
183  .useFullModel()
184  .pose(_robot->getGlobalPose() * tcpInRoot * _tcpToBase)
186  }
187 } // namespace armarx
188 //read config
189 namespace armarx
190 {
191  NJointControllerConfigPtr
192  CartesianImpedanceControllerWidgetController::readFullCFG() const
193  {
194  ARMARX_TRACE;
195  auto [pos, quat] = readTargetCFG();
196  return NJointControllerConfigPtr::dynamicCast(_settings->readFullCFG(pos, quat));
197  }
198 
199  std::tuple<Eigen::Vector3f, Eigen::Quaternionf>
200  CartesianImpedanceControllerWidgetController::readTargetCFG() const
201  {
202  ARMARX_TRACE;
203  return {_targ.getPos(), _targ.getQuat()};
204  }
205 } // namespace armarx
206 //push buttons
207 namespace armarx
208 {
209  void
210  CartesianImpedanceControllerWidgetController::createController()
211  {
212  ARMARX_TRACE;
214  _ui.widgetRNSSelect->setEnabled(false);
215  _settings->setController(_controller);
216  }
217 
218  void
219  CartesianImpedanceControllerWidgetController::deleteController()
220  {
221  ARMARX_TRACE;
223  _ui.widgetRNSSelect->setEnabled(true);
224  _settings->setController(nullptr);
225  }
226 
227  void
228  CartesianImpedanceControllerWidgetController::on_pushButtonTargSet_clicked()
229  {
230  ARMARX_TRACE;
231  std::lock_guard g{_allMutex};
232  if (!_controller)
233  {
234  return;
235  }
236  const Eigen::Matrix4f pose = _targ.getMat4();
237  _controller->setPose(pose);
238  ARMARX_INFO << "set Target pose to\n" << pose;
239  }
240 
241  void
242  CartesianImpedanceControllerWidgetController::on_pushButtonTargAdd_clicked()
243  {
244  ARMARX_TRACE;
245  std::lock_guard g{_allMutex};
246  if (!_controller || !_rns)
247  {
248  return;
249  }
250  const Eigen::Matrix4f pose = _rns->getTCP()->getPoseInRootFrame() * _targ.getMat4();
251  _controller->setPose(pose);
252  ARMARX_INFO << "set Target pose to\n" << pose;
253  }
254 
255  void
256  CartesianImpedanceControllerWidgetController::on_pushButtonTargGet_clicked()
257  {
258  ARMARX_TRACE;
259  std::lock_guard g{_allMutex};
260  if (!_rns)
261  {
262  return;
263  }
264  const Eigen::Matrix4f pose = _rns->getTCP()->getPoseInRootFrame();
265  const Eigen::Vector3f pos = simox::math::mat4f_to_pos(pose);
266  const Eigen::Vector3f rpy = simox::math::mat4f_to_rpy(pose);
267 
268  _ui.doubleSpinBoxTargTX->setValue(pos(0));
269  _ui.doubleSpinBoxTargTY->setValue(pos(1));
270  _ui.doubleSpinBoxTargTZ->setValue(pos(2));
271 
272  _ui.doubleSpinBoxTargRX->setValue(rpy(0));
273  _ui.doubleSpinBoxTargRY->setValue(rpy(1));
274  _ui.doubleSpinBoxTargRZ->setValue(rpy(2));
275  }
276 
277  void
278  CartesianImpedanceControllerWidgetController::on_comboBoxRNS_currentIndexChanged(
279  const QString& arg1)
280  {
281  ARMARX_TRACE;
282  std::lock_guard g{_allMutex};
283  if (!_robot || _controller)
284  {
285  return;
286  }
287  const std::string rnsName = arg1.toStdString();
288  _rns = _robot->getRobotNodeSet(rnsName);
289  _settings->setRNS(_rns);
290  auto checkSide = [&](const RobotNameHelper::Arm& side)
291  {
292  if (!_rns->getTCP() || side.getTCP() != _rns->getTCP()->getName() ||
293  !side.getRobotNameHelper())
294  {
295  return;
296  }
297  const auto arm = side.addRobot(_robot);
298  _tcpToBase = arm.getTcp2HandRootTransform();
299  _visuHandRobotFile = side.getHandModelPath();
300  _visuHandRobotProject = side.getHandModelPackage();
301  };
302  if (_left.getRobotNameHelper())
303  {
304  checkSide(_left);
305  }
306  if (_right.getRobotNameHelper())
307  {
308  checkSide(_right);
309  }
310 
311  on_pushButtonTargGet_clicked();
312  }
313 
314  void
315  CartesianImpedanceControllerWidgetController::on_radioButtonSelect_clicked()
316  {
317  ARMARX_TRACE;
318  std::lock_guard g{_allMutex};
319  if (_ui.radioButtonSelectLeft->isChecked())
320  {
321  on_comboBoxRNS_currentIndexChanged(QString::fromStdString(_left.getKinematicChain()));
322  }
323  else if (_ui.radioButtonSelectRight->isChecked())
324  {
325  on_comboBoxRNS_currentIndexChanged(QString::fromStdString(_right.getKinematicChain()));
326  }
327  else
328  {
329  on_comboBoxRNS_currentIndexChanged(_ui.comboBoxRNS->currentText());
330  }
331  }
332 } // namespace armarx
RemoteRobot.h
armarx::SpinBoxToPose::setXYZWidgets
void setXYZWidgets(Qwid *x, Qwid *y, Qwid *z)
Definition: SpinBoxToPose.h:86
armarx::CartesianImpedanceControllerWidgetController::timerEvent
void timerEvent(QTimerEvent *event) override
Definition: CartesianImpedanceControllerWidgetController.cpp:119
armarx::CartesianImpedanceControllerConfigWidget::setController
void setController(const NJointTaskSpaceImpedanceControlInterfacePrx &prx)
if null -> send buttons deactivated
Definition: CartesianImpedanceControllerConfigWidget.cpp:189
armarx::CartesianImpedanceControllerConfigWidget
Definition: CartesianImpedanceControllerConfigWidget.h:16
armarx::RobotStateComponentPluginUser::getRobotNameHelper
RobotNameHelperPtr getRobotNameHelper() const
Definition: RobotStateComponentPlugin.cpp:321
armarx::CartesianImpedanceControllerWidgetController::~CartesianImpedanceControllerWidgetController
~CartesianImpedanceControllerWidgetController()
Definition: CartesianImpedanceControllerWidgetController.cpp:55
armarx::viz::Robot::file
Robot & file(std::string const &project, std::string const &filename)
Definition: Robot.h:14
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::SpinBoxToPose::setRPYWidgets
void setRPYWidgets(Qwid *r, Qwid *p, Qwid *y)
Definition: SpinBoxToPose.h:92
armarx::SpinBoxToPose::getMat4
Eigen::Matrix4f getMat4() const
Definition: SpinBoxToPose.h:80
armarx::CartesianImpedanceControllerWidgetController::setupGuiAfterConnect
void setupGuiAfterConnect() override
Definition: CartesianImpedanceControllerWidgetController.cpp:64
armarx::detail::_NJointControllerGuiPluginBase::Base::_allMutex
std::recursive_mutex _allMutex
Definition: NJointControllerGuiPluginBase.h:104
armarx::SpinBoxToPose::getQuat
Eigen::Quaternionf getQuat() const
Definition: SpinBoxToPose.h:72
CartesianImpedanceControllerWidgetController.h
armarx::RobotStateComponentPluginUser::synchronizeLocalClone
bool synchronizeLocalClone(const VirtualRobot::RobotPtr &robot) const
Definition: RobotStateComponentPlugin.cpp:371
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::CartesianImpedanceControllerWidgetController::visuHandAtPose
void visuHandAtPose(const Eigen::Matrix4f &tcpInRoot=Eigen::Matrix4f::Identity())
Definition: CartesianImpedanceControllerWidgetController.cpp:167
armarx::detail::_NJointControllerGuiPluginBase::Base::connectCreateAcivateDeactivateDelete
void connectCreateAcivateDeactivateDelete(QPushButton *cr, QPushButton *ac, QPushButton *dc, QPushButton *de)
Definition: NJointControllerGuiPluginBase.cpp:41
armarx::Arm::getRobotNameHelper
std::shared_ptr< const RobotNameHelper > getRobotNameHelper() const
Definition: RobotNameHelper.cpp:144
armarx::viz::Robot::overrideColor
Robot & overrideColor(Color c)
Definition: Robot.h:36
armarx::viz::Robot
Definition: Robot.h:9
armarx::Arm::getKinematicChain
std::string getKinematicChain() const
Definition: RobotNameHelper.cpp:205
armarx::SpinBoxToPose::getPos
Eigen::Vector3f getPos() const
Definition: SpinBoxToPose.h:64
armarx::CartesianImpedanceControllerWidgetController::CartesianImpedanceControllerWidgetController
CartesianImpedanceControllerWidgetController()
Definition: CartesianImpedanceControllerWidgetController.cpp:35
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::RobotNameHelper::Arm
armarx::Arm Arm
Definition: RobotNameHelper.h:150
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:43
armarx::NJointControllerGuiPluginBase::createController
void createController() override
Definition: NJointControllerGuiPluginBase.h:61
armarx::SpinBoxToPose::setDefaultLimits
void setDefaultLimits()
Definition: SpinBoxToPose.h:106
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
armarx::NJointControllerGuiPluginBase< CartesianImpedanceControllerWidgetController, NJointTaskSpaceImpedanceControlInterfacePrx >::_controller
NJointTaskSpaceImpedanceControlInterfacePrx _controller
Definition: NJointControllerGuiPluginBase.h:123
armarx::NJointControllerGuiPluginBase::deleteController
void deleteController() override
Definition: NJointControllerGuiPluginBase.h:102
armarx::viz::Color::green
static Color green(int g=255, int a=255)
Definition: Color.h:84
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::CartesianImpedanceControllerConfigWidget::setRNS
void setRNS(const VirtualRobot::RobotNodeSetPtr &rns)
Definition: CartesianImpedanceControllerConfigWidget.cpp:195
armarx::NJointControllerGuiPluginBase
Definition: NJointControllerGuiPluginBase.h:30
armarx::CartesianImpedanceControllerWidgetController::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: CartesianImpedanceControllerWidgetController.cpp:110
armarx::detail::_NJointControllerGuiPluginBase::Base::_robot
VirtualRobot::RobotPtr _robot
Definition: NJointControllerGuiPluginBase.h:105
armarx::CartesianImpedanceControllerConfigWidget::readFullCFG
NJointTaskSpaceImpedanceControlConfigPtr readFullCFG(const Eigen::Vector3f &targPos, const Eigen::Quaternionf &targOri) const
Definition: CartesianImpedanceControllerConfigWidget.cpp:100
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::viz::Client::commitLayerContaining
void commitLayerContaining(std::string const &name)
Definition: Client.h:153