00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00039
00040
00041
00042
00043
00044
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
00061 void setRanges(Int inputStart=0, Int outputStart=0, SInt inputEnd=End, SInt outputEnd=End);
00062
00063
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;
00106 Real getInput(Int i) const;
00107 const Vector& getInputs() const;
00108
00109 Int outputSize() const;
00110 String outputName(Int i) const;
00111 void setOutput(Int i, Real value);
00112 void setOutputs(const Vector& values);
00113
00114 protected:
00115 virtual void onUnreference() const
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;
00129
00130 ref<Controllable> controllable;
00131 ref<ControlInterface> adaptedInterface;
00132 mutable ref<AdaptorControlInterface> adaptorInterface;
00133 String newInterfaceName;
00134 String newInterfaceType;
00135
00136 friend class AdaptorControlInterface;
00137 };
00138
00139
00140 }
00141 }
00142
00143 #endif