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
26#include <VirtualRobot/Dynamics/Dynamics.h>
27#include <VirtualRobot/RobotNodeSet.h>
28
31
33
34namespace armarx
35{
36
42
43 void
44 DynamicsHelper::addRobotPart(VirtualRobot::RobotNodeSetPtr rnsJoints,
45 VirtualRobot::RobotNodeSetPtr rnsBodies,
46 rtfilters::RTFilterBasePtr velocityFilter,
47 rtfilters::RTFilterBasePtr accelerationFilter)
48 {
49 DynamicsData data(rnsJoints, rnsBodies);
50 for (size_t i = 0; i < rnsJoints->getSize(); i++)
51 {
52 auto selectedJoint = robotUnit->getSensorDevice(rnsJoints->getNode(i)->getName());
53 if (selectedJoint)
54 {
55 auto sensorValue = const_cast<SensorValue1DoFActuator*>(
56 selectedJoint->getSensorValue()->asA<SensorValue1DoFActuator>());
57 ARMARX_CHECK_EXPRESSION(sensorValue) << rnsJoints->getNode(i)->getName();
58 ARMARX_DEBUG << "will calculate inverse dynamics for robot node "
59 << rnsJoints->getNode(i)->getName();
60 data.nodeSensorVec.push_back(std::make_pair(rnsJoints->getNode(i), sensorValue));
61 }
62 else
63 {
64 ARMARX_INFO << "Joint " << rnsJoints->getNode(i)->getName() << " not found";
65 data.nodeSensorVec.push_back(std::make_pair(rnsJoints->getNode(i), nullptr));
66 }
67 if (velocityFilter)
68 {
69 data.velocityFilter.push_back(velocityFilter->clone());
70 }
71 if (accelerationFilter)
72 {
73 data.accelerationFilter.push_back(accelerationFilter->clone());
74 }
75 }
76 dataMap.emplace(std::make_pair(rnsJoints, data));
77 }
78
79 void
80 DynamicsHelper::update(const IceUtil::Time& sensorValuesTimestamp,
81 const IceUtil::Time& timeSinceLastIteration)
82 {
83 for (auto& pair : dataMap)
84 {
85 // first get current state of the robot (position, velocity, acceleration)
86 auto& data = pair.second;
87 auto& dynamics = data.dynamics;
88 {
89 size_t i = 0;
90 for (auto& node : data.nodeSensorVec)
91 {
92 ARMARX_CHECK_EXPRESSION(node.second) << node.first->getName();
93 data.q(i) = node.second->position;
94 if (i < data.velocityFilter.size() && data.velocityFilter.at(i) &&
95 !std::isnan(node.second->velocity))
96 {
97 data.qDot(i) = data.velocityFilter.at(i)->update(sensorValuesTimestamp,
98 node.second->velocity);
99 }
100 else
101 {
102 data.qDot(i) = node.second->velocity;
103 }
104 if (i < data.accelerationFilter.size() && data.accelerationFilter.at(i) &&
105 !std::isnan(node.second->acceleration))
106 {
107 data.qDDot(i) = data.accelerationFilter.at(i)->update(
108 sensorValuesTimestamp, node.second->acceleration);
109 }
110 else
111 {
112 data.qDDot(i) = node.second->acceleration;
113 }
114 i++;
115 }
116 }
117 // calculate external torques (tau) applied to robot induced by dynamics
118 dynamics.getGravityMatrix(data.q, data.tauGravity);
119 dynamics.getInverseDynamics(data.q, data.qDot, data.qDDot, data.tau);
120 // update virtual sensor values
121 size_t i = 0;
122 for (auto& node : data.nodeSensorVec)
123 {
124 auto torque = data.tau(i);
125 auto gravityTorque = data.tauGravity(i);
126 if (node.second)
127 {
128 node.second->inverseDynamicsTorque = -torque;
129 node.second->gravityTorque = -gravityTorque;
130 node.second->inertiaTorque = -(torque - gravityTorque);
131 }
132 i++;
133 }
134 }
135 }
136
138 VirtualRobot::RobotNodeSetPtr rnsBodies) :
140 {
141 q = qDot = qDDot = tau = tauGravity = Eigen::VectorXd::Zero(rnsJoints->getSize());
142 }
143} // namespace armarx
void addRobotPart(VirtualRobot::RobotNodeSetPtr rnsJoints, VirtualRobot::RobotNodeSetPtr rnsBodies, rtfilters::RTFilterBasePtr velocityFilter=rtfilters::RTFilterBasePtr(), rtfilters::RTFilterBasePtr accelerationFilter=rtfilters::RTFilterBasePtr())
DynamicsHelper(RobotUnit *robotUnit)
std::map< VirtualRobot::RobotNodeSetPtr, DynamicsData > dataMap
void update(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)
The RobotUnit class manages a robot and its controllers.
Definition RobotUnit.h:192
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
std::shared_ptr< RTFilterBase > RTFilterBasePtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
VirtualRobot::RobotNodeSetPtr rnsBodies
VirtualRobot::RobotNodeSetPtr rnsJoints
DynamicsData(VirtualRobot::RobotNodeSetPtr rnsJoints, VirtualRobot::RobotNodeSetPtr rnsBodies)