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 "Component/ModelManager.H"
00039 #include "Raster/GenericFrame.H"
00040 #include "Image/Layout.H"
00041 #include "Media/FrameSeries.H"
00042 #include "Transport/FrameInfo.H"
00043 #include "Image/MatrixOps.H"
00044 #include "GUI/ImageDisplayStream.H"
00045 #include "GUI/XWinManaged.H"
00046 #include "Robots/IRobot/IRobotSim.H"
00047 #include "Ice/IceImageUtils.H"
00048 #include "Ice/ImageIce.ice.H"
00049 #include <Ice/Ice.h>
00050 #include <Ice/Service.h>
00051 #include "Ice/IRobot.ice.H"
00052
00053 #include <stdio.h>
00054 #include <stdlib.h>
00055
00056 #define KEY_UP 98
00057 #define KEY_DOWN 104
00058 #define KEY_LEFT 100
00059 #define KEY_RIGHT 102
00060
00061 class IRobotI : public ModelComponent, public Robots::IRobot, public IceUtil::Thread
00062 {
00063 public:
00064 IRobotI(ModelManager& mgr,
00065 nub::soft_ref<OutputFrameSeries> ofs,
00066 const std::string& descrName = "IRobotSimService",
00067 const std::string& tagName = "IRobotSimService") :
00068 ModelComponent(mgr, descrName, tagName),
00069 itsOfs(ofs),
00070 itsWorldView(true),
00071 itsCurrentSpeed(0),
00072 itsCurrentSteering(0)
00073 {
00074 itsIRobotSim = nub::soft_ref<IRobotSim>(new IRobotSim(mgr));
00075 addSubComponent(itsIRobotSim);
00076 }
00077
00078 virtual void run()
00079 {
00080 itsIRobotSim->initViewport();
00081 while(true)
00082 {
00083 itsIRobotSim->simLoop();
00084
00085
00086
00087 float rightWheel = (itsCurrentSpeed*10) + (itsCurrentSteering*10);
00088 float leftWheel = (itsCurrentSpeed*10) - (itsCurrentSteering*10);
00089
00090 itsIRobotSim->setMotors(rightWheel, leftWheel);
00091
00092
00093
00094
00095
00096
00097 itsCurrentFrame = flipVertic(itsIRobotSim->getFrame(-1));
00098
00099
00100 }
00101
00102 }
00103
00104
00105 virtual float getSpeed(const Ice::Current&) { return itsCurrentSpeed; }
00106 virtual short setSpeed(const float speed, const Ice::Current&) { itsCurrentSpeed = speed; return 0; }
00107 virtual float getSteering(const Ice::Current&) { return itsCurrentSteering; }
00108 virtual short setSteering(const float steeringPos, const Ice::Current&) { itsCurrentSteering = steeringPos; return 0;}
00109 virtual ImageIceMod::ImageIce getImageSensor(const short i, bool color, const Ice::Current&)
00110 {
00111 return Image2Ice(itsCurrentFrame);
00112 }
00113 virtual ImageIceMod::DimsIce getImageSensorDims(const short i, const Ice::Current&)
00114 {
00115 ImageIceMod::DimsIce dims;
00116 dims.w = 0;
00117 dims.h = 0;
00118 return dims;
00119 }
00120 virtual float getSensorValue(const short i, const Ice::Current&){
00121 switch (i)
00122 {
00123 case 0: return itsIRobotSim->getXPos();
00124 case 1: return itsIRobotSim->getYPos();
00125 case 2: return itsIRobotSim->getOri();
00126 }
00127
00128 return 0;
00129 }
00130
00131 virtual bool getSensors(float &xPos, float &yPos, float &ori, const Ice::Current&)
00132 {
00133 itsIRobotSim->getSensors(xPos, yPos, ori);
00134 return true;
00135 }
00136
00137 virtual bool getDistanceAngle(float &dist, float &ang, const Ice::Current&)
00138 {
00139 float xPos, yPos, ori;
00140 itsIRobotSim->getSensors(xPos, yPos, ori);
00141
00142
00143
00144 return false;
00145
00146 }
00147
00148
00149 virtual void motorsOff(const short i, const Ice::Current&){
00150 itsCurrentSpeed = 0;
00151 itsCurrentSteering = 0;
00152 itsIRobotSim->setMotors(0, 0);
00153 }
00154 virtual void setMotor(const short i, const float val, const Ice::Current&){ }
00155 virtual short sendRawCmd(const std::string& data, const Ice::Current&) { return 0; }
00156 virtual void playSong(const short song, const Ice::Current&) { }
00157 virtual void shutdown(const Ice::Current&) { }
00158 virtual void sendStart(const Ice::Current&) { }
00159 virtual void setMode(const Robots::IRobotModes demo, const Ice::Current&) { }
00160 virtual void setDemo(const short demo, const Ice::Current&) { }
00161 virtual void setLED(const short led, const short color, const short intensity, const Ice::Current&) { }
00162
00163
00164 private:
00165 nub::soft_ref<OutputFrameSeries> itsOfs;
00166 nub::soft_ref<IRobotSim> itsIRobotSim;
00167 Image<PixRGB<byte> > itsCurrentFrame;
00168 bool itsWorldView;
00169 float itsCurrentSpeed;
00170 float itsCurrentSteering;
00171
00172 };
00173
00174
00175
00176 class IRobotSimService : public Ice::Service {
00177 protected:
00178 virtual bool start(int, char*[]);
00179 private:
00180 Ice::ObjectAdapterPtr itsAdapter;
00181 ModelManager *itsMgr;
00182 };
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 bool IRobotSimService::start(int argc, char* argv[])
00193 {
00194
00195 itsMgr = new ModelManager("IRobot Simulator Service");
00196
00197 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*itsMgr));
00198 itsMgr->addSubComponent(ofs);
00199
00200
00201 nub::ref<IRobotI> iRobotI(new IRobotI(*itsMgr, ofs));
00202 itsMgr->addSubComponent(iRobotI);
00203
00204
00205 if (itsMgr->parseCommandLine(argc, argv, "", 0, 0) == false)
00206 return(1);
00207
00208 char endpointBuff[64];
00209 sprintf(endpointBuff, "default -p %d", 10000);
00210 itsAdapter = communicator()->createObjectAdapterWithEndpoints("IRobotSimAdapter", endpointBuff);
00211
00212 Ice::ObjectPtr object = iRobotI.get();
00213 Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("IRobotService"));
00214 itsAdapter->activate();
00215
00216 itsMgr->start();
00217
00218
00219 IceUtil::ThreadPtr iRobotIThread = iRobotI.get();
00220 iRobotIThread->start();
00221 LINFO("Started");
00222
00223 return true;
00224
00225 }
00226
00227
00228 int main(int argc, char** argv) {
00229 IRobotSimService svc;
00230 return svc.main(argc, argv);
00231
00232 }
00233