Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

robot/sim/SimulatedBasicEnvironment

Go to the documentation of this file.
00001 /* **-*-c++-*-**************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008         
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013         
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018   $Id: SimulatedBasicEnvironment 1033 2004-02-11 20:47:52Z jungd $
00019   $Revision: 1.10 $
00020   $Date: 2004-02-11 15:47:52 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #ifndef _ROBOT_SIM_SIMULATEDBASICENVIRONMENT_
00026 #define _ROBOT_SIM_SIMULATEDBASICENVIRONMENT_
00027 
00028 #include <robot/sim/sim>
00029 
00030 #include <robot/sim/BasicEnvironment>
00031 
00032 #include <osg/Group>
00033 
00034 #include <base/Externalizable>
00035 #include <base/Simulatable>
00036 #include <base/Orient>
00037 #include <base/Trajectory>
00038 
00039 #include <gfx/Color3>
00040 #include <gfx/OSGWorld>
00041 
00042 #include <physics/Shape>
00043 #include <physics/SolidSystem>
00044 #include <physics/CollisionCuller>
00045 
00046 #include <robot/sim/SimulatedRobotDescription>
00047 #include <robot/sim/SimulatedRobot>
00048 #include <robot/sim/SimulatedTool>
00049 
00050 
00051 
00052 namespace robot {
00053 namespace sim {
00054 
00055 /**
00056  * A basic 'no-frills' simulated environment for robots
00057  *  Robot instances added must be of type SimulatedRobot.
00058  */
00059 class SimulatedBasicEnvironment : public robot::sim::BasicEnvironment, public gfx::OSGWorld
00060 {
00061 public:
00062   SimulatedBasicEnvironment(ref<base::VFileSystem> fs, ref<base::Cache> cache, const String& name = "", bool dynamic=true);
00063   SimulatedBasicEnvironment(const SimulatedBasicEnvironment& e);
00064 
00065   virtual void setDynamic(bool enabled);
00066   
00067   virtual String className() const { return String("SimulatedBasicEnvironment"); }
00068   
00069   virtual Object& clone() const { return *(NewObj SimulatedBasicEnvironment(*this)); }
00070 
00071 
00072   // factory  
00073   virtual ref<RobotDescription> newRobotDescription() const 
00074     { return ref<RobotDescription>(NewObj SimulatedRobotDescription()); }
00075 
00076   virtual ref<ToolDescription> newToolDescription() const
00077     { return ref<ToolDescription>(NewObj ToolDescription()); }
00078 
00079   // methods to add/remove robots
00080   virtual ref<Robot> addRobot(ref<const robot::RobotDescription> robotDescription,
00081                               const base::Point3& position, 
00082                               const base::Orient& orientation,
00083                               bool anchored = false);
00084    
00085   virtual void removeRobot(ref<robot::Robot> robot);
00086 
00087   virtual Int numRobots() const { return robots.size(); }
00088   virtual ref<Robot> getRobot(Int i) { return base::elementAt(robots,i); }
00089 
00090 
00091   // methods to add/remove tools
00092 
00093   virtual ref<Tool> addTool(ref<const robot::ToolDescription> toolDescription,
00094                             const base::Point3& position, 
00095                             const base::Orient& orientation);
00096 
00097   virtual void removeTool(ref<Tool> tool);
00098 
00099   virtual Int numTools() const { return tools.size(); };
00100   virtual ref<Tool> getTool(Int i) { return base::elementAt(tools,i); }
00101   virtual ref<const Tool> getTool(Int i) const { return base::elementAt(tools,i); }
00102 
00103   /// place the specified tool within grasping range of the end-effector of the specified robot manipulator
00104   virtual void placeToolInProximity(ref<Tool> tool, ref<Robot> robot, Int manipulatorIndex=0);
00105 
00106 
00107   // methods to add/remove simple objects to the environment
00108   
00109   virtual ref<BasicEnvironment::Obstacle> addBoxObstacle(base::Dimension3 dim, 
00110                                                          const base::Point3& position, 
00111                                                          const base::Orient& orientation,
00112                                                          const String& name = "");
00113   
00114   virtual ref<BasicEnvironment::Obstacle> addSphereObstacle(Real radius,
00115                                                             const base::Point3& position, 
00116                                                             const base::Orient& orientation,
00117                                                             const String& name = "");
00118 
00119   virtual ref<BasicEnvironment::Obstacle> addBoxObstacle(base::Dimension3 dim, 
00120                                                          const base::Point3& position, 
00121                                                          const base::Orient& orientation,
00122                                                          ref<const physics::Material> material,
00123                                                          const String& name = "");
00124   
00125   virtual ref<BasicEnvironment::Obstacle> addSphereObstacle(Real radius,
00126                                                             const base::Point3& position, 
00127                                                             const base::Orient& orientation,
00128                                                             ref<const physics::Material> material,
00129                                                             const String& name = "");
00130 
00131   virtual void removeObstacle(ref<BasicEnvironment::Obstacle> obstacle);
00132 
00133 
00134   virtual void setObstacleColor(ref<BasicEnvironment::Obstacle> obstacle, const gfx::Color3& color);
00135   virtual void setObstacleDensity(ref<BasicEnvironment::Obstacle> obstacle, Real density);
00136   
00137   virtual Int numObstacles() const { return obstacles.size(); }
00138   virtual ref<Obstacle> getObstacle(Int i) { return base::elementAt(obstacles,i); }
00139   virtual ref<const Obstacle> getObstacle(Int i) const { return base::elementAt(obstacles,i); }
00140 
00141 
00142   // OSGWorld (Visual)
00143   virtual osg::Node* createOSGVisual(Visual::Attributes visualAttributes=0) const;
00144 
00145   // Simulatable
00146   virtual void preSimulate();
00147   virtual void simulateForSimTime(const base::Time& dt);
00148 
00149   // Externalizable
00150   virtual bool formatSupported(const String format, Real version = 1.0, ExternalizationType type = IO) const; ///< query if specific format is supported (for input, output or both)
00151   virtual void externalize(base::Externalizer& e, const String format = "", Real version = 1.0); ///< read or write object state to Externalizer
00152 
00153 protected:
00154 
00155   class SolidObstacle : public Obstacle
00156   {
00157   public:
00158     SolidObstacle(base::Dimension3 dims,
00159              const base::Point3& position, 
00160              const base::Orient& orientation,
00161              ref<physics::Solid> solid)
00162       : Obstacle(dims, position, orientation), 
00163         solid(solid) {}
00164     
00165     SolidObstacle(Real radius,
00166              const base::Point3& position, 
00167              const base::Orient& orientation,
00168              ref<physics::Solid> solid)
00169       : Obstacle(radius, position, orientation),
00170         solid(solid) {}
00171 
00172     virtual String className() const { return String("SolidObstacle"); }
00173     
00174     ref<physics::Solid> solid;
00175   };
00176 
00177 
00178   class SolidTool : public Tool
00179   {
00180   public:
00181     SolidTool(String name, ref<const robot::ToolDescription> toolDescription,
00182               const base::Point3& position, 
00183               const base::Orient& orientation, 
00184               ref<SimulatedTool> simTool)
00185       : Tool(name, toolDescription, position, orientation),
00186         simTool(simTool) {}
00187     
00188     virtual String className() const { return String("SolidTool"); }
00189 
00190     ref<SimulatedTool> simTool;
00191   };
00192 
00193 
00194   void construct();
00195 
00196   ref<physics::Solid> findObstacle(ref<Obstacle> obstacle);
00197 
00198   /// true for a dynamic simulation, fasle for static (no force/torque or velocity control, just position control)
00199   bool dynamic;
00200 
00201   ref<physics::SolidSystem>       system;            ///< SolidSystem for simulating physical bodies in this environment
00202   ref<physics::CollidableGroup>   collidables;       ///< Collidables for items in the environment
00203   ref<physics::Collidable>        groundCollidable;  ///< Collidable for the ground
00204   ref<physics::CollisionCuller>   collisionCuller;   ///< CollisionCuller registering which Collidables shouldn't be collided
00205   ref<physics::Solid>             ground;            ///< Solid ground (flat)
00206   ref<physics::ConstraintGroup>   cgroup;            ///< constraints (e.g. for fixing robots to the ground)
00207 
00208   typedef reflist<robot::sim::SimulatedRobot> RobotList;
00209   typedef std::list<bool> RobotAnchoredList;
00210   typedef reflist<Tool> ToolList;
00211   typedef reflist<SolidObstacle> ObstacleList;
00212 
00213   RobotList    robots;    ///< list of robots in the environment
00214   ToolList     tools;     ///< list of tools in the environment
00215   ObstacleList obstacles; ///< list of Solid obstacles in the environment
00216   RobotAnchoredList robotsAnchored; ///< for each robot, is it anchored to the environment?
00217   
00218   mutable Visual::Attributes attributes;
00219   mutable osg::ref_ptr<osg::Group> rootnode;
00220 
00221   /// check each tool for proximity with end-effectors of each robot
00222   void checkTools();
00223 };
00224 
00225 
00226 }
00227 } // robot::sim
00228 
00229 #endif

Generated on Thu Jul 29 15:56:39 2004 for OpenSim by doxygen 1.3.6