Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

robot/control/ControllableAdaptor

Go to the documentation of this file.
00001 /* **-*-c++-*-**************************************************************
00002   Copyright (C)2002 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008         
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013         
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018   $Id: ControllableAdaptor 1037 2004-02-11 20:50:18Z jungd $
00019   $Revision: 1.8 $
00020   $Date: 2004-02-11 15:50:18 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #ifndef _ROBOT_CONTROLLABLEADAPTOR_
00026 #define _ROBOT_CONTROLLABLEADAPTOR_
00027 
00028 #include <robot/control/control>
00029 
00030 #include <robot/Controller>
00031 #include <robot/Controllable>
00032 
00033 
00034 namespace robot {
00035 namespace control {
00036 
00037 
00038 /// Adapts one of a Controllable's ControlInterfaces in various ways.
00039 /**
00040  * For example, by selecting a range of inputs and outputs.
00041  * \todo perhaps a ControlInterfaceAdaptor would be a better idea
00042  *       (avoid dependance on Controllable). Could implement a
00043  *       ControlInterfaceCombiner to joint interfaces.
00044  *       Couldn't then have any active adaptation scheme as is possible with this class.
00045  */
00046 class ControllableAdaptor : public robot::Controller, public robot::Controllable
00047 {
00048 public:
00049   enum AdaptorType { PassThrough, Range, Stride };
00050   enum Param { End = -1 };
00051 
00052   ControllableAdaptor(AdaptorType type, ref<Controllable> controllable, 
00053                       const String& interfaceName="", const String& adaptedInterfaceName="",
00054                       const String& adaptedInterfaceType="");
00055   ControllableAdaptor(const ControllableAdaptor& c);
00056 
00057   virtual String className() const { return String("ControllableAdaptor"); }
00058   virtual Object& clone() const { return *NewObj ControllableAdaptor(*this); } 
00059 
00060   /// select subranges of input & output vectors 
00061   void setRanges(Int inputStart=0, Int outputStart=0, SInt inputEnd=End, SInt outputEnd=End);
00062 
00063   /// select periodic elements from input & output vectors 
00064   void setStrides(Int inputStart=0, Int outputStart=0, Int inputStride=1, Int outputStride=1);
00065 
00066 
00067   virtual void setControlInterface(ref<ControlInterface> controlInterface);
00068 
00069   virtual bool isConnected() const { return (adaptedInterface!=0); }
00070 
00071   virtual bool iterate(const base::Time& time); 
00072 
00073   virtual ref<ControlInterface> getControlInterface(String interfaceName="") throw(std::invalid_argument);
00074 
00075 protected:
00076 
00077   AdaptorType type;
00078   bool rangesSet;
00079   Int inputStart, outputStart;
00080   mutable SInt inputEnd, outputEnd;
00081   bool stridesSet;
00082   Int inputStride, outputStride;
00083   
00084 
00085 
00086   void indexOutOfRange() const
00087     { throw std::out_of_range(Exception("index out of range")); }
00088 
00089   Int adaptInputIndex(Int i) const;
00090   Int adaptOutputIndex(Int i) const;
00091 
00092 
00093   class AdaptorControlInterface : public robot::ControlInterface
00094   {
00095   public:
00096     AdaptorControlInterface(ref<ControllableAdaptor> c)
00097       : c(c) { setName(c->newInterfaceName); setType(c->newInterfaceType); }
00098     AdaptorControlInterface(const AdaptorControlInterface& i)
00099       : c(i.c), inputs(i.inputs), outputsSize(i.outputsSize) {}
00100 
00101     virtual String className() const { return String("AdaptorControlInterface"); }
00102     virtual Object& clone() const { return *NewObj AdaptorControlInterface(*this); } 
00103 
00104     Int inputSize() const;
00105     String inputName(Int i) const; ///< \todo implement
00106     Real getInput(Int i) const;
00107     const Vector& getInputs() const;
00108     
00109     Int outputSize() const;
00110     String outputName(Int i) const; ///< \todo implement
00111     void setOutput(Int i, Real value);
00112     void setOutputs(const Vector& values);
00113     
00114   protected:
00115     virtual void onUnreference() const ///< break cyclic reference with c
00116     {
00117       if ((referenceCount()==1) && (c->referenceCount()==1)) 
00118         Release(c);
00119     }
00120 
00121     mutable ref<ControllableAdaptor> c;
00122     mutable Vector inputs;
00123     mutable Int outputsSize;
00124 
00125     friend class ControllableAdaptor;
00126   };
00127 
00128   virtual void onUnreference() const; ///< break cyclic reference with adaptorInterface
00129 
00130   ref<Controllable> controllable;          ///< Controllable we're adapting 
00131   ref<ControlInterface> adaptedInterface;  ///< interface to be adapted
00132   mutable ref<AdaptorControlInterface> adaptorInterface; ///< interface that adapts the controllable
00133   String newInterfaceName;
00134   String newInterfaceType;
00135 
00136   friend class AdaptorControlInterface;
00137 };
00138 
00139 
00140 }
00141 } // robot::control
00142 
00143 #endif

Generated on Thu Jul 29 15:56:31 2004 for OpenSim by doxygen 1.3.6