DynamicsHelper.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2019
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "DynamicsHelper.h"
25 
28 
30 
31 namespace armarx
32 {
33 
34  DynamicsHelper::DynamicsHelper(RobotUnit* robotUnit) : robotUnit(robotUnit)
35  {
36 
38  }
39 
40  void
41  DynamicsHelper::addRobotPart(VirtualRobot::RobotNodeSetPtr rnsJoints,
42  VirtualRobot::RobotNodeSetPtr rnsBodies,
43  rtfilters::RTFilterBasePtr velocityFilter,
44  rtfilters::RTFilterBasePtr accelerationFilter)
45  {
46  DynamicsData data(rnsJoints, rnsBodies);
47  for (size_t i = 0; i < rnsJoints->getSize(); i++)
48  {
49  auto selectedJoint = robotUnit->getSensorDevice(rnsJoints->getNode(i)->getName());
50  if (selectedJoint)
51  {
52  auto sensorValue = const_cast<SensorValue1DoFActuator*>(
53  selectedJoint->getSensorValue()->asA<SensorValue1DoFActuator>());
54  ARMARX_CHECK_EXPRESSION(sensorValue) << rnsJoints->getNode(i)->getName();
55  ARMARX_DEBUG << "will calculate inverse dynamics for robot node "
56  << rnsJoints->getNode(i)->getName();
57  data.nodeSensorVec.push_back(std::make_pair(rnsJoints->getNode(i), sensorValue));
58  }
59  else
60  {
61  ARMARX_INFO << "Joint " << rnsJoints->getNode(i)->getName() << " not found";
62  data.nodeSensorVec.push_back(std::make_pair(rnsJoints->getNode(i), nullptr));
63  }
64  if (velocityFilter)
65  {
66  data.velocityFilter.push_back(velocityFilter->clone());
67  }
68  if (accelerationFilter)
69  {
70  data.accelerationFilter.push_back(accelerationFilter->clone());
71  }
72  }
73  dataMap.emplace(std::make_pair(rnsJoints, data));
74  }
75 
76  void
77  DynamicsHelper::update(const IceUtil::Time& sensorValuesTimestamp,
78  const IceUtil::Time& timeSinceLastIteration)
79  {
80  for (auto& pair : dataMap)
81  {
82  // first get current state of the robot (position, velocity, acceleration)
83  auto& data = pair.second;
84  auto& dynamics = data.dynamics;
85  {
86  size_t i = 0;
87  for (auto& node : data.nodeSensorVec)
88  {
89  ARMARX_CHECK_EXPRESSION(node.second) << node.first->getName();
90  data.q(i) = node.second->position;
91  if (i < data.velocityFilter.size() && data.velocityFilter.at(i) &&
92  !std::isnan(node.second->velocity))
93  {
94  data.qDot(i) = data.velocityFilter.at(i)->update(sensorValuesTimestamp,
95  node.second->velocity);
96  }
97  else
98  {
99  data.qDot(i) = node.second->velocity;
100  }
101  if (i < data.accelerationFilter.size() && data.accelerationFilter.at(i) &&
102  !std::isnan(node.second->acceleration))
103  {
104  data.qDDot(i) = data.accelerationFilter.at(i)->update(
105  sensorValuesTimestamp, node.second->acceleration);
106  }
107  else
108  {
109  data.qDDot(i) = node.second->acceleration;
110  }
111  i++;
112  }
113  }
114  // calculate external torques (tau) applied to robot induced by dynamics
115  dynamics.getGravityMatrix(data.q, data.tauGravity);
116  dynamics.getInverseDynamics(data.q, data.qDot, data.qDDot, data.tau);
117  // update virtual sensor values
118  size_t i = 0;
119  for (auto& node : data.nodeSensorVec)
120  {
121  auto torque = data.tau(i);
122  auto gravityTorque = data.tauGravity(i);
123  if (node.second)
124  {
125  node.second->inverseDynamicsTorque = -torque;
126  node.second->gravityTorque = -gravityTorque;
127  node.second->inertiaTorque = -(torque - gravityTorque);
128  }
129  i++;
130  }
131  }
132  }
133 
134 } // namespace armarx
armarx::DynamicsHelper::robotUnit
RobotUnit * robotUnit
Definition: DynamicsHelper.h:75
armarx::rtfilters::RTFilterBasePtr
std::shared_ptr< RTFilterBase > RTFilterBasePtr
Definition: RTFilterBase.h:34
armarx::DynamicsHelper::DynamicsHelper
DynamicsHelper(RobotUnit *robotUnit)
Definition: DynamicsHelper.cpp:34
RobotUnit.h
DynamicsHelper.h
armarx::DynamicsHelper::addRobotPart
void addRobotPart(VirtualRobot::RobotNodeSetPtr rnsJoints, VirtualRobot::RobotNodeSetPtr rnsBodies, rtfilters::RTFilterBasePtr velocityFilter=rtfilters::RTFilterBasePtr(), rtfilters::RTFilterBasePtr accelerationFilter=rtfilters::RTFilterBasePtr())
Definition: DynamicsHelper.cpp:41
armarx::RobotUnitModule::Devices::getSensorDevice
ConstSensorDevicePtr getSensorDevice(const std::string &deviceName) const
TODO move to attorney for NJointControllerBase.
Definition: RobotUnitModuleDevices.cpp:109
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::DynamicsHelper::DynamicsData
Definition: DynamicsHelper.h:45
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
ExpressionException.h
armarx::DynamicsHelper::dataMap
std::map< VirtualRobot::RobotNodeSetPtr, DynamicsData > dataMap
Definition: DynamicsHelper.h:76
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_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::DynamicsHelper::update
void update(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)
Definition: DynamicsHelper.cpp:77
armarx::RobotUnit
The RobotUnit class manages a robot and its controllers.
Definition: RobotUnit.h:180
RTFilterBase.h
armarx::SensorValue1DoFActuator
Definition: SensorValue1DoFActuator.h:76
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28