test-SeaBee3Simulator.C
00001 #include "Component/ModelManager.H"
00002 #include "Component/ModelComponent.H"
00003 #include "Component/ModelOptionDef.H"
00004 #include "Robots/SeaBeeIII/SeaBee3Simulator.H"
00005 #include <Ice/Ice.h>
00006 #include <Ice/Service.h>
00007 #include "Ice/RobotSimEvents.ice.H"
00008 #include "Ice/RobotBrainObjects.ice.H"
00009 #include "Ice/SimEventsUtils.H"
00010 #include "Ice/IceImageUtils.H"
00011 #include "Image/MatrixOps.H"
00012 #include "Media/FrameSeries.H"
00013
00014
00015
00016 void handle_keys(nub::soft_ref<OutputFrameSeries> ofs, nub::soft_ref<SeaBee3Simulator> subSim)
00017 {
00018
00019 const nub::soft_ref<ImageDisplayStream> ids =
00020 ofs->findFrameDestType<ImageDisplayStream>();
00021
00022 const rutz::shared_ptr<XWinManaged> uiwin =
00023 ids.is_valid()
00024 ? ids->getWindow("subSim")
00025 : rutz::shared_ptr<XWinManaged>();
00026
00027 int key = uiwin->getLastKeyPress();
00028 if (key != -1)
00029 {
00030 float yaw = 0;
00031 float forward = 0;
00032 float up = 0;
00033
00034 switch(key)
00035 {
00036 case 8:
00037 case 38: up = -100.0; break;
00038 case 14:
00039 case 52: up = 100.0; break;
00040 case 43:
00041 case 33: yaw = 100.0; break;
00042 case 39:
00043 case 32: yaw = -100.0; break;
00044 case 10:
00045 case 40: forward = 100.0; break;
00046 case 16:
00047 case 54: forward = -100.0; break;
00048 case 65:
00049 yaw = 0;
00050 forward = 0;
00051 up = 0;
00052 break;
00053 case 20:
00054 case 24:
00055 exit(0);
00056 break;
00057 }
00058
00059 float forwardLeftThruster = forward;
00060 float forwardRightThruster = forward;
00061 float verticalLeftThruster = up;
00062 float verticalRightThruster = up;
00063 float forwardStrafeThruster = yaw;
00064 float backStrafeThruster = -yaw;
00065
00066 subSim->setThrusters(forwardLeftThruster, forwardRightThruster, verticalLeftThruster, verticalRightThruster,
00067 forwardStrafeThruster, backStrafeThruster);
00068
00069 LINFO("Key is %i\n", key);
00070 }
00071 }
00072
00073
00074 class RobotBrainServiceService : public Ice::Service {
00075 protected:
00076 virtual bool start(int, char* argv[]);
00077 virtual bool stop() {
00078 if (itsMgr)
00079 delete itsMgr;
00080 return true;
00081 }
00082
00083 private:
00084 Ice::ObjectAdapterPtr itsAdapter;
00085 ModelManager *itsMgr;
00086 };
00087
00088 bool RobotBrainServiceService::start(int argc, char* argv[])
00089 {
00090 char adapterStr[255];
00091
00092
00093 int port = RobotBrainObjects::RobotBrainPort;
00094 bool connected = false;
00095
00096 while(!connected)
00097 {
00098 try
00099 {
00100 LINFO("Trying Port:%d", port);
00101 sprintf(adapterStr, "default -p %i", port);
00102 itsAdapter = communicator()->createObjectAdapterWithEndpoints("SeaBee3SimulatorAdapter",
00103 adapterStr);
00104 connected = true;
00105 }
00106 catch(Ice::SocketException)
00107 {
00108 port++;
00109 }
00110 }
00111
00112
00113 itsMgr = new ModelManager("SeaBee3SimulatorServiceManager");
00114
00115 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*itsMgr));
00116 itsMgr->addSubComponent(ofs);
00117
00118
00119 LINFO("Starting SeaBee3 Simulator");
00120 nub::ref<SeaBee3Simulator> subSim(new SeaBee3Simulator(*itsMgr, "SeaBee3Simulator", "SeaBee3Simulator"));
00121 itsMgr->addSubComponent(subSim);
00122 subSim->init(communicator(), itsAdapter);
00123
00124 itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0);
00125
00126 itsAdapter->activate();
00127
00128 itsMgr->start();
00129
00130 while(1){
00131 Layout<PixRGB<byte> > outDisp;
00132
00133 subSim->simLoop();
00134 Image<PixRGB<byte> > forwardCam = flipVertic(subSim->getFrame(1));
00135 Image<PixRGB<byte> > downwardCam = flipVertic(subSim->getFrame(2));
00136
00137 outDisp = vcat(outDisp, hcat(forwardCam, downwardCam));
00138
00139 ofs->writeRgbLayout(outDisp, "subSim", FrameInfo("subSim", SRC_POS));
00140
00141 handle_keys(ofs, subSim);
00142 }
00143
00144
00145 return true;
00146 }
00147
00148
00149 int main(int argc, char** argv) {
00150
00151 RobotBrainServiceService svc;
00152 return svc.main(argc, argv);
00153 }
00154
00155
00156