59 moveit_msgs::msg::RobotTrajectory & computedJointTrajectory)
61 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] getting current state.. waiting");
69 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] getting joint names");
70 auto currentjointnames =
currentState->getJointModelGroup(groupname)->getActiveJointModelNames();
77 std::vector<double> jointPositions;
78 currentState->copyJointGroupPositions(groupname, jointPositions);
80 std::vector<std::vector<double>> trajectory;
81 std::vector<rclcpp::Duration> trajectoryTimeStamps;
83 trajectory.push_back(jointPositions);
84 trajectoryTimeStamps.push_back(rclcpp::Duration(0s));
87 rclcpp::Time referenceTime(first.header.stamp);
89 std::vector<int> discontinuityIndexes;
95 auto req = std::make_shared<moveit_msgs::srv::GetPositionIK::Request>();
98 req->ik_request.ik_link_name = *
tipLink_;
99 req->ik_request.robot_state.joint_state.name = currentjointnames;
100 req->ik_request.robot_state.joint_state.position = jointPositions;
102 req->ik_request.group_name = groupname;
103 req->ik_request.avoid_collisions =
true;
106 req->ik_request.pose_stamped = pose;
108 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] IK request: " << k <<
" " << *req);
110 auto resfut =
iksrv_->async_send_request(req);
112 auto status = resfut.wait_for(3s);
113 if (status == std::future_status::ready)
117 auto & prevtrajpoint = trajectory.back();
120 auto res = resfut.get();
121 std::stringstream ss;
122 for (
size_t j = 0; j < res->solution.joint_state.position.size(); j++)
124 auto & jointname = res->solution.joint_state.name[j];
125 auto it = std::find(currentjointnames.begin(), currentjointnames.end(), jointname);
126 if (it != currentjointnames.end())
128 int index = std::distance(currentjointnames.begin(), it);
129 jointPositions[index] = res->solution.joint_state.position[j];
130 ss << jointname <<
"(" << index <<
"): " << jointPositions[index] << std::endl;
135 size_t jointindex = 0;
136 int discontinuityJointIndex = -1;
137 double discontinuityDeltaJointIndex = -1;
142 !(*allowInitialTrajectoryStateJointDiscontinuity_));
145 for (jointindex = 0; jointindex < jointPositions.size(); jointindex++)
147 deltajoint = jointPositions[jointindex] - prevtrajpoint[jointindex];
149 if (fabs(deltajoint) > 0.3 )
151 discontinuityDeltaJointIndex = deltajoint;
152 discontinuityJointIndex = jointindex;
157 if (ikAttempts > 0 && discontinuityJointIndex != -1)
165 bool discontinuity =
false;
168 discontinuityIndexes.push_back(k);
169 discontinuity =
true;
174 if (discontinuity && discontinuityJointIndex != -1)
177 std::stringstream ss;
179 << currentjointnames[discontinuityJointIndex]
180 <<
" IK discontinuity : " << discontinuityDeltaJointIndex << std::endl
181 <<
"prev joint value: " << prevtrajpoint[discontinuityJointIndex] << std::endl
182 <<
"current joint value: " << jointPositions[discontinuityJointIndex] << std::endl;
185 for (
size_t ji = 0; ji < jointPositions.size(); ji++)
187 ss << currentjointnames[ji] <<
": " << jointPositions[ji] << std::endl;
190 for (
size_t kindex = 0; kindex < trajectory.size(); kindex++)
192 ss <<
"[" << kindex <<
"]: " << trajectory[kindex][discontinuityJointIndex]
198 ss <<
"This is the first posture of the trajectory. Maybe the robot initial posture is "
199 "not coincident to the initial posture of the generated joint trajectory."
203 RCLCPP_ERROR_STREAM(
getLogger(), ss.str());
205 trajectory.push_back(jointPositions);
206 rclcpp::Duration durationFromStart = rclcpp::Time(pose.header.stamp) - referenceTime;
207 trajectoryTimeStamps.push_back(durationFromStart);
213 trajectory.push_back(jointPositions);
214 rclcpp::Duration durationFromStart = rclcpp::Time(pose.header.stamp) - referenceTime;
215 trajectoryTimeStamps.push_back(durationFromStart);
217 RCLCPP_DEBUG_STREAM(
getLogger(),
"IK solution: " << res->solution.joint_state);
218 RCLCPP_DEBUG_STREAM(
getLogger(),
"trajpoint: " << std::endl << ss.str());
238 computedJointTrajectory.joint_trajectory.joint_names = currentjointnames;
240 for (
auto & p : trajectory)
250 trajectory_msgs::msg::JointTrajectoryPoint jp;
252 jp.time_from_start = trajectoryTimeStamps[i];
253 computedJointTrajectory.joint_trajectory.points.push_back(jp);
257 if (discontinuityIndexes.size())
259 if (discontinuityIndexes[0] == 0)
387 tf2::Transform localdirection;
388 localdirection.setIdentity();
389 localdirection.setOrigin(tf2::Vector3(0.05, 0, 0));
394 visualization_msgs::msg::Marker marker;
395 marker.header.frame_id = frameid;
396 marker.header.stamp =
getNode()->now();
397 marker.ns =
"trajectory";
399 marker.type = visualization_msgs::msg::Marker::ARROW;
400 marker.action = visualization_msgs::msg::Marker::ADD;
401 marker.scale.x = 0.005;
402 marker.scale.y = 0.01;
403 marker.scale.z = 0.01;
404 marker.color.a = 0.8;
405 marker.color.r = 1.0;
409 geometry_msgs::msg::Point start, end;
414 tf2::Transform basetransform;
415 tf2::fromMsg(pose.pose, basetransform);
418 end.x = localdirection.getOrigin().x();
419 end.y = localdirection.getOrigin().y();
420 end.z = localdirection.getOrigin().z();
422 marker.pose.position = pose.pose.position;
423 marker.pose.orientation = pose.pose.orientation;
424 marker.points.push_back(start);
425 marker.points.push_back(end);
void pushTrajectory(std::string name, const moveit_msgs::msg::RobotTrajectory &trajectory, moveit_msgs::msg::MoveItErrorCodes result)