00001
00010 #include "CeilingNavigation.h"
00011 #include "cv.h"
00012 #include "highgui.h"
00013
00014
00015
00016
00017 #ifdef _USE_SHOW_MAP
00018 #include "Common/sharedmem.h"
00019 robot_status *rstat = NULL;
00020 int count = 130;
00021 int delaycount=0;
00022 #endif
00023
00024 FILE *fp;
00025
00026
00027
00028 static const char* ceilingnavigation_spec[] =
00029 {
00030 "implementation_id", "CeilingNavigation",
00031 "type_name", "CeilingNavigation",
00032 "description", "CeilingNavigation",
00033 "version", "1.0.0",
00034 "vendor", "NAIST and FSI",
00035 "category", "Navigation",
00036 "activity_type", "PERIODIC",
00037 "kind", "DataFlowComponent",
00038 "max_instance", "1",
00039 "language", "C++",
00040 "lang_type", "compile",
00041
00042 #ifdef _OFFLINE_IMAGE
00043 "conf.default.NavigationMap", "./CeilingImage2_Small.BMP",
00044 "conf.default.OfflineImage", "./offline_image",
00045 "conf.default.X_POS", "367",
00046 "conf.default.Y_POS", "51",
00047 "conf.default.THETA", "-2.0",
00048 #else
00049 "conf.default.NavigationMap", "./rotRTCcenterCeiling.BMP",
00050 "conf.default.OfflineImage", "",
00051 "conf.default.X_POS", "88",
00052 "conf.default.Y_POS", "76",
00053 "conf.default.THETA", "0",
00054
00055 #endif
00056 "conf.default.BlockResolution", "1",
00057 "conf.default.BlockSize", "96",
00058 "conf.default.BlockCoefficient", "0.414",
00059 "conf.default.SearchScope", "2",
00060 "conf.default.BlackWhiteValue", "220",
00061 "conf.default.Center_X", "160",
00062 "conf.default.Center_Y", "120",
00063 "conf.default.RealMapHeight", "100.0",
00064 "conf.default.RealMapWidth", "100.0",
00065 "conf.default.VirtualMapHeight", "100.0",
00066 "conf.default.VirtualMapWidth", "100.0",
00067 ""
00068 };
00069
00070
00071 CeilingNavigation::CeilingNavigation(RTC::Manager* manager)
00072
00073 : RTC::DataFlowComponentBase(manager),
00074 m_CameraDataIn("CameraData", m_CameraData),
00075 m_LocalizedPositionIn("LocalizedPosition", m_LocalizedPosition),
00076 m_CeilingPositionOut("CeilingPosition", m_CeilingPosition),
00077
00078 dummy(0)
00079 {
00080
00081
00082
00083 registerInPort("CameraData", m_CameraDataIn);
00084 registerInPort("LocalizedPosition", m_LocalizedPositionIn);
00085
00086
00087 registerOutPort("CeilingPosition", m_CeilingPositionOut);
00088
00089 }
00090
00091 CeilingNavigation::~CeilingNavigation()
00092 {
00093 }
00094
00095
00096
00097 RTC::ReturnCode_t CeilingNavigation::onInitialize()
00098 {
00099
00100
00101 #ifdef _OFFLINE_IMAGE
00102 bindParameter("NavigationMap", m_NavigationMap, "./CeilingImage2_Small.BMP");
00103 bindParameter("OfflineImage", m_OfflineImage, "./offline_image");
00104 bindParameter("X_POS", x_pos, "367");
00105 bindParameter("Y_POS", y_pos, "51");
00106 bindParameter("THETA", theta, "-2.0");
00107 #else
00108 bindParameter("NavigationMap", m_NavigationMap, "./rotRTCcenterCeiling.BMP");
00109 bindParameter("OfflineImage", m_OfflineImage, "");
00110 bindParameter("X_POS", x_pos, "88");
00111 bindParameter("Y_POS", y_pos, "76");
00112 bindParameter("THETA", theta, "0");
00113 #endif
00114 bindParameter("BlockResolution", m_BlockResolution, "1");
00115 bindParameter("BlockSize", m_BlockSize, "96");
00116 bindParameter("BlockCoefficient", m_BlockCoefficient, "0.414");
00117 bindParameter("SearchScope", m_SearchScope, "2");
00118 bindParameter("BlackWhiteValue", m_BlackWhiteValue, "220");
00119 bindParameter("Center_X", m_Center_X, "160");
00120 bindParameter("Center_Y", m_Center_Y, "120");
00121 bindParameter("RealMapHeight",m_RealMapHeight,"100.0");
00122 bindParameter("RealMapWidth",m_RealMapWidth,"100.0");
00123 bindParameter("VirtualMapHeight",m_VirtualMapHeight,"100.0");
00124 bindParameter("VirtualMapWidth",m_VirtualMapWidth,"100.0");
00125
00126
00127
00128 #ifdef _USE_SHOW_MAP
00129 rstat = (robot_status *)InitSharedMem(SHM_KEY, sizeof(robot_status));
00130 #endif
00131
00132
00133 mPerPix = 0.05;
00134 camX = 0.05;
00135 camY = -0.17;
00136
00137 fp = fopen("trajectory.dat", "w");
00138
00139 return RTC::RTC_OK;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 RTC::ReturnCode_t CeilingNavigation::onStartup(RTC::UniqueId ec_id)
00151 {
00152
00153 m_CeilingMap.ReadNavigationMap(m_NavigationMap);
00154
00155
00156 #if 0 // コンフィグに移動
00157 #ifdef _OFFLINE_IMAGE
00158 m_CeilingMap.SetLocation(376, 51, -2.00);
00159 #else
00160 m_CeilingMap.SetLocation(x_pos, y_pos, theta);
00161 #endif
00162 #endif
00163
00164 return RTC::RTC_OK;
00165 }
00166
00167
00168 RTC::ReturnCode_t CeilingNavigation::onShutdown(RTC::UniqueId ec_id)
00169 {
00170
00171 fclose(fp);
00172 return RTC::RTC_OK;
00173 }
00174
00175 RTC::ReturnCode_t CeilingNavigation::onActivated(RTC::UniqueId ec_id)
00176 {
00177 ImageData cameraImage;
00178
00179
00180 if(m_OfflineImage.length() == 0){
00181 while(!m_CameraDataIn.isEmpty())
00182 m_CameraDataIn.read();
00183
00184 cameraImage.SetData(m_CameraData);
00185 }
00186
00187 #ifdef _OFFLINE_IMAGE
00188 else{
00189 char fname[256];
00190
00191 memset(fname, 0, sizeof(fname));
00192 sprintf( fname, "%s/cap_%05d.png", m_OfflineImage.c_str(), count );
00193 cameraImage.LoadImage( fname, CV_LOAD_IMAGE_GRAYSCALE);
00194 }
00195 #endif
00196
00197
00198 m_BlockMat.CreateBlockMap ( m_Center_X, m_Center_Y, cameraImage.GetWidth(),
00199 cameraImage.GetHeight(), m_BlockSize, m_BlockCoefficient, m_BlockResolution, m_BlackWhiteValue);
00200
00201 return RTC::RTC_OK;
00202
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212 RTC::ReturnCode_t CeilingNavigation::onExecute(RTC::UniqueId ec_id)
00213 {
00214 ImageData cameraImage;
00215
00216 if(m_OfflineImage.length() == 0){
00217 m_CameraDataIn.read();
00218
00219
00220
00221 cameraImage.SetData(m_CameraData);
00222 }
00223 #ifdef _OFFLINE_IMAGE
00224 else{
00225 char fname[256];
00226
00227
00228 memset(fname, 0, sizeof(fname));
00229 if(count > 1061) count = 130;
00230 sprintf( fname, "%s/cap_%05d.png", m_OfflineImage.c_str(), count++ );
00231 cameraImage.LoadImage( fname, CV_LOAD_IMAGE_GRAYSCALE);
00232 }
00233 #endif
00234
00235
00236
00237 if(m_LocalizedPositionIn.isNew())
00238 {
00239 while(!m_LocalizedPositionIn.isEmpty())
00240 m_LocalizedPositionIn.read();
00241
00242
00243
00244
00245 theta = m_LocalizedPosition.data.heading + M_PI/2.0;
00246 if (theta > M_PI)
00247 theta = -M_PI * 2 + theta;
00248 else if (theta < -M_PI)
00249 theta = M_PI * 2 + theta;
00250
00251 x_pos = round( m_LocalizedPosition.data.position.x / mPerPix ) + 69 - m_BlockSize/2;
00252 y_pos = - (unsigned long)round( m_LocalizedPosition.data.position.y / mPerPix ) + (333.0 - 63.0) - m_BlockSize/2;
00253
00254 }
00255
00256 m_CeilingMap.SetLocation(x_pos, y_pos, theta);
00257
00258
00259 m_BlockMat.MapTracking(m_CeilingMap, cameraImage);
00260
00261 theta = m_CeilingMap.GetTheta();
00262 x_pos = m_CeilingMap.GetPosX();
00263 y_pos = m_CeilingMap.GetPosY();
00264
00265
00266
00267
00268
00269
00270
00271 if(delaycount == 0){
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 m_CeilingPosition.data.position.x = mPerPix * ((double)(x_pos + m_BlockSize/2) - 69.0 );
00289 m_CeilingPosition.data.position.y = mPerPix * ((double)(y_pos + m_BlockSize/2) - (333.0 - 63.0)) * (-1);
00290
00291 m_CeilingPosition.data.heading = theta - M_PI/2.0;
00292 if (m_CeilingPosition.data.heading > M_PI)
00293 m_CeilingPosition.data.heading = -M_PI * 2 + m_CeilingPosition.data.heading;
00294 else if (m_CeilingPosition.data.heading < -M_PI)
00295 m_CeilingPosition.data.heading = M_PI * 2 + m_CeilingPosition.data.heading;
00296
00297
00298 m_CeilingPosition.data.position.x = m_CeilingPosition.data.position.x - camX * cos(m_CeilingPosition.data.heading) + camY * sin(m_CeilingPosition.data.heading);
00299 m_CeilingPosition.data.position.y = m_CeilingPosition.data.position.y - camX * sin(m_CeilingPosition.data.heading) - camY * cos(m_CeilingPosition.data.heading);
00300
00301 std::cout <<"[output Data]: "<< m_CeilingPosition.data.position.x << ","
00302 << m_CeilingPosition.data.position.y << ","
00303 <<m_CeilingPosition.data.heading << std::endl;
00304
00305
00306 m_CeilingPositionOut.write();
00307
00308
00309 fprintf(fp,"%f %f %f\n",m_CeilingPosition.data.position.x,m_CeilingPosition.data.position.y,m_CeilingPosition.data.heading);
00310
00311 delaycount = 8;
00312 }
00313 delaycount--;
00314
00315 usleep(10000);
00316
00317 return RTC::RTC_OK;
00318 }
00319
00320
00321
00322 extern "C"
00323 {
00324
00325 void CeilingNavigationInit(RTC::Manager* manager)
00326 {
00327 RTC::Properties profile(ceilingnavigation_spec);
00328 manager->registerFactory(profile,
00329 RTC::Create<CeilingNavigation>,
00330 RTC::Delete<CeilingNavigation>);
00331 }
00332
00333 };