NJointTCPController.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  */
24 #include "NJointTCPController.h"
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<NJointTCPController>
39  registrationControllerNJointTCPController("NJointTCPController");
40 
41  std::string
42  NJointTCPController::getClassName(const Ice::Current&) const
43  {
44  return "NJointTCPController";
45  }
46 
47  void
49  {
51  }
52 
53  void
54  NJointTCPController::rtRun(const IceUtil::Time& sensorValuesTimestamp,
55  const IceUtil::Time& timeSinceLastIteration)
56  {
57  auto mode = rtGetControlStruct().mode;
58 
59  Eigen::VectorXf x;
60  if (mode == VirtualRobot::IKSolver::All)
61  {
62  x.resize(6);
66  }
67  else if (mode == VirtualRobot::IKSolver::Position)
68  {
69  x.resize(3);
71  }
72  else if (mode == VirtualRobot::IKSolver::Orientation)
73  {
74  x.resize(3);
77  }
78  else
79  {
80  // No mode has been set
81  return;
82  }
83 
84  Eigen::MatrixXf jacobiInv = ik->getPseudoInverseJacobianMatrix(tcp, mode);
85  Eigen::VectorXf jointTargetVelocities = jacobiInv * x;
86 
87  for (size_t i = 0; i < targets.size(); ++i)
88  {
89  targets.at(i)->velocity = jointTargetVelocities(i);
90  }
91  }
92 
93  ::armarx::WidgetDescription::WidgetSeq
95  {
96  using namespace armarx::WidgetDescription;
97  ::armarx::WidgetDescription::WidgetSeq widgets;
98  auto addSlider = [&](const std::string& label, float limit)
99  {
100  widgets.emplace_back(new Label(false, label));
101  {
102  FloatSliderPtr c_x = new FloatSlider;
103  c_x->name = label;
104  c_x->min = -limit;
105  c_x->defaultValue = 0.0f;
106  c_x->max = limit;
107  widgets.emplace_back(c_x);
108  }
109  };
110 
111  addSlider("x", 100);
112  addSlider("y", 100);
113  addSlider("z", 100);
114  addSlider("roll", 0.5);
115  addSlider("pitch", 0.5);
116  addSlider("yaw", 0.5);
117  return widgets;
118  }
119 
122  const VirtualRobot::RobotPtr& robot,
123  const std::map<std::string, ConstControlDevicePtr>& controlDevices,
124  const std::map<std::string, ConstSensorDevicePtr>&)
125  {
126  using namespace armarx::WidgetDescription;
127  HBoxLayoutPtr layout = new HBoxLayout;
128 
129  LabelPtr label = new Label;
130  label->text = "nodeset name";
131  layout->children.emplace_back(label);
132  StringComboBoxPtr box = new StringComboBox;
133  box->defaultIndex = 0;
134 
135  box->options = robot->getRobotNodeSetNames();
136  box->name = "nodeSetName";
137  layout->children.emplace_back(box);
138 
139  LabelPtr labelTCP = new Label;
140  labelTCP->text = "tcp name";
141  layout->children.emplace_back(labelTCP);
142  StringComboBoxPtr boxTCP = new StringComboBox;
143  boxTCP->defaultIndex = 0;
144 
145  boxTCP->options = robot->getRobotNodeNames();
146  boxTCP->options.insert(boxTCP->options.begin(), DEFAULT_TCP_STRING);
147  boxTCP->name = "tcpName";
148  layout->children.emplace_back(boxTCP);
149 
150  LabelPtr labelMode = new Label;
151  labelMode->text = "mode";
152  layout->children.emplace_back(labelMode);
153  StringComboBoxPtr boxMode = new StringComboBox;
154  boxMode->defaultIndex = 0;
155 
156  boxMode->options = {"Position", "Orientation", "Both"};
157  boxMode->name = "mode";
158  layout->children.emplace_back(boxMode);
159 
160 
161  // auto sliders = createSliders();
162  // layout->children.insert(layout->children.end(),
163  // sliders.begin(),
164  // sliders.end());
165  layout->children.emplace_back(new HSpacer);
166  return layout;
167  }
168 
169  NJointTCPControllerConfigPtr
171  {
172  return new NJointTCPControllerConfig{values.at("nodeSetName")->getString(),
173  values.at("tcpName")->getString()};
174  }
175 
177  const NJointTCPControllerConfigPtr& config,
178  const VirtualRobot::RobotPtr& r)
179  {
180  ARMARX_CHECK_EXPRESSION(robotUnit);
181  ARMARX_CHECK_EXPRESSION(!config->nodeSetName.empty());
183  auto nodeset = rtGetRobot()->getRobotNodeSet(config->nodeSetName);
184  ARMARX_CHECK_EXPRESSION(nodeset) << config->nodeSetName;
185  for (size_t i = 0; i < nodeset->getSize(); ++i)
186  {
187  auto jointName = nodeset->getNode(i)->getName();
188  ControlTargetBase* ct = useControlTarget(jointName, ControlModes::Velocity1DoF);
189  const SensorValueBase* sv = useSensorValue(jointName);
190  targets.push_back(ct->asA<ControlTarget1DoFActuatorVelocity>());
191  sensors.push_back(sv->asA<SensorValue1DoFActuatorPosition>());
192  };
193  ik.reset(new VirtualRobot::DifferentialIK(
194  nodeset, rtGetRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
195  tcp = (config->tcpName.empty() || config->tcpName == DEFAULT_TCP_STRING)
196  ? nodeset->getTCP()
197  : rtGetRobot()->getRobotNode(config->tcpName);
198  ARMARX_CHECK_EXPRESSION(tcp) << config->tcpName;
199 
200  nodeSetName = config->nodeSetName;
201  }
202 
203  void
205  float yVel,
206  float zVel,
207  float rollVel,
208  float pitchVel,
209  float yawVel,
211  {
213  getWriterControlStruct().xVel = xVel;
214  getWriterControlStruct().yVel = yVel;
215  getWriterControlStruct().zVel = zVel;
216  getWriterControlStruct().rollVel = rollVel;
217  getWriterControlStruct().pitchVel = pitchVel;
218  getWriterControlStruct().yawVel = yawVel;
219  getWriterControlStruct().mode = mode;
221  }
222 
223  std::string
225  {
226  return nodeSetName;
227  }
228 
229  void
231  {
232  }
233 
236  {
237  using namespace armarx::WidgetDescription;
238  HBoxLayoutPtr layout = new HBoxLayout;
239  auto sliders = createSliders();
240  layout->children.insert(layout->children.end(), sliders.begin(), sliders.end());
241  return {{"ControllerTarget", layout}};
242  }
243 
244  void
246  const StringVariantBaseMap& valueMap,
247  const Ice::Current&)
248  {
249  if (name == "ControllerTarget")
250  {
252  getWriterControlStruct().xVel = valueMap.at("x")->getFloat();
253  getWriterControlStruct().yVel = valueMap.at("y")->getFloat();
254  getWriterControlStruct().zVel = valueMap.at("z")->getFloat();
255  getWriterControlStruct().rollVel = valueMap.at("roll")->getFloat();
256  getWriterControlStruct().pitchVel = valueMap.at("pitch")->getFloat();
257  getWriterControlStruct().yawVel = valueMap.at("yaw")->getFloat();
259  }
260  else
261  {
262  ARMARX_WARNING << "Unknown function name called: " << name;
263  }
264  }
265 } // namespace armarx
armarx::NJointTCPController::setVelocities
void setVelocities(float xVel, float yVel, float zVel, float rollVel, float pitchVel, float yawVel, VirtualRobot::IKSolver::CartesianSelection mode)
Definition: NJointTCPController.cpp:204
armarx::NJointControllerWithTripleBuffer< NJointTCPControllerControlData >::reinitTripleBuffer
void reinitTripleBuffer(const NJointTCPControllerControlData &initial)
Definition: NJointControllerWithTripleBuffer.h:68
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
armarx::NJointControllerBase::useSynchronizedRtRobot
const VirtualRobot::RobotPtr & useSynchronizedRtRobot(bool updateCollisionModel=false)
Requests a VirtualRobot for use in rtRun *.
Definition: NJointController.cpp:293
armarx::NJointControllerWithTripleBuffer< NJointTCPControllerControlData >::rtGetControlStruct
const NJointTCPControllerControlData & rtGetControlStruct() const
Definition: NJointControllerWithTripleBuffer.h:32
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
GfxTL::Orientation
ScalarT Orientation(const VectorXD< 2, ScalarT > &p1, const VectorXD< 2, ScalarT > &p2, const VectorXD< 2, ScalarT > &c)
Definition: Orientation.h:9
armarx::SensorValueBase::asA
const T * asA() const
Definition: SensorValueBase.h:82
armarx::SensorValueBase
The SensorValueBase class.
Definition: SensorValueBase.h:40
armarx::NJointTCPController::GenerateConfigDescription
static WidgetDescription::WidgetPtr GenerateConfigDescription(const VirtualRobot::RobotPtr &, const std::map< std::string, ConstControlDevicePtr > &, const std::map< std::string, ConstSensorDevicePtr > &)
Definition: NJointTCPController.cpp:121
armarx::ControlTargetBase
Brief description of class JointControlTargetBase.
Definition: ControlTargetBase.h:47
armarx::NJointTCPController::callDescribedFunction
void callDescribedFunction(const std::string &name, const StringVariantBaseMap &valueMap, const Ice::Current &) override
Definition: NJointTCPController.cpp:245
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
DEFAULT_TCP_STRING
#define DEFAULT_TCP_STRING
Definition: NJointTCPController.cpp:33
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::NJointTCPController::NJointTCPController
NJointTCPController(RobotUnit *prov, const NJointTCPControllerConfigPtr &config, const VirtualRobot::RobotPtr &r)
Definition: NJointTCPController.cpp:176
armarx::NJointTCPControllerControlData::yawVel
float yawVel
Definition: NJointTCPController.h:62
armarx::NJointControllerWithTripleBuffer< NJointTCPControllerControlData >::getWriterControlStruct
NJointTCPControllerControlData & getWriterControlStruct()
Definition: NJointControllerWithTripleBuffer.h:54
armarx::NJointControllerWithTripleBuffer< NJointTCPControllerControlData >::LockGuardType
std::lock_guard< std::recursive_mutex > LockGuardType
Definition: NJointControllerWithTripleBuffer.h:14
armarx::NJointTCPControllerControlData::pitchVel
float pitchVel
Definition: NJointTCPController.h:61
armarx::NJointTCPControllerControlData::rollVel
float rollVel
Definition: NJointTCPController.h:60
armarx::NJointTCPController::getFunctionDescriptions
WidgetDescription::StringWidgetDictionary getFunctionDescriptions(const Ice::Current &) const override
Definition: NJointTCPController.cpp:235
armarx::NJointTCPController::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: NJointTCPController.cpp:230
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::NJointTCPController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: NJointTCPController.cpp:42
armarx::WidgetDescription
Definition: DefaultWidgetDescriptions.cpp:27
armarx::NJointTCPControllerControlData
Definition: NJointTCPController.h:54
armarx::RemoteGui::Client::FloatSlider
Definition: Widgets.h:107
armarx::NJointTCPControllerControlData::xVel
float xVel
Definition: NJointTCPController.h:57
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::NJointTCPController::getNodeSetName
std::string getNodeSetName() const
Definition: NJointTCPController.cpp:224
armarx::NJointControllerWithTripleBuffer< NJointTCPControllerControlData >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
armarx::NJointTCPControllerControlData::zVel
float zVel
Definition: NJointTCPController.h:59
armarx::NJointControllerWithTripleBuffer< NJointTCPControllerControlData >::controlDataMutex
MutexType controlDataMutex
Definition: NJointControllerWithTripleBuffer.h:73
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::NJointTCPController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: NJointTCPController.cpp:54
armarx::NJointTCPController::createSliders
::armarx::WidgetDescription::WidgetSeq createSliders()
Definition: NJointTCPController.cpp:94
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::registrationControllerNJointTCPController
NJointControllerRegistration< NJointTCPController > registrationControllerNJointTCPController("NJointTCPController")
armarx::NJointTCPController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: NJointTCPController.cpp:48
armarx::NJointTCPControllerControlData::mode
VirtualRobot::IKSolver::CartesianSelection mode
Definition: NJointTCPController.h:63
armarx::RobotUnit
The RobotUnit class manages a robot and its controllers.
Definition: RobotUnit.h:180
armarx::NJointTCPControllerConfig
Definition: NJointTCPController.h:40
armarx::NJointTCPController::GenerateConfigFromVariants
static NJointTCPControllerConfigPtr GenerateConfigFromVariants(const StringVariantBaseMap &values)
Definition: NJointTCPController.cpp:170
armarx::navigation::core::Position
Eigen::Vector3f Position
Definition: basic_types.h:36
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::NJointTCPControllerControlData::yVel
float yVel
Definition: NJointTCPController.h:58
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
NJointTCPController.h