00001 #include "Component/ModelComponent.H" 00002 #include "Component/ModelParam.H" 00003 #include "Media/FrameSeries.H" 00004 #include "Transport/FrameInfo.H" 00005 #include "Raster/GenericFrame.H" 00006 #include "Image/Image.H" 00007 #include "Image/DrawOps.H" 00008 #include "Image/Point3D.H" 00009 #include "GUI/XWinManaged.H" 00010 #include "GUI/ImageDisplayStream.H" 00011 #include "Robots/RobotBrain/RobotBrainComponent.H" 00012 #include "Util/Timer.H" 00013 #include "GUI/SimpleMeter.H" 00014 00015 #include "Ice/RobotBrainObjects.ice.H" 00016 #include "Ice/RobotSimEvents.ice.H" 00017 #include <IceUtil/Thread.h> 00018 00019 #include <vector> 00020 #include <deque> 00021 #include <list> 00022 00023 #include "Devices/Serial.H" 00024 #include "Controllers/PID.H" 00025 00026 00027 typedef struct { 00028 double dist,angle; 00029 } sonarData; 00030 00031 typedef struct{ 00032 float time,transVel,rotVel,encoderX,encoderY,encoderOri,rcTransVel,rcRotVel; 00033 int rcMode; 00034 } motorData; 00035 typedef struct{ 00036 float time; 00037 std::string imagePath ; 00038 } imageData; 00039 //typedef struct{ 00040 // float x; 00041 // float y; 00042 // float z; 00043 // 00044 //}Point3DwithColor; 00045 template<typename T> 00046 class Point3DwithColor 00047 { 00048 public: 00049 //Point3DwithColor(const Point3DwithColor&); 00050 //Point3DwithColor& operator=(const Point3DwithColor&); 00051 T x,y,z; 00052 PixRGB<byte> color; 00053 Point3DwithColor(const T xx,const T yy, const T zz) 00054 { 00055 x = xx;y=yy;z=zz; 00056 color = PixRGB<byte>(0,0,0); 00057 } 00058 00059 ~Point3DwithColor(){}; 00060 }; 00061 template<typename T> 00062 class velocityQue{ 00063 // Prevent copy and assignment 00064 velocityQue(const velocityQue&); 00065 velocityQue& operator=(const velocityQue&); 00066 //Max size of data 00067 int m_size; 00068 //Current size total 00069 int m_total; 00070 std::deque<T> m_data; 00071 00072 public: 00073 velocityQue():m_size(0) 00074 { 00075 } 00076 velocityQue(int n):m_size(0) 00077 { 00078 m_size = n; 00079 } 00080 void add(const T& data){ 00081 // if the queue is full yet, we pop the oldest data out 00082 if(m_total >= m_size) 00083 { 00084 m_data.pop_back(); 00085 }else{ 00086 m_total++; 00087 } 00088 m_data.push_front(data); 00089 } 00090 std::vector<T> getVector() 00091 { 00092 return std::vector<T>(m_data.begin(),m_data.end()); 00093 } 00094 ~velocityQue(){} 00095 }; 00096 00097 //! The BeoPilot module is responsible for actually driving the BeoBot2. All motor 00098 //! requests are serviced to hardware by this module through MotorRequest messages. 00099 class BeoPilot : public RobotBrainComponent 00100 { 00101 public: 00102 00103 BeoPilot(int id, OptionManager& mgr, 00104 const std::string& descrName = "BeoPilot", 00105 const std::string& tagName = "BeoPilot"); 00106 00107 ~BeoPilot(); 00108 00109 //! Evolve keeps the motor driver propeller up to date on the latest motor speed requests. 00110 //! If the evolve loop ever hangs for more than 1/4 second, then the propeller will shut down 00111 //! the motors until it recieves a new message. 00112 virtual void evolve(); 00113 00114 virtual void updateMessage(const RobotSimEvents::EventMessagePtr& eMsg, 00115 const Ice::Current&); 00116 00117 virtual void registerTopics(); 00118 00119 void checkLogFile(); 00120 //! Check the serial port to ensure that we are connected to a 'motorboard' propeller, 00121 //! and also check the sanity of our command line options 00122 00123 void loadFromLogFile(); 00124 void start3(); 00125 00126 //Draw the updated local debugging GUI 00127 void updateGUI(); 00128 00129 //Send the motor speeds ([-1 .. 1]) to the motordriver/propeller 00130 void SetMotors(float motor1speed, float motor2speed); 00131 00132 //Get the RC transmitter status 00133 unsigned char getRCStatus(); 00134 00135 //Get the status of an RC channel 00136 unsigned int getRCChannel(int channel); 00137 00138 //Get the Enabled status from RC 00139 unsigned char getRCEnabled(); 00140 00141 //Get the Mix speed status from MotorBoard 00142 unsigned int getRCSpeed(); 00143 00144 //Get all of the RC status data in one frame 00145 void UpdateRCStatus(); 00146 00147 //Reset the encoder value 00148 void resetEncoder(); 00149 00150 //Update position after get odometry data 00151 void UpdatePosition(void); 00152 00153 //Get Log Data from current time 00154 void getCurrentLogData(void); 00155 00156 //Redraw map base on the current scale 00157 void reDrawMap(double scale); 00158 00159 //Draw all point at map 00160 void drawAllMap(void); 00161 int file_exists(const char * fileName) 00162 { 00163 struct stat buf; 00164 int i = stat(fileName,&buf); 00165 if(i == 0) 00166 return 1; 00167 return 0; 00168 00169 00170 } 00171 private: 00172 nub::soft_ref<Serial> itsSerial; 00173 Timer itsSerialTimer; 00174 IceUtil::Mutex itsSerialMutex; 00175 00176 int itsCurrMessageID; 00177 00178 ////////////////////////////////////////// 00179 //Log Data Structures 00180 ////////////////////////////////////////// 00181 std::vector<motorData> itsLogData; 00182 std::vector<imageData> itsLogImageData; 00183 int itsCurrentTimeIndex; 00184 int itsCurrentTimeIndex2; 00185 Timer itsLogTimer; 00186 bool itsLoadFromLogFile; 00187 int itsLogRemoteMode; 00188 double itsLogLeftMotorVelocity; 00189 double itsLogRightMotorVelocity; 00190 //Point3D<double> itsLogPosition; 00191 Point3DwithColor<double> itsLogPosition; 00192 Point3DwithColor<double> itsLogDiffPosition; 00193 00194 00195 ////////////////////////////////////////// 00196 //Motor Data Structures 00197 ////////////////////////////////////////// 00198 IceUtil::Mutex itsSpeedMutex; 00199 double itsForwardSpeedReq; 00200 double itsRotationalSpeedReq; 00201 double itsForwardSpeed; 00202 double itsRotationalSpeed; 00203 00204 double itsRcTransSpeed; 00205 double itsRcRotSpeed; 00206 double itsRcTransSpeedCapped; 00207 double itsRcRotSpeedCapped; 00208 00209 double itsTransCap; 00210 double itsRotCap; 00211 00212 std::vector<int> itsRCChannels; 00213 int itsEmergencyMode; 00214 int itsRemoteMode; 00215 float itsMotor1Speed; 00216 float itsMotor2Speed; 00217 int itsLeftEncoder; 00218 int itsRightEncoder; 00219 int itsLastLeftEncoder; 00220 int itsLastRightEncoder; 00221 Point3DwithColor<double> itsPosition; 00222 Point3DwithColor<double> itsDiffPosition; 00223 Image<PixRGB<byte> > itsMapImage; 00224 Timer itsEncoderTimer; 00225 double itsLeftMotorVelocity; 00226 double itsRightMotorVelocity; 00227 00228 PID<float> itsPidLeft; 00229 PID<float> itsPidRight; 00230 00231 ////////////////////////////////////////// 00232 //Sonar Data Structures 00233 ////////////////////////////////////////// 00234 IceUtil::Mutex itsSonarMutex; 00235 std::vector<double> itsSonarReadings; 00236 std::vector<double> itsSonarAngles; 00237 Timer itsSonarTimer; 00238 bool itsSonarProxAlert; // Are we in a sonar proximity alert state? 00239 float itsSonarTimeout; // The number of seconds allowed to elapse between 00240 //sonar readings before we kill the motors 00241 00242 ////////////////////////////////////////// 00243 //Display Data Structures 00244 ////////////////////////////////////////// 00245 Image<PixRGB<byte> > itsControlImage; 00246 Image<PixRGB<byte> > itsDispImage; 00247 Image<PixRGB<byte> > itsDispCameraImage; 00248 XWinManaged itsMeterXwin; 00249 // XWinManaged itsMapXwin; 00250 Timer itsDisplayTimer; 00251 float itsDisplayUpdateRate; 00252 float itsMapScale; 00253 std::vector<SimpleMeter> itsChannelMeters; 00254 velocityQue<float> itsVelocityQue; 00255 velocityQue<float> itsVelocityTargetQue; 00256 std::vector<Point3DwithColor<double> >itsLogPositionMap; 00257 std::vector<Point3DwithColor<double> >itsPositionMap; 00258 ///////////////////////////////////////// 00259 //System Parameters 00260 ///////////////////////////////////////// 00261 OModelParam<float> itsMinimumSafeDistance; 00262 OModelParam<float> itsMaxSpeed; 00263 OModelParam<bool> itsIgnoreSonar; 00264 OModelParam<std::string> itsLogFile; 00265 OModelParam<int> itsReplaySpeed; 00266 00267 }; 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277