botControlRC.C
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdio.h>
00039 #include "Component/ModelManager.H"
00040 #include "Image/Image.H"
00041 #include "Image/ImageSet.H"
00042 #include "Image/ShapeOps.H"
00043 #include "Image/DrawOps.H"
00044 #include "Image/ColorOps.H"
00045 #include "Image/MathOps.H"
00046 #include "Image/Layout.H"
00047 #include "GUI/XWinManaged.H"
00048 #include "Corba/ImageOrbUtil.H"
00049 #include "Corba/CorbaUtil.H"
00050 #include "Corba/Objects/BotControlSK.hh"
00051 #include "Neuro/EnvVisualCortex.H"
00052
00053
00054 #define UP 98
00055 #define DOWN 104
00056 #define LEFT 100
00057 #define RIGHT 102
00058
00059
00060
00061 int main(int argc, char** argv)
00062 {
00063
00064
00065 try {
00066 MYLOGVERB = LOG_INFO;
00067 ModelManager *mgr = new ModelManager("Test ObjRec");
00068
00069 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
00070
00071
00072 CORBA::Object_ptr objBotControlRef[10]; int nBotControlObj;
00073 if (!getMultiObjectRef(orb, "botControl.irobot",
00074 objBotControlRef, nBotControlObj))
00075 {
00076 printf("Can not find any object to bind with\n");
00077 return 1;
00078 } else {
00079 printf("Found %i object, binding to the last one\n", nBotControlObj);
00080 }
00081
00082 BotControl_var botControl = BotControl::_narrow(objBotControlRef[nBotControlObj-1]);
00083
00084 if( CORBA::is_nil(botControl) ) {
00085 printf("Can't narrow reference to type Echo (or it was nil).\n");
00086 return 1;
00087 }
00088
00089
00090 nub::ref<EnvVisualCortex> evc(new EnvVisualCortex(*mgr));
00091 mgr->addSubComponent(evc);
00092 mgr->exportOptions(MC_RECURSE);
00093
00094 if (mgr->parseCommandLine(
00095 (const int)argc, (const char**)argv, "", 0, 0) == false)
00096 return 1;
00097
00098 mgr->start();
00099
00100
00101 XWinManaged* xwin = new XWinManaged(Dims(320*2,240), -1, -1, "Bot Control");
00102 Image<PixRGB<byte> > img;
00103
00104 float currentSpeed = 0;
00105 float currentSteering = 0;
00106 while(true)
00107 {
00108 ImageOrb *orbImg = botControl->getImageSensor(0);
00109 if (orbImg != NULL)
00110 {
00111 orb2Image(*orbImg, img);
00112 }
00113 img = rescale(img, 320, 240);
00114
00115 evc->input(img);
00116
00117 Image<float> vcxMap = evc->getVCXmap();
00118
00119 Point2D<int> maxPos; float maxVal;
00120
00121 vcxMap = rescale(vcxMap, img.getDims());
00122 findMax(vcxMap, maxPos, maxVal);
00123 inplaceNormalize(vcxMap, 0.0F, 255.0F);
00124
00125 drawCircle(img, maxPos, 20, PixRGB<byte>(255,0,0));
00126
00127 Layout<PixRGB<byte> > outDisp;
00128 outDisp = vcat(outDisp, hcat(img, toRGB((Image<byte>)vcxMap)));
00129 xwin->drawRgbLayout(outDisp);
00130
00131
00132 int key = xwin->getLastKeyPress();
00133
00134 switch (key)
00135 {
00136 case UP:
00137 currentSpeed += 10;
00138 botControl->setSpeed(currentSpeed);
00139 currentSteering = 0;
00140 botControl->setSteering(0);
00141 break;
00142 case DOWN:
00143 currentSpeed -= 10;
00144 botControl->setSpeed(currentSpeed);
00145 currentSteering = 0;
00146 botControl->setSteering(0);
00147 break;
00148 case LEFT:
00149 currentSteering +=10;
00150 botControl->setSpeed(100);
00151 botControl->setSteering(currentSteering);
00152 break;
00153 case RIGHT:
00154 currentSteering -=10;
00155 botControl->setSpeed(100);
00156 botControl->setSteering(currentSteering);
00157 break;
00158 case 65:
00159 currentSpeed = 0;
00160 currentSteering = 0;
00161 botControl->setSteering(currentSteering);
00162 botControl->setSpeed(currentSpeed);
00163 break;
00164 case 33:
00165 botControl->playSong(0);
00166 break;
00167 case 40:
00168 botControl->dock();
00169 break;
00170 case 27:
00171 botControl->setOIMode(131);
00172 break;
00173
00174 default:
00175 if (key != -1)
00176 printf("Unknown Key %i\n", key);
00177 break;
00178
00179 }
00180
00181 usleep(10000);
00182
00183 }
00184
00185 orb->destroy();
00186 }
00187 catch(CORBA::TRANSIENT&) {
00188 printf("Caught system exception TRANSIENT -- unable to contact the server \n");
00189 }
00190 catch(CORBA::SystemException& ex) {
00191 printf("Caught a CORBA::%s\n", ex._name());
00192 }
00193 catch(CORBA::Exception& ex) {
00194 printf("Caught CORBA::Exception: %s\n", ex._name());
00195 }
00196 catch(omniORB::fatalException& fe) {
00197 printf("Caught omniORB::fatalException:\n");
00198 printf(" file: %s", fe.file());
00199 printf(" line: %i", fe.line());
00200 printf(" mesg: %s", fe.errmsg());
00201 }
00202 return 0;
00203 }