Homepage Demos Overview Downloads Tutorials Reference
Credits

Controller.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_Controller_h
00003 #define INCLUDED_Controller_h
00004 
00005 #include "Controls/ControlBase.h"
00006 #include "Behaviors/BehaviorBase.h"
00007 #include "Events/EventBase.h"
00008 #include "Events/EventRouter.h"
00009 #include "Motion/MotionManager.h"
00010 #include "Wireless/Wireless.h"
00011 #include "Wireless/Socket.h"
00012 #include <stack>
00013 
00014 //! Handles the menu/command system... when it detects the EmergencyStopMC is activated, it'll kick into high priority.
00015 /*! Keeps track of a command stack.  A Control can designate another sub-control, which will receive events until it finishes\n
00016  *  Events will then be sent to the parent again.
00017  *
00018  *  The GUI uses the same commands as the user (makes it much easier to have only one parser).
00019  *  The commands are:
00020  *  - '<tt>!refresh</tt>' - redisplays the current control (handy on first connecting, or when other output has scrolled it off the screen)
00021  *  - '<tt>!reset</tt>' - return to the root control
00022  *  - '<tt>!next</tt>' - calls ControlBase::doNextItem() of the current control
00023  *  - '<tt>!prev</tt>' - calls ControlBase::doPrevItem() of the current control
00024  *  - '<tt>!select</tt>' - calls ControlBase::doSelect() of the current control
00025  *  - '<tt>!cancel</tt>' - calls ControlBase::doCancel() of the current control
00026  *  - '<tt>!msg </tt><i>text</i>' - broadcasts <i>text</i> as a TextMsgEvent
00027  *  - '<tt>!root </tt><i>text</i>' - calls ControlBase::takeInput(<i>text</i>) on the root control
00028  *  - '<tt>!hello</tt>' - responds with '<tt>hello\n</tt><i>count</i>' where <i>count</i> is the number of times '<tt>!hello</tt>' has been sent.  Good for detecting first connection after boot vs. a reconnect.
00029  *  - '<tt>!hilight</tt> [<i>n1</i> [<i>n2</i> [...]]]' - hilights zero, one, or more items in the menu
00030  *  - '<tt>!input </tt><i>text</i>' - calls ControlBase::takeInput(<i>text</i>) on the currently hilighted control(s)
00031  *  - any text not beginning with '!' - sent to ControlBase::takeInput() of the current control
00032  *
00033  *  In return, to send the menus to the GUI, the following messages are sent: (newlines are required where shown)
00034  *  - '<tt>push</tt>' - signals a submenu has been activated
00035  *  - '<tt>pop</tt>' - signals a submenu has been closed
00036  *  - '<tt>refresh</tt>\n
00037  *    <i>text:title</i>\n
00038  *    <i>int:numitems</i>\n
00039  *    <i>bool:hasSubmenus</i><sub>1</sub>\n
00040  *    <i>bool:hilighted</i><sub>1</sub>\n
00041  *    <i>text:item-title</i><sub>1</sub>\n
00042  *    <i>text:item-description</i><sub>1</sub>\n
00043  *    ...\n
00044  *    <i>bool:hasSubmenus<sub>numitems</sub></i>\n
00045  *    <i>bool:hilighted<sub>numitems</sub></i>\n
00046  *    <i>text:item-title<sub>numitems</sub></i>\n
00047  *    <i>text:item-description<sub>numitems</sub></i>' - refreshes the current menu\n
00048  *  - '<tt>status </tt><i>text</i>' - sets the status bar to <i>text</i> (until the next refresh)
00049  *  - '<tt>load</tt>\n
00050  *    <i>text:classname</i>\n
00051  *    <i>text:instancename</i>
00052  *    <i>int:port</i>\n
00053  *    [<i>arg1</i> [<i>arg2</i> [...]]]' - tells the GUI to load the java class named <i>classname</i>, and have it connect to <i>port</i>, passing it the argument list.
00054  *    <i>classname</i> should contain a constructor of the form <tt>Classname(String </tt><i>host</i>, <tt>int </tt><i>port</i>, <tt>String </tt><i>args</i><tt>[])</tt>
00055  *    the argument list is parsed as if it were on the console - unescaped or unquoted spaces will separate args into elements in the array
00056  *  - '<tt>close</tt>\n
00057  *    <i>text:instancename</i>' - calls <tt>close()</tt> on an object previously created by a <tt>load</tt> message.
00058  *    The Java object is expected to contain a function <tt>void close()</tt>.
00059  *  - '<tt>goodbye</tt>\n
00060  *    Indicates the connection is about to be closed purposefully, to differentiate from an accidental cut off.
00061  *  
00062  *  bool types are expected to be numerical values, 0 for false,
00063  *  non-zero for true.
00064  *
00065  *  <tt>load</tt> and <tt>close</tt> are intended to allow pop-up
00066  *  windows for custom displays.
00067  *
00068  *  The upstream is the responsibility of the individual Controls, but
00069  *  the protocol is listed here to keep it together.  When a control's
00070  *  state changes, it's that control's responsiblity to refresh the UI
00071  *  (LEDs, console, and GUI as appropriate).  Thus, future extensions
00072  *  to the upstream protocol are between the control which will use it
00073  *  and the GUI.  Future extensions to the downstream protocol would
00074  *  involve changing Controller and the GUI.
00075  *
00076  *  The Controller may connect to serr in the future to pop-up an alert
00077  *  anytime output to serr occurs.
00078  *
00079  *  Note that all state is maintained on the robot - even if the GUI
00080  *  is connected, you can still use the buttons to interact with the
00081  *  controller, and the GUI will update to reflect the changes.  In
00082  *  HCI (Human Computer Interaction) parlance, this is the MVC,
00083  *  Model-View-Controller architecture, almost by necessity. (HCI
00084  *  happens to be my double major when I was an undergrad ;)
00085  *    
00086  */
00087 class Controller : public BehaviorBase, public EventTrapper {
00088 public:
00089   Controller() : display(MotionManager::invalid_MC_ID), estop_id(MotionManager::invalid_MC_ID), root(NULL), cmdstack(), last_time(0), cur_time(0), nextEv_val(0), nextEv_dur(0), prevEv_val(0), prevEv_dur(0), alreadyGotBoth(false), isControlling(false), gui_comm(NULL)  {init();} //!< Constructor
00090   Controller(ControlBase* r) : display(MotionManager::invalid_MC_ID), estop_id(MotionManager::invalid_MC_ID), root(r), cmdstack(), last_time(0), cur_time(0), nextEv_val(0), nextEv_dur(0), prevEv_val(0), prevEv_dur(0), alreadyGotBoth(false), isControlling(false), gui_comm(NULL) { init(); } //!< Constructor, sets a default root control
00091   virtual ~Controller() {
00092     cout << "~Controller()..." << endl;
00093     delete root;
00094     theOneController=NULL;
00095     cout << "~Controller()-DONE" << endl;
00096   } //!< Destructor
00097 
00098   //@{
00099   //! event masks used by processEvent()
00100   static EventBase nextItem; 
00101   static EventBase prevItem;
00102   static EventBase nextItemFast;
00103   static EventBase prevItemFast;
00104   static EventBase selectItem;
00105   static EventBase cancel; //@}
00106 
00107   virtual void DoStart(); //!< register for events and resets the cmdstack
00108   virtual void DoStop(); //!< stop listening for events and resets the cmdstack
00109   virtual bool trapEvent(const EventBase& e); //!< passes an event to the top control
00110   virtual void processEvent(const EventBase& e); //!< just for e-stop activation/deactivation
00111   
00112   void reset();   //!< will take the command stack back down to the root
00113   void refresh(); //!< refreshes the display, for times like sub-control dying, the previous control needs to reset it's display
00114 
00115   void push(ControlBase* c); //!< puts a new control on top
00116   void pop();                //!< kills the top control, goes to previous
00117   ControlBase* top() { return cmdstack.top(); } //!< returns the current control
00118 
00119   Controller& setRoot(ControlBase* r); //!< sets the root level control
00120 
00121   Controller& setEStopID(MotionManager::MC_ID estopid); //!< Sets the emergency stop MC to monitor for pausing
00122   
00123   virtual std::string getName() const { return "Controller"; }
00124   static std::string getClassDescription() { return "Provides interface for activating/deactivating controls (and through them, behaviors)"; }
00125   
00126 
00127 
00128   static void loadGUI(const std::string& type, const std::string& name, unsigned int port) {loadGUI(type,name,port,std::vector<std::string>());} //!< attempts to open a Java object on the desktop
00129   static void loadGUI(const std::string& type, const std::string& name, unsigned int port, const std::vector<std::string>& args); //!< attempts to open a Java object on the desktop
00130   static void closeGUI(const std::string& name); //!< calls close() on a Java object loaded with loadGUI() (on the desktop)
00131 
00132   static int gui_comm_callback(char *buf, int bytes); //!< called by wireless when there's new data from the GUI
00133   static int console_callback(char *buf, int bytes);  //!< called by wireless when someone has entered new data on the tekkotsu console (NOT cin)
00134 
00135 protected:
00136   //! assigns appropriate values to the static event bases
00137   void init();
00138   
00139   //! called with each line that's entered on the tekkotsu console or from the GUI
00140   void takeLine(const std::string& s);
00141 
00142   //! sets a config value - some values may require additional processing (done here) to have the new values take effect
00143   int setConfig(const char *str);
00144 
00145   //! maintains top Control
00146   /*! @param next one of: @li NULL: pop() ::cmdstack @li ::cmdstack.top(): nothing @li other address: ::push(@a next)
00147    *  @return true, all the time, for convenience from trapEvent() */
00148   bool setNext(ControlBase* next);
00149 
00150   //! called when the estop switches on
00151   /*!  causes the top control to activate, registers for button events */
00152   void activate();
00153 
00154   //! called when the estop switches off\n
00155   /*!  causes the top control to deactivate, stops listening for buttons */
00156   void deactivate();
00157   
00158   //! @brief returns true if a valid control is available on the stack
00159   /*!  if the stack is empty, will push root if it's non-null */
00160   bool chkCmdStack();
00161 
00162   //! invalid_MC_ID if not active, otherwise id of high priority LEDs
00163   MotionManager::MC_ID display;
00164 
00165   //! the EmergencyStopMC MC_ID that this Controller is monitoring
00166   MotionManager::MC_ID estop_id;
00167 
00168   //! the base control, if cmdstack underflows, it will be reset to this
00169   ControlBase * root;
00170 
00171   /*! @brief the stack of the current control hierarchy\n
00172    *  should never contain NULL entries */
00173   std::stack< ControlBase* > cmdstack;
00174 
00175   //! returns true when the current time and last time are in different periods
00176   static bool calcPulse(unsigned int t, unsigned int last, unsigned int period) {
00177     if(period<t-last)
00178       return true;
00179     bool nextclock=(t/period)&1;
00180     bool lastclock=(last/period)&1;
00181     return (lastclock!=nextclock);
00182   }
00183 
00184 
00185   unsigned int last_time; //!< the time of the last event
00186   unsigned int cur_time; //!< the time of the current event (do*() can check this instead of calling get_time() )
00187   float nextEv_val; //!< the magnitude of the last next event (::nextItem)
00188   unsigned int nextEv_dur; //!< the duration of the last next event (::nextItem)
00189   float prevEv_val; //!< the magnitude of the last prev event (::prevItem)
00190   unsigned int prevEv_dur; //!< the duration of the last prev event (::prevItem)
00191   bool alreadyGotBoth; //!< if doReadStdIn() was already called, but the buttons are both still down
00192   bool isControlling; //!< true if the Controller is currently active (in the activate()/deactivate() sense, not DoStart()/DoStop() sense - use isActive() for that...)
00193 
00194   Socket * gui_comm; //!< the socket to listen on for the gui
00195   static Controller * theOneController; //!< currently can't pull connection socket off of server socket, so only one Controller
00196   
00197 private:
00198   Controller(const Controller&); //!< shouldn't be called...
00199   Controller& operator=(const Controller&); //!< shouldn't be called...
00200 };
00201 
00202 /*! @file
00203  * @brief Describes Controller class, a behavior that should be started whenever the emergency stop goes on to provide menus for robot control
00204  * @author ejt (Creator)
00205  *
00206  * $Author: ejt $
00207  * $Name: tekkotsu-2_0 $
00208  * $Revision: 1.26 $
00209  * $State: Exp $
00210  * $Date: 2004/01/21 03:52:39 $
00211  */
00212 
00213 #endif

Tekkotsu v2.0
Generated Wed Jan 21 03:20:28 2004 by Doxygen 1.3.4