ImpedanceController.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 Jianfeng Gao ( jianfeng dot gao at kit dot edu )
17  * @date 2023
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #include "ImpedanceController.h"
25 
26 
28 {
29  NJointControllerRegistration<NJointTSImpedanceMPController> registrationControllerNJointTSImpedanceMPController("NJointTSImpedanceMPController");
30 
31 
33  const RobotUnitPtr& robotUnit,
34  const NJointControllerConfigPtr& config,
35  const VirtualRobot::RobotPtr& robot):
37  mp::MPPool{}
38  {
39  ConfigPtrT cfg = ConfigPtrT::dynamicCast(config);
40  auto configData = ::armarx::fromAron<AronDTO, BO>(cfg->config);
41  validateConfigData(configData);
42  bufferUserToAdditionalTask.reinitAllBuffers(configData);
43  bufferUserToRt.reinitAllBuffers(configData);
44  ARMARX_IMPORTANT << VAROUT(configData.mpConfig.mpList.size());
45  createMPs(configData.mpConfig);
46  isMPReady.store(true);
47  ARMARX_IMPORTANT << "NJointTSImpedanceMPController construction done";
48  }
49 
50 
51  std::string NJointTSImpedanceMPController::getClassName(const Ice::Current &) const
52  {
53  return "NJointTSImpedanceMPController";
54  }
55 
56 
58  {
60  if (isFinished("all"))
61  {
62  isMPReady.store(false);
64  reconfigureMPs(cfg.mpConfig);
65  isMPReady.store(true);
66 
67  ARMARX_IMPORTANT << "reconfigure MP: reinitialize the mp input output, as well as the rt related buffer values";
68  VirtualRobot::RobotNodeSetPtr rns = rtGetRobot()->getRobotNodeSet(kinematicChainName);
69  Eigen::Matrix4f currentPose = rns->getTCP()->getPoseInRootFrame();
70  for (auto& _mp: mps)
71  {
72  if (_mp.second.mp->getClassName() == "TSMP")
73  {
74  ARMARX_IMPORTANT << "init input output data for " << _mp.second.mp->getMPName();
75  std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input)->pose = currentPose;
76  std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output)->pose = currentPose;
77  }
78  else if (_mp.second.mp->getClassName() == "JSMP") {
79  ARMARX_IMPORTANT << "init input output data for " << _mp.second.mp->getMPName();
80  std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angleRadian = rns->getJointValuesEigen();
81  }
82  }
83  }
84  }
85 
86 
88  {
89  // NJointTaskspaceImpedanceController::additionalTask();
90  robotUnit->updateVirtualRobot(nonRtRobot);
91 
92  auto& rt2Add = bufferRtToAdditionalTask.getUpToDateReadBuffer();
93  auto& u2RtConfig = bufferUserToRt.getWriteBuffer();
94  auto& u2AddConfig = bufferUserToAdditionalTask.getWriteBuffer();
95 
96  /// parse arguments and run mp
97  if (isMPReady.load())
98  {
99  for (auto& _mp: mps)
100  {
101  if (_mp.second.mp->getClassName() == "TSMP")
102  {
103  mp::TSMPInputPtr in = std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input);
104  in->pose = controller.tcp->getPoseInRootFrame();
105  in->vel = controller.bufferNonRtToOnPublish.getWriteBuffer().currentTwist;
106  in->deltaT = rt2Add.deltaT;
107  }
108  else if (_mp.second.mp->getClassName() == "JSMP")
109  {
110  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
111  for (size_t i = 0; i < rt2Add.jointPosition.size(); i++)
112  {
113  in->angleRadian(i) = rt2Add.jointPosition[i];
114  in->angularVel(i) = rt2Add.jointVelocity[i];
115  }
116  in->deltaT = rt2Add.deltaT;
117  }
118  }
119  runMPs();
120  /// set mp target to rt buffer
121  for (auto& _mp: mps)
122  {
123  if (_mp.second.mp->getClassName() == "TSMP")
124  {
125  mp::TSMPOutputPtr out = std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output);
126  u2RtConfig.desiredPose = out->pose;
127  u2RtConfig.desiredTwist = out->vel;
128  u2AddConfig.desiredPose = out->pose;
129  u2AddConfig.desiredTwist = out->vel;
130  }
131  else if (_mp.second.mp->getClassName() == "JSMP")
132  {
133  mp::JSMPOutputPtr out = std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output);
134  u2RtConfig.desiredNullspaceJointAngles.value() = out->angleRadian;
135  u2AddConfig.desiredNullspaceJointAngles.value() = out->angleRadian;
136  }
137  }
140  }
141  controller.updateControlStatus(u2AddConfig, rt2Add);
142  }
143 
145  {
146  ARMARX_IMPORTANT << "rt pre activate: reinitialize the mp input output, as well as the rt related buffer values";
147  VirtualRobot::RobotNodeSetPtr rns = rtGetRobot()->getRobotNodeSet(kinematicChainName);
148  Eigen::Matrix4f currentPose = rns->getTCP()->getPoseInRootFrame();
150  for (auto& _mp: mps)
151  {
152  if (_mp.second.mp->getClassName() == "TSMP")
153  {
154  ARMARX_IMPORTANT << "init input output data for " << _mp.second.mp->getMPName();
155  std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input)->pose = currentPose;
156  std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output)->pose = currentPose;
157  }
158  else if (_mp.second.mp->getClassName() == "JSMP")
159  {
160  ARMARX_IMPORTANT << "init input output data for " << _mp.second.mp->getMPName();
161  std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angleRadian = rns->getJointValuesEigen();
162  std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angularVel =
163  Eigen::VectorXf::Zero(rns->getJointValues().size());
164  std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angleRadian =
165  rns->getJointValuesEigen();
166  std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angularVel =
167  Eigen::VectorXf::Zero(rns->getJointValues().size());
168  }
169  }
170  }
171 } /// namespace armarx::control::njoint_mp_controller::task_space
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::NJointTSImpedanceMPController
NJointTSImpedanceMPController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: ImpedanceController.cpp:32
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController
Brief description of class NJointTaskspaceImpedanceController.
Definition: ImpedanceController.h:52
armarx::TripleBuffer::getWriteBuffer
T & getWriteBuffer()
Definition: TripleBuffer.h:90
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::bufferRtToAdditionalTask
TripleBuffer< law::RobotStatus > bufferRtToAdditionalTask
Definition: ImpedanceController.h:106
armarx::control::common::mp::JSMPInputPtr
std::shared_ptr< JSMPInput > JSMPInputPtr
Definition: JSMP.h:45
armarx::TripleBuffer::commitWrite
void commitWrite()
Definition: TripleBuffer.h:146
armarx::control::common::mp::MPPool::runMPs
void runMPs()
Definition: MPPool.cpp:68
armarx::control::common::mp::JSMPOutputPtr
std::shared_ptr< JSMPOutput > JSMPOutputPtr
Definition: JSMP.h:46
armarx::control::njoint_mp_controller::task_space::registrationControllerNJointTSImpedanceMPController
NJointControllerRegistration< NJointTSImpedanceMPController > registrationControllerNJointTSImpedanceMPController("NJointTSImpedanceMPController")
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::updateConfig
void updateConfig(const ::armarx::aron::data::dto::DictPtr &dto, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
NJointController interface.
Definition: ImpedanceController.cpp:215
armarx::NJointControllerBase::rtGetRobot
const VirtualRobot::RobotPtr & rtGetRobot()
TODO make protected and use attorneys.
Definition: NJointControllerBase.h:845
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::bufferUserToRt
TripleBuffer< BO > bufferUserToRt
Definition: ImpedanceController.h:74
armarx::control::common::mp::TSMPOutputPtr
std::shared_ptr< TSMPOutput > TSMPOutputPtr
Definition: TSMP.h:47
controller
Definition: AddOperation.h:39
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: ImpedanceController.cpp:51
ImpedanceController.h
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::kinematicChainName
std::string kinematicChainName
variables
Definition: ImpedanceController.h:99
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::nonRtRobot
VirtualRobot::RobotPtr nonRtRobot
Definition: ImpedanceController.h:112
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::additionalTask
void additionalTask() override
Definition: ImpedanceController.cpp:87
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::bufferUserToAdditionalTask
TripleBuffer< BO > bufferUserToAdditionalTask
Definition: ImpedanceController.h:73
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: ImpedanceController.cpp:277
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::updateConfig
void updateConfig(const ::armarx::aron::data::dto::DictPtr &dto, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: ImpedanceController.cpp:57
utils.h
armarx::control::njoint_mp_controller::task_space
This file is part of ArmarX.
Definition: AdmittanceController.cpp:28
armarx::control::common::mp::MPPool::reconfigureMPs
void reconfigureMPs(const MP::MPListConfig &mpListConfig)
Definition: MPPool.cpp:62
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: ImpedanceController.cpp:144
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::control::common::mp::MPPool
Definition: MPPool.h:45
armarx::control::common::mp::MPPool::mps
std::map< std::string, MPInputOutput > mps
Definition: MPPool.h:91
aron_conversions.h
armarx::control::common::mp::TSMPInputPtr
std::shared_ptr< TSMPInput > TSMPInputPtr
Definition: TSMP.h:46
armarx::TripleBuffer::getUpToDateReadBuffer
const T & getUpToDateReadBuffer() const
Definition: TripleBuffer.h:108
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::robotUnit
RobotUnitPtr robotUnit
Definition: ImpedanceController.h:116
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::control::common::mp::MPPool::isFinished
bool isFinished(const std::string &mpName="all", const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:210