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
00039 #include "Component/ModelManager.H"
00040 #include "Image/Image.H"
00041 #include "Image/Transforms.H"
00042 #include "Image/DrawOps.H"
00043 #include "Image/FilterOps.H"
00044 #include "Image/Rectangle.H"
00045 #include "Image/ShapeOps.H"
00046 #include "Image/ColorOps.H"
00047 #include "Image/Rectangle.H"
00048 #include "Image/MathOps.H"
00049 #include "Image/Layout.H"
00050 #include "Media/FrameSeries.H"
00051 #include "Transport/FrameInfo.H"
00052 #include "Raster/GenericFrame.H"
00053 #include "Raster/Raster.H"
00054 #include "GUI/XWinManaged.H"
00055
00056 #define KEY_UP 98
00057 #define KEY_DOWN 104
00058 #define KEY_LEFT 100
00059 #define KEY_RIGHT 102
00060
00061 struct State
00062 {
00063 int x;
00064 int y;
00065 int ori;
00066
00067 State(int _x, int _y, int _ori) :
00068 x(_x), y(_y), ori(_ori)
00069 {}
00070 };
00071
00072
00073 void drawRobot(Image<PixRGB<byte> > &img, const State& robotState)
00074 {
00075 drawCircle(img,
00076 Point2D<int>(img.getWidth()/2 + robotState.x, img.getHeight()/2+robotState.y),
00077 15, PixRGB<byte>(0,255,0));
00078
00079
00080 float ori = (float)robotState.ori*M_PI/180;
00081 int x1 = int(cos(ori)*10);
00082 int y1 = int(sin(ori)*10);
00083
00084 drawLine(img,
00085 Point2D<int>(img.getWidth()/2 + robotState.x, img.getHeight()/2+robotState.y),
00086 Point2D<int>(img.getWidth()/2 + robotState.x+x1, img.getHeight()/2+robotState.y+y1),
00087 PixRGB<byte>(255,0,0));
00088
00089
00090 }
00091
00092 State moveRobot (State& curState, float tranVel, float rotVel)
00093 {
00094
00095 rotVel += 1.0e-10;
00096 float dt = 1.0;
00097 State newState(0,0,0);
00098 float ori = (float)curState.ori*M_PI/180;
00099 newState.x = curState.x + (int)(-tranVel/rotVel*sin(ori) + tranVel/rotVel*sin(ori+rotVel*dt));
00100 newState.y = curState.y + (int)(tranVel/rotVel*cos(ori) - tranVel/rotVel*cos(ori+rotVel*dt));
00101 newState.ori = curState.ori + (int)((rotVel*dt)*180/M_PI);
00102 return newState;
00103
00104 }
00105
00106 float sample(float b)
00107 {
00108 int dist = 1;
00109 switch (dist)
00110 {
00111 case 0:
00112 {
00113 float sum =0;
00114
00115 for(int i=0; i<12; i++)
00116 sum += (randomDouble()*b*2)-b;
00117 return 0.5*sum;
00118 }
00119 case 1:
00120 {
00121 return sqrt(6.0)/2.0 *
00122 (((randomDouble()*b*2)-b) + ((randomDouble()*b*2)-b));
00123 }
00124 }
00125
00126 return 0;
00127 }
00128
00129
00130 void drawSamples(Image<PixRGB<byte> > &img, float tranVel, float rotVel, const State& curState)
00131 {
00132 rotVel += 1.0e-10;
00133 float dt = 1.0;
00134
00135 float alpha_1 = 1.5; float alpha_2 = 0;
00136 float alpha_3 = 0.5; float alpha_4 = 0;
00137 float alpha_5 = 0.2; float alpha_6 = 0;
00138
00139
00140 float tranVel_hat = tranVel + sample(alpha_1+alpha_2);
00141 float rotVel_hat = rotVel + sample(alpha_3 + alpha_4);
00142 float gamma_hat = sample(alpha_5 + alpha_6);
00143
00144 State predictState(0,0,0);
00145 float ori = (float)curState.ori*M_PI/180;
00146 predictState.x = curState.x + (int)(-tranVel_hat/rotVel_hat*sin(ori) + tranVel_hat/rotVel_hat*sin(ori+rotVel_hat*dt));
00147 predictState.y = curState.y + (int)(tranVel_hat/rotVel_hat*cos(ori) - tranVel_hat/rotVel_hat*cos(ori+rotVel_hat*dt));
00148 predictState.ori = curState.ori + (int)((rotVel_hat*dt)*180/M_PI) + (int)((gamma_hat*dt)*180/M_PI);
00149
00150 Point2D<int> pPt(img.getWidth()/2 + predictState.x, img.getHeight()/2+predictState.y);
00151 if (img.coordsOk(pPt))
00152 img.setVal(pPt, PixRGB<byte>(255,0,255));
00153
00154 }
00155
00156
00157 int main(const int argc, const char **argv)
00158 {
00159 MYLOGVERB = LOG_INFO;
00160 ModelManager *mgr = new ModelManager("Test Motion Models");
00161
00162 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00163 mgr->addSubComponent(ofs);
00164
00165 mgr->exportOptions(MC_RECURSE);
00166
00167 if (mgr->parseCommandLine(
00168 (const int)argc, (const char**)argv, "", 0, 0) == false)
00169 return 1;
00170
00171 mgr->start();
00172
00173 initRandomNumbers();
00174
00175 State robotState(0,0,0);
00176
00177
00178 while(1)
00179 {
00180 Image<PixRGB<byte> > world(512,512, ZEROS);
00181 LINFO("State: %i,%i %i", robotState.x, robotState.y, robotState.ori);
00182 drawRobot(world, robotState);
00183
00184 float transVel = 100;
00185 float rotVel = 1;
00186
00187 for(int i=0; i<100; i++)
00188 drawSamples(world, transVel, rotVel, robotState);
00189
00190 robotState = moveRobot(robotState, transVel, rotVel);
00191
00192 ofs->writeRGB(world, "Output", FrameInfo("Output", SRC_POS));
00193 getchar();
00194
00195 }
00196 mgr->stop();
00197
00198 return 0;
00199 }
00200