TCPControllerSubUnit.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 RobotAPI::ArmarXObjects::TCPControllerSubUnit
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "TCPControllerSubUnit.h"
24 
28 #include <RobotAPI/interface/units/RobotUnit/NJointCartesianVelocityControllerWithRamp.h>
30 
31 
32 using namespace armarx;
33 
34 void
36 {
37 }
38 
41 {
44 }
45 
46 void
48 {
50  ARMARX_CHECK_EXPRESSION(!this->coordinateTransformationRobot);
51  coordinateTransformationRobot = robot;
52 
53  ARMARX_CHECK_EXPRESSION(!robotUnit);
55  robotUnit = rUnit;
56 }
57 
58 void
60 {
62  << "Setting cycle time in RT-Controller does not have an effect";
63 }
64 
65 void
66 TCPControllerSubUnit::setTCPVelocity(const std::string& nodeSetName,
67  const std::string& tcpName,
68  const FramedDirectionBasePtr& translationVelocity,
69  const FramedDirectionBasePtr& orientationVelocityRPY,
70  const Ice::Current& c)
71 {
72  std::unique_lock lock(dataMutex);
73  ARMARX_CHECK_EXPRESSION(coordinateTransformationRobot->hasRobotNodeSet(nodeSetName))
74  << "The robot does not have the node set: " + nodeSetName;
75  std::string tcp;
76  if (tcpName.empty())
77  {
78  tcp = coordinateTransformationRobot->getRobotNodeSet(nodeSetName)->getTCP()->getName();
79  }
80  else
81  {
82  ARMARX_CHECK_EXPRESSION(coordinateTransformationRobot->hasRobotNode(tcpName))
83  << "The robot does not have the node: " + tcpName;
84  tcp = tcpName;
85  }
86 
87  robotUnit->updateVirtualRobot(coordinateTransformationRobot);
88 
89  float xVel = 0.f;
90  float yVel = 0.f;
91  float zVel = 0.f;
92  float rollVel = 0.f;
93  float pitchVel = 0.f;
94  float yawVel = 0.f;
95 
96 
97  FramedDirectionPtr globalTransVel = FramedDirectionPtr::dynamicCast(translationVelocity);
98  if (globalTransVel)
99  {
100  globalTransVel->changeFrame(coordinateTransformationRobot,
101  coordinateTransformationRobot->getRootNode()->getName());
102  xVel = globalTransVel->x;
103  yVel = globalTransVel->y;
104  zVel = globalTransVel->z;
105  }
106 
107  FramedDirectionPtr globalOriVel = FramedDirectionPtr::dynamicCast(orientationVelocityRPY);
108  if (globalOriVel)
109  {
110  globalOriVel->changeFrame(coordinateTransformationRobot,
111  coordinateTransformationRobot->getRootNode()->getName());
112  rollVel = globalOriVel->x;
113  pitchVel = globalOriVel->y;
114  yawVel = globalOriVel->z;
115  }
116 
118  bool noMode = false;
119  if (globalTransVel && globalOriVel)
120  {
122  }
123  else if (globalTransVel && !globalOriVel)
124  {
126  }
127  else if (!globalTransVel && globalOriVel)
128  {
130  }
131  else
132  {
133  noMode = true;
134  }
135  ARMARX_DEBUG << "CartesianSelection-Mode: " << (int)mode;
136  auto controllerName =
137  this->getName() + "_" + tcp + "_" + nodeSetName + "_mode_" + std::to_string((int)mode);
138  auto NJointControllers = robotUnit->getNJointControllerNames();
139  NJointCartesianVelocityControllerWithRampPtr tcpController;
140  bool nodeSetAlreadyControlled = false;
141  for (auto& name : NJointControllers)
142  {
143  NJointControllerBasePtr controller;
144  try
145  {
146  controller = robotUnit->getNJointControllerNotNull(name);
147  }
148  catch (...)
149  {
150  continue;
151  }
152 
153  tcpController = NJointCartesianVelocityControllerWithRampPtr::dynamicCast(controller);
154  if (!tcpController)
155  {
156  continue;
157  }
158  if (tcpController->getNodeSetName() == nodeSetName &&
159  tcpController->getInstanceName() == controllerName)
160  {
161  nodeSetAlreadyControlled = true;
162  break;
163  }
164  }
165 
166  if (!nodeSetAlreadyControlled)
167  {
168  NJointCartesianVelocityControllerWithRampConfigPtr config =
169  new NJointCartesianVelocityControllerWithRampConfig(
170  nodeSetName, tcp, mode, 500, 1, 2, 0.3f, 2);
171  // NJointCartesianVelocityControllerConfigPtr config = new NJointCartesianVelocityControllerConfig(nodeSetName, tcp, mode);
172  NJointCartesianVelocityControllerWithRampPtr ctrl =
173  NJointCartesianVelocityControllerWithRampPtr::dynamicCast(
174  robotUnit->createNJointController("NJointCartesianVelocityControllerWithRamp",
175  controllerName,
176  config,
177  true,
178  true));
179 
180  tcpController = ctrl;
181  tcpControllerNameMap[nodeSetName] = controllerName;
182  tcpController->setKpJointLimitAvoidance(getProperty<float>("AvoidJointLimitsKp").getValue(),
183  c);
184  }
185 
186 
187  // Only activate controller if at least either translationVelocity or orientaionVelocity is set
188  if (!noMode)
189  {
190  ARMARX_CHECK_EXPRESSION(tcpController);
191  // ARMARX_CHECK_EXPRESSION(tcpController->getObjectScheduler());
192  tcpController->setTargetVelocity(xVel, yVel, zVel, rollVel, pitchVel, yawVel, c);
193  // if (!tcpController->getObjectScheduler()->waitForObjectStateMinimum(eManagedIceObjectStarted, 5000))
194  // {
195  // ARMARX_ERROR << "NJointController was not initialized after 5000ms - bailing out!";
196  // return;
197  // }
198  if (!tcpController->isControllerActive())
199  {
200  tcpController->activateController();
201  }
202  }
203 }
204 
205 bool
207 {
208  std::unique_lock lock(dataMutex);
209  for (auto& pair : tcpControllerNameMap)
210  {
211  auto ctrl = robotUnit->getNJointController(pair.second);
212  if (ctrl && ctrl->isControllerActive())
213  {
214  return true;
215  }
216  }
217  return false;
218 }
219 
220 void
222  const std::set<std::string>& changedProperties)
223 {
224  if (changedProperties.count("AvoidJointLimitsKp") && robotUnit)
225  {
226  float avoidJointLimitsKp = getProperty<float>("AvoidJointLimitsKp").getValue();
227  auto activeNJointControllers =
228  robotUnit->getNJointControllersNotNull(robotUnit->getNJointControllerNames());
229  for (NJointControllerBasePtr controller : activeNJointControllers)
230  {
231  auto tcpController =
232  NJointCartesianVelocityControllerWithRampPtr::dynamicCast(controller);
233  if (tcpController)
234  {
235  tcpController->setKpJointLimitAvoidance(avoidJointLimitsKp, Ice::emptyCurrent);
236  }
237  }
238  }
239 }
armarx::TCPControllerSubUnit::componentPropertiesUpdated
void componentPropertiesUpdated(const std::set< std::string > &changedProperties) override
Implement this function if you would like to react to changes in the properties.
Definition: TCPControllerSubUnit.cpp:221
armarx::JointAndNJointControllers
Structure used by the RobotUnit to swap lists of Joint and NJoint controllers.
Definition: JointAndNJointControllers.h:32
RobotUnit.h
armarx::NJointTaskSpaceDMPControllerMode::ePosition
@ ePosition
Definition: ControllerInterface.ice:36
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::RobotUnitModule::ControllerManagement::createNJointController
NJointControllerInterfacePrx createNJointController(const std::string &className, const std::string &instanceName, const NJointControllerConfigPtr &config, const Ice::Current &=Ice::emptyCurrent) override
Cretes a NJointControllerBase.
Definition: RobotUnitModuleControllerManagement.cpp:176
NJointCartesianVelocityControllerWithRamp.h
armarx::RobotUnitModule::ControlThreadDataBuffer::updateVirtualRobot
void updateVirtualRobot(const VirtualRobot::RobotPtr &robot) const
Updates the given VirtualRobot with the current joint values.
Definition: RobotUnitModuleControlThreadDataBuffer.cpp:361
IceInternal::Handle< FramedDirection >
armarx::TCPControllerSubUnit::setCycleTime
void setCycleTime(Ice::Int milliseconds, const Ice::Current &c=Ice::emptyCurrent) override
Definition: TCPControllerSubUnit.cpp:59
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:177
controller
Definition: AddOperation.h:39
armarx::TCPControllerSubUnit::setup
void setup(RobotUnit *rUnit, VirtualRobot::RobotPtr robot)
Definition: TCPControllerSubUnit.cpp:47
armarx::TCPControllerSubUnit::setTCPVelocity
void setTCPVelocity(const std::string &nodeSetName, const std::string &tcpName, const ::armarx::FramedDirectionBasePtr &translationVelocity, const ::armarx::FramedDirectionBasePtr &orientationVelocityRPY, const Ice::Current &c=Ice::emptyCurrent) override
Definition: TCPControllerSubUnit.cpp:66
FramedPose.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::RobotUnitModule::ControllerManagement::getNJointControllerNotNull
const NJointControllerBasePtr & getNJointControllerNotNull(const std::string &name) const
Returns a pointer to the NJointControllerBase.
Definition: RobotUnitModuleControllerManagement.cpp:124
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
NJointCartesianVelocityController.h
armarx::RobotUnitModule::ControllerManagement::getNJointController
NJointControllerInterfacePrx getNJointController(const std::string &name, const Ice::Current &=Ice::emptyCurrent) const override
Returns a proxy to the NJointControllerBase.
Definition: RobotUnitModuleControllerManagement.cpp:660
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
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::NJointTaskSpaceDMPControllerMode::eAll
@ eAll
Definition: ControllerInterface.ice:38
armarx::TCPControllerSubUnit::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: TCPControllerSubUnit.cpp:40
armarx::TCPControllerSubUnitPropertyDefinitions
Definition: TCPControllerSubUnit.h:39
armarx::NJointTaskSpaceDMPControllerMode::eOrientation
@ eOrientation
Definition: ControllerInterface.ice:37
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::RobotUnit
The RobotUnit class manages a robot and its controllers.
Definition: RobotUnit.h:180
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::TCPControllerSubUnit::update
void update(const SensorAndControl &sc, const JointAndNJointControllers &c) override
Definition: TCPControllerSubUnit.cpp:35
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::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
TCPControllerSubUnit.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::RobotUnitModule::ControllerManagement::getNJointControllerNames
Ice::StringSeq getNJointControllerNames(const Ice::Current &=Ice::emptyCurrent) const override
Returns the names of all NJointControllers.
Definition: RobotUnitModuleControllerManagement.cpp:313
armarx::TCPControllerSubUnit::isRequested
bool isRequested(const Ice::Current &=Ice::emptyCurrent) override
Definition: TCPControllerSubUnit.cpp:206
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18