condition_example.cpp
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 #include <ros/ros.h>
00014 #include <actionlib/server/simple_action_server.h>
00015 #include <behavior_tree_core/BTAction.h>
00016 
00017 #include <string>
00018 
00019 enum Status {RUNNING, SUCCESS, FAILURE};
00020 
00021 
00022 class BTAction
00023 {
00024 protected:
00025     ros::NodeHandle nh_;
00026     // NodeHandle instance must be created before this line. Otherwise strange errors may occur.
00027     actionlib::SimpleActionServer<behavior_tree_core::BTAction> as_;
00028     std::string action_name_;
00029     // create messages that are used to published feedback/result
00030     behavior_tree_core::BTFeedback feedback_;
00031     behavior_tree_core::BTResult result_;
00032 
00033 public:
00034     explicit BTAction(std::string name) :
00035         as_(nh_, name, boost::bind(&BTAction::execute_callback, this, _1), false),
00036         action_name_(name)
00037     {
00038         // start the action server (action in sense of Actionlib not BT action)
00039         as_.start();
00040         ROS_INFO("Condition Server Started");
00041     }
00042 
00043     ~BTAction(void)
00044     { }
00045     void execute_callback(const behavior_tree_core::BTGoalConstPtr &goal)
00046     {
00047         if (true)
00048         {
00049             set_status(SUCCESS);
00050         }
00051         else
00052         {
00053             set_status(FAILURE);
00054         }
00055     }
00056 
00057     //  returns the status to the client (Behavior Tree)
00058     void set_status(int status)
00059     {
00060         // Set The feedback and result of BT.action
00061         feedback_.status = status;
00062         result_.status = feedback_.status;
00063         // publish the feedback
00064         as_.publishFeedback(feedback_);
00065         // setSucceeded means that it has finished the action (it has returned SUCCESS or FAILURE).
00066         as_.setSucceeded(result_);
00067 
00068         switch (status)  // Print for convenience
00069         {
00070         case SUCCESS:
00071             ROS_INFO("Condition %s Succeeded", ros::this_node::getName().c_str() );
00072             break;
00073         case FAILURE:
00074             ROS_INFO("Condition %s Failed", ros::this_node::getName().c_str() );
00075             break;
00076         default:
00077             break;
00078         }
00079     }
00080 };
00081 
00082 int main(int argc, char** argv)
00083 {
00084     ros::init(argc, argv, "condition");
00085     ROS_INFO(" Enum: %d", RUNNING);
00086     ROS_INFO(" condition Ready for Ticks");
00087     BTAction bt_action(ros::this_node::getName());
00088     ros::spin();
00089     return 0;
00090 }


behavior_tree_leaves
Author(s): Michele Colledanchise
autogenerated on Thu Jun 6 2019 18:19:15