00001
00002 #ifndef INCLUDED_MCNode_h_
00003 #define INCLUDED_MCNode_h_
00004
00005 #include "Behaviors/StateNode.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Motion/MMAccessor.h"
00008 #include "IPC/SharedObject.h"
00009
00010
00011 class MCNodeBase : public StateNode {
00012 public:
00013 static const char defName[];
00014 static const char defDesc[];
00015
00016
00017 virtual ~MCNodeBase() { delete mc; mc=NULL; }
00018
00019
00020 virtual void stop();
00021
00022
00023 virtual void doEvent();
00024
00025
00026
00027 virtual void setMC(MotionManager::MC_ID mcid);
00028
00029
00030 virtual MotionManager::MC_ID getMC_ID() { return mc_id; }
00031
00032
00033 virtual float getPriority() const { return priority; }
00034
00035
00036 virtual void setPriority(const float p);
00037
00038
00039
00040
00041
00042
00043 virtual void motionFails();
00044
00045 static std::string getClassDescription() { return defName; }
00046 virtual std::string getDescription() const { return getClassDescription(); }
00047
00048 protected:
00049
00050 MCNodeBase(bool expectCompletion)
00051 : StateNode(), mc(NULL), mc_id(MotionManager::invalid_MC_ID),
00052 priority(MotionManager::kStdPriority), mcCompletes(expectCompletion)
00053 {}
00054
00055
00056 MCNodeBase(const std::string &node_name, bool expectCompletion=true)
00057 : StateNode(node_name), mc(NULL), mc_id(MotionManager::invalid_MC_ID),
00058 priority(MotionManager::kStdPriority), mcCompletes(expectCompletion)
00059 {}
00060
00061
00062
00063
00064 virtual SharedObjectBase& getPrivateMC()=0;
00065
00066
00067 virtual bool hasPrivateMC() { return mc!=NULL; }
00068
00069 SharedObjectBase* mc;
00070 MotionManager::MC_ID mc_id;
00071 float priority;
00072 bool mcCompletes;
00073
00074 private:
00075 MCNodeBase(const MCNodeBase&);
00076 MCNodeBase& operator=(const MCNodeBase&);
00077 };
00078
00079
00080 template<class T, const char* mcName=MCNodeBase::defName, const char* mcDesc=MCNodeBase::defDesc, bool completes=true>
00081 class MCNode : public MCNodeBase {
00082 public:
00083
00084 MCNode()
00085 : MCNodeBase(completes)
00086 {}
00087
00088
00089 explicit MCNode(const std::string& nm)
00090 : MCNodeBase(nm,completes)
00091 {}
00092
00093
00094 explicit MCNode(const char* nm)
00095 : MCNodeBase(nm,completes)
00096 {}
00097
00098
00099 virtual ~MCNode() {}
00100
00101
00102
00103 virtual MMAccessor<T> getMC();
00104
00105
00106 virtual void preStart();
00107
00108
00109 virtual void postStart();
00110
00111 static std::string getClassDescription() { return mcDesc; }
00112 virtual std::string getDescription() const { return getClassDescription(); }
00113
00114 protected:
00115 explicit MCNode(bool subCompletes) : MCNodeBase(subCompletes) {}
00116
00117
00118 MCNode(const std::string& nm, bool subCompletes) : MCNodeBase(nm,subCompletes) {}
00119
00120 virtual SharedObject<T>& getPrivateMC();
00121 };
00122
00123
00124
00125
00126
00127
00128 template<class T, const char* mcName, const char* mcDesc, bool completes>
00129 MMAccessor<T> MCNode<T,mcName,mcDesc,completes>::getMC() {
00130 if(mc_id==MotionManager::invalid_MC_ID) {
00131
00132 return MMAccessor<T>(*getPrivateMC(),false);
00133 } else {
00134
00135 return MMAccessor<T>(mc_id);
00136 }
00137 }
00138
00139 template<class T, const char* mcName, const char* mcDesc, bool completes>
00140 SharedObject<T>& MCNode<T,mcName,mcDesc,completes>::getPrivateMC() {
00141 if(mc==NULL)
00142 mc=new SharedObject<T>;
00143 return dynamic_cast<SharedObject<T>&>(*mc);
00144 }
00145
00146 template<class T, const char* mcName, const char* mcDesc, bool completes>
00147 void MCNode<T,mcName,mcDesc,completes>::preStart() {
00148 StateNode::preStart();
00149 getMC()->stop();
00150 }
00151
00152 template<class T, const char* mcName, const char* mcDesc, bool completes>
00153 void MCNode<T,mcName,mcDesc,completes>::postStart() {
00154 StateNode::postStart();
00155 if(mc_id==MotionManager::invalid_MC_ID)
00156 mc_id = motman->addPersistentMotion(getPrivateMC(),priority);
00157 else {
00158 getMC()->start();
00159 motman->setPriority(mc_id,priority);
00160 }
00161 erouter->addListener(this,EventBase::motmanEGID,mc_id,EventBase::statusETID);
00162 getMC()->setDirty();
00163 }
00164
00165
00166
00167
00168
00169
00170 #endif