00001 #ifndef MOVEBASE_BT_NODES_H 00002 #define MOVEBASE_BT_NODES_H 00003 00004 #include "behaviortree_cpp/behavior_tree.h" 00005 00006 // Custom type 00007 struct Pose2D 00008 { 00009 double x, y, theta; 00010 }; 00011 00012 inline void SleepMS(int ms) 00013 { 00014 std::this_thread::sleep_for(std::chrono::milliseconds(ms)); 00015 } 00016 00017 namespace BT 00018 { 00019 // This template specialization is needed only if you want 00020 // to AUTOMATICALLY convert a NodeParameter into a Pose2D 00021 // In other words, implement it if you want to be able to do: 00022 // 00023 // TreeNode::getParam<Pose2D>(key, ...) 00024 // 00025 template <> 00026 Pose2D convertFromString(const StringView& key) 00027 { 00028 // three real numbers separated by semicolons 00029 auto parts = BT::splitString(key, ';'); 00030 if (parts.size() != 3) 00031 { 00032 throw std::runtime_error("invalid input)"); 00033 } 00034 else 00035 { 00036 Pose2D output; 00037 output.x = convertFromString<double>(parts[0]); 00038 output.y = convertFromString<double>(parts[1]); 00039 output.theta = convertFromString<double>(parts[2]); 00040 return output; 00041 } 00042 } 00043 } 00044 00045 // This is an asynchronous operation that will run in a separate thread. 00046 // It requires the NodeParameter "goal". If the key is not provided, the default 00047 // value "0;0;0" is used instead. 00048 00049 class MoveBaseAction : public BT::AsyncActionNode 00050 { 00051 public: 00052 // If your TreeNode requires a NodeParameter, you must define a constructor 00053 // with this signature. 00054 MoveBaseAction(const std::string& name, const BT::NodeParameters& params) 00055 : AsyncActionNode(name, params) 00056 { 00057 } 00058 00059 // It is mandatory to define this static method. 00060 // If you don't, BehaviorTreeFactory::registerNodeType will not compile. 00061 // 00062 static const BT::NodeParameters& requiredNodeParameters() 00063 { 00064 static BT::NodeParameters params = {{"goal", "0;0;0"}}; 00065 return params; 00066 } 00067 00068 BT::NodeStatus tick() override; 00069 00070 virtual void halt() override; 00071 00072 private: 00073 std::atomic_bool _halt_requested; 00074 }; 00075 00076 #endif // MOVEBASE_BT_NODES_H