tRunCtrlSimulator.cpp

00001 /*
00002   tRunCtrl のシミュレートクラス
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "tRunCtrlSimulator.h"
00008 #include "runCtrl.h"
00009 #include "tRunCtrl.h"
00010 #include "taskCtrl.h"
00011 #include "transferCtrl.h"
00012 #include "sh7045lib.h"
00013 extern void init_sci(int port, ConnectionDevice* con);
00014 
00015 
00016 tRunCtrl_Simulator::tRunCtrl_Simulator(void) : server(NULL), con(NULL) {
00017 }
00018 
00019 
00020 tRunCtrl_Simulator::~tRunCtrl_Simulator(void) {
00021   stop_sci(SciPort);
00022   delete con;
00023   delete server;
00024 }
00025 
00026 
00027 void tRunCtrl_Simulator::init(void) {
00028   // サーバの起動
00029   server = new TcpipServer();
00030   server->activate(RunCtrl::SimulatorPort);
00031 
00032   initTransferCtrl(SciPort);
00033   initNodeInfo(&node);
00034   init_tRunCtrlState(&tbl, PACKAGE_NUM_VERSION);
00035   registerStructInfo(&node, (unsigned char *)&tbl, RUN_CTRL_TARGET_ID);
00036 }
00037 
00038 
00039 void tRunCtrl_Simulator::recv(void) {
00040   // 接続
00041   if (!con) {
00042     con = server->accept(0);
00043     if (con) {
00044       init_sci(SciPort, con);
00045     }
00046 #if 0
00047     TcpipDevice* connection = server->accept(0);
00048     if (connection) {
00049       con = new DelaiedTcpipDevice(connection);
00050       init_sci(SciPort, con);
00051     }
00052 #endif
00053   }
00054 
00055   // データの受信、応答
00056   if (con) {
00057     if (con->isConnected()) {
00058       recv_tRunCtrlPacket(&tbl);
00059     } else {
00060       delete con;
00061       con = NULL;
00062     }
00063   }
00064 }
00065 
00066 
00067 void tRunCtrl_Simulator::exec1msec(unsigned long total_msec) {
00068   // 制御処理
00069   if (con) {
00070     update_tRunCtrlState(&tbl);
00071 
00072     VXV::Position3D sim_pos, dummy;
00073     sim_pos = getBodyPosition(dummy);
00074     ticksPos.add(sim_pos, total_msec);
00075   }
00076 }
00077 
00078 
00079 bool tRunCtrl_Simulator::updatePosition(void) {
00080   return true;
00081 }
00082 
00083 
00084 void tRunCtrl_Simulator::setLocalPosition(const VXV::Position3D& position) {
00085 
00086   // X, Y の更新
00087   for (int i = 0; i < 2; ++i) {
00088     tbl.bodyPos.cnt_decimal[i] = 0;
00089   }
00090 
00091   long mm[2] = { position.x, position.y };
00092   for (int i = 0; i < 2; ++i) {
00093     tbl.bodyPos.body_crd.km[i] = 0;
00094     tbl.bodyPos.body_crd.m[i] = 0;
00095     tbl.bodyPos.body_crd.mm[i] = 0;
00096 
00097     while (mm[i] >= 1000000) {
00098       ++(tbl.bodyPos.body_crd.km[i]);
00099       mm[i] -= 1000000;
00100     }
00101     while (mm[i] <= -1000000) {
00102       --(tbl.bodyPos.body_crd.km[i]);
00103       mm[i] += 1000000;
00104     }
00105 
00106     while (mm[i] >= 1000) {
00107       ++(tbl.bodyPos.body_crd.m[i]);
00108       mm[i] -= 1000;
00109     }
00110     while (mm[i] <= -1000) {
00111       --(tbl.bodyPos.body_crd.m[i]);
00112       mm[i] += 1000;
00113     }
00114     tbl.bodyPos.cnt_integer[i] = mm[i] * tbl.bodyPos.cnt_per_m / 1000;
00115   }
00116 
00117   // 角度の更新
00118   long pre_div16_cnt = tbl.bodyPos.div16_cnt;
00119   tbl.bodyPos.div16_cnt =
00120     (static_cast<long>(0x10000 * position.zt.to_rad() / (2.0 * M_PI))
00121      << (DIV16_MUL_SHIFT - 16)) / tbl.bodyPos.div16_cnt_mul;
00122   if (pre_div16_cnt + (tbl.bodyPos.div16_cnt_max>>1) < tbl.bodyPos.div16_cnt) {
00123     --(tbl.bodyPos.rotate_num);
00124   } else if (pre_div16_cnt - (tbl.bodyPos.div16_cnt_max >> 1) <
00125              tbl.bodyPos.div16_cnt) {
00126     ++(tbl.bodyPos.rotate_num);
00127   }
00128 }
00129 
00130 
00131 VXV::Position3D tRunCtrl_Simulator::getBodyPosition(const VXV::Position3D&
00132                                                     position) {
00133   // 筐体位置を返す
00134   int x_mm, y_mm;
00135   get_mmCoordinate(&tbl.gl_crd, &x_mm, &y_mm);
00136   return VXV::Position(x_mm, y_mm,
00137                        VXV::Direction::rad(2.0 * M_PI * tbl.gl_crd.div16
00138                                            / 0x10000));
00139 }
00140 

Generated on Mon Apr 13 22:52:03 2009 by  doxygen 1.5.7.1