component.h

Go to the documentation of this file.
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.14  2008/09/16 14:53:40  martius
00025  *   use cmath instead of math.h
00026  *
00027  *   Revision 1.13  2006/11/30 08:51:39  robot8
00028  *   -update of the evolution projekt
00029  *   -fitness changed
00030  *   -replication changed
00031  *   -added copy function
00032  *
00033  *   Revision 1.12  2006/10/20 13:52:33  robot8
00034  *   -update of the evolution projekt
00035  *   -only changed some parameter
00036  *
00037  *   Revision 1.11  2006/09/11 12:01:31  martius
00038  *   *** empty log message ***
00039  *
00040  *                                                                         *
00041  ***************************************************************************/
00042 
00043 #ifndef _component_h
00044 #define _component_h
00045 
00046 #include <typeinfo>
00047 #include <vector>
00048 #include <list>
00049 
00050 #include <ode/ode.h>
00051 #include <cmath>
00052 
00053 #include "oderobot.h"
00054 #include "joint.h"
00055 
00056 namespace lpzrobots
00057 {
00058   typedef struct _ComponentConf
00059   {
00060     bool completesensormode; //if true, the controler of the Component also gets the sensor values of the robot of the component
00061     bool completemotormode; //if true, the controler of the component also controls the robot of the component
00062     double max_force;
00063     double speed;
00064   } ComponentConf;
00065 
00066   /**
00067    * This is the abstract base class of the component system. A component includes the routines for managing sensor and motor values from a controller.
00068    * With a bunch of components it is possible to create a tree like structure. There is always a root structure.
00069    * 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.
00070    * Between the differnt objects of the component class could exist connections, which are physical represented by Ode-Joints.
00071    * The components use the motor values they get, to control the joints between them.
00072    * From these joints sensor values are read out, which where send with the getSensor () function like a normal robot does.
00073    * But the arrray of sensor values comes from all components within the structure beneth the component, from which the function was called.
00074    * 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.
00075    * These shells build a connection between the physical parts and control these connections, which could be also physical existend.
00076    */
00077   class Component : public OdeRobot
00078     {
00079     public:
00080       ComponentConf conf;
00081 
00082     public:
00083 
00084       /**
00085        *This is the structure of one connection between two components.
00086        **/
00087       typedef struct
00088       {
00089         Component* subcomponent;
00090         Joint* joint;
00091         bool softlink; //if true the connection ends the recursion, false = normal
00092         void *data;
00093       } componentConnection;
00094 
00095       std::vector <componentConnection> connection;
00096       std::vector <Component*> backwardreference;
00097 
00098     public: 
00099       Component* originComponent;
00100       Component* directOriginComponent;
00101 
00102     public:
00103 
00104       Component ( const OdeHandle &odeHandle, const OsgHandle &osgHandle, const ComponentConf& conf);
00105     
00106       ~Component ();
00107 
00108       static ComponentConf getDefaultConf()
00109         {
00110           ComponentConf conf;
00111           conf.completesensormode = true;
00112           conf.completemotormode = false;
00113           conf.max_force = 1;
00114           conf.speed = 1;
00115           return conf;
00116         }
00117 
00118     public:
00119 
00120       /**
00121        *Returns number of sensors; recursivly adding of the number of sensors all subcomponents and the robots of all Subcomponents.
00122        *@return number of sensors of the component and all its subcomponents
00123        **/
00124       virtual int       getSensorNumber ();
00125 
00126       /**
00127        *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.
00128        *@return number of motors of the component and all its subcomponents
00129        **/
00130       virtual int       getMotorNumber (); 
00131 
00132       /**
00133        *Use this, to get all sensor values of all the joints of all subcomponents, and the sensors of all robots, belonging to all subcompionents.
00134        *The sensor values have the following sequence:
00135        *values of the component connecting joints, values of the robot of the component,
00136        *values of component connecting joints of the first subcomponent, values of the robot of the first subcomponent, ...
00137        *@robot sensor values of the connecting joints of this component and all subcomponents
00138        **/
00139       virtual int       getSensors (sensor *sensors, int sensornumber); //returns actual sensorvalues; only for the connecting joints
00140 
00141 
00142       /**
00143        *Sets the motor values for the joints connecting the component with its subcomponents, an recursivly the joints of all subComponents.
00144        *The motors of all robots of the subcomponents is not set.
00145        *@param 
00146        **/
00147       virtual void      setMotors (const motor *motors, int motornumber); //sets actual motorcommands; only for the connecting joints
00148 
00149       /**
00150        *This sets all motors in the component structure to zero, starting from this component.
00151        **/
00152       virtual void    resetMotorsRecursive ( );
00153 
00154       //virtual int     getSensorNumber () = 0; //returns number of sensors; recursivly adding of the number of sensors all subcomponents and the robots of all Subcomponents.
00155 
00156       //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.
00157 
00158       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
00159 
00160       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
00161 
00162       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?
00163 
00164       virtual void      doInternalStuff (const GlobalData &globalData);// this function is called in each timestep.; maybee usefull
00165 
00166       // virtual void   setColor (const Color &col);    sets color of the robot; not nessecary
00167 
00168       virtual Position getPosition () const = 0; //returns position of the object; relates to the robot or Primitive belonging to the component
00169 
00170       /**
00171        *Gets the distance between two components.
00172        *@return distance in space
00173        **/
00174       virtual double getDistanceToComponent ( Component* comp );
00175 
00176       //virtual Position getSpeed () const;//returns linear speed vector of the object; must be computed from all sub-robots
00177 
00178       //virtual matrix::Matrix  getOrientation () const;//returns the orientation of the object;
00179 
00180 
00181       /**
00182        *This is only a simple function, calculating the coordinates of the point exactly between two directly connected components.
00183        *@return Vector containing the Position
00184        *@param index number of the position
00185        **/
00186       virtual osg::Vec3 getPositionbetweenComponents ( Component* component );
00187 
00188       /**
00189        *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.
00190        **/
00191       virtual Primitive* getMainPrimitive () const = 0;
00192 
00193       /**
00194        *Gets the Number of subcomponents of this component.
00195        *@return Number of subcomponents
00196        **/
00197       virtual int getNumberSubcomponents ();
00198 
00199     
00200       /**
00201        *Gets the Number of all Subcomponents recursivly connected.
00202        *@return Number of subcomponents
00203        **/
00204       virtual int getNumberSubcomponentsAll ();
00205 
00206       /**
00207        *This method adds an existing Component as a subcomponent to this component
00208        *@param subcomponent to add
00209        *@param reference to external created joint, which connects both components
00210        *@param true if connection should be a softlink, false else
00211        **/
00212       virtual void addSubcomponent ( Component* newsubcomponent , Joint* newconnectingjoint , bool softlink );
00213 
00214       /**
00215        *This method removes an existing Component as a subcomponent of this component. This also removes all Subcomponents of the subcomponent.
00216        *@param subcomponent number to remove
00217        *@return reference to the removed subcomponent, so that it could be used to do other things
00218        **/
00219       virtual Component* removeSubcomponent ( int removedsubcomponentnumber );
00220 
00221       /**
00222        *This method removes an existing Component as a subcomponent of this component. This also removes all Subcomponents of the subcomponent.
00223        *@param subcomponent to remove
00224        *@return reference to the removed subcomponent, so that it could be used to do other things
00225        **/
00226       virtual Component* removeSubcomponent ( Component* removedsubcomponent );
00227 
00228       /**
00229        *This removes all subcomponents of THIS component, and all their subcomponents, till the whole structure is destroyed.
00230        **/
00231       virtual void removeAllSubcomponentsRecursive ();
00232 
00233       /**
00234        *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.
00235        *@param the component wich is the top of the tree structure, could also be this component itself
00236        **/
00237       virtual void updateOriginsRecursive ( Component* parent );
00238 
00239       /**
00240        *This removes all softlinks of the structure that has THIS as his origin.
00241        **/
00242       virtual void removeSoftlinksRecursive ();
00243 
00244 
00245 
00246       /**
00247        *This method looks if a special component is a subcomponent of this component. Only direct subcomponents are registrated.
00248        *Components which are only connected by softlink are a connection here as well.
00249        *@param the component, which could be a subcomponent of this component
00250        *@return true if it is a subcomponent, false if not
00251        **/
00252       virtual bool hasSubcomponent ( Component* subcomp );
00253 
00254 
00255       /**
00256        *This method looks if a special component is a subcomponent of this component and all its recursive subcomponents.
00257        *Softlinks count as connected, but are not recursivly counted.
00258        *@param the component, which could be a subcomponent
00259        *@return true if it is a subcomponent, false if not
00260        **/
00261       //virtual bool hasSubcomponentAll ( Component* subcomp );
00262 
00263 
00264       /**
00265        *This method looks if a special component somehow connected to this component.
00266        *@param the component, which could be connected
00267        *@return true if it is connected, false if not
00268        **/
00269       virtual bool isComponentConnected ( Component* connectedComp );
00270 
00271       /**
00272        *This garants an direct access to the connections between the components. Be carefull with using the given references. 
00273        *@param the number of the connection
00274        *@return the reference of connection element
00275        **/
00276       virtual componentConnection* getConnection ( unsigned int connectionnumber );
00277 
00278       /**
00279        *This returns the connection that connects to the target component.
00280        *@param the reference to the component which is subcomponent in the searched connection
00281        *@return the reference of connection element
00282        **/
00283       virtual componentConnection* getConnection ( Component* targetcomponent );
00284 
00285       /**
00286        *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.
00287        *@param number of the subcomponent in the subcomponent list of the component
00288        *@param true = connection becomes a softlink
00289        **/
00290       virtual bool setSoftlink ( unsigned int position , bool state );
00291 
00292       /**
00293        *This divides the component structure, following this component into two seperate component structures
00294        *@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
00295        *@param maximum size of the whole Component structure of the first call of this function
00296        *@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
00297        *@return the best Componet for dividing at the moment the function is finished
00298        */
00299       virtual Component* getBestDivideComponent ( double targetrelation , int maxsize , Component* currentBestDivideComponent );
00300 
00301 
00302      
00303     };
00304 
00305 
00306 }
00307 #endif

Generated on Fri Oct 30 16:29:01 2009 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.4.7