Skip to content

Commit 857cf40

Browse files
Update StateSolver to leverage KinematicLimits (#494)
1 parent a4e3541 commit 857cf40

File tree

6 files changed

+85
-56
lines changed

6 files changed

+85
-56
lines changed

tesseract/tesseract_environment/include/tesseract_environment/core/commands.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class ChangeJointPositionLimitsCommand : public Command
368368
public:
369369
ChangeJointPositionLimitsCommand(std::string joint_name, double lower, double upper)
370370
: Command(CommandType::CHANGE_JOINT_POSITION_LIMITS)
371-
, limits_({ std::make_pair(joint_name, std::make_pair(lower, upper)) })
371+
, limits_({ std::make_pair(std::move(joint_name), std::make_pair(lower, upper)) })
372372
{
373373
assert(upper > lower);
374374
}
@@ -389,7 +389,7 @@ class ChangeJointVelocityLimitsCommand : public Command
389389
{
390390
public:
391391
ChangeJointVelocityLimitsCommand(std::string joint_name, double limit)
392-
: Command(CommandType::CHANGE_JOINT_VELOCITY_LIMITS), limits_({ std::make_pair(joint_name, limit) })
392+
: Command(CommandType::CHANGE_JOINT_VELOCITY_LIMITS), limits_({ std::make_pair(std::move(joint_name), limit) })
393393
{
394394
assert(limit > 0);
395395
}
@@ -410,7 +410,7 @@ class ChangeJointAccelerationLimitsCommand : public Command
410410
{
411411
public:
412412
ChangeJointAccelerationLimitsCommand(std::string joint_name, double limit)
413-
: Command(CommandType::CHANGE_JOINT_ACCELERATION_LIMITS), limits_({ std::make_pair(joint_name, limit) })
413+
: Command(CommandType::CHANGE_JOINT_ACCELERATION_LIMITS), limits_({ std::make_pair(std::move(joint_name), limit) })
414414
{
415415
assert(limit > 0);
416416
}

tesseract/tesseract_environment/include/tesseract_environment/core/state_solver.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
3939
#include <tesseract_scene_graph/graph.h>
4040
#include <tesseract_environment/core/types.h>
4141
#include <tesseract_environment/core/commands.h>
42+
#include <tesseract_common/types.h>
4243

4344
#ifdef SWIG
4445
%shared_ptr(tesseract_environment::StateSolver)
@@ -100,10 +101,10 @@ class StateSolver
100101
virtual EnvState::Ptr getRandomState() const = 0;
101102

102103
/**
103-
* @brief Getter for joint_limits_
104-
* @return Matrix of joint limits
104+
* @brief Getter for kinematic limits
105+
* @return The kinematic limits
105106
*/
106-
virtual const Eigen::MatrixX2d& getLimits() const = 0;
107+
virtual const tesseract_common::KinematicLimits& getLimits() const = 0;
107108

108109
/**
109110
* @brief This should clone the object so it may be used in a multi threaded application where each thread would

tesseract/tesseract_environment/include/tesseract_environment/kdl/kdl_state_solver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ class KDLStateSolver : public StateSolver
5858

5959
EnvState::Ptr getRandomState() const override;
6060

61-
const Eigen::MatrixX2d& getLimits() const override;
61+
const tesseract_common::KinematicLimits& getLimits() const override;
6262

6363
private:
6464
tesseract_scene_graph::SceneGraph::ConstPtr scene_graph_; /**< Tesseract Scene Graph */
6565
EnvState::Ptr current_state_; /**< Current state of the environment */
6666
KDL::Tree kdl_tree_; /**< KDL tree object */
6767
std::unordered_map<std::string, unsigned int> joint_to_qnr_; /**< Map between joint name and kdl q index */
6868
KDL::JntArray kdl_jnt_array_; /**< The kdl joint array */
69-
Eigen::MatrixX2d joint_limits_; /**< The joint limits */
69+
tesseract_common::KinematicLimits limits_; /**< The kinematic limits */
7070
std::vector<std::string> joint_names_; /**< The active joint names */
7171

7272
/**

tesseract/tesseract_environment/include/tesseract_environment/ofkt/ofkt_state_solver.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ class OFKTStateSolver : public StateSolver
9999

100100
EnvState::Ptr getRandomState() const override;
101101

102-
const Eigen::MatrixX2d& getLimits() const override;
102+
const tesseract_common::KinematicLimits& getLimits() const override;
103103

104104
private:
105105
EnvState::Ptr current_state_{ std::make_shared<EnvState>() }; /**< Current state of the environment */
106106
std::vector<std::string> joint_names_; /**< The active joint names */
107107
std::unordered_map<std::string, OFKTNode::UPtr> nodes_; /**< The joint name map to node */
108108
std::unordered_map<std::string, OFKTNode*> link_map_; /**< The link name map to node */
109-
Eigen::MatrixX2d limits_; /**< The joint limits corresponding to joint_names_ */
109+
tesseract_common::KinematicLimits limits_; /**< The kinematic limits */
110110
OFKTNode::UPtr root_; /**< The root node of the tree */
111111
int revision_{ 0 }; /**< The environment revision number */
112112

@@ -144,13 +144,13 @@ class OFKTStateSolver : public StateSolver
144144
* @param joint_name The joints name
145145
* @param parent_link_name The joints parent link name
146146
* @param child_link_name The joints child link name
147-
* @param limits The limits container for new joints.
147+
* @param kinematic_joints The vector to store new kinematic joints added to the solver
148148
*/
149149
void addNode(const tesseract_scene_graph::Joint::ConstPtr& joint,
150150
const std::string& joint_name,
151151
const std::string& parent_link_name,
152152
const std::string& child_link_name,
153-
std::vector<std::pair<std::string, std::array<double, 2>>>& limits);
153+
std::vector<tesseract_scene_graph::Joint::ConstPtr>& kinematic_joints);
154154

155155
/**
156156
* @brief Remove a node and all of its children

tesseract/tesseract_environment/src/kdl/kdl_state_solver.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool KDLStateSolver::init(const KDLStateSolver& solver)
5656
kdl_tree_ = solver.kdl_tree_;
5757
joint_to_qnr_ = solver.joint_to_qnr_;
5858
kdl_jnt_array_ = solver.kdl_jnt_array_;
59-
joint_limits_ = solver.joint_limits_;
59+
limits_ = solver.limits_;
6060
joint_names_ = solver.joint_names_;
6161

6262
return true;
@@ -164,10 +164,10 @@ EnvState::ConstPtr KDLStateSolver::getCurrentState() const { return current_stat
164164

165165
EnvState::Ptr KDLStateSolver::getRandomState() const
166166
{
167-
return getState(joint_names_, tesseract_common::generateRandomNumber(joint_limits_));
167+
return getState(joint_names_, tesseract_common::generateRandomNumber(limits_.joint_limits));
168168
}
169169

170-
const Eigen::MatrixX2d& KDLStateSolver::getLimits() const { return joint_limits_; }
170+
const tesseract_common::KinematicLimits& KDLStateSolver::getLimits() const { return limits_; }
171171

172172
bool KDLStateSolver::createKDETree()
173173
{
@@ -180,7 +180,9 @@ bool KDLStateSolver::createKDETree()
180180

181181
current_state_ = std::make_shared<EnvState>();
182182
kdl_jnt_array_.resize(kdl_tree_.getNrOfJoints());
183-
joint_limits_.resize(kdl_tree_.getNrOfJoints(), 2);
183+
limits_.joint_limits.resize(static_cast<long int>(kdl_tree_.getNrOfJoints()), 2);
184+
limits_.velocity_limits.resize(static_cast<long int>(kdl_tree_.getNrOfJoints()));
185+
limits_.acceleration_limits.resize(static_cast<long int>(kdl_tree_.getNrOfJoints()));
184186
joint_names_.resize(kdl_tree_.getNrOfJoints());
185187
size_t j = 0;
186188
for (const auto& seg : kdl_tree_.getSegments())
@@ -197,8 +199,10 @@ bool KDLStateSolver::createKDETree()
197199

198200
// Store joint limits.
199201
const auto& sj = scene_graph_->getJoint(jnt.getName());
200-
joint_limits_(static_cast<long>(j), 0) = sj->limits->lower;
201-
joint_limits_(static_cast<long>(j), 0) = sj->limits->upper;
202+
limits_.joint_limits(static_cast<long>(j), 0) = sj->limits->lower;
203+
limits_.joint_limits(static_cast<long>(j), 1) = sj->limits->upper;
204+
limits_.velocity_limits(static_cast<long>(j)) = sj->limits->velocity;
205+
limits_.acceleration_limits(static_cast<long>(j)) = sj->limits->acceleration;
202206

203207
j++;
204208
}

tesseract/tesseract_environment/src/ofkt/ofkt_state_solver.cpp

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ namespace tesseract_environment
3838
struct ofkt_builder : public boost::dfs_visitor<>
3939
{
4040
ofkt_builder(OFKTStateSolver& tree,
41-
std::vector<std::pair<std::string, std::array<double, 2>>>& limits,
41+
std::vector<tesseract_scene_graph::Joint::ConstPtr>& kinematic_joints,
4242
std::string prefix = "")
43-
: tree_(tree), limits_(limits), prefix_(prefix)
43+
: tree_(tree), kinematic_joints_(kinematic_joints), prefix_(prefix)
4444
{
4545
}
4646

@@ -60,12 +60,12 @@ struct ofkt_builder : public boost::dfs_visitor<>
6060
std::string parent_link_name = prefix_ + joint->parent_link_name;
6161
std::string child_link_name = prefix_ + joint->child_link_name;
6262

63-
tree_.addNode(joint, joint_name, parent_link_name, child_link_name, limits_);
63+
tree_.addNode(joint, joint_name, parent_link_name, child_link_name, kinematic_joints_);
6464
}
6565

6666
protected:
6767
OFKTStateSolver& tree_;
68-
std::vector<std::pair<std::string, std::array<double, 2>>>& limits_;
68+
std::vector<tesseract_scene_graph::Joint::ConstPtr>& kinematic_joints_;
6969
std::string prefix_;
7070
};
7171

@@ -156,9 +156,9 @@ bool OFKTStateSolver::init(tesseract_scene_graph::SceneGraph::ConstPtr scene_gra
156156
link_map_[root_name] = root_.get();
157157
current_state_->link_transforms[root_name] = root_->getWorldTransformation();
158158

159-
std::vector<std::pair<std::string, std::array<double, 2>>> limits;
160-
limits.reserve(scene_graph->getJoints().size());
161-
ofkt_builder builder(*this, limits);
159+
std::vector<tesseract_scene_graph::Joint::ConstPtr> kinematic_joints;
160+
kinematic_joints.reserve(scene_graph->getJoints().size());
161+
ofkt_builder builder(*this, kinematic_joints);
162162

163163
std::map<tesseract_scene_graph::SceneGraph::Vertex, size_t> index_map;
164164
boost::associative_property_map<std::map<tesseract_scene_graph::SceneGraph::Vertex, size_t>> prop_index_map(
@@ -174,11 +174,15 @@ bool OFKTStateSolver::init(tesseract_scene_graph::SceneGraph::ConstPtr scene_gra
174174
boost::visitor(builder).root_vertex(scene_graph->getVertex(root_name)).vertex_index_map(prop_index_map));
175175

176176
// Populate Joint Limits
177-
limits_.resize(static_cast<long>(limits.size()), 2);
178-
for (std::size_t i = 0; i < limits.size(); ++i)
177+
limits_.joint_limits.resize(static_cast<long int>(kinematic_joints.size()), 2);
178+
limits_.velocity_limits.resize(static_cast<long int>(kinematic_joints.size()));
179+
limits_.acceleration_limits.resize(static_cast<long int>(kinematic_joints.size()));
180+
for (std::size_t i = 0; i < kinematic_joints.size(); ++i)
179181
{
180-
limits_(static_cast<long>(i), 0) = limits[i].second[0];
181-
limits_(static_cast<long>(i), 1) = limits[i].second[1];
182+
limits_.joint_limits(static_cast<long>(i), 0) = kinematic_joints[i]->limits->lower;
183+
limits_.joint_limits(static_cast<long>(i), 1) = kinematic_joints[i]->limits->upper;
184+
limits_.velocity_limits(static_cast<long>(i)) = kinematic_joints[i]->limits->velocity;
185+
limits_.acceleration_limits(static_cast<long>(i)) = kinematic_joints[i]->limits->acceleration;
182186
}
183187

184188
revision_ = 1;
@@ -263,15 +267,15 @@ EnvState::ConstPtr OFKTStateSolver::getCurrentState() const { return current_sta
263267

264268
EnvState::Ptr OFKTStateSolver::getRandomState() const
265269
{
266-
return getState(joint_names_, tesseract_common::generateRandomNumber(limits_));
270+
return getState(joint_names_, tesseract_common::generateRandomNumber(limits_.joint_limits));
267271
}
268272

269-
const Eigen::MatrixX2d& OFKTStateSolver::getLimits() const { return limits_; }
273+
const tesseract_common::KinematicLimits& OFKTStateSolver::getLimits() const { return limits_; }
270274

271275
void OFKTStateSolver::onEnvironmentChanged(const Commands& commands)
272276
{
273-
std::vector<std::pair<std::string, std::array<double, 2>>> new_limits;
274-
new_limits.reserve(commands.size());
277+
std::vector<tesseract_scene_graph::Joint::ConstPtr> new_kinematic_joints;
278+
new_kinematic_joints.reserve(commands.size());
275279

276280
std::vector<std::string> removed_joints;
277281
removed_joints.reserve(commands.size());
@@ -292,7 +296,7 @@ void OFKTStateSolver::onEnvironmentChanged(const Commands& commands)
292296
const auto& cmd = static_cast<const tesseract_environment::AddCommand&>(*command);
293297
const tesseract_scene_graph::Joint::ConstPtr& joint = cmd.getJoint();
294298

295-
addNode(joint, joint->getName(), joint->parent_link_name, joint->child_link_name, new_limits);
299+
addNode(joint, joint->getName(), joint->parent_link_name, joint->child_link_name, new_kinematic_joints);
296300
break;
297301
}
298302
case tesseract_environment::CommandType::MOVE_LINK:
@@ -317,7 +321,7 @@ void OFKTStateSolver::onEnvironmentChanged(const Commands& commands)
317321

318322
nodes_.erase(old_joint_name);
319323

320-
addNode(joint, joint->getName(), joint->parent_link_name, joint->child_link_name, new_limits);
324+
addNode(joint, joint->getName(), joint->parent_link_name, joint->child_link_name, new_kinematic_joints);
321325
break;
322326
}
323327
case tesseract_environment::CommandType::MOVE_JOINT:
@@ -369,9 +373,9 @@ void OFKTStateSolver::onEnvironmentChanged(const Commands& commands)
369373
const auto& cmd = static_cast<const tesseract_environment::AddSceneGraphCommand&>(*command);
370374
const tesseract_scene_graph::Joint::ConstPtr& joint = cmd.getJoint();
371375

372-
addNode(joint, joint->getName(), joint->parent_link_name, joint->child_link_name, new_limits);
376+
addNode(joint, joint->getName(), joint->parent_link_name, joint->child_link_name, new_kinematic_joints);
373377

374-
ofkt_builder builder(*this, new_limits, cmd.getPrefix());
378+
ofkt_builder builder(*this, new_kinematic_joints, cmd.getPrefix());
375379

376380
std::map<tesseract_scene_graph::SceneGraph::Vertex, size_t> index_map;
377381
boost::associative_property_map<std::map<tesseract_scene_graph::SceneGraph::Vertex, size_t>> prop_index_map(
@@ -400,8 +404,8 @@ void OFKTStateSolver::onEnvironmentChanged(const Commands& commands)
400404
// Assign the lower/upper. Velocity, acceleration, and effort are ignored
401405
if (it != limits.end())
402406
{
403-
limits_(static_cast<Eigen::Index>(i), 0) = it->second.first;
404-
limits_(static_cast<Eigen::Index>(i), 1) = it->second.second;
407+
limits_.joint_limits(static_cast<Eigen::Index>(i), 0) = it->second.first;
408+
limits_.joint_limits(static_cast<Eigen::Index>(i), 1) = it->second.second;
405409
}
406410
}
407411

@@ -429,26 +433,49 @@ void OFKTStateSolver::onEnvironmentChanged(const Commands& commands)
429433
std::remove_if(joint_names_.begin(), joint_names_.end(), [removed_joints](const std::string& joint_name) {
430434
return (std::find(removed_joints.begin(), removed_joints.end(), joint_name) != removed_joints.end());
431435
}));
432-
Eigen::MatrixX2d l1(limits_.rows() - static_cast<long>(removed_joints.size()), 2);
436+
437+
tesseract_common::KinematicLimits l1;
438+
l1.joint_limits.resize(static_cast<long int>(joint_names_.size()), 2);
439+
l1.velocity_limits.resize(static_cast<long int>(joint_names_.size()));
440+
l1.acceleration_limits.resize(static_cast<long int>(joint_names_.size()));
441+
433442
long cnt = 0;
434-
for (long i = 0; i < limits_.rows(); ++i)
443+
for (long i = 0; i < limits_.joint_limits.rows(); ++i)
444+
{
435445
if (std::find(removed_joints_indices.begin(), removed_joints_indices.end(), i) == removed_joints_indices.end())
436-
l1.row(cnt++) = limits_.row(i);
446+
{
447+
l1.joint_limits.row(cnt) = limits_.joint_limits.row(i);
448+
l1.velocity_limits(cnt) = limits_.velocity_limits(i);
449+
l1.acceleration_limits(cnt) = limits_.acceleration_limits(i);
450+
++cnt;
451+
}
452+
}
437453

438454
limits_ = l1;
439455
}
440456

441457
// Populate Joint Limits
442-
if (new_limits.empty() == false)
458+
if (new_kinematic_joints.empty() == false)
443459
{
444-
Eigen::MatrixX2d l(limits_.rows() + static_cast<long>(new_limits.size()), 2);
445-
Eigen::MatrixX2d l2(static_cast<long>(new_limits.size()), 2);
446-
for (std::size_t i = 0; i < new_limits.size(); ++i)
460+
tesseract_common::KinematicLimits l;
461+
long s = limits_.joint_limits.rows() + static_cast<long>(new_kinematic_joints.size());
462+
l.joint_limits.resize(s, 2);
463+
l.velocity_limits.resize(s);
464+
l.acceleration_limits.resize(s);
465+
466+
limits_.joint_limits.block(0, 0, limits_.joint_limits.rows(), 2) = limits_.joint_limits;
467+
limits_.velocity_limits.head(limits_.joint_limits.rows()) = limits_.velocity_limits;
468+
limits_.acceleration_limits.head(limits_.joint_limits.rows()) = limits_.acceleration_limits;
469+
470+
long cnt = limits_.joint_limits.size();
471+
for (std::size_t i = 0; i < new_kinematic_joints.size(); ++i)
447472
{
448-
l2(static_cast<long>(i), 0) = new_limits[i].second[0];
449-
l2(static_cast<long>(i), 1) = new_limits[i].second[1];
473+
limits_.joint_limits(cnt, 0) = new_kinematic_joints[i]->limits->lower;
474+
limits_.joint_limits(cnt, 1) = new_kinematic_joints[i]->limits->upper;
475+
limits_.velocity_limits(cnt) = new_kinematic_joints[i]->limits->velocity;
476+
limits_.acceleration_limits(cnt) = new_kinematic_joints[i]->limits->acceleration;
477+
++cnt;
450478
}
451-
l << limits_, l2;
452479
limits_ = l;
453480
}
454481

@@ -514,7 +541,7 @@ void OFKTStateSolver::addNode(const tesseract_scene_graph::Joint::ConstPtr& join
514541
const std::string& joint_name,
515542
const std::string& parent_link_name,
516543
const std::string& child_link_name,
517-
std::vector<std::pair<std::string, std::array<double, 2>>>& limits)
544+
std::vector<tesseract_scene_graph::Joint::ConstPtr>& kinematic_joints)
518545
{
519546
OFKTNode::UPtr n;
520547
switch (joint->type)
@@ -544,8 +571,7 @@ void OFKTStateSolver::addNode(const tesseract_scene_graph::Joint::ConstPtr& join
544571
current_state_->joint_transforms[n->getJointName()] = n->getWorldTransformation();
545572
nodes_[joint_name] = std::move(n);
546573
joint_names_.push_back(joint_name);
547-
limits.push_back(
548-
std::make_pair(joint_name, std::array<double, 2>({ joint->limits->lower, joint->limits->upper })));
574+
kinematic_joints.push_back(joint);
549575
break;
550576
}
551577
case tesseract_scene_graph::JointType::CONTINUOUS:
@@ -560,8 +586,7 @@ void OFKTStateSolver::addNode(const tesseract_scene_graph::Joint::ConstPtr& join
560586
current_state_->joint_transforms[n->getJointName()] = n->getWorldTransformation();
561587
nodes_[joint_name] = std::move(n);
562588
joint_names_.push_back(joint_name);
563-
limits.push_back(
564-
std::make_pair(joint_name, std::array<double, 2>({ joint->limits->lower, joint->limits->upper })));
589+
kinematic_joints.push_back(joint);
565590
break;
566591
}
567592
case tesseract_scene_graph::JointType::PRISMATIC:
@@ -576,8 +601,7 @@ void OFKTStateSolver::addNode(const tesseract_scene_graph::Joint::ConstPtr& join
576601
current_state_->joint_transforms[n->getJointName()] = n->getWorldTransformation();
577602
nodes_[joint_name] = std::move(n);
578603
joint_names_.push_back(joint_name);
579-
limits.push_back(
580-
std::make_pair(joint_name, std::array<double, 2>({ joint->limits->lower, joint->limits->upper })));
604+
kinematic_joints.push_back(joint);
581605
break;
582606
}
583607
default:

0 commit comments

Comments
 (0)