00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Robot Group Leipzig * 00003 * martius@informatik.uni-leipzig.de * 00004 * fhesse@informatik.uni-leipzig.de * 00005 * der@informatik.uni-leipzig.de * 00006 * marcel@informatik.uni-leipzig.de * 00007 * * 00008 * This program is free software; you can redistribute it and/or modify * 00009 * it under the terms of the GNU General Public License as published by * 00010 * the Free Software Foundation; either version 2 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This program is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this program; if not, write to the * 00020 * Free Software Foundation, Inc., * 00021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00022 * * 00023 * $Log: component.h,v $ 00024 * Revision 1.15 2010/03/09 11:53:41 martius 00025 * renamed globally ode to ode-dbl 00026 * 00027 * Revision 1.14 2008/09/16 14:53:40 martius 00028 * use cmath instead of math.h 00029 * 00030 * Revision 1.13 2006/11/30 08:51:39 robot8 00031 * -update of the evolution projekt 00032 * -fitness changed 00033 * -replication changed 00034 * -added copy function 00035 * 00036 * Revision 1.12 2006/10/20 13:52:33 robot8 00037 * -update of the evolution projekt 00038 * -only changed some parameter 00039 * 00040 * Revision 1.11 2006/09/11 12:01:31 martius 00041 * *** empty log message *** 00042 * 00043 * * 00044 ***************************************************************************/ 00045 00046 #ifndef _component_h 00047 #define _component_h 00048 00049 #include <typeinfo> 00050 #include <vector> 00051 #include <list> 00052 00053 #include <ode-dbl/ode.h> 00054 #include <cmath> 00055 00056 #include "oderobot.h" 00057 #include "joint.h" 00058 00059 namespace lpzrobots 00060 { 00061 typedef struct _ComponentConf 00062 { 00063 bool completesensormode; //if true, the controler of the Component also gets the sensor values of the robot of the component 00064 bool completemotormode; //if true, the controler of the component also controls the robot of the component 00065 double max_force; 00066 double speed; 00067 } ComponentConf; 00068 00069 /** 00070 * This is the abstract base class of the component system. A component includes the routines for managing sensor and motor values from a controller. 00071 * With a bunch of components it is possible to create a tree like structure. There is always a root structure. 00072 * Components could be used as normal robot objects, so they can get motor and sensor values. These values are shared with all components in the tree structure of a component. 00073 * Between the differnt objects of the component class could exist connections, which are physical represented by Ode-Joints. 00074 * The components use the motor values they get, to control the joints between them. 00075 * From these joints sensor values are read out, which where send with the getSensor () function like a normal robot does. 00076 * But the arrray of sensor values comes from all components within the structure beneth the component, from which the function was called. 00077 * You could say that each component is a non physical shell around a physical object like a primitive or a robot in classical meaning of this project. 00078 * These shells build a connection between the physical parts and control these connections, which could be also physical existend. 00079 */ 00080 class Component : public OdeRobot 00081 { 00082 public: 00083 ComponentConf conf; 00084 00085 public: 00086 00087 /** 00088 *This is the structure of one connection between two components. 00089 **/ 00090 typedef struct 00091 { 00092 Component* subcomponent; 00093 Joint* joint; 00094 bool softlink; //if true the connection ends the recursion, false = normal 00095 void *data; 00096 } componentConnection; 00097 00098 std::vector <componentConnection> connection; 00099 std::vector <Component*> backwardreference; 00100 00101 public: 00102 Component* originComponent; 00103 Component* directOriginComponent; 00104 00105 public: 00106 00107 Component ( const OdeHandle &odeHandle, const OsgHandle &osgHandle, const ComponentConf& conf); 00108 00109 ~Component (); 00110 00111 static ComponentConf getDefaultConf() 00112 { 00113 ComponentConf conf; 00114 conf.completesensormode = true; 00115 conf.completemotormode = false; 00116 conf.max_force = 1; 00117 conf.speed = 1; 00118 return conf; 00119 } 00120 00121 public: 00122 00123 /** 00124 *Returns number of sensors; recursivly adding of the number of sensors all subcomponents and the robots of all Subcomponents. 00125 *@return number of sensors of the component and all its subcomponents 00126 **/ 00127 virtual int getSensorNumber (); 00128 00129 /** 00130 *returns number of motors; recursivly adding of the number of sensors all subcomponents; at the moment only counts Hinge-, Slider-, Hinge2 and Universal-Joints; The Motor-Numbers of the robots of the Components is not counted. 00131 *@return number of motors of the component and all its subcomponents 00132 **/ 00133 virtual int getMotorNumber (); 00134 00135 /** 00136 *Use this, to get all sensor values of all the joints of all subcomponents, and the sensors of all robots, belonging to all subcompionents. 00137 *The sensor values have the following sequence: 00138 *values of the component connecting joints, values of the robot of the component, 00139 *values of component connecting joints of the first subcomponent, values of the robot of the first subcomponent, ... 00140 *@robot sensor values of the connecting joints of this component and all subcomponents 00141 **/ 00142 virtual int getSensors (sensor *sensors, int sensornumber); //returns actual sensorvalues; only for the connecting joints 00143 00144 00145 /** 00146 *Sets the motor values for the joints connecting the component with its subcomponents, an recursivly the joints of all subComponents. 00147 *The motors of all robots of the subcomponents is not set. 00148 *@param 00149 **/ 00150 virtual void setMotors (const motor *motors, int motornumber); //sets actual motorcommands; only for the connecting joints 00151 00152 /** 00153 *This sets all motors in the component structure to zero, starting from this component. 00154 **/ 00155 virtual void resetMotorsRecursive ( ); 00156 00157 //virtual int getSensorNumber () = 0; //returns number of sensors; recursivly adding of the number of sensors all subcomponents and the robots of all Subcomponents. 00158 00159 //virtual int getMotorNumber () = 0; //returns number of motors; recursivly adding of the number of sensors all subcomponents; at the moment only counts Hinge-, Slider-, Hinge2 and Universal-Joints; The Motor-Numbers of the robots of the Components is not counted. 00160 00161 virtual void update () = 0;//update the OSG notes here; update of the underlying robot or Primitive and recursive update of all Components in the Connection-vector 00162 00163 virtual void place (const Pos &pos) = 0;//sets the vehicle to position pos - desired position of the robot; the first component is seen as center of the robot, on which the position pos refers; also recursive place of all subComponents 00164 00165 virtual bool collisionCallback (void *data, dGeomID o1, dGeomID o2);// checks for internal collisions and treats them.; should do nothing, because there should not be any ode-objects belonging to the component, which are not handled elsewhere....and what is with Primitives? are they automaticaly handled? 00166 00167 virtual void doInternalStuff (const GlobalData &globalData);// this function is called in each timestep.; maybee usefull 00168 00169 // virtual void setColor (const Color &col); sets color of the robot; not nessecary 00170 00171 virtual Position getPosition () const = 0; //returns position of the object; relates to the robot or Primitive belonging to the component 00172 00173 /** 00174 *Gets the distance between two components. 00175 *@return distance in space 00176 **/ 00177 virtual double getDistanceToComponent ( Component* comp ); 00178 00179 //virtual Position getSpeed () const;//returns linear speed vector of the object; must be computed from all sub-robots 00180 00181 //virtual matrix::Matrix getOrientation () const;//returns the orientation of the object; 00182 00183 00184 /** 00185 *This is only a simple function, calculating the coordinates of the point exactly between two directly connected components. 00186 *@return Vector containing the Position 00187 *@param index number of the position 00188 **/ 00189 virtual osg::Vec3 getPositionbetweenComponents ( Component* component ); 00190 00191 /** 00192 *return reference to the simple Primitive, or to the main Primitive of the robot assigend to the component. If nothimng is assigned, NULL is returned. 00193 **/ 00194 virtual Primitive* getMainPrimitive () const = 0; 00195 00196 /** 00197 *Gets the Number of subcomponents of this component. 00198 *@return Number of subcomponents 00199 **/ 00200 virtual int getNumberSubcomponents (); 00201 00202 00203 /** 00204 *Gets the Number of all Subcomponents recursivly connected. 00205 *@return Number of subcomponents 00206 **/ 00207 virtual int getNumberSubcomponentsAll (); 00208 00209 /** 00210 *This method adds an existing Component as a subcomponent to this component 00211 *@param subcomponent to add 00212 *@param reference to external created joint, which connects both components 00213 *@param true if connection should be a softlink, false else 00214 **/ 00215 virtual void addSubcomponent ( Component* newsubcomponent , Joint* newconnectingjoint , bool softlink ); 00216 00217 /** 00218 *This method removes an existing Component as a subcomponent of this component. This also removes all Subcomponents of the subcomponent. 00219 *@param subcomponent number to remove 00220 *@return reference to the removed subcomponent, so that it could be used to do other things 00221 **/ 00222 virtual Component* removeSubcomponent ( int removedsubcomponentnumber ); 00223 00224 /** 00225 *This method removes an existing Component as a subcomponent of this component. This also removes all Subcomponents of the subcomponent. 00226 *@param subcomponent to remove 00227 *@return reference to the removed subcomponent, so that it could be used to do other things 00228 **/ 00229 virtual Component* removeSubcomponent ( Component* removedsubcomponent ); 00230 00231 /** 00232 *This removes all subcomponents of THIS component, and all their subcomponents, till the whole structure is destroyed. 00233 **/ 00234 virtual void removeAllSubcomponentsRecursive (); 00235 00236 /** 00237 *This updates the origin references within the component tree. If this is a removed subcomponent for examble, then parent should be this itself, so it is the top of the tree. 00238 *@param the component wich is the top of the tree structure, could also be this component itself 00239 **/ 00240 virtual void updateOriginsRecursive ( Component* parent ); 00241 00242 /** 00243 *This removes all softlinks of the structure that has THIS as his origin. 00244 **/ 00245 virtual void removeSoftlinksRecursive (); 00246 00247 00248 00249 /** 00250 *This method looks if a special component is a subcomponent of this component. Only direct subcomponents are registrated. 00251 *Components which are only connected by softlink are a connection here as well. 00252 *@param the component, which could be a subcomponent of this component 00253 *@return true if it is a subcomponent, false if not 00254 **/ 00255 virtual bool hasSubcomponent ( Component* subcomp ); 00256 00257 00258 /** 00259 *This method looks if a special component is a subcomponent of this component and all its recursive subcomponents. 00260 *Softlinks count as connected, but are not recursivly counted. 00261 *@param the component, which could be a subcomponent 00262 *@return true if it is a subcomponent, false if not 00263 **/ 00264 //virtual bool hasSubcomponentAll ( Component* subcomp ); 00265 00266 00267 /** 00268 *This method looks if a special component somehow connected to this component. 00269 *@param the component, which could be connected 00270 *@return true if it is connected, false if not 00271 **/ 00272 virtual bool isComponentConnected ( Component* connectedComp ); 00273 00274 /** 00275 *This garants an direct access to the connections between the components. Be carefull with using the given references. 00276 *@param the number of the connection 00277 *@return the reference of connection element 00278 **/ 00279 virtual componentConnection* getConnection ( unsigned int connectionnumber ); 00280 00281 /** 00282 *This returns the connection that connects to the target component. 00283 *@param the reference to the component which is subcomponent in the searched connection 00284 *@return the reference of connection element 00285 **/ 00286 virtual componentConnection* getConnection ( Component* targetcomponent ); 00287 00288 /** 00289 *Sets the connection between the component and one of its subcomponents to be a softlink. That means that the recusion for this branch stops here. 00290 *@param number of the subcomponent in the subcomponent list of the component 00291 *@param true = connection becomes a softlink 00292 **/ 00293 virtual bool setSoftlink ( unsigned int position , bool state ); 00294 00295 /** 00296 *This divides the component structure, following this component into two seperate component structures 00297 *@param the relation between the two new component structures, if it is 1/2 the structures would have the same number of components or only have a difference by one if the whole structures has an odd number of components 00298 *@param maximum size of the whole Component structure of the first call of this function 00299 *@param the best component for dividing at the moment of calling this function; it should be NULL for the first call of the function, so that no best component is set up till now 00300 *@return the best Componet for dividing at the moment the function is finished 00301 */ 00302 virtual Component* getBestDivideComponent ( double targetrelation , int maxsize , Component* currentBestDivideComponent ); 00303 00304 00305 00306 }; 00307 00308 00309 } 00310 #endif