IRobotSim.H

00001 /*!@file Robots/IRobot/IRobotSim.C IRobot Simulator */
00002 
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00006 // University of Southern California (USC) and the iLab at USC.         //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/IRobot/IRobotSim.H $
00036 // $Id: IRobotSim.H 10794 2009-02-08 06:21:09Z itti $
00037 //
00038 
00039 #ifndef IRobotSim_H_DEFINED
00040 #define IRobotSim_H_DEFINED
00041 
00042 #include "Component/ModelComponent.H"
00043 #include "Component/ModelParam.H"
00044 #include "Image/Image.H"
00045 #include "Image/Pixels.H"
00046 #include "GUI/ViewPort.H"
00047 #include "GUI/XWinManaged.H"
00048 #include "Util/Types.H"
00049 #include <deque>
00050 #include <pthread.h>
00051 #include <stdarg.h>
00052 #include <ode/ode.h>
00053 #include <ode/collision.h>
00054 
00055 #include <vector>
00056 
00057 class IRobotSim : public ModelComponent
00058 {
00059 public:
00060 
00061 #define ROBOT_BITFIELD 1
00062 #define GROUND_BITFIELD 2
00063 #define OBJ_BITFIELD 4
00064 
00065   enum OBJECT_TYPE {BOX, CYLINDER, SPHERE, TREE, DS, TOBJ};
00066   typedef struct{
00067     dBodyID body;
00068     dGeomID geom;
00069     OBJECT_TYPE type;
00070     float color[3];
00071     ViewPort::TEXTURES texture;
00072     ViewPort::DSObject object;
00073     Texture *texturePtr;
00074     int textureID;
00075   } Object;
00076 
00077   //Constructor
00078   IRobotSim(OptionManager& mgr,
00079           const std::string& descrName = "IRobotSim",
00080           const std::string& tagName = "IRobotSim",
00081           bool showWorld=false);
00082   ~IRobotSim();
00083 
00084   //###########################################################
00085   void start2();
00086   void handleWinEvents(XEvent& event);
00087 
00088   void simLoop();
00089   void startSim(void);
00090   void updateSensors();
00091   Image<PixRGB<byte> > getFrame(int camera);
00092 
00093   void getSensors(float &xPos, float &yPos, float &ori);
00094 
00095   void setMotors(double rightSpeed, double leftSpeed);
00096 
00097   dWorldID        getWorld() { return world; }
00098   dJointGroupID getContactgroup() { return contactgroup; }
00099 
00100   void addObject(OBJECT_TYPE objType,double initPos[3],double objSize[3],
00101       bool addToSpace = true,
00102       const char* texture=NULL);
00103 
00104  // void nearCallback (void *data, dGeomID o1, dGeomID o2);
00105  //
00106 
00107   float getXPos() { return itsXPos; }
00108   float getYPos() { return itsYPos; }
00109   float getOri() { return itsOri; }
00110 
00111   //INitalize the viewport; This needs to be called from the same thread that callsed getFrame
00112   void initViewport();
00113 
00114 
00115   dGeomID getCasterGeom() { return itsCasterGeom; }
00116   dGeomID getRobotGeom() { return itsRobotGeom; }
00117   dGeomID getGroundGeom() { return ground; }
00118 
00119 private:
00120 
00121   void makeRobot();
00122   void drawRobot();
00123 
00124   //World
00125   void drawWorld();
00126   void makeWorld();
00127 
00128   dWorldID        world;
00129   dSpaceID        space;
00130   dGeomID          ground;
00131   dJointGroupID contactgroup;
00132 
00133   float   itsRobotStartZ;
00134   dBodyID itsRobotBody;
00135   dGeomID itsRobotGeom;
00136 
00137   dBodyID itsRightWheelBody;
00138   dGeomID itsRightWheelGeom;
00139   dJointID itsRightWheelJoint;
00140 
00141   dBodyID  itsLeftWheelBody;
00142   dGeomID  itsLeftWheelGeom;
00143   dJointID itsLeftWheelJoint;
00144 
00145   dBodyID  itsCasterBody;
00146   dGeomID  itsCasterGeom;
00147 
00148   double itsRobotRadius;
00149   double itsRobotHeight;
00150   double itsRobotWeight;
00151   double itsRobotWheelRadius;
00152 
00153   ViewPort *itsVP;
00154 
00155   double itsRightWheelSpeed;
00156   double itsLeftWheelSpeed;
00157 
00158   //sensors
00159   double itsXPos;
00160   double itsYPos;
00161   double itsOri;
00162   double itsRightEncoder;
00163   double itsLeftEncoder;
00164   int itsPrevRightWheelAng;
00165   int itsPrevLeftWheelAng;
00166 
00167   bool itsWorldView;
00168   bool itsShowWorld;
00169   XWinManaged *itsWorldDisp;
00170 
00171   std::vector<Object> itsObjects;
00172 
00173   pthread_mutex_t itsDispLock;
00174 };
00175 
00176 #endif
00177 
00178 // ######################################################################
00179 /* So things look consistent in everyone's emacs... */
00180 /* Local Variables: */
00181 /* indent-tabs-mode: nil */
00182 /* End: */
Generated on Sun May 8 08:41:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3