test-IRobotSim.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 "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 <stdio.h>
00048 #include <stdlib.h>
00049
00050 #define KEY_UP 98
00051 #define KEY_DOWN 104
00052 #define KEY_LEFT 100
00053 #define KEY_RIGHT 102
00054
00055 bool worldView = true;
00056
00057 void handle_keys(nub::soft_ref<OutputFrameSeries> ofs, nub::soft_ref<IRobotSim> iRobotSim)
00058 {
00059
00060 const nub::soft_ref<ImageDisplayStream> ids =
00061 ofs->findFrameDestType<ImageDisplayStream>();
00062
00063 const rutz::shared_ptr<XWinManaged> uiwin =
00064 ids.is_valid()
00065 ? ids->getWindow("IRobotSim")
00066 : rutz::shared_ptr<XWinManaged>();
00067
00068
00069 int key = uiwin->getLastKeyPress();
00070 if (key != -1)
00071 {
00072 switch(key)
00073 {
00074 case KEY_UP:
00075 iRobotSim->setMotors(10, 10);
00076 break;
00077 case KEY_DOWN:
00078 iRobotSim->setMotors(-2.5, -2.5);
00079 break;
00080 case KEY_LEFT:
00081 iRobotSim->setMotors(2.5, -2.5);
00082 break;
00083 case KEY_RIGHT:
00084 iRobotSim->setMotors(-2.5, 2.5);
00085 break;
00086 case 65:
00087 iRobotSim->setMotors(0, 0);
00088 break;
00089 case 25:
00090 worldView = !worldView;
00091 break;
00092 default:
00093 LINFO("Unkown key %i\n", key);
00094 break;
00095 }
00096
00097 }
00098 }
00099
00100
00101 int main(int argc, char *argv[])
00102 {
00103
00104 ModelManager manager("IRobot Simulator");
00105
00106 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00107 manager.addSubComponent(ofs);
00108
00109
00110 nub::soft_ref<IRobotSim> iRobotSim(new IRobotSim(manager));
00111 manager.addSubComponent(iRobotSim);
00112
00113
00114 if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00115
00116 LINFO("s");
00117
00118 manager.start();
00119 LINFO("s1");
00120
00121 iRobotSim->initViewport();
00122
00123
00124 while(1){
00125 Layout<PixRGB<byte> > outDisp;
00126
00127 iRobotSim->simLoop();
00128
00129 Image<PixRGB<byte> > camImage;
00130
00131 if (worldView)
00132 camImage = flipVertic(iRobotSim->getFrame(-1));
00133 else
00134 camImage = flipVertic(iRobotSim->getFrame(1));
00135
00136
00137 float xPos, yPos, ori;
00138 iRobotSim->getSensors(xPos, yPos, ori);
00139
00140
00141 ofs->writeRGB(camImage, "IRobotSim", FrameInfo("IRobotSim", SRC_POS));
00142
00143 handle_keys(ofs, iRobotSim);
00144
00145 }
00146 return 0;
00147
00148 }