Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve robotic_manipulation_node stability #8

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ArmController {

void SetReferenceFrame(std::string const &frame);

void WaitForClockMessage();

private:
std::shared_ptr<moveit::planning_interface::MoveGroupInterface> m_pandaArm;
std::shared_ptr<moveit::planning_interface::MoveGroupInterface> m_hand;
Expand Down
49 changes: 36 additions & 13 deletions ros2_ws/src/robotic_manipulation/src/arm_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "robotic_manipulation/arm_controller.h"

#include <rosgraph_msgs/msg/clock.hpp>

ArmController::ArmController() {
m_node = rclcpp::Node::make_shared("arm_controller");
m_node->set_parameter(rclcpp::Parameter("use_sim_time", true));

WaitForClockMessage();

m_executor.add_node(m_node);
m_spinner = std::thread([this]() { m_executor.spin(); });

Expand Down Expand Up @@ -41,21 +46,25 @@ geometry_msgs::msg::Pose ArmController::CalculatePose(double x, double y,

bool ArmController::MoveThroughWaypoints(const std::vector<geometry_msgs::msg::Pose>& waypoints) {
auto logger = m_node->get_logger();
moveit_msgs::msg::RobotTrajectory trajectory;
if (m_pandaArm->computeCartesianPath(waypoints, 0.01, 0.0, trajectory) ==
-1) {
RCLCPP_ERROR(logger,
"MoveThroughWaypoints: Failed to compute Cartesian path");
return false;
}

while (m_pandaArm->execute(trajectory) !=
moveit::core::MoveItErrorCode::SUCCESS) {
RCLCPP_ERROR(logger, "MoveThroughWaypoints: Failed to execute trajectory");
return false;
const int NumTries = 10;
for (int i = 0; i < NumTries; i++) {
moveit_msgs::msg::RobotTrajectory trajectory;
if (m_pandaArm->computeCartesianPath(waypoints, 0.01, 0.0, trajectory) ==
-1) {
RCLCPP_ERROR(logger,
"MoveThroughWaypoints: Failed to compute Cartesian path");
continue;
}

if (m_pandaArm->execute(trajectory) == moveit::core::MoveItErrorCode::SUCCESS) {
return true;
}
RCLCPP_ERROR(logger, "MoveThroughWaypoints: Failed to execute trajectory, trying again...");
}

return true;

RCLCPP_ERROR(logger, "MoveThroughWaypoints: Failed to execute trajectory after %d tries", NumTries);
return false;
}

void ArmController::Open() {
Expand Down Expand Up @@ -102,4 +111,18 @@ void ArmController::SetJointValues(std::vector<double> const &jointValues) {

void ArmController::SetReferenceFrame(std::string const &frame) {
m_pandaArm->setPoseReferenceFrame(frame);
}

void ArmController::WaitForClockMessage() {
bool clock_received = false;
auto qos = rclcpp::QoS(rclcpp::KeepLast(1));
qos.best_effort();
auto subscription = m_node->create_subscription<rosgraph_msgs::msg::Clock>(
"/clock", qos, [&] (rosgraph_msgs::msg::Clock::SharedPtr) {
clock_received = true;
});
while (!clock_received) {
rclcpp::spin_some(m_node);
}
subscription.reset();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ int main(int argc, char *argv[]) {
startingPose = armController->CaptureJointValues();

armController->SetJointValues(startingPose);
armController->Close();

StateController state;
state.Begin(*armController);
Expand Down