00001 #include <sstream>
00002
00003 #include "Vision/RawCameraGenerator.h"
00004 #include "Vision/RLEGenerator.h"
00005 #include "Vision/RegionGenerator.h"
00006 #include "Vision/SegmentedColorGenerator.h"
00007
00008 #include "DualCoding/SketchData.h"
00009 #include "DualCoding/ShapeBlob.h"
00010 #include "DualCoding/Sketch.h"
00011
00012 #include "Vision/VisualOdometry/VisualOdometry.h"
00013 #include "Crew/Lookout.h"
00014 #include "Crew/MapBuilder.h"
00015 #ifdef TGT_HAS_WALK
00016 # include "Crew/Pilot.h"
00017 #endif
00018 #if (defined(TGT_HAS_ARMS) || !defined(STRICT_TGT_MODEL)) && !defined(TGT_IS_AIBO)
00019 # include "Crew/Grasper.h"
00020 #endif
00021 #include "Localization/ShapeBasedParticleFilter.h"
00022
00023 #include "ViewerConnection.h"
00024 #include "VRmixin.h"
00025
00026 #include "Behaviors/Controller.h"
00027
00028 using namespace std;
00029
00030 namespace DualCoding {
00031
00032
00033
00034 VRmixin* VRmixin::theOne=NULL;
00035
00036
00037 template<ReferenceFrameType_t _refFrameType,int const init_id, size_t const _width, size_t const _height>
00038 static SketchSpace& createStaticSkS(const std::string& _name) {
00039 static SketchSpace SkS(_name,_refFrameType,init_id,_width,_height);
00040
00041 return SkS;
00042 }
00043
00044 SketchSpace& VRmixin::getCamSkS() {
00045 return createStaticSkS<camcentric,10000,CameraResolutionX,CameraResolutionY>("cam");
00046 }
00047 SketchSpace& VRmixin::getLocalSkS() {
00048 return createStaticSkS<egocentric,20000,WORLD_WIDTH,WORLD_HEIGHT>("local");
00049 }
00050 SketchSpace& VRmixin::getWorldSkS() {
00051 return createStaticSkS<allocentric,30000,WORLD_WIDTH,WORLD_HEIGHT>("world");
00052 }
00053 ShapeSpace& VRmixin::getGroundShS() {
00054 static ShapeSpace ShS(&VRmixin::getCamSkS(),90000,"ground",egocentric);
00055 return ShS;
00056 }
00057
00058 SketchSpace& VRmixin::camSkS=VRmixin::getCamSkS();
00059 ShapeSpace& VRmixin::camShS=VRmixin::getCamSkS().getDualSpace();
00060
00061 ShapeSpace& VRmixin::groundShS=VRmixin::getGroundShS();
00062
00063 SketchSpace& VRmixin::localSkS=VRmixin::getLocalSkS();
00064 ShapeSpace& VRmixin::localShS=VRmixin::getLocalSkS().getDualSpace();
00065
00066 SketchSpace& VRmixin::worldSkS=VRmixin::getWorldSkS();
00067 ShapeSpace& VRmixin::worldShS=VRmixin::getWorldSkS().getDualSpace();
00068
00069 Shape<AgentData> VRmixin::theAgent;
00070
00071 Socket *VRmixin::camDialogSock=NULL;
00072 Socket *VRmixin::camSketchSock=NULL;
00073 Socket *VRmixin::localDialogSock=NULL;
00074 Socket *VRmixin::localSketchSock=NULL;
00075 Socket *VRmixin::worldDialogSock=NULL;
00076 Socket *VRmixin::worldSketchSock=NULL;
00077
00078 MapBuilder* VRmixin::mapBuilder = NULL;
00079 Lookout* VRmixin::lookout = NULL;
00080 #ifdef TGT_HAS_WALK
00081 Pilot* VRmixin::pilot = NULL;
00082 #endif
00083 #if (defined(TGT_HAS_ARMS) || !defined(STRICT_TGT_MODEL)) && !defined(TGT_IS_AIBO)
00084 Grasper* VRmixin::grasper = NULL;
00085 #endif
00086
00087 VisualOdometry* VRmixin::imageOdometry = NULL;
00088
00089 ShapeBasedParticleFilter* VRmixin::particleFilter = NULL;
00090
00091 unsigned int VRmixin::instanceCount = 0;
00092 unsigned int VRmixin::crewCount = 0;
00093
00094 bool VRmixin::isWalkingFlag = false;
00095
00096
00097 std::vector<ShapeRoot> VRmixin::drawShapes;
00098
00099 VRmixin::VRmixin() {
00100 if ( instanceCount++ == 0 ) {
00101
00102 cout << "Initializing VRmixin statics" << endl;
00103 SketchRoot::reset();
00104 if (theOne != NULL) {
00105 if ( ! mapBuilder->isRetained() )
00106 cerr << "VRmixin statics already constructed!?!?!" << endl;
00107 return;
00108 }
00109 theOne=this;
00110
00111 camSkS.requireIdx();
00112 localSkS.requireIdx();
00113 worldSkS.requireIdx();
00114
00115 camDialogSock=wireless->socket(Socket::SOCK_STREAM, 1024, DIALOG_BUFFER_SIZE);
00116 camSketchSock=wireless->socket(Socket::SOCK_STREAM, 1024, SKETCH_BUFFER_SIZE);
00117 worldDialogSock=wireless->socket(Socket::SOCK_STREAM, 1024, DIALOG_BUFFER_SIZE);
00118 worldSketchSock=wireless->socket(Socket::SOCK_STREAM, 1024, SKETCH_BUFFER_SIZE);
00119 localDialogSock=wireless->socket(Socket::SOCK_STREAM, 1024, DIALOG_BUFFER_SIZE);
00120 localSketchSock=wireless->socket(Socket::SOCK_STREAM, 1024, SKETCH_BUFFER_SIZE);
00121
00122 wireless->setReceiver(camDialogSock->sock, &camDialogSockCallback);
00123 wireless->setReceiver(worldDialogSock->sock, &worldDialogSockCallback);
00124 wireless->setReceiver(localDialogSock->sock, &localDialogSockCallback);
00125
00126 wireless->setDaemon(camDialogSock, true);
00127 wireless->setDaemon(camSketchSock, true);
00128 wireless->setDaemon(worldDialogSock, true);
00129 wireless->setDaemon(worldSketchSock, true);
00130 wireless->setDaemon(localDialogSock, true);
00131 wireless->setDaemon(localSketchSock, true);
00132
00133 wireless->listen(camDialogSock, CAM_DIALOG_PORT);
00134 wireless->listen(camSketchSock, CAM_SKETCH_PORT);
00135 wireless->listen(worldDialogSock, WORLD_DIALOG_PORT);
00136 wireless->listen(worldSketchSock, WORLD_SKETCH_PORT);
00137 wireless->listen(localDialogSock, LOCAL_DIALOG_PORT);
00138 wireless->listen(localSketchSock, LOCAL_SKETCH_PORT);
00139
00140 camSkS.viewer->setDialogSocket(camDialogSock, CAM_DIALOG_PORT);
00141 camSkS.viewer->setSketchSocket(camSketchSock, CAM_SKETCH_PORT);
00142 localSkS.viewer->setDialogSocket(localDialogSock, LOCAL_DIALOG_PORT);
00143 localSkS.viewer->setSketchSocket(localSketchSock, LOCAL_SKETCH_PORT);
00144 worldSkS.viewer->setDialogSocket(worldDialogSock, WORLD_DIALOG_PORT);
00145 worldSkS.viewer->setSketchSocket(worldSketchSock, WORLD_SKETCH_PORT);
00146
00147 theAgent = Shape<AgentData>(worldShS);
00148 theAgent->setColor(rgb(0,0,255));
00149 theAgent->setObstacle(false);
00150 }
00151
00152 }
00153
00154 VRmixin::~VRmixin() {
00155 if ( --instanceCount == 0 ) {
00156
00157 cout << "Destructing VRmixin statics" << endl;
00158 if (theOne == NULL) {
00159 cerr << "VRmixin statics already destructed!?!?!" << endl;
00160 return;
00161 }
00162 theOne=NULL;
00163
00164 wireless->setDaemon(camDialogSock, false);
00165 wireless->setDaemon(camSketchSock, false);
00166 wireless->setDaemon(localDialogSock,false);
00167 wireless->setDaemon(localSketchSock, false);
00168 wireless->setDaemon(worldDialogSock,false);
00169 wireless->setDaemon(worldSketchSock, false);
00170
00171 wireless->close(camSketchSock->sock);
00172 wireless->close(camDialogSock->sock);
00173 wireless->close(localSketchSock->sock);
00174 wireless->close(localDialogSock->sock);
00175 wireless->close(worldSketchSock->sock);
00176 wireless->close(worldDialogSock->sock);
00177
00178
00179 drawShapes.clear();
00180
00181 camShS.clear();
00182 camSkS.bumpRefreshCounter();
00183 camSkS.clear();
00184
00185 localShS.clear();
00186 localSkS.bumpRefreshCounter();
00187 localSkS.clear();
00188
00189 theAgent = Shape<AgentData>();
00190 worldShS.clear();
00191 worldSkS.bumpRefreshCounter();
00192 worldSkS.clear();
00193
00194 camSkS.freeIndexes();
00195 localSkS.freeIndexes();
00196 worldSkS.freeIndexes();
00197
00198 SketchRoot::reset();
00199 }
00200 }
00201
00202 void VRmixin::startCrew() {
00203 if ( crewCount++ == 0 ) {
00204 cout << "Starting crew." << endl;
00205 mapBuilder = new MapBuilder;
00206 lookout = new Lookout;
00207 #ifdef TGT_HAS_WALK
00208 pilot = new Pilot;
00209 #endif
00210 #if (defined(TGT_HAS_ARMS) || !defined(STRICT_TGT_MODEL)) && !defined(TGT_IS_AIBO)
00211 grasper = new Grasper;
00212 #endif
00213 if ( particleFilter == NULL )
00214 particleFilter = new ShapeBasedParticleFilter(camShS,localShS,worldShS);
00215
00216
00217
00218
00219 mapBuilder->start();
00220 lookout->start();
00221 #ifdef TGT_HAS_WALK
00222 pilot->start();
00223 #endif
00224 #if (defined(TGT_HAS_ARMS) || !defined(STRICT_TGT_MODEL)) && !defined(TGT_IS_AIBO)
00225 grasper->start();
00226 #endif
00227 }
00228 }
00229
00230 void VRmixin::stopCrew() {
00231 if ( --crewCount == 0 ) {
00232 cout << "Stopping crew." << endl;
00233
00234 #if (defined(TGT_HAS_ARMS) || !defined(STRICT_TGT_MODEL)) && !defined(TGT_IS_AIBO)
00235 grasper->stop();
00236 #endif
00237 #ifdef TGT_HAS_WALK
00238 pilot->stop();
00239 #endif
00240 lookout->stop();
00241 mapBuilder->stop();
00242 SketchRoot::reset();
00243 delete particleFilter;
00244 particleFilter = NULL;
00245 delete imageOdometry;
00246 imageOdometry = NULL;
00247 }
00248 }
00249
00250 void VRmixin::requireCrew(const std::string &memberName) {
00251 if ( crewCount == 0 )
00252 std::cerr << "\n*** ERROR: " << memberName
00253 << " invoked outside of VisualRoutinesStateNode / VRmixin ***" << std::endl;
00254 }
00255
00256 #ifdef TGT_HAS_CAMERA
00257 void VRmixin::projectToGround() {
00258 projectToGround(kine->linkToBase(CameraFrameOffset));
00259 }
00260 #endif
00261
00262 void VRmixin::projectToGround(const fmat::Transform& camToBase) {
00263 projectToGround(camToBase, kine->calculateGroundPlane());
00264 }
00265
00266 void VRmixin::projectToGround(const fmat::Transform& camToBase, const PlaneEquation& groundplane) {
00267 groundShS.clear();
00268 groundShS.importShapes(camShS.allShapes());
00269 vector<ShapeRoot> &groundShapes_vec = groundShS.allShapes();
00270 for(size_t i = 0; i < groundShapes_vec.size(); i++)
00271 groundShapes_vec[i]->projectToGround(camToBase, groundplane);
00272 }
00273
00274 void VRmixin::refreshSketchWorld() {
00275 if ( Controller::theOneController != NULL)
00276 Controller::theOneController->refreshSketchWorld();
00277 }
00278
00279 void VRmixin::refreshSketchLocal() {
00280 if ( Controller::theOneController != NULL)
00281 Controller::theOneController->refreshSketchLocal();
00282 }
00283
00284 void VRmixin::refreshSketchCamera() {
00285 if ( Controller::theOneController != NULL)
00286 Controller::theOneController->refreshSketchCamera();
00287 }
00288
00289 void VRmixin::autoRefreshSketchWorld() {
00290 if (autoRefreshWorldAllowed)
00291 refreshSketchWorld();
00292 }
00293
00294 void VRmixin::autoRefreshSketchLocal() {
00295 if (autoRefreshLocalAllowed)
00296 refreshSketchLocal();
00297 }
00298
00299 void VRmixin::autoRefreshSketchCamera() {
00300 if (autoRefreshCameraAllowed)
00301 refreshSketchCamera();
00302 }
00303
00304 int VRmixin::camDialogSockCallback(char *buf, int bytes) {
00305 static std::string incomplete;
00306 dialogCallback(buf, bytes, incomplete, theOne->camSkS, theOne->camShS);
00307 return 0;
00308 }
00309
00310 int VRmixin::localDialogSockCallback(char *buf, int bytes) {
00311 static std::string incomplete;
00312 dialogCallback(buf, bytes, incomplete, theOne->localSkS, theOne->localShS);
00313 return 0;
00314 }
00315
00316 int VRmixin::worldDialogSockCallback(char *buf, int bytes) {
00317 static std::string incomplete;
00318 dialogCallback(buf, bytes, incomplete, theOne->worldSkS, theOne->worldShS);
00319 return 0;
00320 }
00321
00322 void VRmixin::dialogCallback(char* buf, int bytes, std::string& incomplete,
00323 SketchSpace& SkS, ShapeSpace& ShS) {
00324 std::string s(buf,bytes);
00325
00326 while(s.size()>0) {
00327 size_t endline=s.find('\n');
00328 if(endline==std::string::npos) {
00329 incomplete+=s;
00330 return;
00331 }
00332 else {
00333 incomplete+=s.substr(0,endline);
00334 theOne->processSketchRequest(incomplete,SkS,ShS);
00335 incomplete.erase();
00336 s=s.substr(endline+1);
00337 }
00338 }
00339 return;
00340 }
00341
00342 bool VRmixin::encodeSketch(const SketchDataRoot& image)
00343 {
00344 unsigned int avail = SKETCH_BUFFER_SIZE-1;
00345 Socket* sketchSock = image.getSpace().viewer->getSketchSocket();
00346 char* buf=(char*)sketchSock->getWriteBuffer(avail);
00347 ASSERTRETVAL(buf!=NULL,"could not get buffer",false);
00348 unsigned int used = image.saveBuffer(buf, avail);
00349 sketchSock->write(used);
00350 return true;
00351 }
00352
00353
00354 Sketch<uchar> VRmixin::sketchFromSeg() {
00355 Sketch<uchar> cam(camSkS, "camimage");
00356 cam->setColorMap(segMap);
00357 size_t const npixels = cam->getNumPixels();
00358 cmap_t* seg_image = ProjectInterface::defSegmentedColorGenerator->getImage(CAM_LAYER,CAM_CHANNEL);
00359 memcpy(cam->getRawPixels(), seg_image, npixels);
00360 return cam;
00361 }
00362
00363
00364 Sketch<uchar> VRmixin::sketchFromChannel(const RawCameraGenerator::channel_id_t chan) {
00365 const RawCameraGenerator::channel_id_t the_chan =
00366 (chan >= 0 && chan < RawCameraGenerator::NUM_CHANNELS) ? chan : RawCameraGenerator::CHAN_Y;
00367 Sketch<uchar> cam(camSkS,"sketchFromChannel");
00368 cam->setColorMap(grayMap);
00369 uchar* campixels = cam->getRawPixels();
00370 int const incr = ProjectInterface::defRawCameraGenerator->getIncrement(CAM_LAYER);
00371 int const skip = ProjectInterface::defRawCameraGenerator->getSkip(CAM_LAYER);
00372 uchar* chan_ptr = ProjectInterface::defRawCameraGenerator->getImage(CAM_LAYER,the_chan);
00373 if(chan_ptr==NULL) {
00374 memset(campixels,0,cam->getHeight()*cam->getWidth());
00375 } else {
00376 chan_ptr -= incr;
00377 for (unsigned int row = 0; row < cam->getHeight(); row++) {
00378 for (unsigned int col = 0; col < cam->getWidth(); col++)
00379 *campixels++ = *(chan_ptr += incr);
00380 chan_ptr += skip;
00381 }
00382 }
00383 return cam;
00384 }
00385
00386 Sketch<uchar> VRmixin::sketchFromRawY() {
00387 return sketchFromChannel(RawCameraGenerator::CHAN_Y);
00388 }
00389
00390 Sketch<usint> VRmixin::sketchFromDepth() {
00391 Sketch<usint> depth(camSkS,"sketchFromDepth");
00392 depth->setColorMap(jetMapScaled);
00393 usint* depthpixels = depth->getRawPixels();
00394 int const incr = ProjectInterface::defRawDepthGenerator->getIncrement(CAM_LAYER);
00395 int const skip = ProjectInterface::defRawDepthGenerator->getSkip(CAM_LAYER);
00396 uchar* chanY_ptr = ProjectInterface::defRawDepthGenerator->getImage(CAM_LAYER, RawCameraGenerator::CHAN_Y);
00397 uchar* chanU_ptr = ProjectInterface::defRawDepthGenerator->getImage(CAM_LAYER, RawCameraGenerator::CHAN_U);
00398 for (unsigned int row = 0; row < depth->getHeight(); row++) {
00399 for (unsigned int col = 0; col < depth->getWidth(); col++)
00400 *depthpixels++ = *(chanY_ptr += incr) | (*(chanU_ptr += incr))<<8;
00401 chanY_ptr += skip;
00402 chanU_ptr += skip;
00403 }
00404 return depth;
00405 }
00406
00407 Sketch<yuv> VRmixin::sketchFromYUV() {
00408 Sketch<yuv> cam(camSkS,"sketchFromYUV");
00409 cam->setColorMap(segMap);
00410 yuv* campixels = cam->getRawPixels();
00411 int const incr = ProjectInterface::defRawCameraGenerator->getIncrement(CAM_LAYER);
00412 int const skip = ProjectInterface::defRawCameraGenerator->getSkip(CAM_LAYER);
00413 uchar* chanY_ptr = ProjectInterface::defRawCameraGenerator->getImage(CAM_LAYER,RawCameraGenerator::CHAN_Y);
00414 uchar* chanU_ptr = ProjectInterface::defRawCameraGenerator->getImage(CAM_LAYER,RawCameraGenerator::CHAN_U);
00415 uchar* chanV_ptr = ProjectInterface::defRawCameraGenerator->getImage(CAM_LAYER,RawCameraGenerator::CHAN_V);
00416 if ( chanY_ptr == NULL )
00417 memset(campixels,0,cam->getHeight()*cam->getWidth()*sizeof(rgb));
00418 else {
00419
00420 chanY_ptr -= incr;
00421 chanU_ptr -= incr;
00422 chanV_ptr -= incr;
00423 for (unsigned int row = 0; row < cam->getHeight(); row++) {
00424 for (unsigned int col = 0; col < cam->getWidth(); col++)
00425 *campixels++ = yuv(*(chanY_ptr += incr), *(chanU_ptr += incr), *(chanV_ptr += incr));
00426 chanY_ptr += skip;
00427 chanU_ptr += skip;
00428 chanV_ptr += skip;
00429 }
00430 }
00431 return cam;
00432 }
00433
00434
00435 vector<Shape<BlobData> >
00436 VRmixin::getBlobsFromRegionGenerator(const color_index color,
00437 const int minarea,
00438 const BlobData::BlobOrientation_t orient,
00439 const coordinate_t height,
00440 const int maxblobs) {
00441 vector<Shape<BlobData> > result;
00442 const CMVision::run<uchar> *sketch_buffer = reinterpret_cast<const CMVision::run<uchar>*>
00443 (ProjectInterface::defRLEGenerator->getImage(CAM_LAYER,CAM_CHANNEL));
00444 const CMVision::color_class_state* ccs = reinterpret_cast<const CMVision::color_class_state*>
00445 (ProjectInterface::defRegionGenerator->getImage(CAM_LAYER,CAM_CHANNEL));
00446
00447
00448 const rgb rgbvalue = ProjectInterface::getColorRGB(color);
00449 const CMVision::region* list_head = ccs[color].list;
00450 for (int i=0; list_head!=NULL && i<maxblobs && list_head->area >= minarea; list_head = list_head->next, i++) {
00451 BlobData* blobdat = BlobData::new_blob(camShS,*list_head, sketch_buffer, orient, height, rgbvalue);
00452 result.push_back(Shape<BlobData>(blobdat));
00453 }
00454 return result;
00455 }
00456
00457 void VRmixin::processSketchRequest(const std::string &line,
00458 SketchSpace& SkS,
00459 ShapeSpace& ShS)
00460 {
00461 Socket* dialogSock = SkS.viewer->getDialogSocket();
00462 if(line.compare(0,strlen("size"),"size")==0) {
00463 dialogSock->printf("size begin\n");
00464 dialogSock->printf("width %d\nheight %d\n",int(SkS.getWidth()),int(SkS.getHeight()));
00465 dialogSock->printf("size end\n");
00466 }
00467 else if(line.compare(0,strlen("list"),"list")==0) {
00468 dialogSock->printf("list begin\n");
00469 SkS.viewer->writeBigString(SkS.getTmatForGUI());
00470 SkS.viewer->writeBigString(SkS.getSketchListForGUI());
00471 SkS.viewer->writeBigString(ShS.getShapeListForGUI());
00472 dialogSock->printf("list end\n");
00473 } else if(line.compare(0,strlen("colors"),"colors")==0) {
00474 dialogSock->printf("colors begin\n");
00475 const CMVision::color_class_state *ccs = ProjectInterface::defSegmentedColorGenerator->getColors();
00476 unsigned int i;
00477 for (i = 0; i < ProjectInterface::defSegmentedColorGenerator->getNumColors(); i++) {
00478 char color_buffer[512];
00479 sprintf(color_buffer,"%d,%d,%d,%s\n",ccs[i].color.red,ccs[i].color.green,ccs[i].color.blue,ccs[i].name);
00480 SkS.viewer->writeBigString(color_buffer);
00481 }
00482 dialogSock->printf("colors end\n");
00483 } else if(line.compare(0,strlen("get"),"get")==0) {
00484 std::string tempstring = line.substr(strlen("get"),
00485 line.length()-strlen("get"));
00486 std::istringstream ist(tempstring);
00487 int requested_id = -1;
00488 ist >> requested_id;
00489 dialogSock->printf("get read:%d\n",requested_id);
00490 SketchDataRoot* sketchptr=(SkS.retrieveSketch(requested_id));
00491 if(sketchptr != NULL)
00492 encodeSketch(*sketchptr);
00493 dialogSock->printf("get end\n");
00494 } else {
00495 dialogSock->printf("Invalid command\n");
00496 }
00497 }
00498
00499 bool VRmixin::autoRefreshWorldAllowed = true;
00500 bool VRmixin::autoRefreshLocalAllowed = true;
00501 bool VRmixin::autoRefreshCameraAllowed = true;
00502
00503 Point VRmixin::robotObstaclesPt = Point(0, 0, 0, allocentric);
00504 AngTwoPi VRmixin::robotObstaclesOri = 0;
00505
00506 }