NJointTaskSpaceImpedanceController.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 TaskSpaceActiveImpedanceControl::ArmarXObjects::NJointTaskSpaceImpedanceController
17  * @author zhou ( you dot zhou at kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 #include <SimoxUtility/math/convert/mat4f_to_pos.h>
26 #include <SimoxUtility/math/convert/mat4f_to_quat.h>
27 
29 
30 using namespace armarx;
31 
33  registrationControllerDSRTController("NJointTaskSpaceImpedanceController");
34 
36  const RobotUnitPtr& robotUnit,
37  const NJointControllerConfigPtr& config,
39 {
40 
41  NJointTaskSpaceImpedanceControlConfigPtr cfg =
42  NJointTaskSpaceImpedanceControlConfigPtr::dynamicCast(config);
43 
45  ARMARX_CHECK_EXPRESSION(robotUnit);
46  ARMARX_CHECK_EXPRESSION(!cfg->nodeSetName.empty());
48  VirtualRobot::RobotNodeSetPtr rns = rtGetRobot()->getRobotNodeSet(cfg->nodeSetName);
49 
50  jointNames.clear();
51  ARMARX_CHECK_EXPRESSION(rns) << cfg->nodeSetName;
52  for (size_t i = 0; i < rns->getSize(); ++i)
53  {
54  std::string jointName = rns->getNode(i)->getName();
55  jointNames.push_back(jointName);
56  ControlTargetBase* ct = useControlTarget(jointName, ControlModes::Torque1DoF);
57  ARMARX_CHECK_EXPRESSION(ct) << jointName;
58  const SensorValueBase* sv = useSensorValue(jointName);
59  ARMARX_CHECK_EXPRESSION(sv) << jointName;
60  auto casted_ct = ct->asA<ControlTarget1DoFActuatorTorque>();
61  ARMARX_CHECK_EXPRESSION(casted_ct) << jointName;
62  targets.push_back(casted_ct);
63 
64  const SensorValue1DoFActuatorTorque* torqueSensor =
65  sv->asA<SensorValue1DoFActuatorTorque>();
66  const SensorValue1DoFActuatorVelocity* velocitySensor =
67  sv->asA<SensorValue1DoFActuatorVelocity>();
68  const SensorValue1DoFActuatorPosition* positionSensor =
69  sv->asA<SensorValue1DoFActuatorPosition>();
70  if (!torqueSensor)
71  {
72  ARMARX_WARNING << "No Torque sensor available for " << jointName;
73  }
74 
75 
76  torqueSensors.push_back(torqueSensor);
77  velocitySensors.push_back(velocitySensor);
78  positionSensors.push_back(positionSensor);
79  };
80 
81  controller.initialize(rns);
82 
84  initData.desiredOrientation = cfg->desiredOrientation;
85  initData.desiredPosition = cfg->desiredPosition;
86 
87  initData.Kpos = cfg->Kpos;
88  initData.Dpos = cfg->Dpos;
89  initData.Kori = cfg->Kori;
90  initData.Dori = cfg->Dori;
91 
92  initData.Knull = cfg->Knull;
93  initData.Dnull = cfg->Dnull;
94  initData.desiredJointPosition = cfg->desiredJointPositions;
95 
96  initData.torqueLimit = cfg->torqueLimit;
97  reinitTripleBuffer(initData);
98 }
99 
100 void
102  const IceUtil::Time& /*timeSinceLastIteration*/)
103 {
105  const auto& jointDesiredTorques =
106  controller.run(rtGetControlStruct(), torqueSensors, velocitySensors, positionSensors);
107  ARMARX_CHECK_EQUAL(targets.size(), static_cast<size_t>(jointDesiredTorques.size()));
108 
109  for (size_t i = 0; i < targets.size(); ++i)
110  {
111  targets.at(i)->torque = jointDesiredTorques(i);
112  if (!targets.at(i)->isValid())
113  {
114  targets.at(i)->torque = 0;
115  }
116  }
117 
118  debugDataInfo.getWriteBuffer().desiredForce_x = controller.dbg.tcpDesiredForce(0);
119  debugDataInfo.getWriteBuffer().desiredForce_y = controller.dbg.tcpDesiredForce(1);
120  debugDataInfo.getWriteBuffer().desiredForce_z = controller.dbg.tcpDesiredForce(2);
121  debugDataInfo.getWriteBuffer().tcpDesiredTorque_x = controller.dbg.tcpDesiredTorque(0);
122  debugDataInfo.getWriteBuffer().tcpDesiredTorque_y = controller.dbg.tcpDesiredTorque(1);
123  debugDataInfo.getWriteBuffer().tcpDesiredTorque_z = controller.dbg.tcpDesiredTorque(2);
124  debugDataInfo.getWriteBuffer().quatError = controller.dbg.errorAngle;
125  debugDataInfo.getWriteBuffer().posiError = controller.dbg.posiError;
126  debugDataInfo.commitWrite();
127 }
128 
129 void
132  const DebugObserverInterfacePrx& debugObs)
133 {
134  StringVariantBaseMap datafields;
135  auto values = debugDataInfo.getUpToDateReadBuffer().desired_torques;
136  for (auto& pair : values)
137  {
138  datafields[pair.first] = new Variant(pair.second);
139  }
140 
141  datafields["desiredForce_x"] =
142  new Variant(debugDataInfo.getUpToDateReadBuffer().desiredForce_x);
143  datafields["desiredForce_y"] =
144  new Variant(debugDataInfo.getUpToDateReadBuffer().desiredForce_y);
145  datafields["desiredForce_z"] =
146  new Variant(debugDataInfo.getUpToDateReadBuffer().desiredForce_z);
147 
148  datafields["tcpDesiredTorque_x"] =
149  new Variant(debugDataInfo.getUpToDateReadBuffer().tcpDesiredTorque_x);
150  datafields["tcpDesiredTorque_y"] =
151  new Variant(debugDataInfo.getUpToDateReadBuffer().tcpDesiredTorque_y);
152  datafields["tcpDesiredTorque_z"] =
153  new Variant(debugDataInfo.getUpToDateReadBuffer().tcpDesiredTorque_z);
154 
155  datafields["quatError"] = new Variant(debugDataInfo.getUpToDateReadBuffer().quatError);
156  datafields["posiError"] = new Variant(debugDataInfo.getUpToDateReadBuffer().posiError);
157 
158  debugObs->setDebugChannel("DSControllerOutput", datafields);
159 }
160 
161 void
162 NJointTaskSpaceImpedanceController::setPosition(const Eigen::Vector3f& target, const Ice::Current&)
163 {
167 }
168 
169 void
171  const Ice::Current&)
172 {
176 }
177 
178 void
180  const Eigen::Quaternionf& ori,
181  const Ice::Current&)
182 {
187 }
188 
189 void
191 {
193  getWriterControlStruct().desiredPosition = simox::math::mat4f_to_pos(mat);
194  getWriterControlStruct().desiredOrientation = simox::math::mat4f_to_quat(mat);
196 }
197 
198 void
200  const Ice::FloatSeq& vals,
201  const Ice::Current&)
202 {
204  if (name == "Kpos")
205  {
206  ARMARX_CHECK_EQUAL(vals.size(), 3);
207 
208  Eigen::Vector3f vec;
209  for (size_t i = 0; i < 3; ++i)
210  {
211  vec(i) = vals.at(i);
212  }
214  }
215  else if (name == "Kori")
216  {
217  ARMARX_CHECK_EQUAL(vals.size(), 3);
218 
219  Eigen::Vector3f vec;
220  for (size_t i = 0; i < 3; ++i)
221  {
222  vec(i) = vals.at(i);
223  }
225  }
226  else if (name == "Dpos")
227  {
228  ARMARX_CHECK_EQUAL(vals.size(), 3);
229 
230  Eigen::Vector3f vec;
231  for (size_t i = 0; i < 3; ++i)
232  {
233  vec(i) = vals.at(i);
234  }
236  }
237  else if (name == "Dori")
238  {
239  ARMARX_CHECK_EQUAL(vals.size(), 3);
240 
241  Eigen::Vector3f vec;
242  for (size_t i = 0; i < 3; ++i)
243  {
244  vec(i) = vals.at(i);
245  }
247  }
248  else if (name == "Knull")
249  {
250  ARMARX_CHECK_EQUAL(vals.size(), 8);
251 
252  Eigen::VectorXf vec;
253  vec.setZero(8);
254  for (size_t i = 0; i < 8; ++i)
255  {
256  vec(i) = vals.at(i);
257  }
259  }
260  else if (name == "Dnull")
261  {
262  ARMARX_CHECK_EQUAL(vals.size(), 8);
263 
264  Eigen::VectorXf vec;
265  vec.setZero(8);
266  for (size_t i = 0; i < 8; ++i)
267  {
268  vec(i) = vals.at(i);
269  }
271  }
273 }
274 
275 void
277  const Eigen::VectorXf& knull,
278  const Eigen::VectorXf& dnull,
279  const Ice::Current&)
280 {
282  ARMARX_CHECK_EQUAL(static_cast<std::size_t>(joint.size()), targets.size());
283  ARMARX_CHECK_EQUAL(static_cast<std::size_t>(knull.size()), targets.size());
284  ARMARX_CHECK_EQUAL(static_cast<std::size_t>(dnull.size()), targets.size());
285  getWriterControlStruct().Knull = knull;
286  getWriterControlStruct().Dnull = dnull;
289 }
290 
291 void
293  const NJointTaskSpaceImpedanceControlRuntimeConfig& cfg,
294  const Ice::Current&)
295 {
297  ARMARX_CHECK_EQUAL(static_cast<std::size_t>(cfg.Knull.size()), targets.size());
298  ARMARX_CHECK_EQUAL(static_cast<std::size_t>(cfg.Dnull.size()), targets.size());
299  ARMARX_CHECK_EQUAL(static_cast<std::size_t>(cfg.desiredJointPositions.size()), targets.size());
300 
301  getWriterControlStruct().Kpos = cfg.Kpos;
302  getWriterControlStruct().Dpos = cfg.Dpos;
303  getWriterControlStruct().Kori = cfg.Kori;
304  getWriterControlStruct().Dori = cfg.Dori;
305 
306  getWriterControlStruct().Knull = cfg.Knull;
307  getWriterControlStruct().Dnull = cfg.Dnull;
308  getWriterControlStruct().desiredJointPosition = cfg.desiredJointPositions;
309 
310  getWriterControlStruct().torqueLimit = cfg.torqueLimit;
312 }
313 
314 void
316 {
317 }
armarx::NJointControllerWithTripleBuffer< CartesianImpedanceController::Config >::reinitTripleBuffer
void reinitTripleBuffer(const CartesianImpedanceController::Config &initial)
Definition: NJointControllerWithTripleBuffer.h:68
armarx::NJointTaskSpaceImpedanceController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: NJointTaskSpaceImpedanceController.cpp:101
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::CartesianImpedanceController::Config::Kori
Eigen::Vector3f Kori
Definition: CartesianImpedanceController.h:20
armarx::TripleBuffer::getWriteBuffer
T & getWriteBuffer()
Definition: TripleBuffer.h:90
armarx::NJointControllerRegistration
Definition: NJointControllerRegistry.h:74
armarx::NJointControllerBase::useSynchronizedRtRobot
const VirtualRobot::RobotPtr & useSynchronizedRtRobot(bool updateCollisionModel=false)
Requests a VirtualRobot for use in rtRun *.
Definition: NJointController.cpp:293
armarx::NJointControllerWithTripleBuffer< CartesianImpedanceController::Config >::rtGetControlStruct
const CartesianImpedanceController::Config & rtGetControlStruct() const
Definition: NJointControllerWithTripleBuffer.h:32
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::SensorValueBase::asA
const T * asA() const
Definition: SensorValueBase.h:82
armarx::SensorValueBase
The SensorValueBase class.
Definition: SensorValueBase.h:40
armarx::ControlTargetBase
Brief description of class JointControlTargetBase.
Definition: ControlTargetBase.h:47
armarx::TripleBuffer::commitWrite
void commitWrite()
Definition: TripleBuffer.h:146
armarx::CartesianImpedanceController::Config::Knull
Eigen::VectorXf Knull
Definition: CartesianImpedanceController.h:22
armarx::CartesianImpedanceController::Config::desiredJointPosition
Eigen::VectorXf desiredJointPosition
Definition: CartesianImpedanceController.h:24
armarx::NJointControllerBase::useControlTarget
ControlTargetBase * useControlTarget(const std::string &deviceName, const std::string &controlMode)
Declares to calculate the ControlTarget for the given ControlDevice in the given ControlMode when rtR...
Definition: NJointController.cpp:410
armarx::CartesianImpedanceController::Config::Dnull
Eigen::VectorXf Dnull
Definition: CartesianImpedanceController.h:23
armarx::NJointTaskSpaceImpedanceController::setPositionOrientation
void setPositionOrientation(const Eigen::Vector3f &pos, const Eigen::Quaternionf &ori, const Ice::Current &) override
Definition: NJointTaskSpaceImpedanceController.cpp:179
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::CartesianImpedanceController::Config::torqueLimit
float torqueLimit
Definition: CartesianImpedanceController.h:25
armarx::NJointControllerWithTripleBuffer< CartesianImpedanceController::Config >::getWriterControlStruct
CartesianImpedanceController::Config & getWriterControlStruct()
Definition: NJointControllerWithTripleBuffer.h:54
armarx::CartesianImpedanceController::Config::Dpos
Eigen::Vector3f Dpos
Definition: CartesianImpedanceController.h:19
armarx::NJointControllerWithTripleBuffer< CartesianImpedanceController::Config >::LockGuardType
std::lock_guard< std::recursive_mutex > LockGuardType
Definition: NJointControllerWithTripleBuffer.h:14
armarx::NJointTaskSpaceImpedanceController::setNullspaceConfig
void setNullspaceConfig(const Eigen::VectorXf &joint, const Eigen::VectorXf &knull, const Eigen::VectorXf &dnull, const Ice::Current &) override
Definition: NJointTaskSpaceImpedanceController.cpp:276
armarx::NJointTaskSpaceImpedanceController::setPosition
void setPosition(const Eigen::Vector3f &, const Ice::Current &) override
Definition: NJointTaskSpaceImpedanceController.cpp:162
armarx::CartesianImpedanceController::Config::desiredOrientation
Eigen::Quaternionf desiredOrientation
Definition: CartesianImpedanceController.h:17
armarx::NJointControllerBase::rtGetRobot
const VirtualRobot::RobotPtr & rtGetRobot()
TODO make protected and use attorneys.
Definition: NJointControllerBase.h:845
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:177
armarx::CartesianImpedanceController::Config::Dori
Eigen::Vector3f Dori
Definition: CartesianImpedanceController.h:21
controller
Definition: AddOperation.h:39
armarx::NJointControllerWithTripleBuffer< CartesianImpedanceController::Config >::rtUpdateControlStruct
bool rtUpdateControlStruct()
Definition: NJointControllerWithTripleBuffer.h:38
armarx::NJointControllerWithTripleBuffer< CartesianImpedanceController::Config >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
armarx::CartesianImpedanceController::Config::desiredPosition
Eigen::Vector3f desiredPosition
Definition: CartesianImpedanceController.h:16
armarx::NJointControllerWithTripleBuffer< CartesianImpedanceController::Config >::controlDataMutex
MutexType controlDataMutex
Definition: NJointControllerWithTripleBuffer.h:73
armarx::NJointTaskSpaceImpedanceController::setConfig
void setConfig(const NJointTaskSpaceImpedanceControlRuntimeConfig &cfg, const Ice::Current &) override
Definition: NJointTaskSpaceImpedanceController.cpp:292
armarx::CartesianImpedanceController::Config
Definition: CartesianImpedanceController.h:14
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
registrationControllerDSRTController
NJointControllerRegistration< NJointTaskSpaceImpedanceController > registrationControllerDSRTController("NJointTaskSpaceImpedanceController")
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::NJointTaskSpaceImpedanceController::onPublish
void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &) override
Definition: NJointTaskSpaceImpedanceController.cpp:130
armarx::Quaternion< float, 0 >
armarx::NJointTaskSpaceImpedanceController::setOrientation
void setOrientation(const Eigen::Quaternionf &, const Ice::Current &) override
Definition: NJointTaskSpaceImpedanceController.cpp:170
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::NJointTaskSpaceImpedanceController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: NJointTaskSpaceImpedanceController.cpp:315
armarx::TripleBuffer::getUpToDateReadBuffer
const T & getUpToDateReadBuffer() const
Definition: TripleBuffer.h:108
armarx::NJointTaskSpaceImpedanceController::NJointTaskSpaceImpedanceController
NJointTaskSpaceImpedanceController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: NJointTaskSpaceImpedanceController.cpp:35
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
NJointTaskSpaceImpedanceController.h
armarx::NJointTaskSpaceImpedanceController::setPose
void setPose(const Eigen::Matrix4f &mat, const Ice::Current &) override
Definition: NJointTaskSpaceImpedanceController.cpp:190
armarx::CartesianImpedanceController::Config::Kpos
Eigen::Vector3f Kpos
Definition: CartesianImpedanceController.h:18
NJointControllerRegistry.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::NJointControllerBase::useSensorValue
const SensorValueBase * useSensorValue(const std::string &sensorDeviceName) const
Get a const ptr to the given SensorDevice's SensorValue.
Definition: NJointController.cpp:383
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::NJointTaskSpaceImpedanceController::setImpedanceParameters
void setImpedanceParameters(const std::string &, const Ice::FloatSeq &, const Ice::Current &) override
Definition: NJointTaskSpaceImpedanceController.cpp:199