HandControlBase.cpp
Go to the documentation of this file.
1 #include "HandControlBase.h"
2 
4 {
5 
7  const HandConfigDict& cfg)
8  {
9  for (auto& pair : cfg)
10  {
11  auto handNodeSet = rtRobot->getRobotNodeSet(pair.first);
12  hands.emplace(pair.first, std::make_unique<HandData>(handNodeSet, pair.second));
13  }
14  }
15 
16  void
17  HandControlBase::appendDevice(const std::string& handNodeSet,
18  const SensorValueBase* sv,
20  const std::string& jointName)
21  {
23  hands.at(handNodeSet)->sensorDevices.append(sv, jointName);
24 
26  auto* casted_ct = ct->asA<ControlTarget1DoFActuatorPosition>();
27  ARMARX_CHECK_EXPRESSION(casted_ct);
28  hands.at(handNodeSet)->targets.push_back(casted_ct);
29  }
30 
31  void
33  {
34  for (auto& pair : hands)
35  {
36  auto& hand = pair.second;
37  hand->bufferNonRTToRT.getWriteBuffer() = hand->targetNonRT;
38  hand->bufferNonRTToRT.commitWrite();
39  }
40  }
41 
42  void
44  {
45  for (auto& pair : hands)
46  {
47  auto& hand = pair.second;
48  hand->targetNonRT = hand->bufferUserToNonRT.getUpToDateReadBuffer();
49  }
50  }
51 
52  void
53  HandControlBase::updateRTStatus(const double deltaT)
54  {
55  for (auto& pair : hands)
56  {
57  pair.second->rts.deltaT = deltaT;
58  pair.second->sensorDevices.getJointAngles(pair.second->rts.jointPosition.value());
59  pair.second->bufferRTToNonRT.getWriteBuffer() = pair.second->rts;
60  pair.second->bufferRTToNonRT.commitWrite();
61  }
62  }
63 
64  void
66  {
67  for (auto& pair : hands)
68  {
69  pair.second->setTarget();
70  }
71  }
72 
73  void
75  {
76  for (auto& pair : cfg)
77  {
78  if (pair.second.jointPosition.has_value())
79  {
80  auto& target = hands.at(pair.first)->bufferUserToNonRT.getWriteBuffer();
81  target.jointPosition = pair.second.jointPosition;
82  hands.at(pair.first)->bufferUserToNonRT.commitWrite();
83  }
84  }
85  }
86 
87  void
89  {
90  for (auto& pair : hands)
91  {
92  cfgToUpdate.at(pair.first) = pair.second->bufferNonRTToRT.getUpToDateReadBuffer();
93  }
94  }
95 
96  void
98  {
99  for (auto& pair : hands)
100  {
101  cfgToUpdate.at(pair.first) = pair.second->targetNonRT;
102  pair.second->bufferNonRTToRT.reinitAllBuffers(pair.second->targetNonRT);
103  pair.second->bufferUserToNonRT.reinitAllBuffers(pair.second->targetNonRT);
104  }
105  }
106 
107  void
109  {
110  for (auto& pair : hands)
111  {
112  ARMARX_INFO << "rtPreActivate for hand " << pair.first;
113  pair.second->rtPreActivate();
114  }
115  }
116 
117  void
119  {
120  }
121 
122  /// ------------------------------------- HandData ---------------------------------------------
123  void
125  {
126  ARMARX_CHECK_EQUAL(targetRT.jointPosition.value().size(), jointLimMin.size());
127  ARMARX_CHECK_EQUAL(targetRT.jointPosition.value().size(), jointLimMax.size());
128  targetRT.jointPosition.value() =
129  targetRT.jointPosition.value().cwiseMax(jointLimMin).cwiseMin(jointLimMax);
130  }
131 
132  void
134  {
135  targetRT = bufferNonRTToRT.getUpToDateReadBuffer();
136  if (targetRT.jointPosition.has_value())
137  {
138  validate();
139  for (size_t i = 0; i < targets.size(); i++)
140  {
141  targets[i]->position = targetRT.jointPosition.value()(i);
142  }
143  }
144  }
145 
146  HandControlBase::HandData::HandData(const VirtualRobot::RobotNodeSetPtr& robotNodeSet,
147  const HandConfig& cfg) :
148  kinematicChainName(robotNodeSet->getName()),
149  jointNames(robotNodeSet->getNodeNames()),
150  nJoints(robotNodeSet->getSize()),
151  targetRT(cfg),
152  targetNonRT(cfg)
153  {
154  rts.deltaT = 0.0;
155  rts.jointPosition = Eigen::VectorXf::Zero(nJoints);
156  jointLimMin = Eigen::Map<Eigen::VectorXf>(robotNodeSet->getNodeLimitsLo().data(), nJoints);
157  jointLimMax = Eigen::Map<Eigen::VectorXf>(robotNodeSet->getNodeLimitsHi().data(), nJoints);
158  ARMARX_CHECK_EQUAL(static_cast<size_t>(jointLimMin.size()), nJoints);
159  ARMARX_CHECK_EQUAL(static_cast<size_t>(jointLimMax.size()), nJoints);
160  }
161 
162  void
164  {
165  ARMARX_CHECK_EQUAL(sensorDevices.positionSensors.size(), nJoints)
166  << "In your class constructor, did you forget to call HandControlBase::appendDevice( "
167  "... )?";
168  ARMARX_CHECK_EQUAL(targets.size(), nJoints);
169  sensorDevices.getJointAngles(rts.jointPosition.value());
170  rts.jointPosition.value() =
171  rts.jointPosition.value().cwiseMax(jointLimMin).cwiseMin(jointLimMax);
172 
173  if (not targetRT.jointPosition.has_value())
174  {
175  targetRT.jointPosition = rts.jointPosition;
176  targetNonRT = targetRT;
177  }
178  ARMARX_CHECK_EQUAL(static_cast<size_t>(targetRT.jointPosition.value().size()), nJoints);
179  bufferNonRTToRT.reinitAllBuffers(targetRT);
180  bufferRTToNonRT.reinitAllBuffers(rts);
181  bufferUserToNonRT.reinitAllBuffers(targetRT);
182  bufferRTToUser.reinitAllBuffers(targetRT);
183  ARMARX_INFO_S << kinematicChainName << " joints " << targetRT.jointPosition.value();
184  }
185 
186 
187 } // namespace armarx::control::njoint_controller::core
armarx::control::njoint_controller::core::HandControlBase::setTargets
void setTargets()
Definition: HandControlBase.cpp:65
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
HandControlBase.h
armarx::SensorValueBase
The SensorValueBase class.
Definition: SensorValueBase.h:40
armarx::control::njoint_controller::core::HandControlBase::nonRTUpdateStatus
void nonRTUpdateStatus()
Definition: HandControlBase.cpp:43
armarx::ControlTargetBase
Brief description of class JointControlTargetBase.
Definition: ControlTargetBase.h:47
armarx::control::njoint_controller::core::HandControlBase::HandData::rtPreActivate
void rtPreActivate()
Definition: HandControlBase.cpp:163
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::control::njoint_controller::core
This file is part of ArmarX.
Definition: CollisionAvoidanceCore.cpp:3
armarx::control::njoint_controller::core::HandControlBase::nonRTSetTarget
void nonRTSetTarget()
Definition: HandControlBase.cpp:32
armarx::control::njoint_controller::core::HandControlBase::hands
std::map< std::string, HandPtr > hands
Definition: HandControlBase.h:118
armarx::control::njoint_controller::core::HandControlBase::rtPreActivate
void rtPreActivate()
Definition: HandControlBase.cpp:108
armarx::control::njoint_controller::core::HandControlBase::HandControlBase
HandControlBase(const VirtualRobot::RobotPtr &rtRobot, const HandConfigDict &cfg)
Definition: HandControlBase.cpp:6
armarx::control::njoint_controller::core::HandControlBase::HandData::jointLimMax
Eigen::VectorXf jointLimMax
Definition: HandControlBase.h:77
armarx::control::njoint_controller::core::HandControlBase::HandData::validate
void validate()
----------------------------------— HandData ------------------------------------------—
Definition: HandControlBase.cpp:124
armarx::control::njoint_controller::core::HandControlBase::rtPostDeactivate
void rtPostDeactivate()
Definition: HandControlBase.cpp:118
armarx::control::njoint_controller::core::HandControlBase::HandData::setTarget
void setTarget()
Definition: HandControlBase.cpp:133
armarx::control::njoint_controller::core::HandControlBase::HandData::HandData
HandData(const VirtualRobot::RobotNodeSetPtr &robotNodeSet, const HandConfig &cfg)
Definition: HandControlBase.cpp:146
armarx::control::njoint_controller::core::HandControlBase::HandData::rts
RTStatus rts
Definition: HandControlBase.h:81
armarx::control::njoint_controller::core::HandControlBase::HandData::nJoints
size_t nJoints
Definition: HandControlBase.h:64
armarx::control::njoint_controller::core::HandControlBase::updateConfig
void updateConfig(const HandConfigDict &cfg)
Definition: HandControlBase.cpp:74
armarx::control::njoint_controller::core::HandControlBase::appendDevice
void appendDevice(const std::string &handNodeSet, const SensorValueBase *sv, ControlTargetBase *ct, const std::string &jointName)
Definition: HandControlBase.cpp:17
armarx::control::njoint_controller::core::HandControlBase::HandData::targetRT
HandConfig targetRT
Definition: HandControlBase.h:79
armarx::navigation::client::validate
void validate(const std::vector< WaypointTarget > &path)
Definition: ice_conversions.h:70
armarx::control::njoint_controller::core::HandControlBase::HandData::jointLimMin
Eigen::VectorXf jointLimMin
Definition: HandControlBase.h:76
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_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::control::njoint_controller::core::HandControlBase::HandData::RTStatus::deltaT
double deltaT
Definition: HandControlBase.h:73
armarx::control::njoint_controller::core::HandControlBase::HandConfigDict
std::map< std::string, HandConfig > HandConfigDict
Definition: HandControlBase.h:57
armarx::control::njoint_controller::core::HandControlBase::updateRTStatus
void updateRTStatus(const double deltaT)
Definition: HandControlBase.cpp:53
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
ARMARX_CHECK_EQUAL
#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...
Definition: ExpressionException.h:130
armarx::control::njoint_controller::core::HandControlBase::getConfig
void getConfig(HandConfigDict &cfgToUpdate)
Definition: HandControlBase.cpp:88
armarx::control::njoint_controller::core::HandControlBase::HandConfig
law::arondto::HandStatus HandConfig
Definition: HandControlBase.h:56
armarx::control::njoint_controller::core::HandControlBase::reinitBuffer
void reinitBuffer(HandConfigDict &cfgToUpdate)
Definition: HandControlBase.cpp:97
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18