00001 /** 00002 \file Robots/LoBot/control/LoBehavior.H 00003 \brief An ABC for Robolocust behaviours. 00004 00005 This file defines an abstract base class that provides a common 00006 interface for the different behaviours that constitute lobot's 00007 behaviour-based robot controller. 00008 */ 00009 00010 // //////////////////////////////////////////////////////////////////// // 00011 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00012 // by the University of Southern California (USC) and the iLab at USC. // 00013 // See http://iLab.usc.edu for information about this project. // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00016 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00017 // in Visual Environments, and Applications'' by Christof Koch and // 00018 // Laurent Itti, California Institute of Technology, 2001 (patent // 00019 // pending; application number 09/912,225 filed July 23, 2001; see // 00020 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00021 // //////////////////////////////////////////////////////////////////// // 00022 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00023 // // 00024 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00025 // redistribute it and/or modify it under the terms of the GNU General // 00026 // Public License as published by the Free Software Foundation; either // 00027 // version 2 of the License, or (at your option) any later version. // 00028 // // 00029 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00030 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00031 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00032 // PURPOSE. See the GNU General Public License for more details. // 00033 // // 00034 // You should have received a copy of the GNU General Public License // 00035 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00036 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00037 // Boston, MA 02111-1307 USA. // 00038 // //////////////////////////////////////////////////////////////////// // 00039 // 00040 // Primary maintainer for this file: mviswana usc edu 00041 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/control/LoBehavior.H $ 00042 // $Id: LoBehavior.H 13608 2010-06-23 04:27:01Z mviswana $ 00043 // 00044 00045 #ifndef LOBOT_BEHAVIOR_DOT_H 00046 #define LOBOT_BEHAVIOR_DOT_H 00047 00048 //------------------------------ HEADERS -------------------------------- 00049 00050 // lobot headers 00051 #include "Robots/LoBot/ui/LoDrawable.H" 00052 #include "Robots/LoBot/thread/LoThread.H" 00053 00054 //----------------------------- NAMESPACE ------------------------------- 00055 00056 namespace lobot { 00057 00058 //------------------------- CLASS DEFINITION ---------------------------- 00059 00060 /** 00061 \class lobot::Behavior 00062 \brief An ABC defining the common interface for all of lobot's 00063 behaviours. 00064 00065 This class implements an abstract base class for all the behaviours 00066 that make up the multithreaded Robolocust behaviour-based control 00067 system. Each behaviour is a goal-directed action that ties together 00068 a sensorimotor loop of some sort and runs in a separate thread. 00069 Behaviours are instantiated by the main thread during Robolocust's 00070 start-up sequence via an object factory. Which behaviours the robot 00071 controller should run are specified in the config file. 00072 00073 Each behaviour's main loop should take care to coordinate accesses to 00074 various shared sensors and other resources using the Robolocust 00075 UpdateLock. 00076 00077 Also, since a behaviour runs in its own thread, all subclasses *must* 00078 call Thread::start() in their constructors. 00079 */ 00080 class Behavior : public Drawable, private Thread { 00081 // Prevent copy and assignment 00082 Behavior(const Behavior&) ; 00083 Behavior& operator=(const Behavior&) ; 00084 00085 public: 00086 /// Each behaviour has a name that must be set by the client module. 00087 /// 00088 /// NOTE: Being a public data member, this field is rife with 00089 /// possibilities for abuse. But, hopefully, clients will refrain from 00090 /// doing so (after all, what's the point in making life harder for 00091 /// yourself?). The intent here is to set this field when the 00092 /// behaviour is instantiated and leave it the hell alone thereafter. 00093 std::string name ; 00094 00095 protected: 00096 /// Every behaviour must wait a bit after its action method is 00097 /// executed before the next iteration. This update delay is a user 00098 /// setting specified in the config file. The delay is expected to be 00099 /// in milliseconds. 00100 /// 00101 /// CAUTION: Since the update delay for each behaviour is 00102 /// customizable, it is possible for users to completely ruin the 00103 /// lobot controller by providing bizarre values. Therefore, each 00104 /// behaviour is expected to guard itself against such weirdness. 00105 int m_update_delay ; 00106 00107 /// A protected constructor because behaviours are instantiated with 00108 /// an object factory and not directly by clients. 00109 /// 00110 /// Derived classes must specify an appropriate update delay to use 00111 /// when they invoke this constructor. It is up to each individual 00112 /// behaviour to guard against possibly catastrophic update delay 00113 /// settings in the config file and provide reasonable defaults and 00114 /// boundaries. 00115 /// 00116 /// Optionally, behaviours interested in visualization may provide the 00117 /// names and geometries for their respective drawables. 00118 Behavior(int update_delay, 00119 const std::string& drawable_name = "", 00120 const Drawable::Geometry& = Drawable::Geometry()) ; 00121 00122 // Since we're using private inheritance, Thread::start() won't be 00123 // visible to subclasses without this explicit say-so. 00124 using Thread::start ; 00125 00126 /// This method implements the behaviour's main loop, taking care of 00127 /// checking with the lobot::Shutdown object whether or not it's time 00128 /// to quit. 00129 /// 00130 /// NOTE: Derived classes may but generally should not provide their 00131 /// own implementations of the Thread::run() method. If, for some 00132 /// reason, a subclass of lobot::Behavior needs to define its own 00133 /// version of run(), it should be sure to check the status of the 00134 /// lobot::Shutdown object. 00135 void run() ; 00136 00137 /// This method implements the behaviour's action. Each behaviour must 00138 /// provide its own custom implementation of this function. 00139 /// 00140 /// NOTE: This function is called by run() and is already ensconced 00141 /// within a loop. Derived classes should *not* run their own infinite 00142 /// or main-loop type loops in this method. Instead, they have to 00143 /// provide only the *body* of the main loop. That is, the 00144 /// Behavior::run() method implements the actual main loop of the 00145 /// behaviour while the derived class merely provides the individual 00146 /// steps that go into that main loop. 00147 /// 00148 /// Also: Behavior subclasses should use UpdateLock as required in 00149 /// their definitions of the action() method to ensure proper access 00150 /// to shared sensor objects and other resources. 00151 virtual void action() = 0 ; 00152 00153 /// These methods provides derived classes hooks for implementing any 00154 /// pre- and post-run operations. pre_run() is called right before the 00155 /// main loop is entered and post_run() right after it is exited. 00156 /// 00157 /// Derived classes are not required to define these functions. The 00158 /// default versions implemented in the Behavior base class do 00159 /// nothing. 00160 //@{ 00161 virtual void pre_run() ; 00162 virtual void post_run() ; 00163 //@} 00164 00165 public: 00166 /// Clean-up. 00167 virtual ~Behavior() ; 00168 } ; 00169 00170 //----------------------------------------------------------------------- 00171 00172 } // end of namespace encapsulating this file's definitions 00173 00174 #endif 00175 00176 /* So things look consistent in everyone's emacs... */ 00177 /* Local Variables: */ 00178 /* indent-tabs-mode: nil */ 00179 /* End: */