tree_node.h
Go to the documentation of this file.
00001 /* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved
00002 *
00003 *   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
00004 *   to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00005 *   and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
00006 *   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00007 *
00008 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00009 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00010 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00011 */
00012 
00013 
00014 #ifndef TREE_NODE_H
00015 #define TREE_NODE_H
00016 
00017 
00018 #ifndef _COLORS_
00019 #define _COLORS_
00020 
00021 /* FOREGROUND */
00022 #define RST  "\x1B[0m"
00023 #define KRED  "\x1B[31m"
00024 #define KGRN  "\x1B[32m"
00025 #define KYEL  "\x1B[33m"
00026 #define KBLU  "\x1B[34m"
00027 #define KMAG  "\x1B[35m"
00028 #define KCYN  "\x1B[36m"
00029 #define KWHT  "\x1B[37m"
00030 
00031 #define FRED(x) KRED x RST
00032 #define FGRN(x) KGRN x RST
00033 #define FYEL(x) KYEL x RST
00034 #define FBLU(x) KBLU x RST
00035 #define FMAG(x) KMAG x RST
00036 #define FCYN(x) KCYN x RST
00037 #define FWHT(x) KWHT x RST
00038 
00039 #define BOLD(x) "\x1B[1m" x RST
00040 #define UNDL(x) "\x1B[4m" x RST
00041 
00042 #endif
00043 
00044    #define DEBUG  // uncomment this line if you want to print debug messages
00045 
00046 #ifdef DEBUG
00047   // #define DEBUG_STDERR(x) (std::cerr << (x))
00048 #define DEBUG_STDOUT(str) do { std::cout << str << std::endl; } while ( false )  // NOLINT(whitespace/braces)
00049 
00050 
00051 #else
00052 #define DEBUG_STDOUT(str)
00053 #endif
00054 
00055 
00056 #include <iostream>
00057 #include <unistd.h>
00058 
00059 #include <string>
00060 
00061 #include <thread>
00062 #include <chrono>
00063 #include <mutex>
00064 #include <condition_variable>
00065 
00066 
00067 #include <tick_engine.h>
00068 #include <exceptions.h>
00069 
00070 namespace BT
00071 {
00072 // Enumerates the possible types of a node, for drawinf we have do discriminate whoich control node it is:
00073 
00074 enum NodeType {ACTION_NODE, CONDITION_NODE, CONTROL_NODE};
00075 enum DrawNodeType {PARALLEL, SELECTOR, SEQUENCE, SEQUENCESTAR, SELECTORSTAR, ACTION, CONDITION, DECORATOR};
00076 // Enumerates the states every node can be in after execution during a particular
00077 // time step:
00078 // - "Success" indicates that the node has completed running during this time step;
00079 // - "Failure" indicates that the node has determined it will not be able to complete
00080 //   its task;
00081 // - "Running" indicates that the node has successfully moved forward during this
00082 //   time step, but the task is not yet complete;
00083 // - "Idle" indicates that the node hasn't run yet.
00084 // - "Halted" indicates that the node has been halted by its father.
00085 enum ReturnStatus {RUNNING, SUCCESS, FAILURE, IDLE, HALTED, EXIT};
00086 
00087 // Enumerates the options for when a parallel node is considered to have failed:
00088 // - "FAIL_ON_ONE" indicates that the node will return failure as soon as one of
00089 //   its children fails;
00090 // - "FAIL_ON_ALL" indicates that all of the node's children must fail before it
00091 //   returns failure.
00092 enum FailurePolicy {FAIL_ON_ONE, FAIL_ON_ALL};
00093 enum ResetPolity   {ON_SUCCESS_OR_FAILURE, ON_SUCCESS, ON_FAILURE};
00094 
00095 // Enumerates the options for when a parallel node is considered to have succeeded:
00096 // - "SUCCEED_ON_ONE" indicates that the node will return success as soon as one
00097 //   of its children succeeds;
00098 // - "BT::SUCCEED_ON_ALL" indicates that all of the node's children must succeed before
00099 //   it returns success.
00100 enum SuccessPolicy {SUCCEED_ON_ONE, SUCCEED_ON_ALL};
00101 
00102 // Abstract base class for Behavior Tree Nodes
00103 class TreeNode
00104 {
00105 private:
00106     // Node name
00107     std::string name_;
00108     bool has_parent_;
00109 
00110 
00111 
00112 
00113 protected:
00114     // The node state that must be treated in a thread-safe way
00115     bool is_state_updated_;
00116     ReturnStatus status_;
00117     ReturnStatus color_status_;
00118 
00119 
00120     std::mutex state_mutex_;
00121     std::mutex color_state_mutex_;
00122     std::condition_variable state_condition_variable_;
00123     // Node type
00124     NodeType type_;
00125     // position and offset for horizontal positioning when drawing
00126     float x_shift_, x_pose_;
00127 
00128 public:
00129     // The thread that will execute the node
00130     std::thread thread_;
00131 
00132     // Node semaphore to simulate the tick
00133     // (and to synchronize fathers and children)
00134     TickEngine tick_engine;
00135 
00136 
00137 
00138 
00139     // The constructor and the distructor
00140     explicit TreeNode(std::string name);
00141     ~TreeNode();
00142 
00143     // The method that is going to be executed when the node receive a tick
00144     virtual BT::ReturnStatus Tick() = 0;
00145 
00146     // The method used to interrupt the execution of the node
00147     virtual void Halt() = 0;
00148 
00149     // The method that retrive the state of the node
00150     // (conditional waiting and mutual access)
00151     // ReturnStatus GetNodeState();
00152     void SetNodeState(ReturnStatus new_state);
00153     void set_color_status(ReturnStatus new_color_status);
00154 
00155     // Methods used to access the node state without the
00156     // conditional waiting (only mutual access)
00157     ReturnStatus ReadState();
00158     ReturnStatus get_color_status();
00159     virtual int DrawType() = 0;
00160     virtual void ResetColorState() = 0;
00161     virtual int Depth() = 0;
00162 
00163 
00164     //  Getters and setters
00165     void set_x_pose(float x_pose);
00166     float get_x_pose();
00167 
00168     void set_x_shift(float x_shift);
00169     float get_x_shift();
00170 
00171 
00172 
00173     ReturnStatus get_status();
00174     void set_status(ReturnStatus new_status);
00175 
00176 
00177     std::string get_name();
00178     void set_name(std::string new_name);
00179 
00180     NodeType get_type();
00181     bool has_parent();
00182     void set_has_parent(bool value);
00183 };
00184 }  // namespace BT
00185 
00186 #endif  // TREE_NODE_H


behavior_tree_core
Author(s): Michele Colledanchise
autogenerated on Thu Jun 6 2019 18:19:08