inputHandle.cpp

00001 /*
00002   入力をロボットの移動速度に変換
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "inputHandle.h"
00008 #include "vutils.h"
00009 #include "timeUtils.h"
00010 
00011 
00012 void drawJetMode(const robotInput_t& ri) {
00013   static bool pre_jet = false;
00014   static unsigned long draw_index = 0;
00015 
00016   if (ri.jet != pre_jet) {
00017     pre_jet = ri.jet;
00018     if (ri.jet) {
00019       // 描画
00020       draw_index = vmonitor::drawText("Jet !", VXV::Grid(10, 10));
00021     } else {
00022       // 削除
00023       vmonitor::clear(draw_index);
00024     }
00025   }
00026 }
00027 
00028 
00029 void drawCaptures(URGCtrl& urg, RunCtrl& run) {
00030   static TicksPosition ticksPos;
00031   static unsigned long pre_index = 0;
00032 
00033   ticksPos.add(run.crd_position, run.crd_ticks);
00034 
00035   urg.convert(ticksPos);
00036   vmonitor::clear(pre_index);
00037   pre_index = vmonitor::drawPoints(urg.crd_points, Red);
00038 
00039   ticksPos.del_olderThan(urg.crd_ticks - 5000);
00040 }
00041 
00042 
00043 // 加速度を考慮した速度計算
00044 static double setVelocity(double now, double ref,
00045                           double RefAcc, long diff) {
00046   double sign = ref - now;
00047   double add = RefAcc * diff / 1000.0;
00048   double value = now + ((sign > 0.0) ? +add : -add);
00049   if (sign > 0.0) {
00050     if (value > ref) {
00051       value = ref;
00052     }
00053   } else {
00054     if (value < ref) {
00055       value = ref;
00056     }
00057   }
00058   return value;
00059 }
00060 
00061 
00062 // 入力の取得
00063 robotInput_t inputHandle(const JoystickCtrl& js,
00064                          const UserInput::userInput_t& key) {
00065   static unsigned long pre_ticks = VXV::GetTicks();
00066   static double now_straight = 0;
00067   static double now_rotate = 0.0;
00068   const double RotateRef = (2.0 * M_PI) / 4.0;
00069   const double RotateAcc = (2.0 * M_PI) * 3.0 / 4.0;
00070   const int StraightRef = 300;
00071   const int StraightAcc = 300;
00072 
00073   unsigned long ticks = VXV::GetTicks();
00074   double ref_straight = 0;
00075   double ref_rotate = 0.0;
00076   robotInput_t ri;
00077   ri.jet = false;
00078 
00079   // キー入力
00080   if (key.nowPressedCode(SDLK_RSHIFT) || key.nowPressedCode(SDLK_LSHIFT)) {
00081     ref_straight = StraightRef;
00082   }
00083   if (key.nowPressedCode(SDLK_a)) {
00084     ref_rotate = RotateRef;
00085   }
00086   if (key.nowPressedCode(SDLK_s)) {
00087     ref_rotate -= RotateRef;
00088   }
00089   if (key.nowPressedCode(SDLK_RETURN)) {
00090     ri.jet = true;
00091   }
00092 
00093   // ジョイスティック入力
00094   if (js.isActivated()) {
00095     int js_x = -js.getAxisValue(0);
00096     ref_rotate += (js_x > 3200) ? RotateRef : (js_x < -3200) ? -RotateRef : 0;
00097 
00098     if (js.getButtonValue(0)) {
00099       // 並進移動(前進)
00100       ref_straight = StraightRef;
00101     } else if (js.getButtonValue(3)) {
00102       ref_straight = -StraightRef;
00103     }
00104     if (js.getButtonValue(6) || js.getButtonValue(7)) {
00105       // 並進速度を倍にする
00106       ri.jet = true;
00107     }
00108   }
00109   if (ri.jet) {
00110     ref_straight *= 2.0;
00111   }
00112   long diff = ticks - pre_ticks;
00113   pre_ticks = ticks;
00114   now_straight = setVelocity(now_straight, ref_straight, StraightAcc, diff);
00115   now_rotate = setVelocity(now_rotate, ref_rotate, RotateAcc, diff);
00116 
00117   ri.straight = static_cast<int>(now_straight);
00118   ri.rotate = VXV::Direction::rad(now_rotate);
00119   return ri;
00120 }
00121 
00122 
00123 // ロボットの制御
00124 void robotCtrl(mRunCtrl& run, const robotInput_t& ri) {
00125   enum { RIGHT = 0, LEFT = 1 };
00126 
00127   // 並進速度、回転速度、トレッドから車輪毎の目標回転速度を計算
00128   int whl_mm_vel[2];
00129   int rotate_mm_vel = static_cast<int>(BODY_TREAD_MM * ri.rotate.to_rad() / 2);
00130   whl_mm_vel[RIGHT] = ri.straight + rotate_mm_vel;
00131   whl_mm_vel[LEFT] = ri.straight - rotate_mm_vel;
00132 
00133   for (int i = 0; i < 2; ++i) {
00134     run.setWheelVel(i, whl_mm_vel[i]);
00135   }
00136 }
00137 

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