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 <Ice/Ice.h>
00039 #include "Ice/IRobot.ice.H"
00040 #include "Ice/ImageIce.ice.H"
00041 #include "Ice/IceImageUtils.H"
00042
00043 #include "Component/ModelManager.H"
00044 #include "Media/FrameSeries.H"
00045 #include "Transport/FrameInfo.H"
00046 #include "Raster/GenericFrame.H"
00047 #include "Image/Image.H"
00048 #include "Image/DrawOps.H"
00049 #include "Image/ColorOps.H"
00050 #include "Image/CutPaste.H"
00051 #include "Image/ShapeOps.H"
00052 #include "Image/MathOps.H"
00053 #include "GUI/XWinManaged.H"
00054 #include "GUI/ImageDisplayStream.H"
00055 #include "Util/Timer.H"
00056 #include "Robots/RobotBrain/RobotCommon.H"
00057 #include "Neuro/getSaliency.H"
00058
00059 using namespace std;
00060 using namespace Robots;
00061
00062 #define KEY_UP 98
00063 #define KEY_DOWN 104
00064 #define KEY_LEFT 100
00065 #define KEY_RIGHT 102
00066
00067 int getKey(nub::ref<OutputFrameSeries> &ofs)
00068 {
00069 const nub::soft_ref<ImageDisplayStream> ids =
00070 ofs->findFrameDestType<ImageDisplayStream>();
00071
00072 const rutz::shared_ptr<XWinManaged> uiwin =
00073 ids.is_valid()
00074 ? ids->getWindow("Output")
00075 : rutz::shared_ptr<XWinManaged>();
00076 if (uiwin.is_valid())
00077 return uiwin->getLastKeyPress();
00078
00079 return -1;
00080 }
00081
00082
00083 int main(int argc, char** argv)
00084 {
00085
00086 MYLOGVERB = LOG_INFO;
00087 ModelManager manager("test-IRobot");
00088
00089 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00090 manager.addSubComponent(ofs);
00091
00092 nub::ref<GetSaliency> saliency(new GetSaliency(manager));
00093 manager.addSubComponent(saliency);
00094
00095 Timer localTimer;
00096
00097 int status = 0;
00098 Ice::CommunicatorPtr ic;
00099 try {
00100 ic = Ice::initialize(argc, argv);
00101 Ice::ObjectPrx base = ic->stringToProxy(
00102 "IRobotService:default -p 10000 -h " ROBOT_IP);
00103 IRobotPrx iRobot = IRobotPrx::checkedCast(base);
00104 if(!iRobot)
00105 throw "Invalid proxy";
00106
00107 manager.exportOptions(MC_RECURSE);
00108
00109 if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false)
00110 return 1;
00111 manager.start();
00112
00113
00114 iRobot->sendStart();
00115 iRobot->setMode(Robots::SafeMode);
00116 localTimer.reset();
00117
00118
00119 float speed = 0, steering = 0;
00120 Timer localTimer;
00121
00122 int id = 0;
00123 id=id;
00124
00125 Image<PixRGB<byte> > iRobotImg(320,240, ZEROS);
00126 ofs->writeRGB(iRobotImg, "Output", FrameInfo("Output", SRC_POS));
00127
00128 while(true)
00129 {
00130 localTimer.reset();
00131
00132 ImageIceMod::ImageIce imgIce = iRobot->getImageSensor(320*4 + id, false);
00133
00134
00135 if (imgIce.pixSize == 1)
00136 iRobotImg = toRGB(Ice2Image<byte>(imgIce));
00137 else
00138 iRobotImg = Ice2Image<PixRGB<byte> >(imgIce);
00139
00140
00141
00142
00143 const int num_salient_spots = saliency->compute(iRobotImg, SimTime::SECS(1));
00144 Image<float> salmap(iRobotImg.getDims(),ZEROS);
00145
00146 salmap = rescale(saliency->getSalmap(), iRobotImg.getDims());
00147
00148 inplaceNormalize(salmap, 0.0F, 255.0F);
00149
00150 LINFO("%d value at loc = %f", num_salient_spots, salmap.getVal(150));
00151 LINFO("img size = %d,%d, salmap size = %d,%d",iRobotImg.getWidth(),
00152 iRobotImg.getHeight(),salmap.getWidth(),salmap.getHeight());
00153
00154 Image<PixRGB<byte> > dispImage(642,240, ZEROS);
00155
00156
00157 inplacePaste(dispImage, iRobotImg, Point2D<int> (0,0));
00158 inplacePaste(dispImage, toRGB<byte>(salmap), Point2D<int> (321,0));
00159
00160
00161
00162 float dist, ang;
00163 iRobot->getDistanceAngle(dist, ang);
00164 printf("%f %f %f %f %f %i\n", speed, steering, dist, ang, localTimer.get()/1000.0F,
00165 ofs->frame());
00166 fflush(stdout);
00167
00168
00169
00170
00171
00172
00173 ofs->updateNext();
00174
00175
00176 ofs->writeRGB(dispImage, "Output", FrameInfo("Output", SRC_POS));
00177
00178
00179
00180 usleep(100000);
00181 int key = getKey(ofs);
00182
00183 if (key != -1)
00184 {
00185
00186 switch(key)
00187 {
00188 case KEY_UP:
00189 speed = 0.05;
00190 steering = 0;
00191 iRobot->setSteering(steering);
00192 iRobot->setSpeed(speed);
00193 break;
00194 case KEY_DOWN:
00195 speed = -0.05;
00196 steering = 0;
00197 iRobot->setSteering(steering);
00198 iRobot->setSpeed(speed);
00199 break;
00200 case KEY_LEFT:
00201 steering = 0.05;
00202 iRobot->setSteering(steering);
00203 iRobot->setSpeed(speed);
00204 break;
00205 case KEY_RIGHT:
00206 steering = -0.05;
00207 iRobot->setSteering(steering);
00208 iRobot->setSpeed(speed);
00209 break;
00210 case 65:
00211 speed = 0;
00212 steering = 0;
00213 iRobot->motorsOff(0);
00214 break;
00215 case 33:
00216 iRobot->setMode(Robots::SafeMode);
00217
00218 iRobot->playSong(0);
00219 break;
00220 case 40:
00221
00222 iRobot->setMode(Robots::CoverAndDockMode);
00223 break;
00224
00225 case 39:
00226 {
00227 float chargeState = iRobot->getSensorValue(21);
00228 float voltage = iRobot->getSensorValue(22);
00229 float current = iRobot->getSensorValue(23);
00230 float batteryTemp = iRobot->getSensorValue(24);
00231 float batteryCharge = iRobot->getSensorValue(25);
00232 float batteryCapacity = iRobot->getSensorValue(26);
00233
00234 LINFO("ChargeState %i", (int)chargeState);
00235 LINFO("Voltage %i mV", (unsigned int)voltage);
00236 LINFO("Current %i mA", (int)current);
00237 LINFO("Battery Temperature %iC", (int)batteryTemp);
00238 LINFO("Battery Charge %i mAh", (int)batteryCharge);
00239 LINFO("Battery Capacity %i mAh", (int)batteryCapacity);
00240 }
00241 break;
00242 case 27:
00243 iRobot->sendStart();
00244 iRobot->setMode(Robots::SafeMode);
00245 break;
00246
00247
00248 default:
00249 LINFO("Unknown key %i\n", key);
00250 break;
00251 }
00252 localTimer.reset();
00253
00254
00255
00256
00257 }
00258 }
00259
00260 }
00261 catch (const Ice::Exception& ex) {
00262 cerr << ex << endl;
00263 status = 1;
00264 }
00265 catch(const char* msg) {
00266 cerr << msg << endl;
00267 status = 1;
00268 }
00269 if (ic)
00270 ic->destroy();
00271 return status;
00272 }