diff --git a/mep3_behavior/include/mep3_behavior/move_action.hpp b/mep3_behavior/include/mep3_behavior/move_action.hpp index c22cf081..8d9734db 100644 --- a/mep3_behavior/include/mep3_behavior/move_action.hpp +++ b/mep3_behavior/include/mep3_behavior/move_action.hpp @@ -165,6 +165,7 @@ namespace mep3_behavior BT::InputPort("frame_id"), BT::InputPort("ignore_obstacles"), BT::InputPort("mode"), + BT::InputPort("reversing"), BT::OutputPort("error")}; } @@ -172,6 +173,7 @@ namespace mep3_behavior { double yaw_deg; int mode; + int reversing; std::string position; std::string token; @@ -183,6 +185,7 @@ namespace mep3_behavior getInput("angular_velocity", goal.angular_properties.max_velocity); getInput("angular_velocity", goal.angular_properties.max_acceleration); getInput("mode", mode); + getInput("reversing", reversing); std::istringstream iss(position); std::getline(iss, token, ';'); @@ -194,6 +197,7 @@ namespace mep3_behavior std::getline(iss, token, ';'); goal.target.theta = std::stod(token) * M_PI / 180.0; goal.mode = mode; + goal.reversing = reversing; std::cout << "RotateAction: setGoal" << std::endl; std::cout << " x: " << goal.target.x << std::endl; @@ -206,6 +210,7 @@ namespace mep3_behavior std::cout << " anguar_velocity: " << goal.angular_properties.max_velocity << std::endl; std::cout << " anguar_acceleration: " << goal.angular_properties.max_acceleration << std::endl; std::cout << " ignore_obstacles: " << goal.ignore_obstacles << std::endl; + std::cout << " reversing: " << goal.reversing << std::endl; return true; } diff --git a/mep3_behavior/include/mep3_behavior/navigate_to_action.hpp b/mep3_behavior/include/mep3_behavior/navigate_to_action.hpp index 984f15dc..d04ddb94 100644 --- a/mep3_behavior/include/mep3_behavior/navigate_to_action.hpp +++ b/mep3_behavior/include/mep3_behavior/navigate_to_action.hpp @@ -18,6 +18,7 @@ #include #include #include +#include #include "behaviortree_cpp/behavior_tree.h" #include "behaviortree_cpp/bt_factory.h" @@ -25,11 +26,13 @@ #include "mep3_behavior/pose_2d.hpp" #include "mep3_behavior/blackboard.hpp" #include "nav2_msgs/action/navigate_to_pose.hpp" +#include "nav2_msgs/action/follow_path.hpp" +#include "geometry_msgs/msg/pose_stamped.hpp" namespace mep3_behavior { class NavigateToAction - : public BT::RosActionNode + : public BT::RosActionNode { public: NavigateToAction(const std::string &name, @@ -40,36 +43,35 @@ namespace mep3_behavior { if (!getInput("goal", target_pose_)) throw BT::RuntimeError( - "Navigate action requires 'goal' argument" - ); + "Navigate action requires 'goal' argument"); getInput("behavior_tree", behavior_tree_); std::string table = this->config().blackboard->get("table"); BT::Pose2D goal_offset; - if (table.length() > 0 && getInput("goal_" + table, goal_offset)) { + if (table.length() > 0 && getInput("goal_" + table, goal_offset)) + { target_pose_ += goal_offset; } BT::TeamColor color = this->config().blackboard->get("color"); - if (color == BT::TeamColor::GREEN) { + if (color == BT::TeamColor::GREEN) + { target_pose_ = BT::mirrorPose(target_pose_); } - } static BT::PortsList providedPorts() { // Static parameters BT::PortsList port_list = { - BT::InputPort("goal"), - BT::InputPort("behavior_tree") - }; + BT::InputPort("goal"), + BT::InputPort("behavior_tree")}; // Dynamic parameters - for (std::string table : BT::SharedBlackboard::access()->get>("predefined_tables")) { + for (std::string table : BT::SharedBlackboard::access()->get>("predefined_tables")) + { port_list.insert( - BT::InputPort("goal_" + table) - ); + BT::InputPort("goal_" + table)); } return port_list; @@ -77,9 +79,9 @@ namespace mep3_behavior bool setGoal(Goal &goal) override { - std::cout << "Navigating to x=" << target_pose_.x \ - << " y=" << target_pose_.y \ - << " θ=" << target_pose_.theta << "°" << std::endl; + std::cout << "Navigating to x=" << target_pose_.x + << " y=" << target_pose_.y + << " θ=" << target_pose_.theta << "°" << std::endl; goal.pose.header.frame_id = "map"; goal.pose.header.stamp = node_->get_clock()->now(); @@ -98,7 +100,7 @@ namespace mep3_behavior return true; } - BT::NodeStatus onResultReceived(const WrappedResult &/*wr*/) override + BT::NodeStatus onResultReceived(const WrappedResult & /*wr*/) override { return BT::NodeStatus::SUCCESS; } @@ -108,6 +110,87 @@ namespace mep3_behavior std::string behavior_tree_; }; + class FollowPathAction + : public BT::RosActionNode + { + public: + FollowPathAction(const std::string &name, + const BT::NodeConfiguration &conf, + const BT::ActionNodeParams ¶ms, + typename std::shared_ptr action_client) + : BT::RosActionNode(name, conf, params, action_client) + { + if (!getInput("path", target_pose_)) + throw BT::RuntimeError( + "Follow path action requires 'path' argument"); + } + + static BT::PortsList providedPorts() + { + // Static parameters + BT::PortsList port_list = { + BT::InputPort("path")}; + + return port_list; + } + + bool setGoal(Goal &goal) override + { + // std::cout << "Foll to x=" << target_pose_.x + // << " y=" << target_pose_.y + // << " θ=" << target_pose_.theta << "°" << std::endl; + + // // Position + + std::istringstream iss(target_pose_); + std::string entry; + std::vector poses; + goal.path.header.frame_id = "map"; + goal.path.header.stamp = node_->get_clock()->now(); + + while (std::getline(iss, entry, '|')) + { + std::istringstream entryStream(entry); + std::string x, y, angle; + + std::getline(entryStream, x, ';'); + std::getline(entryStream, y, ';'); + std::getline(entryStream, angle, ';'); + double theta = std::stod(angle) * M_PI / 180.0; + auto pose_stamped = geometry_msgs::msg::PoseStamped(); + + pose_stamped.header.frame_id = "map"; + pose_stamped.header.stamp = node_->get_clock()->now(); + + pose_stamped.pose.position.x = std::stod(x); + pose_stamped.pose.position.y = std::stod(y); + pose_stamped.pose.position.z = 0.0; + + pose_stamped.pose.orientation.x = 0.0; + pose_stamped.pose.orientation.y = 0.0; + + // https://math.stackexchange.com/a/1972382 + pose_stamped.pose.orientation.z = std::sin(theta / 2.0); + pose_stamped.pose.orientation.w = std::cos(theta / 2.0); + + poses.push_back(pose_stamped); + + std::cout + << "x: " << x << " y: " << y << " theta: " << theta << std::endl; + } + goal.path.poses = poses; + return true; + } + + BT::NodeStatus onResultReceived(const WrappedResult & /*wr*/) override + { + return BT::NodeStatus::SUCCESS; + } + + private: + std::string target_pose_; + }; + } // namespace mep3_behavior #endif // MEP3_BEHAVIOR_TREE__NAVIGATE_TO_ACTION_HPP_ diff --git a/mep3_behavior/src/mep3_behavior_tree.cpp b/mep3_behavior/src/mep3_behavior_tree.cpp index 8db74432..103c9acf 100644 --- a/mep3_behavior/src/mep3_behavior_tree.cpp +++ b/mep3_behavior/src/mep3_behavior_tree.cpp @@ -135,6 +135,7 @@ int main(int argc, char **argv) BT::RegisterRosAction(factory, "JointPosition", {node, "joint_position_command", std::chrono::seconds(30)}); BT::RegisterRosAction(factory, "Navigate", {node, "navigate_to_pose", std::chrono::seconds(30)}); + BT::RegisterRosAction(factory, "FollowPath", {node, "follow_path", std::chrono::seconds(30)}); BT::RegisterRosAction(factory, "Pump", {node, "pump", std::chrono::seconds(30)}); BT::RegisterRosAction(factory, "Translate", {node, "move/move", std::chrono::seconds(30)}); BT::RegisterRosAction(factory, "Rotate", {node, "move/move", std::chrono::seconds(30)}); diff --git a/mep3_behavior/strategies/project.btproj b/mep3_behavior/strategies/project.btproj index 840158fc..d82a16f3 100644 --- a/mep3_behavior/strategies/project.btproj +++ b/mep3_behavior/strategies/project.btproj @@ -29,6 +29,9 @@ For example, 0x12345 For example, 0x32;0x34 + + + @@ -48,6 +51,7 @@ + diff --git a/mep3_behavior/strategies/try_translate.xml b/mep3_behavior/strategies/try_translate.xml index c6ab7975..ea111a0a 100644 --- a/mep3_behavior/strategies/try_translate.xml +++ b/mep3_behavior/strategies/try_translate.xml @@ -1,21 +1,60 @@ - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + @@ -45,7 +84,8 @@ angular_velocity="1.0" mode="7" linear_acceleration="0.8" - angular_acceleration="1.8"/> + angular_acceleration="1.8" + reversing="0"/> - - - + + - + angular_acceleration="1.8" + reversing="2"/> + + + @@ -201,26 +251,15 @@ - - - - - - - - - - + @@ -236,7 +275,8 @@ angular_velocity="1.0" mode="7" linear_acceleration="0.8" - angular_acceleration="1.8"/> + angular_acceleration="1.8" + reversing="0"/> + angular_acceleration="1.8" + reversing="0"/> + angular_acceleration="1.8" + reversing="0"/> + + + @@ -316,19 +362,14 @@ default="0.8"/> + - - points scored, can be negative - unique task name -