Skip to content

Commit 9a493a5

Browse files
committed
incoming action messages are now remapped based on initialized order
1 parent 80d9d1c commit 9a493a5

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

joint_trajectory_controller/include/joint_trajectory_controller/trajectory.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,34 @@ class Trajectory
118118
bool sampled_already_ = false;
119119
};
120120

121+
/**
122+
* \return The map between \p t1 indices (implicitly encoded in return vector indices) to \t2 indices.
123+
* If \p t1 is <tt>"{C, B}"</tt> and \p t2 is <tt>"{A, B, C, D}"</tt>, the associated mapping vector is
124+
* <tt>"{2, 1}"</tt>.
125+
*/
126+
template <class T>
127+
inline std::vector<unsigned int> mapping(const T& t1, const T& t2)
128+
{
129+
typedef unsigned int SizeType;
130+
131+
// t1 must be a subset of t2
132+
if (t1.size() > t2.size()) {return std::vector<SizeType>();}
133+
134+
std::vector<SizeType> mapping_vector(t1.size()); // Return value
135+
for (typename T::const_iterator t1_it = t1.begin(); t1_it != t1.end(); ++t1_it)
136+
{
137+
typename T::const_iterator t2_it = std::find(t2.begin(), t2.end(), *t1_it);
138+
if (t2.end() == t2_it) {return std::vector<SizeType>();}
139+
else
140+
{
141+
const SizeType t1_dist = std::distance(t1.begin(), t1_it);
142+
const SizeType t2_dist = std::distance(t2.begin(), t2_it);
143+
mapping_vector[t1_dist] = t2_dist;
144+
}
145+
}
146+
return mapping_vector;
147+
}
148+
121149
} // namespace joint_trajectory_controller
122150

123151
#endif // JOINT_TRAJECTORY_CONTROLLER__TRAJECTORY_HPP_

joint_trajectory_controller/src/joint_trajectory_controller.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,17 @@ rclcpp_action::GoalResponse JointTrajectoryController::goal_callback(
611611
"Joints on incoming goal don't match the controller joints.");
612612
return rclcpp_action::GoalResponse::REJECT;
613613
}
614+
615+
std::vector<unsigned int> mapping_vector = mapping(goal->trajectory.joint_names, joint_names_);
616+
if (mapping_vector.empty())
617+
{
618+
RCLCPP_ERROR(
619+
lifecycle_node_->get_logger(),
620+
"Joints on incoming goal don't match the controller joints.");
621+
return rclcpp_action::GoalResponse::REJECT;
622+
}
623+
624+
goal->trajectory.joint_names.size() != joint_names_.size();
614625
}
615626

616627
RCLCPP_ERROR(lifecycle_node_->get_logger(), "Accepted new action goal");
@@ -653,6 +664,38 @@ void JointTrajectoryController::feedback_setup_callback(
653664
rt_active_goal_.reset();
654665
auto traj_msg = std::make_shared<trajectory_msgs::msg::JointTrajectory>(
655666
goal_handle->get_goal()->trajectory);
667+
668+
// rearrange all points in the trajectory message based on mapping
669+
std::vector<unsigned int> mapping_vector = mapping(traj_msg->joint_names, joint_names_);
670+
auto remap = [](const std::vector<double>& to_remap, const std::vector<unsigned int>& mapping)
671+
-> std::vector<double>
672+
{
673+
if (to_remap.size() != mapping.size())
674+
return to_remap;
675+
std::vector<double> output;
676+
output.resize(mapping.size(), 0.0);
677+
for (auto index = 0ul; index<mapping.size(); ++index)
678+
{
679+
unsigned int map_index = mapping[index];
680+
output[map_index] = to_remap[index];
681+
}
682+
return output;
683+
};
684+
for (auto index = 0ul; index < traj_msg->points.size(); ++index)
685+
{
686+
traj_msg->points[index].positions =
687+
remap(traj_msg->points[index].positions, mapping_vector);
688+
689+
traj_msg->points[index].velocities =
690+
remap(traj_msg->points[index].velocities, mapping_vector);
691+
692+
traj_msg->points[index].accelerations =
693+
remap(traj_msg->points[index].accelerations, mapping_vector);
694+
695+
traj_msg->points[index].effort =
696+
remap(traj_msg->points[index].effort, mapping_vector);
697+
}
698+
656699
traj_external_point_ptr_->update(traj_msg);
657700

658701
RealtimeGoalHandlePtr rt_goal = std::make_shared<RealtimeGoalHandle>(goal_handle);

0 commit comments

Comments
 (0)