#include #include #include #include "rclcpp/rclcpp.hpp" #include "mg_navigation.hpp" #include "handlers/dwa.hpp" namespace mg { template rclcpp_action::GoalResponse MgNavigationServer::handle_goal(const rclcpp_action::GoalUUID& /*unused*/, typename T::Goal::ConstSharedPtr /*unused*/, const char* N) { RCLCPP_INFO(get_logger(), "Recieved goal for action: [%s]", N); const std::lock_guard lock(mtx); if (is_processing) { return rclcpp_action::GoalResponse::REJECT; } else { is_processing = true; return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE; } } template rclcpp_action::CancelResponse MgNavigationServer::handle_cancel( const std::shared_ptr >, const char* N) { RCLCPP_INFO(this->get_logger(), "Recieved cancel request for action: [%s]", N); return rclcpp_action::CancelResponse::ACCEPT; } template void MgNavigationServer::handle_accepted(const std::shared_ptr > gh, const char* N) { RCLCPP_INFO(this->get_logger(), "Goal accepted for action: [%s]", N); using namespace std::placeholders; auto t = std::bind(&MgNavigationServer::execute, this, _1); std::thread{ t, gh }.detach(); } template void MgNavigationServer::execute(const std::shared_ptr > gh) { DWA dwm = DWA(gh, tf2_buffer, *this); dwm.execute(); is_processing = false; mtx.unlock(); } MgNavigationServer::MgNavigationServer(rclcpp::NodeOptions& opts) : rclcpp::Node("MgNavigationServer", opts) { tf2_buffer = std::make_shared(get_clock()); tf2_listener = std::make_shared(*tf2_buffer); path_buffer_ = std::make_shared(this); pub_twist = create_publisher("diffdrive_controller/cmd_vel", 2); obstacle_manager_ = std::make_shared(this, tf2_buffer); using namespace std::placeholders; sv_move_global = rclcpp_action::create_server( this, "MoveGlobal", std::bind(&MgNavigationServer::handle_goal, this, _1, _2, "MoveGlobal"), std::bind(&MgNavigationServer::handle_cancel, this, _1, "MoveGlobal"), std::bind(&MgNavigationServer::handle_accepted, this, _1, "MoveGlobal")); sv_move_point = rclcpp_action::create_server( this, "MovePoint", std::bind(&MgNavigationServer::handle_goal, this, _1, _2, "MovePoint"), std::bind(&MgNavigationServer::handle_cancel, this, _1, "MovePoint"), std::bind(&MgNavigationServer::handle_accepted, this, _1, "MovePoint")); sv_move_straight = rclcpp_action::create_server( this, "MoveStraight", std::bind(&MgNavigationServer::handle_goal, this, _1, _2, "MoveStraight"), std::bind(&MgNavigationServer::handle_cancel, this, _1, "MoveStraight"), std::bind(&MgNavigationServer::handle_accepted, this, _1, "MoveStraight")); sv_look_at = rclcpp_action::create_server( this, "LookAt", std::bind(&MgNavigationServer::handle_goal, this, _1, _2, "LookAt"), std::bind(&MgNavigationServer::handle_cancel, this, _1, "LookAt"), std::bind(&MgNavigationServer::handle_accepted, this, _1, "LookAt")); sv_rotate = rclcpp_action::create_server( this, "Rotate", std::bind(&MgNavigationServer::handle_goal, this, _1, _2, "Rotate"), std::bind(&MgNavigationServer::handle_cancel, this, _1, "Rotate"), std::bind(&MgNavigationServer::handle_accepted, this, _1, "Rotate")); } }; int main(int argc, char** argv) { rclcpp::init(argc, argv); rclcpp::NodeOptions opts; rclcpp::spin(std::make_shared(opts)); rclcpp::shutdown(); return 0; }