NJointCartesianTorqueController.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 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
25 
26 #include <VirtualRobot/RobotNodeSet.h>
27 
29 
30 #include "../RobotUnit.h"
31 
32 
33 #define DEFAULT_TCP_STRING "default TCP"
34 
35 namespace armarx
36 {
37 
38  NJointControllerRegistration<NJointCartesianTorqueController>
39  registrationControllerNJointCartesianTorqueController("NJointCartesianTorqueController");
40 
41  std::string
43  {
44  return "NJointCartesianTorqueController";
45  }
46 
48  RobotUnit*,
49  NJointCartesianTorqueControllerConfigPtr config,
50  const VirtualRobot::RobotPtr& r)
51  {
52  ARMARX_CHECK_EXPRESSION(!config->nodeSetName.empty());
54  VirtualRobot::RobotNodeSetPtr rns = rtGetRobot()->getRobotNodeSet(config->nodeSetName);
55  ARMARX_CHECK_EXPRESSION(rns) << config->nodeSetName;
56  for (size_t i = 0; i < rns->getSize(); ++i)
57  {
58  std::string jointName = rns->getNode(i)->getName();
59  ControlTargetBase* ct = useControlTarget(jointName, ControlModes::Torque1DoF);
60  //const SensorValueBase* sv = useSensorValue(jointName);
61  targets.push_back(ct->asA<ControlTarget1DoFActuatorTorque>());
62 
63  /*const SensorValue1DoFActuatorTorque* torqueSensor = sv->asA<SensorValue1DoFActuatorTorque>();
64  const SensorValue1DoFGravityTorque* gravityTorqueSensor = sv->asA<SensorValue1DoFGravityTorque>();
65  if (!torqueSensor)
66  {
67  ARMARX_WARNING << "No Torque sensor available for " << jointName;
68  }
69  if (!gravityTorqueSensor)
70  {
71  ARMARX_WARNING << "No Gravity Torque sensor available for " << jointName;
72  }
73  torqueSensors.push_back(torqueSensor);
74  gravityTorqueSensors.push_back(gravityTorqueSensor);*/
75  };
76 
77  tcp = (config->tcpName.empty() || config->tcpName == DEFAULT_TCP_STRING)
78  ? rns->getTCP()
79  : rtGetRobot()->getRobotNode(config->tcpName);
80  ARMARX_CHECK_EXPRESSION(tcp) << config->tcpName;
81 
82  nodeSetName = config->nodeSetName;
83 
84  ik.reset(new VirtualRobot::DifferentialIK(
85  rns, rns->getRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
86 
87 
88  /*NJointCartesianTorqueControllerControlData initData;
89  initData.nullspaceJointVelocities.resize(tcpController->rns->getSize(), 0);
90  initData.torqueKp.resize(tcpController->rns->getSize(), 0);
91  initData.mode = ModeFromIce(config->mode);
92  reinitTripleBuffer(initData);*/
93  }
94 
95  void
97  {
98  }
99 
100  void
102  const IceUtil::Time& timeSinceLastIteration)
103  {
104  Eigen::VectorXf cartesianFT(6);
105  cartesianFT << rtGetControlStruct().forceX / 1000, rtGetControlStruct().forceY / 1000,
108 
109  Eigen::MatrixXf jacobi =
110  ik->getJacobianMatrix(tcp, VirtualRobot::IKSolver::CartesianSelection::All);
111  Eigen::MatrixXf jacobiT = jacobi.transpose();
112 
113 
114  Eigen::VectorXf jointTargetTorques = jacobiT * cartesianFT;
115 
116  ARMARX_IMPORTANT << deactivateSpam(0.5) << VAROUT(jointTargetTorques.transpose());
117 
118  for (size_t i = 0; i < targets.size(); ++i)
119  {
120  targets.at(i)->torque = jointTargetTorques(i);
121  }
122  }
123 
124  WidgetDescription::HBoxLayoutPtr
126  {
127  using namespace armarx::WidgetDescription;
128  HBoxLayoutPtr layout = new HBoxLayout;
129  auto addSlider = [&](const std::string& label, float min, float max, float defaultValue)
130  {
131  layout->children.emplace_back(new Label(false, label));
132  FloatSliderPtr floatSlider = new FloatSlider;
133  floatSlider->name = label;
134  floatSlider->min = min;
135  floatSlider->defaultValue = defaultValue;
136  floatSlider->max = max;
137  layout->children.emplace_back(floatSlider);
138  };
139 
140  addSlider("forceX", -10, 10, 0);
141  addSlider("forceY", -10, 10, 0);
142  addSlider("forceZ", -10, 10, 0);
143  addSlider("torqueX", -1, 1, 0);
144  addSlider("torqueY", -1, 1, 0);
145  addSlider("torqueZ", -1, 1, 0);
146 
147  return layout;
148  }
149 
150  /*WidgetDescription::HBoxLayoutPtr NJointCartesianTorqueController::createJointSlidersLayout(float min, float max, float defaultValue) const
151  {
152  using namespace armarx::WidgetDescription;
153  HBoxLayoutPtr layout = new HBoxLayout;
154  auto addSlider = [&](const std::string & label)
155  {
156  layout->children.emplace_back(new Label(false, label));
157  FloatSliderPtr floatSlider = new FloatSlider;
158  floatSlider->name = label;
159  floatSlider->min = min;
160  floatSlider->defaultValue = defaultValue;
161  floatSlider->max = max;
162  layout->children.emplace_back(floatSlider);
163  };
164 
165  for (const VirtualRobot::RobotNodePtr& rn : tcpController->rns->getAllRobotNodes())
166  {
167  addSlider(rn->getName());
168  }
169 
170  return layout;
171  }*/
172 
175  const VirtualRobot::RobotPtr& robot,
176  const std::map<std::string, ConstControlDevicePtr>& controlDevices,
177  const std::map<std::string, ConstSensorDevicePtr>&)
178  {
179  using namespace armarx::WidgetDescription;
180  HBoxLayoutPtr layout = new HBoxLayout;
181 
182  LabelPtr label = new Label;
183  label->text = "nodeset name";
184  layout->children.emplace_back(label);
185  StringComboBoxPtr box = new StringComboBox;
186  box->defaultIndex = 0;
187 
188  box->options = robot->getRobotNodeSetNames();
189  box->name = "nodeSetName";
190  layout->children.emplace_back(box);
191 
192  LabelPtr labelTCP = new Label;
193  labelTCP->text = "tcp name";
194  layout->children.emplace_back(labelTCP);
195  StringComboBoxPtr boxTCP = new StringComboBox;
196  boxTCP->defaultIndex = 0;
197 
198  boxTCP->options = robot->getRobotNodeNames();
199  boxTCP->options.insert(boxTCP->options.begin(), DEFAULT_TCP_STRING);
200  boxTCP->name = "tcpName";
201  layout->children.emplace_back(boxTCP);
202 
203  //LabelPtr labelMode = new Label;
204  //labelMode->text = "mode";
205  //layout->children.emplace_back(labelMode);
206  //StringComboBoxPtr boxMode = new StringComboBox;
207  //boxMode->defaultIndex = 0;
208  //
209  //boxMode->options = {"Position", "Orientation", "Both"};
210  //boxMode->name = "mode";
211  //layout->children.emplace_back(boxMode);
212 
213 
214  layout->children.emplace_back(new HSpacer);
215  return layout;
216  }
217 
218  NJointCartesianTorqueControllerConfigPtr
220  {
221  return new NJointCartesianTorqueControllerConfig(values.at("nodeSetName")->getString(),
222  values.at("tcpName")->getString());
223  }
224 
225  std::string
227  {
228  return nodeSetName;
229  }
230 
231  void
233  {
234  }
235 
238  {
239  using namespace armarx::WidgetDescription;
240  HBoxLayoutPtr layout = createSliders();
241 
242  return {{"ControllerTarget", layout}};
243  }
244 
245  void
247  const StringVariantBaseMap& valueMap,
248  const Ice::Current&)
249  {
250  if (name == "ControllerTarget")
251  {
253  getWriterControlStruct().forceX = valueMap.at("forceX")->getFloat();
254  getWriterControlStruct().forceY = valueMap.at("forceY")->getFloat();
255  getWriterControlStruct().forceZ = valueMap.at("forceZ")->getFloat();
256  getWriterControlStruct().torqueX = valueMap.at("torqueX")->getFloat();
257  getWriterControlStruct().torqueY = valueMap.at("torqueY")->getFloat();
258  getWriterControlStruct().torqueZ = valueMap.at("torqueZ")->getFloat();
260  }
261  else
262  {
263  ARMARX_WARNING << "Unknown function name called: " << name;
264  }
265  }
266 } // namespace armarx
267 
268 void
270  float forceY,
271  float forceZ,
272  float torqueX,
273  float torqueY,
274  float torqueZ,
275  const Ice::Current&)
276 {
278  getWriterControlStruct().forceX = forceX;
279  getWriterControlStruct().forceY = forceY;
280  getWriterControlStruct().forceZ = forceZ;
281  getWriterControlStruct().torqueX = torqueX;
282  getWriterControlStruct().torqueY = torqueY;
283  getWriterControlStruct().torqueZ = torqueZ;
285 }
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
armarx::NJointCartesianTorqueControllerControlData::torqueX
float torqueX
Definition: NJointCartesianTorqueController.h:64
armarx::NJointControllerBase::useSynchronizedRtRobot
const VirtualRobot::RobotPtr & useSynchronizedRtRobot(bool updateCollisionModel=false)
Requests a VirtualRobot for use in rtRun *.
Definition: NJointController.cpp:293
armarx::NJointControllerWithTripleBuffer< NJointCartesianTorqueControllerControlData >::rtGetControlStruct
const NJointCartesianTorqueControllerControlData & rtGetControlStruct() const
Definition: NJointControllerWithTripleBuffer.h:32
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::NJointCartesianTorqueController::callDescribedFunction
void callDescribedFunction(const std::string &name, const StringVariantBaseMap &valueMap, const Ice::Current &) override
Definition: NJointCartesianTorqueController.cpp:246
armarx::ControlTargetBase
Brief description of class JointControlTargetBase.
Definition: ControlTargetBase.h:47
armarx::WidgetDescription::StringWidgetDictionary
::std::map<::std::string, ::armarx::WidgetDescription::WidgetPtr > StringWidgetDictionary
Definition: NJointControllerBase.h:69
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::RemoteGui::Client::HSpacer
Definition: Widgets.h:209
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::NJointCartesianTorqueController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: NJointCartesianTorqueController.cpp:42
armarx::NJointControllerWithTripleBuffer< NJointCartesianTorqueControllerControlData >::getWriterControlStruct
NJointCartesianTorqueControllerControlData & getWriterControlStruct()
Definition: NJointControllerWithTripleBuffer.h:54
armarx::NJointCartesianTorqueController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: NJointCartesianTorqueController.cpp:96
armarx::NJointControllerWithTripleBuffer< NJointCartesianTorqueControllerControlData >::LockGuardType
std::lock_guard< std::recursive_mutex > LockGuardType
Definition: NJointControllerWithTripleBuffer.h:14
armarx::NJointCartesianTorqueController::GenerateConfigFromVariants
static NJointCartesianTorqueControllerConfigPtr GenerateConfigFromVariants(const StringVariantBaseMap &values)
Definition: NJointCartesianTorqueController.cpp:219
armarx::NJointCartesianTorqueControllerControlData::forceY
float forceY
Definition: NJointCartesianTorqueController.h:62
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::NJointControllerBase::rtGetRobot
const VirtualRobot::RobotPtr & rtGetRobot()
TODO make protected and use attorneys.
Definition: NJointControllerBase.h:845
visionx::voxelgrid::Label
uint32_t Label
Type of an object label.
Definition: types.h:7
armarx::NJointCartesianTorqueController::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: NJointCartesianTorqueController.cpp:232
armarx::WidgetDescription
Definition: DefaultWidgetDescriptions.cpp:27
armarx::NJointCartesianTorqueController::NJointCartesianTorqueController
NJointCartesianTorqueController(RobotUnit *prov, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
DEFAULT_TCP_STRING
#define DEFAULT_TCP_STRING
Definition: NJointCartesianTorqueController.cpp:33
armarx::RemoteGui::Client::FloatSlider
Definition: Widgets.h:107
armarx::NJointCartesianTorqueControllerControlData::torqueZ
float torqueZ
Definition: NJointCartesianTorqueController.h:66
armarx::NJointCartesianTorqueControllerControlData::torqueY
float torqueY
Definition: NJointCartesianTorqueController.h:65
armarx::NJointCartesianTorqueController::setControllerTarget
void setControllerTarget(float forceX, float forceY, float forceZ, float torqueX, float torqueY, float torqueZ, const Ice::Current &) override
Definition: NJointCartesianTorqueController.cpp:269
armarx::NJointCartesianTorqueController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: NJointCartesianTorqueController.cpp:101
armarx::NJointCartesianTorqueController::createSliders
::armarx::WidgetDescription::HBoxLayoutPtr createSliders()
Definition: NJointCartesianTorqueController.cpp:125
armarx::NJointControllerWithTripleBuffer< NJointCartesianTorqueControllerControlData >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
armarx::NJointCartesianTorqueController::getNodeSetName
std::string getNodeSetName() const
Definition: NJointCartesianTorqueController.cpp:226
armarx::NJointControllerWithTripleBuffer< NJointCartesianTorqueControllerControlData >::controlDataMutex
MutexType controlDataMutex
Definition: NJointControllerWithTripleBuffer.h:73
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::RemoteGui::Client::HBoxLayout
Definition: Widgets.h:160
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::NJointCartesianTorqueControllerControlData::forceZ
float forceZ
Definition: NJointCartesianTorqueController.h:63
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::NJointCartesianTorqueController::getFunctionDescriptions
WidgetDescription::StringWidgetDictionary getFunctionDescriptions(const Ice::Current &) const override
Definition: NJointCartesianTorqueController.cpp:237
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
armarx::RobotUnit
The RobotUnit class manages a robot and its controllers.
Definition: RobotUnit.h:180
NJointCartesianTorqueController.h
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
armarx::NJointCartesianTorqueController::GenerateConfigDescription
static WidgetDescription::WidgetPtr GenerateConfigDescription(const VirtualRobot::RobotPtr &, const std::map< std::string, ConstControlDevicePtr > &, const std::map< std::string, ConstSensorDevicePtr > &)
Definition: NJointCartesianTorqueController.cpp:174
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::NJointCartesianTorqueControllerControlData::forceX
float forceX
Definition: NJointCartesianTorqueController.h:61
NJointControllerRegistry.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::registrationControllerNJointCartesianTorqueController
NJointControllerRegistration< NJointCartesianTorqueController > registrationControllerNJointCartesianTorqueController("NJointCartesianTorqueController")
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18