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