Skip to content

Commit 8b5004f

Browse files
Move parts of ChainPublisher's implementation from orogen component to lib (#6)
* test moving functionality to library * modify .orogen file to point to library for robot_frameTypes * simplify component code for updated robot_frames library * bug fix fixed a pointer and deleting the pointer at cleanup modify variable name fix loop iteration * clear ports and vector for every configuration --------- Authored-by: Ravisankar Selvaraju <[email protected]>
1 parent eb4e65a commit 8b5004f

File tree

4 files changed

+26
-199
lines changed

4 files changed

+26
-199
lines changed

robot_frames.orogen

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name "robot_frames"
22
using_library "robot_frames"
33

4-
import_types_from "robot_framesTypes.hpp"
4+
import_types_from "robot_frames/robot_framesTypes.hpp"
55

66
import_types_from "base"
77
#import_types_from "transformer"

robot_framesTypes.hpp

-21
This file was deleted.

tasks/ChainPublisher.cpp

+19-158
Original file line numberDiff line numberDiff line change
@@ -18,60 +18,6 @@ ChainPublisher::~ChainPublisher()
1818
{
1919
}
2020

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-
}
7521

7622
/// The following lines are template definitions for the various state machine
7723
// hooks defined by Orocos::RTT. See ChainPublisher.hpp for more detailed
@@ -81,99 +27,22 @@ bool ChainPublisher::configureHook()
8127
{
8228
if (! ChainPublisherBase::configureHook())
8329
return false;
84-
30+
// clear the output_ports_ vector before configuration
31+
output_ports_.clear();
32+
8533
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-
9134
std::string urdf_file_path = _urdf_file.get();
9235

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);
16838

16939
//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()){
17241
LOG_INFO("Adding port %s to component", port_name.c_str());
17342
RTT::OutputPort<base::samples::RigidBodyState>* output_port =
17443
new RTT::OutputPort<base::samples::RigidBodyState>(port_name);
17544
ports()->addPort(port_name, *output_port);
176-
out_ports_[i] = output_port;
45+
output_ports_.push_back(output_port);
17746
}
17847

17948
return true;
@@ -188,28 +57,14 @@ void ChainPublisher::updateHook()
18857
{
18958
ChainPublisherBase::updateHook();
19059

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]);
20566
}
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]);
21167
}
212-
}
21368
}
21469
void ChainPublisher::errorHook()
21570
{
@@ -222,4 +77,10 @@ void ChainPublisher::stopHook()
22277
void ChainPublisher::cleanupHook()
22378
{
22479
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;
22586
}

tasks/ChainPublisher.hpp

+6-19
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
#define ROBOT_FRAMES_CHAINPUBLISHER_TASK_HPP
55

66
#include "robot_frames/ChainPublisherBase.hpp"
7-
#include "robot_framesTypes.hpp"
87
#include <kdl_parser/kdl_parser.hpp>
98
#include <kdl/chainfksolvervel_recursive.hpp>
109
#include <kdl/chainfksolverpos_recursive.hpp>
1110
#include <kdl/jntarray.hpp>
1211
#include <base/samples/RigidBodyState.hpp>
13-
#include <base/Logging.hpp>
12+
#include <base-logging/Logging.hpp>
1413

1514
#include <robot_frames/RobotFrames.hpp>
1615

@@ -21,26 +20,14 @@ class ChainPublisher : public ChainPublisherBase
2120
{
2221
friend class ChainPublisherBase;
2322
protected:
24-
std::vector<KDL::Chain> chains_;
25-
std::vector<std::string> chain_names_;
26-
std::vector<KDL::ChainFkSolverPos_recursive*> pos_solvers_;
27-
std::vector<KDL::Frame> kdl_frames_;
28-
std::vector<base::samples::RigidBodyState> bt_frames_;
29-
std::vector<KDL::JntArray> joint_arrays_;
30-
std::vector<std::vector<std::string> >involved_active_joints_;
31-
base::samples::Joints joint_state_;
32-
KDL::Tree tree_;
33-
int n_defined_chains_;
34-
std::vector<RTT::OutputPort<base::samples::RigidBodyState>*> out_ports_;
35-
36-
void clear_and_resize_vectors();
37-
bool unpack_joints(const base::samples::Joints& joint_state,
38-
const std::vector<std::string>& involved_joints,
39-
KDL::JntArray& joint_array);
23+
base::samples::Joints joint_state;
24+
std::vector<base::samples::RigidBodyState> bt_frames_;
25+
std::vector<RTT::OutputPort<base::samples::RigidBodyState>*> output_ports_;
26+
27+
ChainTransformationCalculator* chain_transformer;
4028

4129
public:
4230
ChainPublisher(std::string const& name = "robot_frames::ChainPublisher");
43-
4431
ChainPublisher(std::string const& name, RTT::ExecutionEngine* engine);
4532

4633
~ChainPublisher();

0 commit comments

Comments
 (0)