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