HandControlBase.cpp
Go to the documentation of this file.
1#include "HandControlBase.h"
2
3#include <VirtualRobot/RobotNodeSet.h> // IWYU pragma: keep
4
6
8
10{
11
13 const VirtualRobot::RobotPtr& rtRobot,
14 const HandConfigDict& cfg)
15 {
16 for (const auto& pair : cfg)
17 {
18 auto handNodeSet = rtRobot->getRobotNodeSet(pair.first);
19 hands.emplace(pair.first,
20 std::make_unique<HandData>(robotUnit, handNodeSet, pair.second));
21 }
22 }
23
24 void
26 {
27 for (auto& pair : hands)
28 {
29 pair.second->setTarget();
30 }
31 }
32
33 void
35 {
36 for (auto& [name, hand] : hands)
37 {
38 hand->targetNonRT = hand->bufferUserToNonRT.getUpToDateReadBuffer();
39 hand->rtsInNonRT = hand->bufferRTToNonRT.getUpToDateReadBuffer();
40 }
41 }
42
43 void
45 {
46 for (auto& [_, hand] : hands)
47 {
48 hand->rts.deltaT = deltaT;
49 hand->rts.accumulateTime += deltaT;
50 hand->bufferRTToNonRT.getWriteBuffer() = hand->rts;
51 hand->bufferRTToNonRT.commitWrite();
52 }
53 }
54
55 void
57 {
58 for (const auto& [name, hand_cfg] : cfg)
59 {
60 if (hand_cfg.jointPosition.has_value())
61 {
62 auto& target = hands.at(name)->bufferUserToNonRT.getWriteBuffer();
63 target.jointPosition = hand_cfg.jointPosition;
64 hands.at(name)->bufferUserToNonRT.commitWrite();
65 }
66 }
67 }
68
69 void
71 {
72 for (auto& [name, hand] : hands)
73 {
74 cfgToUpdate.at(name) = hand->bufferConfigNonRTToUser.getUpToDateReadBuffer();
75 }
76 }
77
78 void
80 {
81 for (auto& [name, hand] : hands)
82 {
83 cfgToUpdate.at(name) = hand->targetNonRT;
84 hand->bufferUserToNonRT.reinitAllBuffers(hand->targetNonRT);
85 hand->bufferConfigNonRTToOnPublish.reinitAllBuffers(hand->targetNonRT);
86 }
87 }
88
89 void
91 {
92 StringVariantBaseMap datafields;
93 auto rtData = hand->bufferConfigNonRTToOnPublish.getUpToDateReadBuffer();
94
95 if (rtData.jointPosition.has_value())
96 {
97 common::debugEigenVec(datafields, "jointPosition", rtData.jointPosition.value());
98 debugObs->setDebugChannel("hand_" + hand->kinematicChainName, datafields);
99 }
100 }
101
102 void
104 {
105 for (auto& pair : hands)
106 {
107 limbPublish(pair.second, debugObs);
108 }
109 }
110
111 void
113 {
114 for (auto& pair : hands)
115 {
116 pair.second->ctrl->activateController();
117 }
118 }
119
120 void
122 {
123 for (auto& pair : hands)
124 {
125 pair.second->rtPreActivate();
126 }
127 }
128
129 void
131 {
132 // The controller should not be deactivated
133 getConfig(cfgToUpdate);
134 }
135
136 /// ------------------------------------- HandData ---------------------------------------------
137 void
139 {
140 ARMARX_CHECK_EQUAL(targetNonRT.jointPosition.value().size(), jointLimMin.size());
141 ARMARX_CHECK_EQUAL(targetNonRT.jointPosition.value().size(), jointLimMax.size());
142
143 targetNonRT.jointPosition.value() =
144 targetNonRT.jointPosition.value().cwiseMax(jointLimMin).cwiseMin(jointLimMax);
145 }
146
147 void
149 {
150 if (targetNonRT.jointPosition.has_value())
151 {
152 validate();
153
154 if (not isTargetChanged())
155 {
156 return;
157 }
158 for (unsigned int i = 0; i < jointNames.size(); i++)
159 {
160 desiredJointPositionMap.at(jointNames[i]) = targetNonRT.jointPosition.value()(i);
161 }
162 }
163 ctrl->setTargetsMap(desiredJointPositionMap);
164
165 bufferConfigNonRTToOnPublish.getWriteBuffer() = targetNonRT;
166 bufferConfigNonRTToOnPublish.commitWrite();
167
168 bufferConfigNonRTToUser.getWriteBuffer() = targetNonRT;
169 bufferConfigNonRTToUser.commitWrite();
170 }
171
172 bool
174 {
175 bool changed =
176 not(targetNonRT.jointPosition.value() - prevTargetNonRT.jointPosition.value())
178 if (changed)
179 {
181 }
182 return changed;
183 }
184
186 const VirtualRobot::RobotNodeSetPtr& robotNodeSet,
187 const HandConfig& cfg) :
188 kinematicChainName(robotNodeSet->getName()),
189 jointNames(robotNodeSet->getNodeNames()),
190 nJoints(robotNodeSet->getSize())
191 {
192 auto controller = robotUnit->getNJointController(cfg.njointControllerName);
193 ARMARX_CHECK_NOT_NULL(controller) << "Hand controller " << cfg.njointControllerName
194 << " is not available in "
195 "robot Unit";
196 ctrl = Ice::checkedCast<hand::HandShapeControllerInterfacePrx>(controller);
198
199 targetNonRT = cfg;
200 rts.deltaT = 0.0;
201 rts.accumulateTime = 0.0;
202 rtsInNonRT = rts;
203 jointLimMin = Eigen::Map<Eigen::VectorXf>(robotNodeSet->getNodeLimitsLo().data(), nJoints);
204 jointLimMax = Eigen::Map<Eigen::VectorXf>(robotNodeSet->getNodeLimitsHi().data(), nJoints);
205 ARMARX_CHECK_EQUAL(static_cast<size_t>(jointLimMin.size()), nJoints);
206 ARMARX_CHECK_EQUAL(static_cast<size_t>(jointLimMax.size()), nJoints);
207
208 for (auto& name : jointNames)
209 {
210 if (name.find(kinematicChainName) == 0)
211 {
212 name.erase(0, kinematicChainName.length());
213 }
214 ARMARX_INFO << "-- " << kinematicChainName << " adding joint: " << name;
215 desiredJointPositionMap.emplace(name, 0.0f);
216 }
217 }
218
219 void
221 {
222 ARMARX_RT_LOGF_INFO("--> rtPreActivate for hand: %s", kinematicChainName.c_str());
223
224 rtsInNonRT = rts;
225 desiredJointPositionMap = ctrl->getJointValuesMap();
226 if (not targetNonRT.jointPosition.has_value())
227 {
228 targetNonRT.jointPosition = Eigen::VectorXf::Zero(jointNames.size());
229 }
230
231 for (auto i = 0; i < targetNonRT.jointPosition.value().size(); ++i)
232 {
233 if (desiredJointPositionMap.count(jointNames[i]) > 0)
234 {
235 targetNonRT.jointPosition.value()(i) = desiredJointPositionMap.at(jointNames[i]);
236 }
237 else
238 {
239 ARMARX_ERROR << "Inconsistent hand joint: " << jointNames[i]
240 << " does not exist in hand shape controller";
241 }
242 }
245 nonRTDeltaT = 0.0;
246 ARMARX_CHECK_EQUAL(static_cast<size_t>(targetNonRT.jointPosition.value().size()), nJoints);
247
248 bufferRTToNonRT.reinitAllBuffers(rts);
249 bufferUserToNonRT.reinitAllBuffers(targetNonRT);
251 bufferConfigNonRTToUser.reinitAllBuffers(targetNonRT);
252 }
253
254
255} // namespace armarx::control::njoint_controller::core
#define ARMARX_RT_LOGF_INFO(...)
void onPublish(const DebugObserverInterfacePrx &debugObs)
void limbPublish(HandPtr &hand, const DebugObserverInterfacePrx &debugObs)
HandControlBase(const RobotUnitPtr &robotUnit, const VirtualRobot::RobotPtr &rtRobot, const HandConfigDict &cfg)
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
void debugEigenVec(StringVariantBaseMap &datafields, const std::string &name, Eigen::VectorXf vec)
Definition utils.cpp:190
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
std::map< std::string, VariantBasePtr > StringVariantBaseMap
IceUtil::Handle< class RobotUnit > RobotUnitPtr
Definition FTSensor.h:34
void validate()
----------------------------------— HandData ------------------------------------------—
HandData(const RobotUnitPtr &robotUnit, const VirtualRobot::RobotNodeSetPtr &robotNodeSet, const HandConfig &cfg)