00001
00002
00003
00004
00005
00006
00007 #include "runCtrl.h"
00008
00009
00010 int RunCtrl::to_div16(const VXV::Direction& t) {
00011 return static_cast<int>(0x10000 * t.to_rad() / (2.0 * M_PI));
00012 }
00013
00014
00015 const VXV::Position RunCtrl::getRunCmdPos(const CoordinateCtrl* crd,
00016 const VXV::Position& position) {
00017 VXV::Position3D run_pos = position;
00018
00019 VXV::Direction rotate = VXV::Direction() - local_offset.zt;
00020 VXV::Position3D offset;
00021 convert(offset, local_offset,
00022 createConvertMatrix(VXV::Position(0, 0, rotate)));
00023 run_pos -= VXV::Position(local_offset.x, local_offset.y, VXV::Direction());
00024 convertWithAngle(run_pos, run_pos, VXV::Position(0, 0, rotate));
00025
00026 if (crd == VXV::GL) {
00027 run_pos = getRootCrd()->getCrdPosition(this, run_pos);
00028 } else {
00029 run_pos = crd->getCrdPosition(this, run_pos);
00030 }
00031 return run_pos;
00032 }
00033
00034
00035 void RunCtrl::stop(void) {
00036 runCommand_t& cmd = state_stack.front().command;
00037 if (sendStop(cmd.send_command, &cmd.send_command_size) < 0) {
00038 cmd.send_command_size = 0;
00039 throw RunCtrl_Exception("Transmit fail: in sendStop()");
00040 }
00041 }
00042
00043
00044 void RunCtrl::followLine(const VXV::Position& position,
00045 const CoordinateCtrl* crd) {
00046 VXV::Position run_pos = getRunCmdPos(crd, position);
00047
00048 runCommand_t& cmd = state_stack.front().command;
00049 if (sendFollowLine(run_pos.x, run_pos.y, to_div16(run_pos.zt),
00050 cmd.send_command, &cmd.send_command_size) < 0) {
00051 cmd.send_command_size = 0;
00052 throw RunCtrl_Exception("Transmit fail: in sendFollowLine()");
00053 }
00054 cmd.position = position;
00055 cmd.crd = crd;
00056 }
00057
00058
00059
00060 void RunCtrl::followCircle(const VXV::Grid& center, int radius,
00061 const CoordinateCtrl* crd) {
00062 int set_radius = (radius != 0) ? radius : 1;
00063 VXV::Position run_pos =
00064 getRunCmdPos(crd, VXV::Position(center.x, center.y, VXV::Direction()));
00065
00066 int follow_r = state_stack.front().params.follow_r;
00067 if (abs(radius) < follow_r) {
00068 setCurveRadius(abs(radius));
00069 }
00070 runCommand_t& cmd = state_stack.front().command;
00071 if (sendFollowCircle(run_pos.x, run_pos.y, set_radius,
00072 cmd.send_command, &cmd.send_command_size) < 0) {
00073 cmd.send_command_size = 0;
00074 throw RunCtrl_Exception("Transmit fail: in sendFollowCircle()");
00075 }
00076 setCurveRadius(follow_r);
00077
00078 cmd.position = VXV::Position(center.x, center.y, VXV::Direction());
00079 cmd.crd = crd;
00080 }
00081
00082
00083 void RunCtrl::followCircleOnTangent(const VXV::Position& position, int radius,
00084 const CoordinateCtrl* crd) {
00085
00086 double t_add = (radius > 0) ? +M_PI/2.0 : -M_PI/2.0;
00087 double radian = position.zt.to_rad() + t_add + M_PI;
00088 VXV::Grid center(position.x + static_cast<int>(abs(radius) * cos(radian)),
00089 position.y + static_cast<int>(abs(radius) * sin(radian)));
00090
00091 return followCircle(center, radius, crd);
00092 }
00093
00094
00095 void RunCtrl::stopToLine(const VXV::Position& position,
00096 const CoordinateCtrl* crd) {
00097 VXV::Position run_pos = getRunCmdPos(crd, position);
00098
00099 runCommand_t& cmd = state_stack.front().command;
00100 if (sendStopToLine(run_pos.x, run_pos.y, to_div16(run_pos.zt),
00101 cmd.send_command, &cmd.send_command_size) < 0) {
00102 cmd.send_command_size = 0;
00103 throw RunCtrl_Exception("Transmit fail: in sendStopToLine()");
00104 }
00105 cmd.position = position;
00106 cmd.crd = crd;
00107 }
00108
00109
00110 void RunCtrl::rotateToDirection(const VXV::Direction& direction,
00111 const CoordinateCtrl* crd) {
00112 VXV::Position run_pos = getRunCmdPos(crd, VXV::Position(0, 0, direction));
00113
00114 runCommand_t& cmd = state_stack.front().command;
00115 if (sendTurnToDirection(to_div16(run_pos.zt),
00116 cmd.send_command, &cmd.send_command_size) < 0) {
00117 cmd.send_command_size = 0;
00118 throw RunCtrl_Exception("Transmit fail: in sendTurnToDirection()");
00119 }
00120 cmd.position = VXV::Position(0, 0, direction);
00121 cmd.crd = crd;
00122 }
00123
00124
00125 void RunCtrl::rotateAngle(const VXV::Direction& direction) {
00126
00127 runCommand_t& cmd = state_stack.front().command;
00128 int div16 = static_cast<int>(0x10000 * direction.getInnerRadian()
00129 / (2.0 * M_PI));
00130 if (sendRotateAngle(div16, cmd.send_command, &cmd.send_command_size) < 0) {
00131 cmd.send_command_size = 0;
00132 throw RunCtrl_Exception("Transmit fail: in sendRotateAngle()");
00133 }
00134 }
00135
00136
00137 void RunCtrl::spin(const VXV::Direction& velocity) {
00138
00139 runCommand_t& cmd = state_stack.front().command;
00140 if (sendSpin(to_div16(velocity),
00141 cmd.send_command, &cmd.send_command_size) < 0) {
00142 cmd.send_command_size = 0;
00143 throw RunCtrl_Exception("Transmit fail: in sendSpin()");
00144 }
00145 }
00146
00147
00148 void RunCtrl::lastMoveCommand(const CoordinateCtrl* crd) {
00149
00150
00151 runCommand_t& cmd = state_stack.front().command;
00152 if (cmd.send_command_size <= 0) {
00153 return;
00154 }
00155 long last_command_unique_id =
00156 getPacketUniqueId((unsigned char*)cmd.send_command);
00157 if (sendLastMoveCommand(last_command_unique_id,
00158 cmd.send_command, cmd.send_command_size) < 0) {
00159 cmd.send_command_size = 0;
00160 throw RunCtrl_Exception("Transmit fail: in sendLastMoveCommand()");
00161 }
00162
00163
00164 if (crd_auto) {
00165 sendUpdatedPosition(cmd.crd, cmd.position);
00166 }
00167 }
00168