move_ctrl.c

00001 /*
00002   移動コマンドの実装
00003   Satofumi KAMIMURA
00004   $Id$
00005 
00006   static int fd な変数が run_ctrl.c で宣言されているので、
00007   このソースコードは、run_ctrl.c にて include することでコンパイルしている
00008 */
00009 
00010 
00011 #if 0
00012 int RunCtrl::to_div16(const VXV::Direction& t) {
00013   return static_cast<int>(0x10000 * t.to_rad() / (2.0 * M_PI));
00014 }
00015 #endif
00016 
00017 
00018 static position_t getRunCmdPos(int crdId, int x, int y, direction_t zt) {
00019   position_t run_pos;
00020   run_pos.x = x;
00021   run_pos.y = y;
00022   run_pos.zt = zt;
00023 
00024   direction_t rotate;
00025   rotate.div16 = -local_offset.zt.div16;
00026   position_t offset;
00027   convert(offset, local_offset,
00028           createConvertMatrix(VXV::Position(0, 0, rotate)));
00029   run_pos -= VXV::Position(local_offset.x, local_offset.y, VXV::Direction());
00030   convertWithAngle(run_pos, run_pos, VXV::Position(0, 0, rotate));
00031 
00032   if (crdId == GL_ID) {
00033     run_pos = getRootCrd()->getCrdPosition(this, run_pos);
00034   } else {
00035     run_pos = crd->getCrdPosition(this, run_pos);
00036   }
00037   return run_pos;
00038 }
00039 #endif
00040 
00041 
00042 int run_stop(void) {
00043   //runCommand_t& cmd = state_stack.front().command;
00044   if (sendStop(cmd.send_command, &cmd.send_command_size) < 0) {
00045     cmd.send_command_size = 0;
00046     //throw RunCtrl_Exception("Transmit fail: in sendStop()");
00047     return -1;
00048   }
00049   return 0;
00050 }
00051 
00052 
00053 int run_followLine(int x, int y, direction_t angle, int crdId) {
00054   //VXV::Position run_pos = getRunCmdPos(crd, position);
00055   position_t run_pos = getRunCmdPos(crdId, x, y, angle);
00056   //runCommand_t& cmd = state_stack.front().command;
00057   if (sendFollowLine(run_pos.x, run_pos.y, to_div16(run_pos.zt),
00058                      cmd.send_command, &cmd.send_command_size) < 0) {
00059     cmd.send_command_size = 0;
00060     throw RunCtrl_Exception("Transmit fail: in sendFollowLine()");
00061   }
00062   cmd.position = position;
00063   cmd.crd = crd;
00064 }
00065 
00066 
00067 #if 0 // !!! 以降は保留中
00068 void RunCtrl::followCircle(const VXV::Grid& center, int radius,
00069                            const CoordinateCtrl* crd) {
00070   int set_radius = (radius != 0) ? radius : 1;
00071   VXV::Position run_pos =
00072     getRunCmdPos(crd, VXV::Position(center.x, center.y, VXV::Direction()));
00073 
00074   int follow_r = state_stack.front().params.follow_r;
00075   if (abs(radius) < follow_r) {
00076     setCurveRadius(abs(radius));
00077   }
00078   runCommand_t& cmd = state_stack.front().command;
00079   if (sendFollowCircle(run_pos.x, run_pos.y, set_radius,
00080                        cmd.send_command, &cmd.send_command_size) < 0) {
00081     cmd.send_command_size = 0;
00082     throw RunCtrl_Exception("Transmit fail: in sendFollowCircle()");
00083   }
00084   setCurveRadius(follow_r);
00085 
00086   cmd.position = VXV::Position(center.x, center.y, VXV::Direction());
00087   cmd.crd = crd;
00088 }
00089 
00090 
00091 void RunCtrl::followCircleOnTangent(const VXV::Position& position, int radius,
00092                                     const CoordinateCtrl* crd) {
00093 
00094   double t_add = (radius > 0) ? +M_PI/2.0 : -M_PI/2.0;
00095   double radian = position.zt.to_rad() + t_add + M_PI;
00096   VXV::Grid center(position.x + static_cast<int>(abs(radius) * cos(radian)),
00097                    position.y + static_cast<int>(abs(radius) * sin(radian)));
00098 
00099   return followCircle(center, radius, crd);
00100 }
00101 
00102 
00103 void RunCtrl::stopToLine(const VXV::Position& position,
00104                          const CoordinateCtrl* crd) {
00105   VXV::Position run_pos = getRunCmdPos(crd, position);
00106 
00107   runCommand_t& cmd = state_stack.front().command;
00108   if (sendStopToLine(run_pos.x, run_pos.y, to_div16(run_pos.zt),
00109                      cmd.send_command, &cmd.send_command_size) < 0) {
00110     cmd.send_command_size = 0;
00111     throw RunCtrl_Exception("Transmit fail: in sendStopToLine()");
00112   }
00113   cmd.position = position;
00114   cmd.crd = crd;
00115 }
00116 
00117 
00118 void RunCtrl::rotateToDirection(const VXV::Direction& direction,
00119                                 const CoordinateCtrl* crd) {
00120   VXV::Position run_pos = getRunCmdPos(crd, VXV::Position(0, 0, direction));
00121 
00122   runCommand_t& cmd = state_stack.front().command;
00123   if (sendTurnToDirection(to_div16(run_pos.zt),
00124                           cmd.send_command, &cmd.send_command_size) < 0) {
00125     cmd.send_command_size = 0;
00126     throw RunCtrl_Exception("Transmit fail: in sendTurnToDirection()");
00127   }
00128   cmd.position = VXV::Position(0, 0, direction);
00129   cmd.crd = crd;
00130 }
00131 
00132 
00133 void RunCtrl::rotateAngle(const VXV::Direction& direction) {
00134 
00135   runCommand_t& cmd = state_stack.front().command;
00136   int div16 = static_cast<int>(0x10000 * direction.getInnerRadian()
00137                                / (2.0 * M_PI));
00138   if (sendRotateAngle(div16, cmd.send_command, &cmd.send_command_size) < 0) {
00139     cmd.send_command_size = 0;
00140     throw RunCtrl_Exception("Transmit fail: in sendRotateAngle()");
00141   }
00142 }
00143 
00144 
00145 void RunCtrl::spin(const VXV::Direction& velocity) {
00146 
00147   runCommand_t& cmd = state_stack.front().command;
00148   if (sendSpin(to_div16(velocity),
00149                cmd.send_command, &cmd.send_command_size) < 0) {
00150     cmd.send_command_size = 0;
00151     throw RunCtrl_Exception("Transmit fail: in sendSpin()");
00152   }
00153 }
00154 
00155 
00156 void RunCtrl::lastMoveCommand(const CoordinateCtrl* crd) {
00157 
00158   // 移動コマンドの再発行
00159   runCommand_t& cmd = state_stack.front().command;
00160   if (cmd.send_command_size <= 0) {
00161     return;
00162   }
00163   long last_command_unique_id =
00164     getPacketUniqueId((unsigned char*)cmd.send_command);
00165   if (sendLastMoveCommand(last_command_unique_id,
00166                           cmd.send_command, cmd.send_command_size) < 0) {
00167     cmd.send_command_size = 0;
00168     throw RunCtrl_Exception("Transmit fail: in sendLastMoveCommand()");
00169   }
00170 
00171   // 位置の再評価
00172   if (crd_auto) {
00173     sendUpdatedPosition(cmd.crd, cmd.position);
00174   }
00175 }
00176 #endif
00177 

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