あ
Dependencies: uw_28015 mbed move4wheel2 EC CruizCore_R6093U CruizCore_R1370P
movement/movement.cpp@1:26fc1b2f1c42, 2019-11-16 (annotated)
- Committer:
- yuki0701
- Date:
- Sat Nov 16 06:26:57 2019 +0000
- Revision:
- 1:26fc1b2f1c42
- Parent:
- 0:b87fd8dd4322
- Child:
- 2:820dcd23c8e3
a;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
la00noix | 0:b87fd8dd4322 | 1 | #include "EC.h" |
la00noix | 0:b87fd8dd4322 | 2 | #include "R1370P.h" |
la00noix | 0:b87fd8dd4322 | 3 | #include "move4wheel.h" |
la00noix | 0:b87fd8dd4322 | 4 | #include "mbed.h" |
la00noix | 0:b87fd8dd4322 | 5 | #include "math.h" |
la00noix | 0:b87fd8dd4322 | 6 | #include "PathFollowing.h" |
la00noix | 0:b87fd8dd4322 | 7 | #include "movement.h" |
la00noix | 0:b87fd8dd4322 | 8 | #include "manual.h" |
la00noix | 0:b87fd8dd4322 | 9 | #include "can.h" |
yuki0701 | 1:26fc1b2f1c42 | 10 | #include "R6093U.h" |
la00noix | 0:b87fd8dd4322 | 11 | |
la00noix | 0:b87fd8dd4322 | 12 | #define PI 3.141592 |
la00noix | 0:b87fd8dd4322 | 13 | |
la00noix | 0:b87fd8dd4322 | 14 | char can_ashileddata[2]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 15 | char can_ashileddata2[8]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 16 | //char can_ashileddata3[2]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 17 | //char can_ashileddata4[2]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 18 | //char can_ashileddata5[2]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 19 | |
la00noix | 0:b87fd8dd4322 | 20 | int can_ashileddata0_0,can_ashileddata0_1,can_ashileddata0_2,can_ashileddata0_3; |
la00noix | 0:b87fd8dd4322 | 21 | |
yuki0701 | 1:26fc1b2f1c42 | 22 | Ec EC2(p16,p15,NC,2048,0.05); |
yuki0701 | 1:26fc1b2f1c42 | 23 | Ec EC1(p18,p17,NC,500,0.05); |
la00noix | 0:b87fd8dd4322 | 24 | |
la00noix | 0:b87fd8dd4322 | 25 | Ticker ec_ticker; //ec角速度計算用ticker |
la00noix | 0:b87fd8dd4322 | 26 | |
yuki0701 | 1:26fc1b2f1c42 | 27 | //R1370P gyro(p9,p10); |
yuki0701 | 1:26fc1b2f1c42 | 28 | |
yuki0701 | 1:26fc1b2f1c42 | 29 | R6093U gyro(p9,p10); |
la00noix | 0:b87fd8dd4322 | 30 | |
la00noix | 0:b87fd8dd4322 | 31 | double new_dist1=0,new_dist2=0; |
la00noix | 0:b87fd8dd4322 | 32 | double old_dist1=0,old_dist2=0; |
la00noix | 0:b87fd8dd4322 | 33 | double d_dist1=0,d_dist2=0; //座標計算用関数 |
la00noix | 0:b87fd8dd4322 | 34 | double d_x,d_y; |
la00noix | 0:b87fd8dd4322 | 35 | //現在地X,y座標、現在角度については、PathFollowingでnow_x,now_y,now_angleを定義済 |
la00noix | 0:b87fd8dd4322 | 36 | double start_x=0,start_y=0; //スタート位置 |
la00noix | 0:b87fd8dd4322 | 37 | double x_out,y_out,r_out; //出力値 |
la00noix | 0:b87fd8dd4322 | 38 | |
la00noix | 0:b87fd8dd4322 | 39 | int16_t m1=0, m2=0, m3=0, m4=0; //int16bit = int2byte |
la00noix | 0:b87fd8dd4322 | 40 | |
la00noix | 0:b87fd8dd4322 | 41 | double xy_type,pm_typeX,pm_typeY,x_base,y_base; |
la00noix | 0:b87fd8dd4322 | 42 | |
la00noix | 0:b87fd8dd4322 | 43 | int flag; |
la00noix | 0:b87fd8dd4322 | 44 | |
yuki0701 | 1:26fc1b2f1c42 | 45 | int RL_mode; |
yuki0701 | 1:26fc1b2f1c42 | 46 | |
la00noix | 0:b87fd8dd4322 | 47 | ///////////////////機体情報をメンバとする構造体"robo_data"と構造体型変数info(←この変数に各センサーにより求めた機体情報(機体位置/機体角度)を格納する)の宣言///////////////// |
la00noix | 0:b87fd8dd4322 | 48 | |
la00noix | 0:b87fd8dd4322 | 49 | /*「info.(機体情報の種類).(使用センサーの種類)」に各情報を格納する |
la00noix | 0:b87fd8dd4322 | 50 | *状況に応じて、どのセンサーにより算出した情報を信用するかを選択し、その都度now_angle,now_x,now_yに代入する。(何種類かのセンサーの情報を混ぜて使用することも可能) |
la00noix | 0:b87fd8dd4322 | 51 | *(ex) |
la00noix | 0:b87fd8dd4322 | 52 | *info.nowX.enc → エンコーダにより算出した機体位置のx座標 |
la00noix | 0:b87fd8dd4322 | 53 | *info.nowY.usw → 超音波センサーにより求めた機体位置のy座標 |
la00noix | 0:b87fd8dd4322 | 54 | */ |
la00noix | 0:b87fd8dd4322 | 55 | |
la00noix | 0:b87fd8dd4322 | 56 | typedef struct { //使用センサーの種類 |
la00noix | 0:b87fd8dd4322 | 57 | double usw; //超音波センサー |
la00noix | 0:b87fd8dd4322 | 58 | double enc; //エンコーダ |
la00noix | 0:b87fd8dd4322 | 59 | double gyro; //ジャイロ |
la00noix | 0:b87fd8dd4322 | 60 | //double line;//ラインセンサー |
la00noix | 0:b87fd8dd4322 | 61 | } robo_sensor; |
la00noix | 0:b87fd8dd4322 | 62 | |
la00noix | 0:b87fd8dd4322 | 63 | typedef struct { //機体情報の種類 |
la00noix | 0:b87fd8dd4322 | 64 | robo_sensor angle; //←機体角度は超音波センサーやラインセンサーからも算出可能なので一応格納先を用意したが、ジャイロの値を完全に信用してもいいかも |
la00noix | 0:b87fd8dd4322 | 65 | robo_sensor nowX; |
la00noix | 0:b87fd8dd4322 | 66 | robo_sensor nowY; |
la00noix | 0:b87fd8dd4322 | 67 | } robo_data; |
la00noix | 0:b87fd8dd4322 | 68 | |
la00noix | 0:b87fd8dd4322 | 69 | robo_data info= {{0,0,0},{0,0,0},{0,0,0}}; //全てのデータを0に初期化 |
la00noix | 0:b87fd8dd4322 | 70 | |
la00noix | 0:b87fd8dd4322 | 71 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
la00noix | 0:b87fd8dd4322 | 72 | |
la00noix | 0:b87fd8dd4322 | 73 | void UserLoopSetting_sensor() |
la00noix | 0:b87fd8dd4322 | 74 | { |
la00noix | 0:b87fd8dd4322 | 75 | |
la00noix | 0:b87fd8dd4322 | 76 | gyro.initialize(); |
la00noix | 0:b87fd8dd4322 | 77 | ec_ticker.attach(&calOmega,0.05); //0.05秒間隔で角速度を計算 |
yuki0701 | 1:26fc1b2f1c42 | 78 | EC1.setDiameter_mm(70); |
yuki0701 | 1:26fc1b2f1c42 | 79 | EC2.setDiameter_mm(70); //測定輪半径//後で測定 |
yuki0701 | 1:26fc1b2f1c42 | 80 | info.nowX.enc = 0; //初期位置の設定 |
yuki0701 | 1:26fc1b2f1c42 | 81 | info.nowY.enc = 0; |
yuki0701 | 1:26fc1b2f1c42 | 82 | } |
yuki0701 | 1:26fc1b2f1c42 | 83 | |
yuki0701 | 1:26fc1b2f1c42 | 84 | void UserLoopSetting_enc_right() |
yuki0701 | 1:26fc1b2f1c42 | 85 | { |
yuki0701 | 1:26fc1b2f1c42 | 86 | info.nowX.enc = 3112; //エンコーダの初期位置の設定(右側フィールド) |
la00noix | 0:b87fd8dd4322 | 87 | info.nowY.enc = 3500; |
yuki0701 | 1:26fc1b2f1c42 | 88 | RL_mode = 0; |
yuki0701 | 1:26fc1b2f1c42 | 89 | } |
yuki0701 | 1:26fc1b2f1c42 | 90 | |
yuki0701 | 1:26fc1b2f1c42 | 91 | void UserLoopSetting_enc_left() |
yuki0701 | 1:26fc1b2f1c42 | 92 | { |
yuki0701 | 1:26fc1b2f1c42 | 93 | info.nowX.enc = -3112; //エンコーダの初期位置の設定(左側フィールド) |
yuki0701 | 1:26fc1b2f1c42 | 94 | info.nowY.enc = 3500; |
yuki0701 | 1:26fc1b2f1c42 | 95 | RL_mode = 1; |
la00noix | 0:b87fd8dd4322 | 96 | } |
la00noix | 0:b87fd8dd4322 | 97 | |
la00noix | 0:b87fd8dd4322 | 98 | void calOmega() //角速度計算関数 |
la00noix | 0:b87fd8dd4322 | 99 | { |
la00noix | 0:b87fd8dd4322 | 100 | EC1.CalOmega(); |
la00noix | 0:b87fd8dd4322 | 101 | EC2.CalOmega(); |
la00noix | 0:b87fd8dd4322 | 102 | } |
la00noix | 0:b87fd8dd4322 | 103 | |
la00noix | 0:b87fd8dd4322 | 104 | void output(double FL,double BL,double BR,double FR) |
la00noix | 0:b87fd8dd4322 | 105 | { |
la00noix | 0:b87fd8dd4322 | 106 | m1=FL; |
la00noix | 0:b87fd8dd4322 | 107 | m2=BL; |
la00noix | 0:b87fd8dd4322 | 108 | m3=BR; |
la00noix | 0:b87fd8dd4322 | 109 | m4=FR; |
la00noix | 0:b87fd8dd4322 | 110 | } |
la00noix | 0:b87fd8dd4322 | 111 | |
la00noix | 0:b87fd8dd4322 | 112 | void base(double FL,double BL,double BR,double FR,double Max) |
la00noix | 0:b87fd8dd4322 | 113 | //いろんな加算をしても最大OR最小がMaxになるような補正//絶対値が一番でかいやつで除算 |
la00noix | 0:b87fd8dd4322 | 114 | //DCモーターならMax=1、マクソンは-4095~4095だからMax=4095にする |
la00noix | 0:b87fd8dd4322 | 115 | { |
la00noix | 0:b87fd8dd4322 | 116 | if(fabs(FL)>=Max||fabs(BL)>=Max||fabs(BR)>=Max||fabs(FR)>=Max) { |
la00noix | 0:b87fd8dd4322 | 117 | |
la00noix | 0:b87fd8dd4322 | 118 | if (fabs(FL)>=fabs(BL)&&fabs(FL)>=fabs(BR)&&fabs(FL)>=fabs(FR))output(Max*FL/fabs(FL),Max*BL/fabs(FL),Max*BR/fabs(FL),Max*FR/fabs(FL)); |
la00noix | 0:b87fd8dd4322 | 119 | else if(fabs(BL)>=fabs(FL)&&fabs(BL)>=fabs(BR)&&fabs(BL)>=fabs(FR))output(Max*FL/fabs(BL),Max*BL/fabs(BL),Max*BR/fabs(BL),Max*FR/fabs(BL)); |
la00noix | 0:b87fd8dd4322 | 120 | else if(fabs(BR)>=fabs(FL)&&fabs(BR)>=fabs(BL)&&fabs(BR)>=fabs(FR))output(Max*FL/fabs(BR),Max*BL/fabs(BR),Max*BR/fabs(BR),Max*FR/fabs(BR)); |
la00noix | 0:b87fd8dd4322 | 121 | else output(Max*FL/fabs(FR),Max*BL/fabs(FR),Max*BR/fabs(FR),Max*FR/fabs(FR)); |
la00noix | 0:b87fd8dd4322 | 122 | } else { |
la00noix | 0:b87fd8dd4322 | 123 | output(FL,BL,BR,FR); |
la00noix | 0:b87fd8dd4322 | 124 | } |
la00noix | 0:b87fd8dd4322 | 125 | } |
la00noix | 0:b87fd8dd4322 | 126 | |
la00noix | 0:b87fd8dd4322 | 127 | void ashi_led() |
la00noix | 0:b87fd8dd4322 | 128 | { |
la00noix | 0:b87fd8dd4322 | 129 | if(now_angle > -1 && now_angle < 1) { |
la00noix | 0:b87fd8dd4322 | 130 | can_ashileddata0_0 = 1; |
la00noix | 0:b87fd8dd4322 | 131 | } else { |
la00noix | 0:b87fd8dd4322 | 132 | can_ashileddata0_0 = 0; |
la00noix | 0:b87fd8dd4322 | 133 | } |
la00noix | 0:b87fd8dd4322 | 134 | |
la00noix | 0:b87fd8dd4322 | 135 | if(now_angle > 350) { |
la00noix | 0:b87fd8dd4322 | 136 | can_ashileddata0_1 = 1; |
la00noix | 0:b87fd8dd4322 | 137 | } else { |
la00noix | 0:b87fd8dd4322 | 138 | can_ashileddata0_1 = 0; |
la00noix | 0:b87fd8dd4322 | 139 | } |
la00noix | 0:b87fd8dd4322 | 140 | |
yuki0701 | 1:26fc1b2f1c42 | 141 | if(RL_mode == 0) { |
yuki0701 | 1:26fc1b2f1c42 | 142 | if(now_x > 3110 && now_x < 3114) { |
yuki0701 | 1:26fc1b2f1c42 | 143 | can_ashileddata0_2 = 1; |
yuki0701 | 1:26fc1b2f1c42 | 144 | } else { |
yuki0701 | 1:26fc1b2f1c42 | 145 | can_ashileddata0_2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 146 | } |
la00noix | 0:b87fd8dd4322 | 147 | |
yuki0701 | 1:26fc1b2f1c42 | 148 | if(now_y > 3498 && now_y < 3502) { |
yuki0701 | 1:26fc1b2f1c42 | 149 | can_ashileddata0_3 = 1; |
yuki0701 | 1:26fc1b2f1c42 | 150 | } else { |
yuki0701 | 1:26fc1b2f1c42 | 151 | can_ashileddata0_3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 152 | } |
yuki0701 | 1:26fc1b2f1c42 | 153 | } else if(RL_mode == 1) { |
yuki0701 | 1:26fc1b2f1c42 | 154 | if(now_x > -3114 && now_x < -3110) { |
yuki0701 | 1:26fc1b2f1c42 | 155 | can_ashileddata0_2 = 1; |
yuki0701 | 1:26fc1b2f1c42 | 156 | } else { |
yuki0701 | 1:26fc1b2f1c42 | 157 | can_ashileddata0_2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 158 | } |
yuki0701 | 1:26fc1b2f1c42 | 159 | |
yuki0701 | 1:26fc1b2f1c42 | 160 | if(now_y > 3498 && now_y < 3502) { |
yuki0701 | 1:26fc1b2f1c42 | 161 | can_ashileddata0_3 = 1; |
yuki0701 | 1:26fc1b2f1c42 | 162 | } else { |
yuki0701 | 1:26fc1b2f1c42 | 163 | can_ashileddata0_3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 164 | } |
la00noix | 0:b87fd8dd4322 | 165 | } |
la00noix | 0:b87fd8dd4322 | 166 | |
la00noix | 0:b87fd8dd4322 | 167 | can_ashileddata[0] = (can_ashileddata0_0<<7 | can_ashileddata0_1<<6 | can_ashileddata0_2<<5 | can_ashileddata0_3<<4); |
la00noix | 0:b87fd8dd4322 | 168 | } |
la00noix | 0:b87fd8dd4322 | 169 | |
la00noix | 0:b87fd8dd4322 | 170 | void calc_gyro() |
la00noix | 0:b87fd8dd4322 | 171 | { |
yuki0701 | 1:26fc1b2f1c42 | 172 | //now_angle=gyro.getAngle(); //ジャイロの値読み込み |
yuki0701 | 1:26fc1b2f1c42 | 173 | now_angle = -gyro.getZ_Angle(); |
yuki0701 | 1:26fc1b2f1c42 | 174 | } |
yuki0701 | 1:26fc1b2f1c42 | 175 | |
yuki0701 | 1:26fc1b2f1c42 | 176 | void print_gyro() |
yuki0701 | 1:26fc1b2f1c42 | 177 | { |
yuki0701 | 1:26fc1b2f1c42 | 178 | while(1) { |
yuki0701 | 1:26fc1b2f1c42 | 179 | //printf("now_gyro = %f\n\r",-gyro.getAngle()); |
yuki0701 | 1:26fc1b2f1c42 | 180 | } |
yuki0701 | 1:26fc1b2f1c42 | 181 | |
la00noix | 0:b87fd8dd4322 | 182 | } |
la00noix | 0:b87fd8dd4322 | 183 | |
la00noix | 0:b87fd8dd4322 | 184 | void calc_xy_enc() //エンコーダ&ジャイロによる座標計算 |
la00noix | 0:b87fd8dd4322 | 185 | { |
yuki0701 | 1:26fc1b2f1c42 | 186 | //now_angle=gyro.getAngle(); //ジャイロの値読み込み |
yuki0701 | 1:26fc1b2f1c42 | 187 | now_angle = -gyro.getZ_Angle(); |
la00noix | 0:b87fd8dd4322 | 188 | |
la00noix | 0:b87fd8dd4322 | 189 | new_dist1=EC1.getDistance_mm(); |
la00noix | 0:b87fd8dd4322 | 190 | new_dist2=EC2.getDistance_mm(); |
la00noix | 0:b87fd8dd4322 | 191 | d_dist1=new_dist1-old_dist1; |
la00noix | 0:b87fd8dd4322 | 192 | d_dist2=new_dist2-old_dist2; |
la00noix | 0:b87fd8dd4322 | 193 | old_dist1=new_dist1; |
la00noix | 0:b87fd8dd4322 | 194 | old_dist2=new_dist2; //微小時間当たりのエンコーダ読み込み |
la00noix | 0:b87fd8dd4322 | 195 | |
la00noix | 0:b87fd8dd4322 | 196 | d_x=d_dist2*sin(now_angle*PI/180)-d_dist1*cos(now_angle*PI/180); |
la00noix | 0:b87fd8dd4322 | 197 | d_y=d_dist2*cos(now_angle*PI/180)+d_dist1*sin(now_angle*PI/180); //微小時間毎の座標変化 |
la00noix | 0:b87fd8dd4322 | 198 | info.nowX.enc = info.nowX.enc + d_x; |
la00noix | 0:b87fd8dd4322 | 199 | info.nowY.enc = info.nowY.enc - d_y; //微小時間毎に座標に加算 |
la00noix | 0:b87fd8dd4322 | 200 | } |
la00noix | 0:b87fd8dd4322 | 201 | |
la00noix | 0:b87fd8dd4322 | 202 | void set_cond(int t, int px, double bx, int py, double by) //超音波センサーを使用するときの条件設定関数 |
la00noix | 0:b87fd8dd4322 | 203 | { |
la00noix | 0:b87fd8dd4322 | 204 | //引数の詳細は関数"calc_xy_usw"参照 |
la00noix | 0:b87fd8dd4322 | 205 | |
la00noix | 0:b87fd8dd4322 | 206 | xy_type = t; |
la00noix | 0:b87fd8dd4322 | 207 | |
la00noix | 0:b87fd8dd4322 | 208 | pm_typeX = px; |
la00noix | 0:b87fd8dd4322 | 209 | x_base = bx; |
la00noix | 0:b87fd8dd4322 | 210 | |
la00noix | 0:b87fd8dd4322 | 211 | pm_typeY = py; |
la00noix | 0:b87fd8dd4322 | 212 | y_base = by; |
la00noix | 0:b87fd8dd4322 | 213 | } |
la00noix | 0:b87fd8dd4322 | 214 | |
la00noix | 0:b87fd8dd4322 | 215 | void calc_xy_usw(double tgt_angle) //超音波センサーによる座標計算(機体が旋回する場合はこの方法による座標計算は出来ない) |
la00noix | 0:b87fd8dd4322 | 216 | { |
la00noix | 0:b87fd8dd4322 | 217 | //tgt_angle:機体の目標角度(運動初期角度と同じ/今大会では0,90,180のみ) |
la00noix | 0:b87fd8dd4322 | 218 | //xy_type:(0:Y軸平行の壁を読む/1:X軸平行の壁を読む/2:X,Y軸平行の壁を共に読む) |
la00noix | 0:b87fd8dd4322 | 219 | //pm_typeX,pm_typeY:(0:各軸正方向側の壁を読む/1:各軸負方向側の壁を読む) |
la00noix | 0:b87fd8dd4322 | 220 | //x_base,y_base:超音波センサーで読む壁の座標(y軸並行の壁のx座標/x軸平行の壁のy座標) |
la00noix | 0:b87fd8dd4322 | 221 | |
la00noix | 0:b87fd8dd4322 | 222 | double R1=240,R2=240,R3=240,R4=240; //機体の中心から各超音波センサーが付いている面までの距離 |
la00noix | 0:b87fd8dd4322 | 223 | double D1=30,D2=0,D3=0,D4=0; //各超音波センサーが付いている面の中心から各超音波センサーまでの距離(時計回りを正とする) |
la00noix | 0:b87fd8dd4322 | 224 | |
yuki0701 | 1:26fc1b2f1c42 | 225 | // now_angle=gyro.getAngle(); //ジャイロの値読み込み |
yuki0701 | 1:26fc1b2f1c42 | 226 | now_angle = -gyro.getZ_Angle(); |
la00noix | 0:b87fd8dd4322 | 227 | |
la00noix | 0:b87fd8dd4322 | 228 | if(tgt_angle==0) { |
la00noix | 0:b87fd8dd4322 | 229 | if((xy_type==0 || xy_type==2) && pm_typeX==0) { |
la00noix | 0:b87fd8dd4322 | 230 | |
la00noix | 0:b87fd8dd4322 | 231 | info.nowX.usw = x_base - (usw_data4 + R4*cos(now_angle*PI/180) + D4*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 232 | |
la00noix | 0:b87fd8dd4322 | 233 | } else if((xy_type==0 || xy_type==2) && pm_typeX==1) { |
la00noix | 0:b87fd8dd4322 | 234 | |
la00noix | 0:b87fd8dd4322 | 235 | info.nowX.usw = x_base + (usw_data3 + R3*cos(now_angle*PI/180) + D3*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 236 | |
la00noix | 0:b87fd8dd4322 | 237 | } |
la00noix | 0:b87fd8dd4322 | 238 | if((xy_type==1 || xy_type==2) && pm_typeY==0) { |
la00noix | 0:b87fd8dd4322 | 239 | |
la00noix | 0:b87fd8dd4322 | 240 | info.nowY.usw = y_base - (usw_data2 + R2*cos(now_angle*PI/180) + D2*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 241 | |
la00noix | 0:b87fd8dd4322 | 242 | } else if((xy_type==1 || xy_type==2) && pm_typeY==1) { |
la00noix | 0:b87fd8dd4322 | 243 | |
la00noix | 0:b87fd8dd4322 | 244 | info.nowY.usw = y_base + (usw_data1 + R1*cos(now_angle*PI/180) + D1*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 245 | |
la00noix | 0:b87fd8dd4322 | 246 | } |
la00noix | 0:b87fd8dd4322 | 247 | |
la00noix | 0:b87fd8dd4322 | 248 | } else if(tgt_angle==90) { |
la00noix | 0:b87fd8dd4322 | 249 | if((xy_type==0 || xy_type==2) && pm_typeX==0) { |
la00noix | 0:b87fd8dd4322 | 250 | |
la00noix | 0:b87fd8dd4322 | 251 | info.nowX.usw = x_base - (usw_data1 + R1*cos((now_angle-tgt_angle)*PI/180) + D1*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 252 | |
la00noix | 0:b87fd8dd4322 | 253 | } else if((xy_type==0 || xy_type==2) && pm_typeX==1) { |
la00noix | 0:b87fd8dd4322 | 254 | |
la00noix | 0:b87fd8dd4322 | 255 | info.nowX.usw = x_base + (usw_data2 + R2*cos((now_angle-tgt_angle)*PI/180) + D2*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 256 | |
la00noix | 0:b87fd8dd4322 | 257 | } |
la00noix | 0:b87fd8dd4322 | 258 | if((xy_type==1 || xy_type==2) && pm_typeY==0) { |
la00noix | 0:b87fd8dd4322 | 259 | |
la00noix | 0:b87fd8dd4322 | 260 | info.nowY.usw = y_base - (usw_data4 + R4*cos((now_angle-tgt_angle)*PI/180) + D4*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 261 | |
la00noix | 0:b87fd8dd4322 | 262 | } else if((xy_type==1 || xy_type==2) && pm_typeY==1) { |
la00noix | 0:b87fd8dd4322 | 263 | |
la00noix | 0:b87fd8dd4322 | 264 | info.nowY.usw = y_base + (usw_data3 + R3*cos((now_angle-tgt_angle)*PI/180) + D3*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 265 | |
la00noix | 0:b87fd8dd4322 | 266 | } |
la00noix | 0:b87fd8dd4322 | 267 | |
la00noix | 0:b87fd8dd4322 | 268 | } else if(tgt_angle==180 || tgt_angle==-180) { |
la00noix | 0:b87fd8dd4322 | 269 | if((xy_type==0 || xy_type==2) && pm_typeX==0) { |
la00noix | 0:b87fd8dd4322 | 270 | |
la00noix | 0:b87fd8dd4322 | 271 | info.nowX.usw = x_base - (usw_data3 + R3*cos((now_angle-tgt_angle)*PI/180) + D3*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 272 | |
la00noix | 0:b87fd8dd4322 | 273 | } else if((xy_type==0 || xy_type==2) && pm_typeX==1) { |
la00noix | 0:b87fd8dd4322 | 274 | |
la00noix | 0:b87fd8dd4322 | 275 | info.nowX.usw = x_base + (usw_data4 + R4*cos((now_angle-tgt_angle)*PI/180) + D4*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 276 | |
la00noix | 0:b87fd8dd4322 | 277 | } |
la00noix | 0:b87fd8dd4322 | 278 | if((xy_type==1 || xy_type==2) && pm_typeY==0) { |
la00noix | 0:b87fd8dd4322 | 279 | |
la00noix | 0:b87fd8dd4322 | 280 | info.nowY.usw = y_base - (usw_data1+ R1*cos((now_angle-tgt_angle)*PI/180) + D1*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 281 | |
la00noix | 0:b87fd8dd4322 | 282 | } else if((xy_type==1 || xy_type==2) && pm_typeY==1) { |
la00noix | 0:b87fd8dd4322 | 283 | |
la00noix | 0:b87fd8dd4322 | 284 | info.nowY.usw = y_base + (usw_data2 + R2*cos((now_angle-tgt_angle)*PI/180) + D2*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 285 | |
la00noix | 0:b87fd8dd4322 | 286 | } |
la00noix | 0:b87fd8dd4322 | 287 | } else if(tgt_angle==-90) { |
la00noix | 0:b87fd8dd4322 | 288 | if((xy_type==0 || xy_type==2) && pm_typeX==0) { |
la00noix | 0:b87fd8dd4322 | 289 | |
la00noix | 0:b87fd8dd4322 | 290 | info.nowX.usw = x_base - (usw_data2 + R2*cos((now_angle-tgt_angle)*PI/180) + D2*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 291 | |
la00noix | 0:b87fd8dd4322 | 292 | } else if((xy_type==0 || xy_type==2) && pm_typeX==1) { |
la00noix | 0:b87fd8dd4322 | 293 | |
la00noix | 0:b87fd8dd4322 | 294 | info.nowX.usw = x_base + (usw_data1 + R1*cos((now_angle-tgt_angle)*PI/180) + D1*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 295 | |
la00noix | 0:b87fd8dd4322 | 296 | } |
la00noix | 0:b87fd8dd4322 | 297 | if((xy_type==1 || xy_type==2) && pm_typeY==0) { |
la00noix | 0:b87fd8dd4322 | 298 | |
la00noix | 0:b87fd8dd4322 | 299 | info.nowY.usw = y_base - (usw_data3 + R3*cos((now_angle-tgt_angle)*PI/180) + D3*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 300 | |
la00noix | 0:b87fd8dd4322 | 301 | } else if((xy_type==1 || xy_type==2) && pm_typeY==1) { |
la00noix | 0:b87fd8dd4322 | 302 | |
la00noix | 0:b87fd8dd4322 | 303 | info.nowY.usw = y_base + (usw_data4 + R4*cos((now_angle-tgt_angle)*PI/180) + D4*sin(now_angle*PI/180)); |
la00noix | 0:b87fd8dd4322 | 304 | |
la00noix | 0:b87fd8dd4322 | 305 | } |
la00noix | 0:b87fd8dd4322 | 306 | } |
la00noix | 0:b87fd8dd4322 | 307 | } |
la00noix | 0:b87fd8dd4322 | 308 | |
la00noix | 0:b87fd8dd4322 | 309 | void calc_xy(double target_angle, double u,double v) |
la00noix | 0:b87fd8dd4322 | 310 | { |
la00noix | 0:b87fd8dd4322 | 311 | //エンコーダにより求めた機体の座標と超音波センサーにより求めた機体の座標を(エンコーダ : 超音波 = u : 1-u / v : 1-v)の割合で混ぜて now_x,now_y に代入する |
la00noix | 0:b87fd8dd4322 | 312 | |
la00noix | 0:b87fd8dd4322 | 313 | calc_xy_enc(); |
la00noix | 0:b87fd8dd4322 | 314 | |
la00noix | 0:b87fd8dd4322 | 315 | if(u != 1 || v != 1) { |
la00noix | 0:b87fd8dd4322 | 316 | calc_xy_usw(target_angle); //エンコーダの値しか使用しない場合は超音波センサーによる座標計算は行わずに計算量を減らす。 |
la00noix | 0:b87fd8dd4322 | 317 | } |
la00noix | 0:b87fd8dd4322 | 318 | |
la00noix | 0:b87fd8dd4322 | 319 | now_x = u * info.nowX.enc + (1-u) * info.nowX.usw; |
la00noix | 0:b87fd8dd4322 | 320 | now_y = v * info.nowY.enc + (1-v) * info.nowY.usw; |
la00noix | 0:b87fd8dd4322 | 321 | |
la00noix | 0:b87fd8dd4322 | 322 | /*if(now_x >-1 && now_x <1 && now_y >-1 && now_y <1){ //スタート時の0合わせ用 |
la00noix | 0:b87fd8dd4322 | 323 | ec_led = 1; |
la00noix | 0:b87fd8dd4322 | 324 | }else{ |
la00noix | 0:b87fd8dd4322 | 325 | ec_led = 0; |
la00noix | 0:b87fd8dd4322 | 326 | } |
la00noix | 0:b87fd8dd4322 | 327 | |
la00noix | 0:b87fd8dd4322 | 328 | if(now_angle >-0.5 && now_angle <0.5){ |
la00noix | 0:b87fd8dd4322 | 329 | gyro_led = 1; |
la00noix | 0:b87fd8dd4322 | 330 | }else{ |
la00noix | 0:b87fd8dd4322 | 331 | gyro_led = 0; |
la00noix | 0:b87fd8dd4322 | 332 | }*/ |
la00noix | 0:b87fd8dd4322 | 333 | } |
la00noix | 0:b87fd8dd4322 | 334 | |
la00noix | 0:b87fd8dd4322 | 335 | void enc_correction(int x_select,int y_select) //エンコーダの座標を超音波センサの座標で上書き |
la00noix | 0:b87fd8dd4322 | 336 | { |
la00noix | 0:b87fd8dd4322 | 337 | //x_select,y_select → (0:上書きしない/1:上書きする) |
la00noix | 0:b87fd8dd4322 | 338 | |
la00noix | 0:b87fd8dd4322 | 339 | if(x_select == 1) { |
la00noix | 0:b87fd8dd4322 | 340 | info.nowX.enc = info.nowX.usw; |
la00noix | 0:b87fd8dd4322 | 341 | } |
la00noix | 0:b87fd8dd4322 | 342 | if(y_select == 1) { |
la00noix | 0:b87fd8dd4322 | 343 | info.nowY.enc = info.nowY.usw; |
la00noix | 0:b87fd8dd4322 | 344 | } |
la00noix | 0:b87fd8dd4322 | 345 | |
la00noix | 0:b87fd8dd4322 | 346 | } |
la00noix | 0:b87fd8dd4322 | 347 | |
la00noix | 0:b87fd8dd4322 | 348 | //ここからそれぞれのプログラム///////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
la00noix | 0:b87fd8dd4322 | 349 | //now_x(現在のx座標),now_y(現在のy座標),now_angle(機体角度(ラジアンではない)(0~360や-180~180とは限らない))(反時計回りが正) |
la00noix | 0:b87fd8dd4322 | 350 | //ジャイロの出力は角度だが三角関数はラジアンとして計算する |
la00noix | 0:b87fd8dd4322 | 351 | //通常の移動+座標のずれ補正+機体の角度補正(+必要に応じさらに別補正) |
la00noix | 0:b87fd8dd4322 | 352 | //ジャイロの仕様上、角度補正をするときに計算式内で角度はそのままよりsinをとったほうがいいかもね |
la00noix | 0:b87fd8dd4322 | 353 | |
la00noix | 0:b87fd8dd4322 | 354 | void purecurve(int type,double u,double v, //正面を変えずに円弧or楕円を描いて曲がる |
la00noix | 0:b87fd8dd4322 | 355 | double point_x1,double point_y1, |
la00noix | 0:b87fd8dd4322 | 356 | double point_x2,double point_y2, |
la00noix | 0:b87fd8dd4322 | 357 | int theta, |
la00noix | 0:b87fd8dd4322 | 358 | double speed, |
la00noix | 0:b87fd8dd4322 | 359 | double q_p,double q_d, |
la00noix | 0:b87fd8dd4322 | 360 | double r_p,double r_d, |
la00noix | 0:b87fd8dd4322 | 361 | double r_out_max, |
la00noix | 0:b87fd8dd4322 | 362 | double target_angle) |
la00noix | 0:b87fd8dd4322 | 363 | //type:動きの種類(8パターン) point_x1,point_y1=出発地点の座標 point_x2,point_x2=目標地点の座標 theta=plotの間隔(0~90°) speed=速度 |
la00noix | 0:b87fd8dd4322 | 364 | { |
la00noix | 0:b87fd8dd4322 | 365 | //-----PathFollowingのパラメーター設定-----// |
la00noix | 0:b87fd8dd4322 | 366 | q_setPDparam(q_p,q_d); //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:b87fd8dd4322 | 367 | r_setPDparam(r_p,r_d); //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:b87fd8dd4322 | 368 | set_r_out(r_out_max); //旋回時の最大出力値設定関数 |
la00noix | 0:b87fd8dd4322 | 369 | set_target_angle(target_angle); //機体目標角度設定関数 |
la00noix | 0:b87fd8dd4322 | 370 | |
la00noix | 0:b87fd8dd4322 | 371 | int s; |
la00noix | 0:b87fd8dd4322 | 372 | int t = 0; |
la00noix | 0:b87fd8dd4322 | 373 | double X,Y;//X=楕円の中心座標、Y=楕円の中心座標 |
la00noix | 0:b87fd8dd4322 | 374 | double a,b; //a=楕円のx軸方向の幅の半分,b=楕円のy軸方向の幅の半分 |
la00noix | 0:b87fd8dd4322 | 375 | double plotx[(90/theta)+1]; //楕円にとるplotのx座標 |
la00noix | 0:b87fd8dd4322 | 376 | double ploty[(90/theta)+1]; |
la00noix | 0:b87fd8dd4322 | 377 | |
la00noix | 0:b87fd8dd4322 | 378 | double x_out,y_out,r_out; |
la00noix | 0:b87fd8dd4322 | 379 | |
la00noix | 0:b87fd8dd4322 | 380 | a=fabs(point_x1-point_x2); |
la00noix | 0:b87fd8dd4322 | 381 | b=fabs(point_y1-point_y2); |
la00noix | 0:b87fd8dd4322 | 382 | |
la00noix | 0:b87fd8dd4322 | 383 | switch(type) { |
la00noix | 0:b87fd8dd4322 | 384 | |
la00noix | 0:b87fd8dd4322 | 385 | case 1://→↑移動 |
la00noix | 0:b87fd8dd4322 | 386 | X=point_x1; |
la00noix | 0:b87fd8dd4322 | 387 | Y=point_y2; |
la00noix | 0:b87fd8dd4322 | 388 | |
la00noix | 0:b87fd8dd4322 | 389 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 390 | plotx[s] = X + a * cos(-PI/2 + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 391 | ploty[s] = Y + b * sin(-PI/2 + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 392 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 393 | } |
la00noix | 0:b87fd8dd4322 | 394 | break; |
la00noix | 0:b87fd8dd4322 | 395 | |
la00noix | 0:b87fd8dd4322 | 396 | case 2://↑→移動 |
la00noix | 0:b87fd8dd4322 | 397 | X=point_x2; |
la00noix | 0:b87fd8dd4322 | 398 | Y=point_y1; |
la00noix | 0:b87fd8dd4322 | 399 | |
la00noix | 0:b87fd8dd4322 | 400 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 401 | plotx[s] = X + a * cos(PI - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 402 | ploty[s] = Y + b * sin(PI - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 403 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 404 | } |
la00noix | 0:b87fd8dd4322 | 405 | break; |
la00noix | 0:b87fd8dd4322 | 406 | |
la00noix | 0:b87fd8dd4322 | 407 | case 3://↑←移動 |
la00noix | 0:b87fd8dd4322 | 408 | X=point_x2; |
la00noix | 0:b87fd8dd4322 | 409 | Y=point_y1; |
la00noix | 0:b87fd8dd4322 | 410 | |
la00noix | 0:b87fd8dd4322 | 411 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 412 | plotx[s] = X + a * cos(s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 413 | ploty[s] = Y + b * sin(s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 414 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 415 | } |
la00noix | 0:b87fd8dd4322 | 416 | break; |
la00noix | 0:b87fd8dd4322 | 417 | |
la00noix | 0:b87fd8dd4322 | 418 | case 4://←↑移動 |
la00noix | 0:b87fd8dd4322 | 419 | X=point_x1; |
la00noix | 0:b87fd8dd4322 | 420 | Y=point_y2; |
la00noix | 0:b87fd8dd4322 | 421 | |
la00noix | 0:b87fd8dd4322 | 422 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 423 | plotx[s] = X + a * cos(-PI/2 - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 424 | ploty[s] = Y + b * sin(-PI/2 - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 425 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 426 | } |
la00noix | 0:b87fd8dd4322 | 427 | break; |
la00noix | 0:b87fd8dd4322 | 428 | |
la00noix | 0:b87fd8dd4322 | 429 | case 5://←↓移動 |
la00noix | 0:b87fd8dd4322 | 430 | X=point_x1; |
la00noix | 0:b87fd8dd4322 | 431 | Y=point_y2; |
la00noix | 0:b87fd8dd4322 | 432 | |
la00noix | 0:b87fd8dd4322 | 433 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 434 | plotx[s] = X + a * cos(PI/2 + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 435 | ploty[s] = Y + b * sin(PI/2 + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 436 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 437 | } |
la00noix | 0:b87fd8dd4322 | 438 | break; |
la00noix | 0:b87fd8dd4322 | 439 | |
la00noix | 0:b87fd8dd4322 | 440 | case 6://↓←移動 |
la00noix | 0:b87fd8dd4322 | 441 | X=point_x2; |
la00noix | 0:b87fd8dd4322 | 442 | Y=point_y1; |
la00noix | 0:b87fd8dd4322 | 443 | |
la00noix | 0:b87fd8dd4322 | 444 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 445 | plotx[s] = X + a * cos(-s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 446 | ploty[s] = Y + b * sin(-s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 447 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 448 | } |
la00noix | 0:b87fd8dd4322 | 449 | break; |
la00noix | 0:b87fd8dd4322 | 450 | |
la00noix | 0:b87fd8dd4322 | 451 | case 7://↓→移動 |
la00noix | 0:b87fd8dd4322 | 452 | X=point_x2; |
la00noix | 0:b87fd8dd4322 | 453 | Y=point_y1; |
la00noix | 0:b87fd8dd4322 | 454 | |
la00noix | 0:b87fd8dd4322 | 455 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 456 | plotx[s] = X + a * cos(PI + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 457 | ploty[s] = Y + b * sin(PI + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 458 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 459 | } |
la00noix | 0:b87fd8dd4322 | 460 | break; |
la00noix | 0:b87fd8dd4322 | 461 | |
la00noix | 0:b87fd8dd4322 | 462 | case 8://→↓移動 |
la00noix | 0:b87fd8dd4322 | 463 | X=point_x1; |
la00noix | 0:b87fd8dd4322 | 464 | Y=point_y2; |
la00noix | 0:b87fd8dd4322 | 465 | |
la00noix | 0:b87fd8dd4322 | 466 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 467 | plotx[s] = X + a * cos(PI/2 - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 468 | ploty[s] = Y + b * sin(PI/2 - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 469 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 470 | } |
la00noix | 0:b87fd8dd4322 | 471 | break; |
la00noix | 0:b87fd8dd4322 | 472 | } |
la00noix | 0:b87fd8dd4322 | 473 | |
la00noix | 0:b87fd8dd4322 | 474 | while(1) { |
la00noix | 0:b87fd8dd4322 | 475 | |
la00noix | 0:b87fd8dd4322 | 476 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 477 | if(id1_value[6] != flag)break; |
la00noix | 0:b87fd8dd4322 | 478 | |
la00noix | 0:b87fd8dd4322 | 479 | calc_xy(target_angle,u,v); |
la00noix | 0:b87fd8dd4322 | 480 | |
la00noix | 0:b87fd8dd4322 | 481 | XYRmotorout(plotx[t],ploty[t],plotx[t+1],ploty[t+1],&x_out,&y_out,&r_out,speed,speed); |
la00noix | 0:b87fd8dd4322 | 482 | CalMotorOut(x_out,y_out,r_out); |
la00noix | 0:b87fd8dd4322 | 483 | //debug_printf("t=%d now_x=%f now_y=%f x_out=%f y_out=%f\n\r",t,now_x,now_y,x_out,y_out); |
la00noix | 0:b87fd8dd4322 | 484 | |
la00noix | 0:b87fd8dd4322 | 485 | base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),4095); //m1~m4に代入 |
la00noix | 0:b87fd8dd4322 | 486 | //debug_printf("t=%d (0)=%f (1)=%f (2)=%f (3)=%f\n\r",t,GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3)); |
la00noix | 0:b87fd8dd4322 | 487 | |
la00noix | 0:b87fd8dd4322 | 488 | if(((plotx[t+1] - now_x)*(plotx[t+1] - plotx[t]) + (ploty[t+1] - now_y)*(ploty[t+1] - ploty[t])) < 0)t++; |
la00noix | 0:b87fd8dd4322 | 489 | |
yuki0701 | 1:26fc1b2f1c42 | 490 | // MaxonControl(m1,m2,m3,m4); //出力 |
yuki0701 | 1:26fc1b2f1c42 | 491 | // debug_printf("t=%d m1=%d m2=%d m3=%d m4=%d x=%f y=%f angle=%f\n\r",t,m1,m2,m3,m4,now_x,now_y,now_angle); |
la00noix | 0:b87fd8dd4322 | 492 | |
la00noix | 0:b87fd8dd4322 | 493 | if(t == (90/theta))break; |
la00noix | 0:b87fd8dd4322 | 494 | } |
la00noix | 0:b87fd8dd4322 | 495 | } |
la00noix | 0:b87fd8dd4322 | 496 | |
la00noix | 0:b87fd8dd4322 | 497 | void gogo_straight(double u,double v, //直線運動プログラム |
la00noix | 0:b87fd8dd4322 | 498 | double x1_point,double y1_point, |
la00noix | 0:b87fd8dd4322 | 499 | double x2_point,double y2_point, |
la00noix | 0:b87fd8dd4322 | 500 | double speed1,double speed2, |
la00noix | 0:b87fd8dd4322 | 501 | double q_p,double q_d, |
la00noix | 0:b87fd8dd4322 | 502 | double r_p,double r_d, |
la00noix | 0:b87fd8dd4322 | 503 | double r_out_max, |
la00noix | 0:b87fd8dd4322 | 504 | double target_angle) |
la00noix | 0:b87fd8dd4322 | 505 | //引数:出発地点の座標(x,y)、目標地点の座標(x,y)、初速度(speed1)、目標速度(speed2)//speed1=speed2 のとき等速運動 |
la00noix | 0:b87fd8dd4322 | 506 | { |
la00noix | 0:b87fd8dd4322 | 507 | //-----PathFollowingのパラメーター設定-----// |
la00noix | 0:b87fd8dd4322 | 508 | q_setPDparam(q_p,q_d); //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:b87fd8dd4322 | 509 | r_setPDparam(r_p,r_d); //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:b87fd8dd4322 | 510 | set_r_out(r_out_max); //旋回時の最大出力値設定関数 |
la00noix | 0:b87fd8dd4322 | 511 | set_target_angle(target_angle); //機体目標角度設定関数 |
la00noix | 0:b87fd8dd4322 | 512 | |
la00noix | 0:b87fd8dd4322 | 513 | while (1) { |
la00noix | 0:b87fd8dd4322 | 514 | |
la00noix | 0:b87fd8dd4322 | 515 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 516 | if(id1_value[6] != flag)break; |
la00noix | 0:b87fd8dd4322 | 517 | |
la00noix | 0:b87fd8dd4322 | 518 | calc_xy(target_angle,u,v); |
la00noix | 0:b87fd8dd4322 | 519 | |
la00noix | 0:b87fd8dd4322 | 520 | XYRmotorout(x1_point,y1_point,x2_point,y2_point,&x_out,&y_out,&r_out,speed1,speed2); |
yuki0701 | 1:26fc1b2f1c42 | 521 | //printf("n_x = %f, n_y = %f,n_angle = %f, t_x = %f, t_y = %f, t_angle = %f, x_out=%lf, y_out=%lf, r_out=%lf\n\r",now_x,now_y,now_angle,x2_point,y2_point,target_angle,x_out, y_out,r_out); |
la00noix | 0:b87fd8dd4322 | 522 | |
la00noix | 0:b87fd8dd4322 | 523 | CalMotorOut(x_out,y_out,r_out); |
la00noix | 0:b87fd8dd4322 | 524 | //printf("out1=%lf, out2=%lf, out3=%lf, out4=%lf\n",GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3)); |
la00noix | 0:b87fd8dd4322 | 525 | |
la00noix | 0:b87fd8dd4322 | 526 | base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),4095); |
la00noix | 0:b87fd8dd4322 | 527 | //printf("m1=%d, m2=%d, m3=%d, m4=%d\r\n",m_1,m_2,m_3,m_4); |
la00noix | 0:b87fd8dd4322 | 528 | |
yuki0701 | 1:26fc1b2f1c42 | 529 | // MaxonControl(m1,m2,m3,m4); |
yuki0701 | 1:26fc1b2f1c42 | 530 | // debug_printf("m1=%d m2=%d m3=%d m4=%d x=%f y=%f angle=%f\n\r",m1,m2,m3,m4,now_x,now_y,now_angle); |
yuki0701 | 1:26fc1b2f1c42 | 531 | printf("m1=%d m2=%d m3=%d m4=%d x=%f y=%f angle=%f\n\r",m1,m2,m3,m4,now_x,now_y,now_angle); |
la00noix | 0:b87fd8dd4322 | 532 | |
la00noix | 0:b87fd8dd4322 | 533 | if(((x2_point - now_x)*(x2_point - x1_point) + (y2_point - now_y)*(y2_point - y1_point)) < 0)break; |
la00noix | 0:b87fd8dd4322 | 534 | } |
la00noix | 0:b87fd8dd4322 | 535 | } |
la00noix | 0:b87fd8dd4322 | 536 | |
la00noix | 0:b87fd8dd4322 | 537 | /*void pos_correction(double tgt_x, double tgt_y, double tgt_angle, double u, double v) //位置補正(使用前にMaxonControl(0,0,0,0)を入れる) |
la00noix | 0:b87fd8dd4322 | 538 | { |
la00noix | 0:b87fd8dd4322 | 539 | |
la00noix | 0:b87fd8dd4322 | 540 | double r, R=10; // r:一回補正が入るごとの機体の位置と目標位置の距離(ズレ) R:補正終了とみなす目標位置からの機体の位置のズレ |
la00noix | 0:b87fd8dd4322 | 541 | double out; |
la00noix | 0:b87fd8dd4322 | 542 | |
la00noix | 0:b87fd8dd4322 | 543 | calc_xy(tgt_angle, u, v); |
la00noix | 0:b87fd8dd4322 | 544 | |
la00noix | 0:b87fd8dd4322 | 545 | while(1) { //機体の位置を目標領域(目標座標+許容誤差)に収める |
la00noix | 0:b87fd8dd4322 | 546 | gogo_straight(u,v,now_x,now_y,tgt_x,tgt_y,200,50,5,0.1,10,0.1,500,tgt_angle); |
la00noix | 0:b87fd8dd4322 | 547 | MaxonControl(0,0,0,0); |
la00noix | 0:b87fd8dd4322 | 548 | |
la00noix | 0:b87fd8dd4322 | 549 | calc_xy(tgt_angle, u, v); |
la00noix | 0:b87fd8dd4322 | 550 | |
la00noix | 0:b87fd8dd4322 | 551 | r=hypot(now_x - tgt_x, now_y - tgt_y); |
la00noix | 0:b87fd8dd4322 | 552 | |
la00noix | 0:b87fd8dd4322 | 553 | if(r < R) break; |
la00noix | 0:b87fd8dd4322 | 554 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 555 | } |
la00noix | 0:b87fd8dd4322 | 556 | |
la00noix | 0:b87fd8dd4322 | 557 | while(1) { |
la00noix | 0:b87fd8dd4322 | 558 | |
la00noix | 0:b87fd8dd4322 | 559 | calc_gyro(); |
la00noix | 0:b87fd8dd4322 | 560 | |
la00noix | 0:b87fd8dd4322 | 561 | out = 10 * (tgt_angle - now_angle); |
la00noix | 0:b87fd8dd4322 | 562 | |
la00noix | 0:b87fd8dd4322 | 563 | if(out > 300) { //0~179°のときは時計回りに回転 |
la00noix | 0:b87fd8dd4322 | 564 | MaxonControl(300,300,300,300); |
la00noix | 0:b87fd8dd4322 | 565 | } else if(out < -300) { |
la00noix | 0:b87fd8dd4322 | 566 | MaxonControl(-300,-300,-300,-300); |
la00noix | 0:b87fd8dd4322 | 567 | } else if(out <= 300 && out > -300) { |
la00noix | 0:b87fd8dd4322 | 568 | MaxonControl(out,out,out,out); |
la00noix | 0:b87fd8dd4322 | 569 | } |
la00noix | 0:b87fd8dd4322 | 570 | |
la00noix | 0:b87fd8dd4322 | 571 | if(tgt_angle - 0.5 < now_angle && now_angle < tgt_angle + 0.5) break; //目標角度からの許容誤差内に機体の角度が収まった時、補正終了 |
la00noix | 0:b87fd8dd4322 | 572 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 573 | } |
la00noix | 0:b87fd8dd4322 | 574 | MaxonControl(0,0,0,0); |
la00noix | 0:b87fd8dd4322 | 575 | }*/ |
la00noix | 0:b87fd8dd4322 | 576 | |
la00noix | 0:b87fd8dd4322 | 577 | void pos_correction(double tgt_x, double tgt_y, double tgt_angle, double u, double v) //改良版 位置補正(使用前にMaxonControl(0,0,0,0)を入れる) |
la00noix | 0:b87fd8dd4322 | 578 | { |
la00noix | 0:b87fd8dd4322 | 579 | //距離に比例させて補正初速度を増加させる。(最大速度を設定しそれ以上は出ないようにする) |
la00noix | 0:b87fd8dd4322 | 580 | |
yuki0701 | 1:26fc1b2f1c42 | 581 | double first_speed, first_speed50 = 10, last_speed = 10, Max_speed = 500; |
la00noix | 0:b87fd8dd4322 | 582 | double r, R=10; // r:一回補正が入るごとの機体の位置と目標位置の距離(ズレ) R:補正終了とみなす目標位置からの機体の位置のズレ |
la00noix | 0:b87fd8dd4322 | 583 | double out; |
la00noix | 0:b87fd8dd4322 | 584 | |
la00noix | 0:b87fd8dd4322 | 585 | calc_xy(tgt_angle, u, v); |
la00noix | 0:b87fd8dd4322 | 586 | |
la00noix | 0:b87fd8dd4322 | 587 | //r = hypot(now_x - tgt_x, now_y - tgt_y); |
la00noix | 0:b87fd8dd4322 | 588 | |
la00noix | 0:b87fd8dd4322 | 589 | while(1) { //機体の位置を目標領域(目標座標+許容誤差)に収める |
yuki0701 | 1:26fc1b2f1c42 | 590 | //printf("col\n\n\n"); |
la00noix | 0:b87fd8dd4322 | 591 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 592 | if(id1_value[6] != flag)break; |
la00noix | 0:b87fd8dd4322 | 593 | |
la00noix | 0:b87fd8dd4322 | 594 | //first_speed = first_speed50 * r / 50; |
la00noix | 0:b87fd8dd4322 | 595 | |
la00noix | 0:b87fd8dd4322 | 596 | /*if(first_speed > Max_speed){ |
la00noix | 0:b87fd8dd4322 | 597 | gogo_straight(u,v,now_x,now_y,tgt_x,tgt_y,Max_speed,Max_speed,5,0.1,10,0.1,500,tgt_angle); |
la00noix | 0:b87fd8dd4322 | 598 | }else{ |
la00noix | 0:b87fd8dd4322 | 599 | gogo_straight(u,v,now_x,now_y,tgt_x,tgt_y,first_speed,last_speed,5,0.1,10,0.1,500,tgt_angle); |
la00noix | 0:b87fd8dd4322 | 600 | }*/ |
la00noix | 0:b87fd8dd4322 | 601 | |
yuki0701 | 1:26fc1b2f1c42 | 602 | //gogo_straight(u,v,now_x,now_y,tgt_x,tgt_y,first_speed50,last_speed,5,0.1,10,0.1,500,tgt_angle); |
yuki0701 | 1:26fc1b2f1c42 | 603 | gogo_straight(u,v,now_x,now_y,tgt_x,tgt_y,10,10,5,0.1,10,0.1,50,tgt_angle); |
yuki0701 | 1:26fc1b2f1c42 | 604 | //gogo_straight(1,1,0,0,200,0,50,500,5,0.1,10,0.1,50,0); |
yuki0701 | 1:26fc1b2f1c42 | 605 | //gogo_straight(u,v,now_x,now_y,0,100,first_speed50,last_speed,5,0.1,10,0.1,500,tgt_angle); |
la00noix | 0:b87fd8dd4322 | 606 | |
yuki0701 | 1:26fc1b2f1c42 | 607 | // MaxonControl(0,0,0,0); |
yuki0701 | 1:26fc1b2f1c42 | 608 | m1 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 609 | m2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 610 | m3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 611 | m4 = 0; |
la00noix | 0:b87fd8dd4322 | 612 | |
la00noix | 0:b87fd8dd4322 | 613 | calc_xy(tgt_angle, u, v); |
la00noix | 0:b87fd8dd4322 | 614 | |
la00noix | 0:b87fd8dd4322 | 615 | r=hypot(now_x - tgt_x, now_y - tgt_y); |
la00noix | 0:b87fd8dd4322 | 616 | |
la00noix | 0:b87fd8dd4322 | 617 | if(r < R) break; |
la00noix | 0:b87fd8dd4322 | 618 | } |
la00noix | 0:b87fd8dd4322 | 619 | |
la00noix | 0:b87fd8dd4322 | 620 | while(1) { |
la00noix | 0:b87fd8dd4322 | 621 | |
la00noix | 0:b87fd8dd4322 | 622 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 623 | if(id1_value[6] != flag)break; |
la00noix | 0:b87fd8dd4322 | 624 | |
la00noix | 0:b87fd8dd4322 | 625 | //calc_gyro(); |
yuki0701 | 1:26fc1b2f1c42 | 626 | // now_angle=gyro.getAngle(); |
yuki0701 | 1:26fc1b2f1c42 | 627 | now_angle = -gyro.getZ_Angle(); |
la00noix | 0:b87fd8dd4322 | 628 | printf("angle = %f\n\r",now_angle); |
la00noix | 0:b87fd8dd4322 | 629 | |
la00noix | 0:b87fd8dd4322 | 630 | out = 10 * (tgt_angle - now_angle); |
la00noix | 0:b87fd8dd4322 | 631 | |
la00noix | 0:b87fd8dd4322 | 632 | if(out > 300) { //0~179°のときは時計回りに回転 |
yuki0701 | 1:26fc1b2f1c42 | 633 | // MaxonControl(-300,-300,-300,-300); |
yuki0701 | 1:26fc1b2f1c42 | 634 | m1 = -300; |
yuki0701 | 1:26fc1b2f1c42 | 635 | m2 = -300; |
yuki0701 | 1:26fc1b2f1c42 | 636 | m3 = -300; |
yuki0701 | 1:26fc1b2f1c42 | 637 | m4 = -300; |
yuki0701 | 1:26fc1b2f1c42 | 638 | |
la00noix | 0:b87fd8dd4322 | 639 | } else if(out < -300) { |
yuki0701 | 1:26fc1b2f1c42 | 640 | // MaxonControl(300,300,300,300); |
yuki0701 | 1:26fc1b2f1c42 | 641 | m1 = 300; |
yuki0701 | 1:26fc1b2f1c42 | 642 | m2 = 300; |
yuki0701 | 1:26fc1b2f1c42 | 643 | m3 = 300; |
yuki0701 | 1:26fc1b2f1c42 | 644 | m4 = 300; |
la00noix | 0:b87fd8dd4322 | 645 | } else if(out <= 300 && out > -300) { |
yuki0701 | 1:26fc1b2f1c42 | 646 | // MaxonControl(-out,-out,-out,-out); |
yuki0701 | 1:26fc1b2f1c42 | 647 | m1 = -out; |
yuki0701 | 1:26fc1b2f1c42 | 648 | m2 = -out; |
yuki0701 | 1:26fc1b2f1c42 | 649 | m3 = -out; |
yuki0701 | 1:26fc1b2f1c42 | 650 | m4 = -out; |
la00noix | 0:b87fd8dd4322 | 651 | } |
la00noix | 0:b87fd8dd4322 | 652 | |
la00noix | 0:b87fd8dd4322 | 653 | if(tgt_angle - 1 < now_angle && now_angle < tgt_angle + 1) break; //目標角度からの許容誤差内に機体の角度が収まった時、補正終了 |
la00noix | 0:b87fd8dd4322 | 654 | } |
yuki0701 | 1:26fc1b2f1c42 | 655 | // MaxonControl(0,0,0,0); |
yuki0701 | 1:26fc1b2f1c42 | 656 | m1 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 657 | m2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 658 | m3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 659 | m4 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 660 | } |
yuki0701 | 1:26fc1b2f1c42 | 661 | |
yuki0701 | 1:26fc1b2f1c42 | 662 | void mt_stop() |
yuki0701 | 1:26fc1b2f1c42 | 663 | { |
yuki0701 | 1:26fc1b2f1c42 | 664 | m1 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 665 | m2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 666 | m3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 667 | m4 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 668 | printf("motor stop\n\r"); |
la00noix | 0:b87fd8dd4322 | 669 | } |