00001 #include "Component/ModelManager.H" 00002 #include "Component/OptionManager.H" 00003 #include "Component/ModelComponent.H" 00004 #include "Component/ModelParam.H" 00005 #include "Media/FrameSeries.H" 00006 #include "Transport/FrameInfo.H" 00007 #include "Raster/GenericFrame.H" 00008 #include "Image/Image.H" 00009 #include "GUI/XWinManaged.H" 00010 #include "GUI/ImageDisplayStream.H" 00011 #include "Util/Timer.H" 00012 #include "Image/Image.H" 00013 #include "Image/Pixels.H" 00014 #include "Robots/RobotBrain/RobotBrainComponent.H" 00015 #include "Ice/RobotBrainObjects.ice.H" 00016 #include "Ice/RobotSimEvents.ice.H" 00017 #include "GUI/ViewPort.H" 00018 #include "Ice/IceImageUtils.H" 00019 #include <IceUtil/Thread.h> 00020 #include <ode/ode.h> 00021 #include <ode/collision.h> 00022 #include "Controllers/PID.H" 00023 #include "Util/Angle.H" 00024 00025 class SeaBee3Simulator : public RobotBrainComponent 00026 { 00027 00028 public: 00029 SeaBee3Simulator(OptionManager &mgr, 00030 const std::string& descrName = "SeaBee3Simulator", 00031 const std::string& tagName = "SeaBee3Simulator"); 00032 00033 ~SeaBee3Simulator(); 00034 00035 virtual void evolve(); 00036 00037 virtual void updateMessage(const RobotSimEvents::EventMessagePtr& eMsg, 00038 const Ice::Current&); 00039 00040 virtual void registerTopics(); 00041 00042 //Get a camera image from one of our virtual cameras 00043 Image<PixRGB<byte> > getFrame(int camera); 00044 00045 dWorldID getWorld() { return world; } 00046 dJointGroupID getContactgroup() { return contactgroup; } 00047 00048 00049 void handleWinEvents(XEvent& event); 00050 00051 void updateSensors(const dReal *pos, const dReal *R); 00052 00053 void getSensors(float &xPos, float &yPos, float &depth, 00054 float &roll, float &pitch, float &yaw); 00055 00056 void simLoop(); 00057 00058 void setThrusters(double forwardLeftThruster, double forwardRightThruster, 00059 double verticalLeftThruster, double verticalRightThruster, 00060 double forwardStrafeThruster, double backStrafeThruster); 00061 00062 private: 00063 bool Initialized; 00064 //Generate the 3D model of the submarine 00065 void makeSub(); 00066 //Draw the submarine 00067 void drawSub(); 00068 00069 //arena 00070 void drawArena(); 00071 void drawWaterSurface(); 00072 void drawGate(const double *pos); 00073 void drawBuoy(const double *pos); 00074 void drawBin(const double ori, const double *pos); 00075 void drawPipeline(const double ori, const double *pos); 00076 void drawPinger(const double *pos); 00077 void drawPool(); 00078 00079 void applyHydrodynamicForces(dReal viscosity); 00080 00081 //Update the PID controllers if they are active, and set the 00082 //proper motor variables 00083 void updateControllers(); 00084 00085 dWorldID world; 00086 dSpaceID space; 00087 dGeomID ground; 00088 dJointGroupID contactgroup; 00089 00090 dBodyID itsSubBody; 00091 dGeomID itsSubGeom; 00092 00093 double itsPoolDepth; 00094 double itsPoolLength; 00095 double itsPoolWidth; 00096 00097 double itsWaterLevel; 00098 double itsSubLength; 00099 double itsSubRadius; 00100 double itsSubWeight; 00101 00102 ViewPort *vp; 00103 00104 double itsForwardLeftThrusterVal; 00105 double itsForwardRightThrusterVal; 00106 double itsFrontStrafeThrusterVal; 00107 double itsBackStrafeThrusterVal; 00108 double itsLeftVertThrusterVal; 00109 double itsRightVertThrusterVal; 00110 00111 00112 00113 bool itsWorldView; 00114 bool itsShowWorld; 00115 XWinManaged *itsWorldDisp; 00116 00117 pthread_mutex_t itsDispLock; 00118 00119 //A timer to keep track of the amount of time since the last 00120 //set of camera messages was sent out. New messages are only 00121 //sent out once the elapsed time surpasses itsCameraUpdateTime 00122 Timer itsCameraTimer; 00123 double itsCameraUpdateTime; 00124 00125 //A timer to keep track of the amount of time since the last 00126 //BeeStem message was sent out. A new message is only sent 00127 //out once the elapsed time surpasses itsBeeStemUpdateTime 00128 Timer itsBeeStemTimer; 00129 double itsBeeStemUpdateTime; 00130 00131 //Virtual BeeStem related variables 00132 double itsSimXPos; 00133 double itsSimYPos; 00134 double itsSimDepth; 00135 double itsSimRoll; 00136 double itsSimPitch; 00137 double itsSimYaw; 00138 00139 int itsDepth; 00140 int itsDesiredHeading; 00141 int itsDesiredDepth; 00142 int itsDesiredSpeed; 00143 int itsHeadingK; 00144 int itsHeadingP; 00145 int itsHeadingD; 00146 int itsHeadingI; 00147 int itsHeadingOutput; 00148 int itsDepthK; 00149 int itsDepthP; 00150 int itsDepthD; 00151 int itsDepthI; 00152 int itsDepthOutput; 00153 00154 bool itsPIDsEnabled; 00155 00156 PID<float> *itsDepthPID; 00157 int itsTargetDepth; 00158 00159 PID<Angle> *itsHeadingPID; 00160 int itsTargetHeading; 00161 00162 int itsTargetSpeed; 00163 00164 IceUtil::Mutex itsStemMutex; 00165 00166 Texture *arenaWallTexture; 00167 Texture *waterSurfaceTexture; 00168 00169 IceUtil::Mutex itsJSMutex; 00170 00171 std::vector<int> itsJSValues; 00172 std::vector<int> itsButValues; 00173 00174 }; 00175