@@ -18,60 +18,6 @@ ChainPublisher::~ChainPublisher()
18
18
{
19
19
}
20
20
21
- void ChainPublisher::clear_and_resize_vectors ()
22
- {
23
- chains_.clear ();
24
- chains_.resize (n_defined_chains_);
25
-
26
- chain_names_.clear ();
27
- chain_names_.resize (n_defined_chains_);
28
-
29
- for (size_t i=0 ; i<pos_solvers_.size (); i++){
30
- delete pos_solvers_[i];
31
- }
32
- pos_solvers_.clear ();
33
- pos_solvers_.resize (n_defined_chains_);
34
-
35
- kdl_frames_.clear ();
36
- kdl_frames_.resize (n_defined_chains_);
37
-
38
- bt_frames_.clear ();
39
- bt_frames_.resize (n_defined_chains_);
40
-
41
- joint_arrays_.clear ();
42
- joint_arrays_.resize (n_defined_chains_);
43
-
44
- involved_active_joints_.clear ();
45
- involved_active_joints_.resize (n_defined_chains_);
46
-
47
- for (size_t i=0 ; i<out_ports_.size (); i++){
48
- ports ()->removePort (out_ports_[i]->getName ());
49
- delete out_ports_[i];
50
- }
51
- out_ports_.clear ();
52
- out_ports_.resize (n_defined_chains_);
53
- }
54
-
55
- bool ChainPublisher::unpack_joints (const base::samples::Joints& joint_state,
56
- const std::vector<std::string>& involved_joints,
57
- KDL::JntArray& joint_array)
58
- {
59
- assert (involved_joints.size () == joint_array.rows ());
60
-
61
- bool ok = true ;
62
- for (size_t i = 0 ; i<involved_joints.size (); i++){
63
- try {
64
- base::JointState js = joint_state.getElementByName (involved_joints[i]);
65
- joint_array (i) = js.position ;
66
- }
67
- catch (base::samples::Joints::InvalidName ex){
68
- LOG_ERROR (" Could not find joint %s in joint state vector." , involved_joints[i].c_str ());
69
- ok = false ;
70
- }
71
- }
72
-
73
- return ok;
74
- }
75
21
76
22
// / The following lines are template definitions for the various state machine
77
23
// hooks defined by Orocos::RTT. See ChainPublisher.hpp for more detailed
@@ -81,99 +27,22 @@ bool ChainPublisher::configureHook()
81
27
{
82
28
if (! ChainPublisherBase::configureHook ())
83
29
return false ;
84
-
30
+ // clear the output_ports_ vector before configuration
31
+ output_ports_.clear ();
32
+
85
33
std::vector<robot_frames::Chain> chain_definitions = _chains.get ();
86
- n_defined_chains_ = chain_definitions.size ();
87
- if (!n_defined_chains_){
88
- LOG_WARN_S << " No chains are defined! This component will do nothing..." << std::endl;
89
- }
90
-
91
34
std::string urdf_file_path = _urdf_file.get ();
92
35
93
- bool st;
94
- st = kdl_parser::treeFromFile (urdf_file_path, tree_);
95
- if (!st){
96
- LOG_ERROR_S << " Error parsing urdf file: " << urdf_file_path << std::endl;
97
- return false ;
98
- }
99
-
100
- // Resize stuff
101
- clear_and_resize_vectors ();
102
-
103
- // Receive Chains from urdf
104
- for (size_t i=0 ; i<n_defined_chains_; i++){
105
- std::string root = chain_definitions[i].root_link ;
106
-
107
- // By entering '__base__' as root or tip link, one could refer to the actual base-link of the robot model
108
- if (root == " __base__" ){
109
- root = tree_.getRootSegment ()->first ;
110
- }
111
- std::string tip = chain_definitions[i].tip_link ;
112
- if (tip == " __base__" ){
113
- tip = tree_.getRootSegment ()->first ;
114
- }
115
-
116
- std::string name = chain_definitions[i].name ;
117
-
118
- st = tree_.getChain (root, tip, chains_[i]);
119
- if (!st){
120
- LOG_ERROR (" Error extracting chain '%s' with root '%s' and tip '%s'. The urdf_file is: %s" ,
121
- name.c_str (), root.c_str (), tip.c_str (), urdf_file_path.c_str ());
122
- return false ;
123
- }
124
- chain_names_[i] = name;
125
- }
126
-
127
- // Determine invloved joits, prepare frames storage and solvers
128
- for (size_t i=0 ; i<n_defined_chains_; i++){
129
- KDL::Chain chain = chains_[i];
130
- // Determine involved joints for each chain
131
- for (uint s=0 ; s<chain.segments .size (); s++){
132
- KDL::Segment segment = chain.segments [s];
133
- std::string jname = segment.getJoint ().getName ();
134
-
135
- if (segment.getJoint ().getType () == KDL::Joint::None){
136
- LOG_DEBUG (" Skipping joint %s of chain %s. Joint is fixed." ,
137
- jname.c_str (),
138
- chain_names_[i].c_str ());
139
- continue ;
140
- }
141
-
142
- involved_active_joints_[i].push_back (jname);
143
- }
144
- // Resize joint arrays
145
- joint_arrays_[i].resize (involved_active_joints_[i].size ());
146
- joint_arrays_[i].data .setZero ();
147
-
148
- // Prepare frames storage
149
- base::samples::RigidBodyState rbs;
150
- if (chain_definitions[i].tip_link_renamed == " " ){
151
- rbs.sourceFrame = chain_definitions[i].tip_link ;
152
- }
153
- else {
154
- rbs.sourceFrame = chain_definitions[i].tip_link_renamed ;
155
- }
156
- if (chain_definitions[i].root_link_renamed == " " ){
157
- rbs.targetFrame = chain_definitions[i].root_link ;
158
- }
159
- else {
160
- rbs.targetFrame = chain_definitions[i].root_link_renamed ;
161
- }
162
- rbs.invalidate ();
163
- bt_frames_[i] = rbs;
164
-
165
- // Prepare solver
166
- pos_solvers_[i] = new KDL::ChainFkSolverPos_recursive (chains_[i]);
167
- }
36
+ // assign the created object to the chain_transformer pointer to access this object in update_hook
37
+ chain_transformer = new ChainTransformationCalculator (chain_definitions, urdf_file_path);
168
38
169
39
// Create dynamic output per chain
170
- for (int i=0 ; i<n_defined_chains_; i++){
171
- std::string port_name = chain_names_[i];
40
+ for (const std::string& port_name : chain_transformer->get_chain_names ()){
172
41
LOG_INFO (" Adding port %s to component" , port_name.c_str ());
173
42
RTT::OutputPort<base::samples::RigidBodyState>* output_port =
174
43
new RTT::OutputPort<base::samples::RigidBodyState>(port_name);
175
44
ports ()->addPort (port_name, *output_port);
176
- out_ports_[i] = output_port;
45
+ output_ports_. push_back ( output_port) ;
177
46
}
178
47
179
48
return true ;
@@ -188,28 +57,14 @@ void ChainPublisher::updateHook()
188
57
{
189
58
ChainPublisherBase::updateHook ();
190
59
191
- while (_input.read (joint_state_, false ) == RTT::NewData){
192
- int st;
193
- // Go thourgh chains
194
- for (size_t i=0 ; i<n_defined_chains_; i++){
195
- KDL::ChainFkSolverPos_recursive* solver = pos_solvers_[i];
196
-
197
- // Extract joint array for chain
198
- if (!unpack_joints (joint_state_, involved_active_joints_[i], joint_arrays_[i]))
199
- throw std::runtime_error (" Unable to correctly unpack joint values on kinematic chain" );
200
-
201
- // Calculate
202
- st = solver->JntToCart (joint_arrays_[i], kdl_frames_[i]);
203
- if (st < 0 ){
204
- LOG_ERROR_S << " Something went wrong solving forward kineamtics for chain " << chain_names_[i] << std::endl;
60
+ // clear bt_frames before each update
61
+ bt_frames_.clear ();
62
+ while (_input.read (joint_state, false ) == RTT::NewData){
63
+ chain_transformer->update_transforms (joint_state, bt_frames_);
64
+ for (int i=0 ; i< output_ports_.size (); i++){
65
+ output_ports_[i]->write (bt_frames_[i]);
205
66
}
206
-
207
- // Convert and write to port
208
- convert (kdl_frames_[i], bt_frames_[i]);
209
- bt_frames_[i].time = joint_state_.time ;
210
- out_ports_[i]->write (bt_frames_[i]);
211
67
}
212
- }
213
68
}
214
69
void ChainPublisher::errorHook ()
215
70
{
@@ -222,4 +77,10 @@ void ChainPublisher::stopHook()
222
77
void ChainPublisher::cleanupHook ()
223
78
{
224
79
ChainPublisherBase::cleanupHook ();
80
+
81
+ // Remove all dynamically created ports
82
+ for (const std::string& port_name : chain_transformer->get_chain_names ())
83
+ ports ()->removePort (port_name);
84
+
85
+ delete chain_transformer;
225
86
}
0 commit comments