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
24
28#include <RobotAPI/interface/units/RobotUnit/NJointCartesianVelocityControllerWithRamp.h>
30
31
32using namespace armarx;
33
34void
38
45
46void
48{
50 ARMARX_CHECK_EXPRESSION(!this->coordinateTransformationRobot);
51 coordinateTransformationRobot = robot;
52
53 ARMARX_CHECK_EXPRESSION(!robotUnit);
55 robotUnit = rUnit;
56}
57
58void
59TCPControllerSubUnit::setCycleTime(Ice::Int, const Ice::Current& c)
60{
62 << "Setting cycle time in RT-Controller does not have an effect";
63}
64
65void
66TCPControllerSubUnit::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
117 CartesianSelectionMode::CartesianSelection mode = CartesianSelectionMode::eAll;
118 bool noMode = false;
119 if (globalTransVel && globalOriVel)
120 {
121 mode = CartesianSelectionMode::eAll;
122 }
123 else if (globalTransVel && !globalOriVel)
124 {
125 mode = CartesianSelectionMode::ePosition;
126 }
127 else if (!globalTransVel && globalOriVel)
128 {
129 mode = CartesianSelectionMode::eOrientation;
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
205bool
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
220void
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}
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
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:99
std::string getName() const
Retrieve name of object.
The RobotUnit class manages a robot and its controllers.
Definition RobotUnit.h:192
void componentPropertiesUpdated(const std::set< std::string > &changedProperties) override
Implement this function if you would like to react to changes in the properties.
void update(const SensorAndControl &sc, const JointAndNJointControllers &c) override
void setCycleTime(Ice::Int milliseconds, const Ice::Current &c=Ice::emptyCurrent) override
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
PropertyDefinitionsPtr createPropertyDefinitions() override
void setup(RobotUnit *rUnit, VirtualRobot::RobotPtr robot)
bool isRequested(const Ice::Current &=Ice::emptyCurrent) override
#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_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< FramedDirection > FramedDirectionPtr
Definition FramedPose.h:84
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
detail::ControlThreadOutputBufferEntry SensorAndControl
Structure used by the RobotUnit to swap lists of Joint and NJoint controllers.