あ
Dependencies: uw_28015 mbed move4wheel2 EC CruizCore_R6093U CruizCore_R1370P
movement/movement.cpp@2:820dcd23c8e3, 2019-12-11 (annotated)
- Committer:
- yuki0701
- Date:
- Wed Dec 11 04:59:36 2019 +0000
- Revision:
- 2:820dcd23c8e3
- Parent:
- 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 "manual.h" |
la00noix | 0:b87fd8dd4322 | 9 | #include "can.h" |
yuki0701 | 1:26fc1b2f1c42 | 10 | #include "R6093U.h" |
yuki0701 | 2:820dcd23c8e3 | 11 | #include "uw.h" |
la00noix | 0:b87fd8dd4322 | 12 | |
la00noix | 0:b87fd8dd4322 | 13 | #define PI 3.141592 |
la00noix | 0:b87fd8dd4322 | 14 | |
la00noix | 0:b87fd8dd4322 | 15 | char can_ashileddata[2]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 16 | char can_ashileddata2[8]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 17 | //char can_ashileddata3[2]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 18 | //char can_ashileddata4[2]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 19 | //char can_ashileddata5[2]= {0}; |
yuki0701 | 1:26fc1b2f1c42 | 20 | |
la00noix | 0:b87fd8dd4322 | 21 | int can_ashileddata0_0,can_ashileddata0_1,can_ashileddata0_2,can_ashileddata0_3; |
la00noix | 0:b87fd8dd4322 | 22 | |
yuki0701 | 1:26fc1b2f1c42 | 23 | Ec EC2(p16,p15,NC,2048,0.05); |
yuki0701 | 2:820dcd23c8e3 | 24 | Ec EC1(p18,p17,NC,2048,0.05); |
yuki0701 | 2:820dcd23c8e3 | 25 | |
yuki0701 | 2:820dcd23c8e3 | 26 | |
yuki0701 | 2:820dcd23c8e3 | 27 | Uw uw1(p28); |
yuki0701 | 2:820dcd23c8e3 | 28 | Uw uw4(p27); |
yuki0701 | 2:820dcd23c8e3 | 29 | |
la00noix | 0:b87fd8dd4322 | 30 | |
la00noix | 0:b87fd8dd4322 | 31 | Ticker ec_ticker; //ec角速度計算用ticker |
yuki0701 | 2:820dcd23c8e3 | 32 | Ticker uw_ticker; //uw値取得用ticker |
la00noix | 0:b87fd8dd4322 | 33 | |
yuki0701 | 1:26fc1b2f1c42 | 34 | //R1370P gyro(p9,p10); |
yuki0701 | 1:26fc1b2f1c42 | 35 | |
yuki0701 | 1:26fc1b2f1c42 | 36 | R6093U gyro(p9,p10); |
la00noix | 0:b87fd8dd4322 | 37 | |
la00noix | 0:b87fd8dd4322 | 38 | double new_dist1=0,new_dist2=0; |
la00noix | 0:b87fd8dd4322 | 39 | double old_dist1=0,old_dist2=0; |
la00noix | 0:b87fd8dd4322 | 40 | double d_dist1=0,d_dist2=0; //座標計算用関数 |
la00noix | 0:b87fd8dd4322 | 41 | double d_x,d_y; |
la00noix | 0:b87fd8dd4322 | 42 | //現在地X,y座標、現在角度については、PathFollowingでnow_x,now_y,now_angleを定義済 |
la00noix | 0:b87fd8dd4322 | 43 | double start_x=0,start_y=0; //スタート位置 |
la00noix | 0:b87fd8dd4322 | 44 | double x_out,y_out,r_out; //出力値 |
la00noix | 0:b87fd8dd4322 | 45 | |
la00noix | 0:b87fd8dd4322 | 46 | int16_t m1=0, m2=0, m3=0, m4=0; //int16bit = int2byte |
la00noix | 0:b87fd8dd4322 | 47 | |
la00noix | 0:b87fd8dd4322 | 48 | double xy_type,pm_typeX,pm_typeY,x_base,y_base; |
la00noix | 0:b87fd8dd4322 | 49 | |
la00noix | 0:b87fd8dd4322 | 50 | int flag; |
la00noix | 0:b87fd8dd4322 | 51 | |
yuki0701 | 1:26fc1b2f1c42 | 52 | int RL_mode; |
yuki0701 | 1:26fc1b2f1c42 | 53 | |
yuki0701 | 2:820dcd23c8e3 | 54 | int uw_flag1 = 0,uw_flag2 = 0,uw_flag3 = 0,uw_flag4 = 0; |
yuki0701 | 2:820dcd23c8e3 | 55 | |
la00noix | 0:b87fd8dd4322 | 56 | ///////////////////機体情報をメンバとする構造体"robo_data"と構造体型変数info(←この変数に各センサーにより求めた機体情報(機体位置/機体角度)を格納する)の宣言///////////////// |
la00noix | 0:b87fd8dd4322 | 57 | |
la00noix | 0:b87fd8dd4322 | 58 | /*「info.(機体情報の種類).(使用センサーの種類)」に各情報を格納する |
la00noix | 0:b87fd8dd4322 | 59 | *状況に応じて、どのセンサーにより算出した情報を信用するかを選択し、その都度now_angle,now_x,now_yに代入する。(何種類かのセンサーの情報を混ぜて使用することも可能) |
la00noix | 0:b87fd8dd4322 | 60 | *(ex) |
la00noix | 0:b87fd8dd4322 | 61 | *info.nowX.enc → エンコーダにより算出した機体位置のx座標 |
la00noix | 0:b87fd8dd4322 | 62 | *info.nowY.usw → 超音波センサーにより求めた機体位置のy座標 |
la00noix | 0:b87fd8dd4322 | 63 | */ |
la00noix | 0:b87fd8dd4322 | 64 | |
la00noix | 0:b87fd8dd4322 | 65 | typedef struct { //使用センサーの種類 |
la00noix | 0:b87fd8dd4322 | 66 | double usw; //超音波センサー |
la00noix | 0:b87fd8dd4322 | 67 | double enc; //エンコーダ |
la00noix | 0:b87fd8dd4322 | 68 | double gyro; //ジャイロ |
la00noix | 0:b87fd8dd4322 | 69 | //double line;//ラインセンサー |
la00noix | 0:b87fd8dd4322 | 70 | } robo_sensor; |
la00noix | 0:b87fd8dd4322 | 71 | |
la00noix | 0:b87fd8dd4322 | 72 | typedef struct { //機体情報の種類 |
la00noix | 0:b87fd8dd4322 | 73 | robo_sensor angle; //←機体角度は超音波センサーやラインセンサーからも算出可能なので一応格納先を用意したが、ジャイロの値を完全に信用してもいいかも |
la00noix | 0:b87fd8dd4322 | 74 | robo_sensor nowX; |
la00noix | 0:b87fd8dd4322 | 75 | robo_sensor nowY; |
la00noix | 0:b87fd8dd4322 | 76 | } robo_data; |
la00noix | 0:b87fd8dd4322 | 77 | |
la00noix | 0:b87fd8dd4322 | 78 | robo_data info= {{0,0,0},{0,0,0},{0,0,0}}; //全てのデータを0に初期化 |
la00noix | 0:b87fd8dd4322 | 79 | |
la00noix | 0:b87fd8dd4322 | 80 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
la00noix | 0:b87fd8dd4322 | 81 | |
la00noix | 0:b87fd8dd4322 | 82 | void UserLoopSetting_sensor() |
la00noix | 0:b87fd8dd4322 | 83 | { |
la00noix | 0:b87fd8dd4322 | 84 | |
la00noix | 0:b87fd8dd4322 | 85 | gyro.initialize(); |
la00noix | 0:b87fd8dd4322 | 86 | ec_ticker.attach(&calOmega,0.05); //0.05秒間隔で角速度を計算 |
yuki0701 | 2:820dcd23c8e3 | 87 | uw_ticker.attach(&cal_uw,0.05); |
yuki0701 | 1:26fc1b2f1c42 | 88 | EC1.setDiameter_mm(70); |
yuki0701 | 1:26fc1b2f1c42 | 89 | EC2.setDiameter_mm(70); //測定輪半径//後で測定 |
yuki0701 | 2:820dcd23c8e3 | 90 | info.nowX.enc = 457; //初期位置の設定 |
yuki0701 | 2:820dcd23c8e3 | 91 | info.nowY.enc = 457; |
yuki0701 | 1:26fc1b2f1c42 | 92 | } |
yuki0701 | 1:26fc1b2f1c42 | 93 | |
yuki0701 | 1:26fc1b2f1c42 | 94 | void UserLoopSetting_enc_right() |
yuki0701 | 1:26fc1b2f1c42 | 95 | { |
yuki0701 | 1:26fc1b2f1c42 | 96 | info.nowX.enc = 3112; //エンコーダの初期位置の設定(右側フィールド) |
la00noix | 0:b87fd8dd4322 | 97 | info.nowY.enc = 3500; |
yuki0701 | 1:26fc1b2f1c42 | 98 | RL_mode = 0; |
yuki0701 | 1:26fc1b2f1c42 | 99 | } |
yuki0701 | 1:26fc1b2f1c42 | 100 | |
yuki0701 | 1:26fc1b2f1c42 | 101 | void UserLoopSetting_enc_left() |
yuki0701 | 1:26fc1b2f1c42 | 102 | { |
yuki0701 | 1:26fc1b2f1c42 | 103 | info.nowX.enc = -3112; //エンコーダの初期位置の設定(左側フィールド) |
yuki0701 | 1:26fc1b2f1c42 | 104 | info.nowY.enc = 3500; |
yuki0701 | 1:26fc1b2f1c42 | 105 | RL_mode = 1; |
la00noix | 0:b87fd8dd4322 | 106 | } |
la00noix | 0:b87fd8dd4322 | 107 | |
la00noix | 0:b87fd8dd4322 | 108 | void calOmega() //角速度計算関数 |
la00noix | 0:b87fd8dd4322 | 109 | { |
la00noix | 0:b87fd8dd4322 | 110 | EC1.CalOmega(); |
la00noix | 0:b87fd8dd4322 | 111 | EC2.CalOmega(); |
yuki0701 | 2:820dcd23c8e3 | 112 | |
yuki0701 | 2:820dcd23c8e3 | 113 | //usw_data1 = 10 * uw1.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 114 | ////usw_data2 = 10 * uw2.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 115 | //usw_data3 = 10 * uw3.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 116 | ////usw_data4 = 10 * uw4.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 117 | } |
yuki0701 | 2:820dcd23c8e3 | 118 | |
yuki0701 | 2:820dcd23c8e3 | 119 | void cal_uw() //uw値取得用 |
yuki0701 | 2:820dcd23c8e3 | 120 | { |
yuki0701 | 2:820dcd23c8e3 | 121 | if(uw_flag1 == 1) { |
yuki0701 | 2:820dcd23c8e3 | 122 | usw_data1 = 10 * uw1.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 123 | //printf("uw1 = %f\n\r",usw_data1); |
yuki0701 | 2:820dcd23c8e3 | 124 | } |
yuki0701 | 2:820dcd23c8e3 | 125 | if(uw_flag2 == 1) { |
yuki0701 | 2:820dcd23c8e3 | 126 | //usw_data2 = 10 * uw2.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 127 | } |
yuki0701 | 2:820dcd23c8e3 | 128 | if(uw_flag3 == 1) { |
yuki0701 | 2:820dcd23c8e3 | 129 | //usw_data3 = 10 * uw3.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 130 | } |
yuki0701 | 2:820dcd23c8e3 | 131 | if(uw_flag4 == 1) { |
yuki0701 | 2:820dcd23c8e3 | 132 | usw_data4 = 10 * uw4.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 133 | //printf("uw4 = %f\n\r",usw_data4); |
yuki0701 | 2:820dcd23c8e3 | 134 | } |
la00noix | 0:b87fd8dd4322 | 135 | } |
la00noix | 0:b87fd8dd4322 | 136 | |
la00noix | 0:b87fd8dd4322 | 137 | void output(double FL,double BL,double BR,double FR) |
la00noix | 0:b87fd8dd4322 | 138 | { |
la00noix | 0:b87fd8dd4322 | 139 | m1=FL; |
la00noix | 0:b87fd8dd4322 | 140 | m2=BL; |
la00noix | 0:b87fd8dd4322 | 141 | m3=BR; |
la00noix | 0:b87fd8dd4322 | 142 | m4=FR; |
la00noix | 0:b87fd8dd4322 | 143 | } |
la00noix | 0:b87fd8dd4322 | 144 | |
la00noix | 0:b87fd8dd4322 | 145 | void base(double FL,double BL,double BR,double FR,double Max) |
la00noix | 0:b87fd8dd4322 | 146 | //いろんな加算をしても最大OR最小がMaxになるような補正//絶対値が一番でかいやつで除算 |
la00noix | 0:b87fd8dd4322 | 147 | //DCモーターならMax=1、マクソンは-4095~4095だからMax=4095にする |
la00noix | 0:b87fd8dd4322 | 148 | { |
la00noix | 0:b87fd8dd4322 | 149 | if(fabs(FL)>=Max||fabs(BL)>=Max||fabs(BR)>=Max||fabs(FR)>=Max) { |
la00noix | 0:b87fd8dd4322 | 150 | |
la00noix | 0:b87fd8dd4322 | 151 | 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 | 152 | 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 | 153 | 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 | 154 | else output(Max*FL/fabs(FR),Max*BL/fabs(FR),Max*BR/fabs(FR),Max*FR/fabs(FR)); |
la00noix | 0:b87fd8dd4322 | 155 | } else { |
la00noix | 0:b87fd8dd4322 | 156 | output(FL,BL,BR,FR); |
la00noix | 0:b87fd8dd4322 | 157 | } |
la00noix | 0:b87fd8dd4322 | 158 | } |
la00noix | 0:b87fd8dd4322 | 159 | |
la00noix | 0:b87fd8dd4322 | 160 | void ashi_led() |
la00noix | 0:b87fd8dd4322 | 161 | { |
la00noix | 0:b87fd8dd4322 | 162 | if(now_angle > -1 && now_angle < 1) { |
la00noix | 0:b87fd8dd4322 | 163 | can_ashileddata0_0 = 1; |
la00noix | 0:b87fd8dd4322 | 164 | } else { |
la00noix | 0:b87fd8dd4322 | 165 | can_ashileddata0_0 = 0; |
la00noix | 0:b87fd8dd4322 | 166 | } |
la00noix | 0:b87fd8dd4322 | 167 | |
la00noix | 0:b87fd8dd4322 | 168 | if(now_angle > 350) { |
la00noix | 0:b87fd8dd4322 | 169 | can_ashileddata0_1 = 1; |
la00noix | 0:b87fd8dd4322 | 170 | } else { |
la00noix | 0:b87fd8dd4322 | 171 | can_ashileddata0_1 = 0; |
la00noix | 0:b87fd8dd4322 | 172 | } |
la00noix | 0:b87fd8dd4322 | 173 | |
yuki0701 | 1:26fc1b2f1c42 | 174 | if(RL_mode == 0) { |
yuki0701 | 1:26fc1b2f1c42 | 175 | if(now_x > 3110 && now_x < 3114) { |
yuki0701 | 1:26fc1b2f1c42 | 176 | can_ashileddata0_2 = 1; |
yuki0701 | 1:26fc1b2f1c42 | 177 | } else { |
yuki0701 | 1:26fc1b2f1c42 | 178 | can_ashileddata0_2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 179 | } |
la00noix | 0:b87fd8dd4322 | 180 | |
yuki0701 | 1:26fc1b2f1c42 | 181 | if(now_y > 3498 && now_y < 3502) { |
yuki0701 | 1:26fc1b2f1c42 | 182 | can_ashileddata0_3 = 1; |
yuki0701 | 1:26fc1b2f1c42 | 183 | } else { |
yuki0701 | 1:26fc1b2f1c42 | 184 | can_ashileddata0_3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 185 | } |
yuki0701 | 1:26fc1b2f1c42 | 186 | } else if(RL_mode == 1) { |
yuki0701 | 1:26fc1b2f1c42 | 187 | if(now_x > -3114 && now_x < -3110) { |
yuki0701 | 1:26fc1b2f1c42 | 188 | can_ashileddata0_2 = 1; |
yuki0701 | 1:26fc1b2f1c42 | 189 | } else { |
yuki0701 | 1:26fc1b2f1c42 | 190 | can_ashileddata0_2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 191 | } |
yuki0701 | 1:26fc1b2f1c42 | 192 | |
yuki0701 | 1:26fc1b2f1c42 | 193 | if(now_y > 3498 && now_y < 3502) { |
yuki0701 | 1:26fc1b2f1c42 | 194 | can_ashileddata0_3 = 1; |
yuki0701 | 1:26fc1b2f1c42 | 195 | } else { |
yuki0701 | 1:26fc1b2f1c42 | 196 | can_ashileddata0_3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 197 | } |
la00noix | 0:b87fd8dd4322 | 198 | } |
la00noix | 0:b87fd8dd4322 | 199 | |
la00noix | 0:b87fd8dd4322 | 200 | can_ashileddata[0] = (can_ashileddata0_0<<7 | can_ashileddata0_1<<6 | can_ashileddata0_2<<5 | can_ashileddata0_3<<4); |
la00noix | 0:b87fd8dd4322 | 201 | } |
la00noix | 0:b87fd8dd4322 | 202 | |
la00noix | 0:b87fd8dd4322 | 203 | void calc_gyro() |
la00noix | 0:b87fd8dd4322 | 204 | { |
yuki0701 | 1:26fc1b2f1c42 | 205 | //now_angle=gyro.getAngle(); //ジャイロの値読み込み |
yuki0701 | 1:26fc1b2f1c42 | 206 | now_angle = -gyro.getZ_Angle(); |
yuki0701 | 1:26fc1b2f1c42 | 207 | } |
yuki0701 | 1:26fc1b2f1c42 | 208 | |
yuki0701 | 1:26fc1b2f1c42 | 209 | void print_gyro() |
yuki0701 | 1:26fc1b2f1c42 | 210 | { |
yuki0701 | 1:26fc1b2f1c42 | 211 | while(1) { |
yuki0701 | 1:26fc1b2f1c42 | 212 | //printf("now_gyro = %f\n\r",-gyro.getAngle()); |
yuki0701 | 1:26fc1b2f1c42 | 213 | } |
yuki0701 | 1:26fc1b2f1c42 | 214 | |
la00noix | 0:b87fd8dd4322 | 215 | } |
la00noix | 0:b87fd8dd4322 | 216 | |
la00noix | 0:b87fd8dd4322 | 217 | void calc_xy_enc() //エンコーダ&ジャイロによる座標計算 |
la00noix | 0:b87fd8dd4322 | 218 | { |
yuki0701 | 1:26fc1b2f1c42 | 219 | //now_angle=gyro.getAngle(); //ジャイロの値読み込み |
yuki0701 | 1:26fc1b2f1c42 | 220 | now_angle = -gyro.getZ_Angle(); |
la00noix | 0:b87fd8dd4322 | 221 | |
la00noix | 0:b87fd8dd4322 | 222 | new_dist1=EC1.getDistance_mm(); |
la00noix | 0:b87fd8dd4322 | 223 | new_dist2=EC2.getDistance_mm(); |
la00noix | 0:b87fd8dd4322 | 224 | d_dist1=new_dist1-old_dist1; |
la00noix | 0:b87fd8dd4322 | 225 | d_dist2=new_dist2-old_dist2; |
la00noix | 0:b87fd8dd4322 | 226 | old_dist1=new_dist1; |
la00noix | 0:b87fd8dd4322 | 227 | old_dist2=new_dist2; //微小時間当たりのエンコーダ読み込み |
la00noix | 0:b87fd8dd4322 | 228 | |
la00noix | 0:b87fd8dd4322 | 229 | d_x=d_dist2*sin(now_angle*PI/180)-d_dist1*cos(now_angle*PI/180); |
la00noix | 0:b87fd8dd4322 | 230 | d_y=d_dist2*cos(now_angle*PI/180)+d_dist1*sin(now_angle*PI/180); //微小時間毎の座標変化 |
la00noix | 0:b87fd8dd4322 | 231 | info.nowX.enc = info.nowX.enc + d_x; |
la00noix | 0:b87fd8dd4322 | 232 | info.nowY.enc = info.nowY.enc - d_y; //微小時間毎に座標に加算 |
la00noix | 0:b87fd8dd4322 | 233 | } |
la00noix | 0:b87fd8dd4322 | 234 | |
la00noix | 0:b87fd8dd4322 | 235 | void set_cond(int t, int px, double bx, int py, double by) //超音波センサーを使用するときの条件設定関数 |
la00noix | 0:b87fd8dd4322 | 236 | { |
la00noix | 0:b87fd8dd4322 | 237 | //引数の詳細は関数"calc_xy_usw"参照 |
la00noix | 0:b87fd8dd4322 | 238 | |
la00noix | 0:b87fd8dd4322 | 239 | xy_type = t; |
la00noix | 0:b87fd8dd4322 | 240 | |
la00noix | 0:b87fd8dd4322 | 241 | pm_typeX = px; |
la00noix | 0:b87fd8dd4322 | 242 | x_base = bx; |
la00noix | 0:b87fd8dd4322 | 243 | |
la00noix | 0:b87fd8dd4322 | 244 | pm_typeY = py; |
la00noix | 0:b87fd8dd4322 | 245 | y_base = by; |
la00noix | 0:b87fd8dd4322 | 246 | } |
la00noix | 0:b87fd8dd4322 | 247 | |
la00noix | 0:b87fd8dd4322 | 248 | void calc_xy_usw(double tgt_angle) //超音波センサーによる座標計算(機体が旋回する場合はこの方法による座標計算は出来ない) |
la00noix | 0:b87fd8dd4322 | 249 | { |
la00noix | 0:b87fd8dd4322 | 250 | //tgt_angle:機体の目標角度(運動初期角度と同じ/今大会では0,90,180のみ) |
la00noix | 0:b87fd8dd4322 | 251 | //xy_type:(0:Y軸平行の壁を読む/1:X軸平行の壁を読む/2:X,Y軸平行の壁を共に読む) |
la00noix | 0:b87fd8dd4322 | 252 | //pm_typeX,pm_typeY:(0:各軸正方向側の壁を読む/1:各軸負方向側の壁を読む) |
la00noix | 0:b87fd8dd4322 | 253 | //x_base,y_base:超音波センサーで読む壁の座標(y軸並行の壁のx座標/x軸平行の壁のy座標) |
la00noix | 0:b87fd8dd4322 | 254 | |
yuki0701 | 2:820dcd23c8e3 | 255 | double R1=414.5,R2=414.5,R3=414.5,R4=414.5; //機体の中心から各超音波センサーが付いている面までの距離 |
yuki0701 | 2:820dcd23c8e3 | 256 | double D1=237.5,D2=237.5,D3=237.5,D4=237.5; //各超音波センサーが付いている面の中心から各超音波センサーまでの距離(時計回りを正とする) |
la00noix | 0:b87fd8dd4322 | 257 | |
yuki0701 | 1:26fc1b2f1c42 | 258 | // now_angle=gyro.getAngle(); //ジャイロの値読み込み |
yuki0701 | 1:26fc1b2f1c42 | 259 | now_angle = -gyro.getZ_Angle(); |
la00noix | 0:b87fd8dd4322 | 260 | |
la00noix | 0:b87fd8dd4322 | 261 | if(tgt_angle==0) { |
la00noix | 0:b87fd8dd4322 | 262 | if((xy_type==0 || xy_type==2) && pm_typeX==0) { |
la00noix | 0:b87fd8dd4322 | 263 | |
la00noix | 0:b87fd8dd4322 | 264 | info.nowX.usw = x_base - (usw_data4 + R4*cos(now_angle*PI/180) + D4*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 265 | uw_flag4 = 1; |
la00noix | 0:b87fd8dd4322 | 266 | |
la00noix | 0:b87fd8dd4322 | 267 | } else if((xy_type==0 || xy_type==2) && pm_typeX==1) { |
la00noix | 0:b87fd8dd4322 | 268 | |
la00noix | 0:b87fd8dd4322 | 269 | info.nowX.usw = x_base + (usw_data3 + R3*cos(now_angle*PI/180) + D3*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 270 | uw_flag3 = 1; |
la00noix | 0:b87fd8dd4322 | 271 | |
la00noix | 0:b87fd8dd4322 | 272 | } |
la00noix | 0:b87fd8dd4322 | 273 | if((xy_type==1 || xy_type==2) && pm_typeY==0) { |
la00noix | 0:b87fd8dd4322 | 274 | |
la00noix | 0:b87fd8dd4322 | 275 | info.nowY.usw = y_base - (usw_data2 + R2*cos(now_angle*PI/180) + D2*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 276 | uw_flag2 = 1; |
la00noix | 0:b87fd8dd4322 | 277 | |
la00noix | 0:b87fd8dd4322 | 278 | } else if((xy_type==1 || xy_type==2) && pm_typeY==1) { |
la00noix | 0:b87fd8dd4322 | 279 | |
la00noix | 0:b87fd8dd4322 | 280 | info.nowY.usw = y_base + (usw_data1 + R1*cos(now_angle*PI/180) + D1*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 281 | uw_flag1 = 1; |
la00noix | 0:b87fd8dd4322 | 282 | |
la00noix | 0:b87fd8dd4322 | 283 | } |
la00noix | 0:b87fd8dd4322 | 284 | |
la00noix | 0:b87fd8dd4322 | 285 | } else if(tgt_angle==90) { |
la00noix | 0:b87fd8dd4322 | 286 | if((xy_type==0 || xy_type==2) && pm_typeX==0) { |
la00noix | 0:b87fd8dd4322 | 287 | |
la00noix | 0:b87fd8dd4322 | 288 | info.nowX.usw = x_base - (usw_data1 + R1*cos((now_angle-tgt_angle)*PI/180) + D1*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 289 | uw_flag1 = 1; |
la00noix | 0:b87fd8dd4322 | 290 | |
la00noix | 0:b87fd8dd4322 | 291 | } else if((xy_type==0 || xy_type==2) && pm_typeX==1) { |
la00noix | 0:b87fd8dd4322 | 292 | |
la00noix | 0:b87fd8dd4322 | 293 | info.nowX.usw = x_base + (usw_data2 + R2*cos((now_angle-tgt_angle)*PI/180) + D2*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 294 | uw_flag2 = 1; |
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_data4 + R4*cos((now_angle-tgt_angle)*PI/180) + D4*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 300 | uw_flag4 = 1; |
la00noix | 0:b87fd8dd4322 | 301 | |
la00noix | 0:b87fd8dd4322 | 302 | } else if((xy_type==1 || xy_type==2) && pm_typeY==1) { |
la00noix | 0:b87fd8dd4322 | 303 | |
la00noix | 0:b87fd8dd4322 | 304 | info.nowY.usw = y_base + (usw_data3 + R3*cos((now_angle-tgt_angle)*PI/180) + D3*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 305 | uw_flag3 = 1; |
la00noix | 0:b87fd8dd4322 | 306 | |
la00noix | 0:b87fd8dd4322 | 307 | } |
la00noix | 0:b87fd8dd4322 | 308 | |
la00noix | 0:b87fd8dd4322 | 309 | } else if(tgt_angle==180 || tgt_angle==-180) { |
la00noix | 0:b87fd8dd4322 | 310 | if((xy_type==0 || xy_type==2) && pm_typeX==0) { |
la00noix | 0:b87fd8dd4322 | 311 | |
la00noix | 0:b87fd8dd4322 | 312 | info.nowX.usw = x_base - (usw_data3 + R3*cos((now_angle-tgt_angle)*PI/180) + D3*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 313 | uw_flag3 = 1; |
la00noix | 0:b87fd8dd4322 | 314 | |
la00noix | 0:b87fd8dd4322 | 315 | } else if((xy_type==0 || xy_type==2) && pm_typeX==1) { |
la00noix | 0:b87fd8dd4322 | 316 | |
la00noix | 0:b87fd8dd4322 | 317 | info.nowX.usw = x_base + (usw_data4 + R4*cos((now_angle-tgt_angle)*PI/180) + D4*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 318 | uw_flag4 = 1; |
la00noix | 0:b87fd8dd4322 | 319 | |
la00noix | 0:b87fd8dd4322 | 320 | } |
la00noix | 0:b87fd8dd4322 | 321 | if((xy_type==1 || xy_type==2) && pm_typeY==0) { |
la00noix | 0:b87fd8dd4322 | 322 | |
la00noix | 0:b87fd8dd4322 | 323 | info.nowY.usw = y_base - (usw_data1+ R1*cos((now_angle-tgt_angle)*PI/180) + D1*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 324 | uw_flag1 = 1; |
la00noix | 0:b87fd8dd4322 | 325 | |
la00noix | 0:b87fd8dd4322 | 326 | } else if((xy_type==1 || xy_type==2) && pm_typeY==1) { |
la00noix | 0:b87fd8dd4322 | 327 | |
la00noix | 0:b87fd8dd4322 | 328 | info.nowY.usw = y_base + (usw_data2 + R2*cos((now_angle-tgt_angle)*PI/180) + D2*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 329 | uw_flag2 = 1; |
la00noix | 0:b87fd8dd4322 | 330 | |
la00noix | 0:b87fd8dd4322 | 331 | } |
la00noix | 0:b87fd8dd4322 | 332 | } else if(tgt_angle==-90) { |
la00noix | 0:b87fd8dd4322 | 333 | if((xy_type==0 || xy_type==2) && pm_typeX==0) { |
la00noix | 0:b87fd8dd4322 | 334 | |
la00noix | 0:b87fd8dd4322 | 335 | info.nowX.usw = x_base - (usw_data2 + R2*cos((now_angle-tgt_angle)*PI/180) + D2*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 336 | uw_flag2 = 1; |
la00noix | 0:b87fd8dd4322 | 337 | |
la00noix | 0:b87fd8dd4322 | 338 | } else if((xy_type==0 || xy_type==2) && pm_typeX==1) { |
la00noix | 0:b87fd8dd4322 | 339 | |
la00noix | 0:b87fd8dd4322 | 340 | info.nowX.usw = x_base + (usw_data1 + R1*cos((now_angle-tgt_angle)*PI/180) + D1*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 341 | uw_flag1 = 1; |
la00noix | 0:b87fd8dd4322 | 342 | |
la00noix | 0:b87fd8dd4322 | 343 | } |
la00noix | 0:b87fd8dd4322 | 344 | if((xy_type==1 || xy_type==2) && pm_typeY==0) { |
la00noix | 0:b87fd8dd4322 | 345 | |
la00noix | 0:b87fd8dd4322 | 346 | info.nowY.usw = y_base - (usw_data3 + R3*cos((now_angle-tgt_angle)*PI/180) + D3*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 347 | uw_flag3 = 1; |
la00noix | 0:b87fd8dd4322 | 348 | |
la00noix | 0:b87fd8dd4322 | 349 | } else if((xy_type==1 || xy_type==2) && pm_typeY==1) { |
la00noix | 0:b87fd8dd4322 | 350 | |
la00noix | 0:b87fd8dd4322 | 351 | info.nowY.usw = y_base + (usw_data4 + R4*cos((now_angle-tgt_angle)*PI/180) + D4*sin(now_angle*PI/180)); |
yuki0701 | 2:820dcd23c8e3 | 352 | uw_flag4 = 1; |
la00noix | 0:b87fd8dd4322 | 353 | |
la00noix | 0:b87fd8dd4322 | 354 | } |
la00noix | 0:b87fd8dd4322 | 355 | } |
la00noix | 0:b87fd8dd4322 | 356 | } |
la00noix | 0:b87fd8dd4322 | 357 | |
yuki0701 | 2:820dcd23c8e3 | 358 | void uwflag_reset() |
yuki0701 | 2:820dcd23c8e3 | 359 | { |
yuki0701 | 2:820dcd23c8e3 | 360 | uw_flag1 = 0; |
yuki0701 | 2:820dcd23c8e3 | 361 | uw_flag2 = 0; |
yuki0701 | 2:820dcd23c8e3 | 362 | uw_flag3 = 0; |
yuki0701 | 2:820dcd23c8e3 | 363 | uw_flag4 = 0; |
yuki0701 | 2:820dcd23c8e3 | 364 | } |
yuki0701 | 2:820dcd23c8e3 | 365 | |
yuki0701 | 2:820dcd23c8e3 | 366 | void uwflag_change(int u1,int u2, int u3, int u4) |
yuki0701 | 2:820dcd23c8e3 | 367 | { |
yuki0701 | 2:820dcd23c8e3 | 368 | uw_flag1 = u1; |
yuki0701 | 2:820dcd23c8e3 | 369 | uw_flag2 = u2; |
yuki0701 | 2:820dcd23c8e3 | 370 | uw_flag3 = u3; |
yuki0701 | 2:820dcd23c8e3 | 371 | uw_flag4 = u4; |
yuki0701 | 2:820dcd23c8e3 | 372 | } |
yuki0701 | 2:820dcd23c8e3 | 373 | |
yuki0701 | 2:820dcd23c8e3 | 374 | |
la00noix | 0:b87fd8dd4322 | 375 | void calc_xy(double target_angle, double u,double v) |
la00noix | 0:b87fd8dd4322 | 376 | { |
la00noix | 0:b87fd8dd4322 | 377 | //エンコーダにより求めた機体の座標と超音波センサーにより求めた機体の座標を(エンコーダ : 超音波 = u : 1-u / v : 1-v)の割合で混ぜて now_x,now_y に代入する |
la00noix | 0:b87fd8dd4322 | 378 | |
la00noix | 0:b87fd8dd4322 | 379 | calc_xy_enc(); |
yuki0701 | 2:820dcd23c8e3 | 380 | //usw_data1 = 10 * uw1.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 381 | ///usw_data2 = 10 * uw2.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 382 | //usw_data3 = 10 * uw3.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 383 | ///usw_data4 = 10 * uw4.get_dist(); |
yuki0701 | 2:820dcd23c8e3 | 384 | |
yuki0701 | 2:820dcd23c8e3 | 385 | //printf("uw2 = %f, uw4 = %f\n\r",usw_data2,usw_data4); |
la00noix | 0:b87fd8dd4322 | 386 | |
la00noix | 0:b87fd8dd4322 | 387 | if(u != 1 || v != 1) { |
la00noix | 0:b87fd8dd4322 | 388 | calc_xy_usw(target_angle); //エンコーダの値しか使用しない場合は超音波センサーによる座標計算は行わずに計算量を減らす。 |
la00noix | 0:b87fd8dd4322 | 389 | } |
la00noix | 0:b87fd8dd4322 | 390 | |
la00noix | 0:b87fd8dd4322 | 391 | now_x = u * info.nowX.enc + (1-u) * info.nowX.usw; |
la00noix | 0:b87fd8dd4322 | 392 | now_y = v * info.nowY.enc + (1-v) * info.nowY.usw; |
la00noix | 0:b87fd8dd4322 | 393 | |
la00noix | 0:b87fd8dd4322 | 394 | /*if(now_x >-1 && now_x <1 && now_y >-1 && now_y <1){ //スタート時の0合わせ用 |
la00noix | 0:b87fd8dd4322 | 395 | ec_led = 1; |
la00noix | 0:b87fd8dd4322 | 396 | }else{ |
la00noix | 0:b87fd8dd4322 | 397 | ec_led = 0; |
la00noix | 0:b87fd8dd4322 | 398 | } |
la00noix | 0:b87fd8dd4322 | 399 | |
la00noix | 0:b87fd8dd4322 | 400 | if(now_angle >-0.5 && now_angle <0.5){ |
la00noix | 0:b87fd8dd4322 | 401 | gyro_led = 1; |
la00noix | 0:b87fd8dd4322 | 402 | }else{ |
la00noix | 0:b87fd8dd4322 | 403 | gyro_led = 0; |
la00noix | 0:b87fd8dd4322 | 404 | }*/ |
la00noix | 0:b87fd8dd4322 | 405 | } |
la00noix | 0:b87fd8dd4322 | 406 | |
la00noix | 0:b87fd8dd4322 | 407 | void enc_correction(int x_select,int y_select) //エンコーダの座標を超音波センサの座標で上書き |
la00noix | 0:b87fd8dd4322 | 408 | { |
la00noix | 0:b87fd8dd4322 | 409 | //x_select,y_select → (0:上書きしない/1:上書きする) |
la00noix | 0:b87fd8dd4322 | 410 | |
la00noix | 0:b87fd8dd4322 | 411 | if(x_select == 1) { |
la00noix | 0:b87fd8dd4322 | 412 | info.nowX.enc = info.nowX.usw; |
la00noix | 0:b87fd8dd4322 | 413 | } |
la00noix | 0:b87fd8dd4322 | 414 | if(y_select == 1) { |
la00noix | 0:b87fd8dd4322 | 415 | info.nowY.enc = info.nowY.usw; |
la00noix | 0:b87fd8dd4322 | 416 | } |
la00noix | 0:b87fd8dd4322 | 417 | |
la00noix | 0:b87fd8dd4322 | 418 | } |
la00noix | 0:b87fd8dd4322 | 419 | |
yuki0701 | 2:820dcd23c8e3 | 420 | void enc_correction2(int x_plot1, int y_plot2) //引数の座標でエンコーダの座標を修正 |
yuki0701 | 2:820dcd23c8e3 | 421 | { |
yuki0701 | 2:820dcd23c8e3 | 422 | info.nowX.enc = x_plot1; |
yuki0701 | 2:820dcd23c8e3 | 423 | info.nowY.enc = y_plot2; |
yuki0701 | 2:820dcd23c8e3 | 424 | } |
yuki0701 | 2:820dcd23c8e3 | 425 | |
la00noix | 0:b87fd8dd4322 | 426 | //ここからそれぞれのプログラム///////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
la00noix | 0:b87fd8dd4322 | 427 | //now_x(現在のx座標),now_y(現在のy座標),now_angle(機体角度(ラジアンではない)(0~360や-180~180とは限らない))(反時計回りが正) |
la00noix | 0:b87fd8dd4322 | 428 | //ジャイロの出力は角度だが三角関数はラジアンとして計算する |
la00noix | 0:b87fd8dd4322 | 429 | //通常の移動+座標のずれ補正+機体の角度補正(+必要に応じさらに別補正) |
la00noix | 0:b87fd8dd4322 | 430 | //ジャイロの仕様上、角度補正をするときに計算式内で角度はそのままよりsinをとったほうがいいかもね |
la00noix | 0:b87fd8dd4322 | 431 | |
la00noix | 0:b87fd8dd4322 | 432 | void purecurve(int type,double u,double v, //正面を変えずに円弧or楕円を描いて曲がる |
la00noix | 0:b87fd8dd4322 | 433 | double point_x1,double point_y1, |
la00noix | 0:b87fd8dd4322 | 434 | double point_x2,double point_y2, |
la00noix | 0:b87fd8dd4322 | 435 | int theta, |
la00noix | 0:b87fd8dd4322 | 436 | double speed, |
la00noix | 0:b87fd8dd4322 | 437 | double q_p,double q_d, |
la00noix | 0:b87fd8dd4322 | 438 | double r_p,double r_d, |
la00noix | 0:b87fd8dd4322 | 439 | double r_out_max, |
yuki0701 | 2:820dcd23c8e3 | 440 | double target_angle, double v_base, double q_out_max) |
la00noix | 0:b87fd8dd4322 | 441 | //type:動きの種類(8パターン) point_x1,point_y1=出発地点の座標 point_x2,point_x2=目標地点の座標 theta=plotの間隔(0~90°) speed=速度 |
la00noix | 0:b87fd8dd4322 | 442 | { |
la00noix | 0:b87fd8dd4322 | 443 | //-----PathFollowingのパラメーター設定-----// |
la00noix | 0:b87fd8dd4322 | 444 | q_setPDparam(q_p,q_d); //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:b87fd8dd4322 | 445 | r_setPDparam(r_p,r_d); //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:b87fd8dd4322 | 446 | set_r_out(r_out_max); //旋回時の最大出力値設定関数 |
yuki0701 | 2:820dcd23c8e3 | 447 | set_q_out(q_out_max); |
la00noix | 0:b87fd8dd4322 | 448 | set_target_angle(target_angle); //機体目標角度設定関数 |
la00noix | 0:b87fd8dd4322 | 449 | |
la00noix | 0:b87fd8dd4322 | 450 | int s; |
la00noix | 0:b87fd8dd4322 | 451 | int t = 0; |
la00noix | 0:b87fd8dd4322 | 452 | double X,Y;//X=楕円の中心座標、Y=楕円の中心座標 |
la00noix | 0:b87fd8dd4322 | 453 | double a,b; //a=楕円のx軸方向の幅の半分,b=楕円のy軸方向の幅の半分 |
la00noix | 0:b87fd8dd4322 | 454 | double plotx[(90/theta)+1]; //楕円にとるplotのx座標 |
la00noix | 0:b87fd8dd4322 | 455 | double ploty[(90/theta)+1]; |
la00noix | 0:b87fd8dd4322 | 456 | |
la00noix | 0:b87fd8dd4322 | 457 | double x_out,y_out,r_out; |
la00noix | 0:b87fd8dd4322 | 458 | |
la00noix | 0:b87fd8dd4322 | 459 | a=fabs(point_x1-point_x2); |
la00noix | 0:b87fd8dd4322 | 460 | b=fabs(point_y1-point_y2); |
la00noix | 0:b87fd8dd4322 | 461 | |
la00noix | 0:b87fd8dd4322 | 462 | switch(type) { |
la00noix | 0:b87fd8dd4322 | 463 | |
la00noix | 0:b87fd8dd4322 | 464 | case 1://→↑移動 |
la00noix | 0:b87fd8dd4322 | 465 | X=point_x1; |
la00noix | 0:b87fd8dd4322 | 466 | Y=point_y2; |
la00noix | 0:b87fd8dd4322 | 467 | |
la00noix | 0:b87fd8dd4322 | 468 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 469 | plotx[s] = X + a * cos(-PI/2 + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 470 | ploty[s] = Y + b * sin(-PI/2 + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 471 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 472 | } |
la00noix | 0:b87fd8dd4322 | 473 | break; |
la00noix | 0:b87fd8dd4322 | 474 | |
la00noix | 0:b87fd8dd4322 | 475 | case 2://↑→移動 |
la00noix | 0:b87fd8dd4322 | 476 | X=point_x2; |
la00noix | 0:b87fd8dd4322 | 477 | Y=point_y1; |
la00noix | 0:b87fd8dd4322 | 478 | |
la00noix | 0:b87fd8dd4322 | 479 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 480 | plotx[s] = X + a * cos(PI - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 481 | ploty[s] = Y + b * sin(PI - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 482 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 483 | } |
la00noix | 0:b87fd8dd4322 | 484 | break; |
la00noix | 0:b87fd8dd4322 | 485 | |
la00noix | 0:b87fd8dd4322 | 486 | case 3://↑←移動 |
la00noix | 0:b87fd8dd4322 | 487 | X=point_x2; |
la00noix | 0:b87fd8dd4322 | 488 | Y=point_y1; |
la00noix | 0:b87fd8dd4322 | 489 | |
la00noix | 0:b87fd8dd4322 | 490 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 491 | plotx[s] = X + a * cos(s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 492 | ploty[s] = Y + b * sin(s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 493 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 494 | } |
la00noix | 0:b87fd8dd4322 | 495 | break; |
la00noix | 0:b87fd8dd4322 | 496 | |
la00noix | 0:b87fd8dd4322 | 497 | case 4://←↑移動 |
la00noix | 0:b87fd8dd4322 | 498 | X=point_x1; |
la00noix | 0:b87fd8dd4322 | 499 | Y=point_y2; |
la00noix | 0:b87fd8dd4322 | 500 | |
la00noix | 0:b87fd8dd4322 | 501 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 502 | plotx[s] = X + a * cos(-PI/2 - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 503 | ploty[s] = Y + b * sin(-PI/2 - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 504 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 505 | } |
la00noix | 0:b87fd8dd4322 | 506 | break; |
la00noix | 0:b87fd8dd4322 | 507 | |
la00noix | 0:b87fd8dd4322 | 508 | case 5://←↓移動 |
la00noix | 0:b87fd8dd4322 | 509 | X=point_x1; |
la00noix | 0:b87fd8dd4322 | 510 | Y=point_y2; |
la00noix | 0:b87fd8dd4322 | 511 | |
la00noix | 0:b87fd8dd4322 | 512 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 513 | plotx[s] = X + a * cos(PI/2 + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 514 | ploty[s] = Y + b * sin(PI/2 + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 515 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 516 | } |
la00noix | 0:b87fd8dd4322 | 517 | break; |
la00noix | 0:b87fd8dd4322 | 518 | |
la00noix | 0:b87fd8dd4322 | 519 | case 6://↓←移動 |
la00noix | 0:b87fd8dd4322 | 520 | X=point_x2; |
la00noix | 0:b87fd8dd4322 | 521 | Y=point_y1; |
la00noix | 0:b87fd8dd4322 | 522 | |
la00noix | 0:b87fd8dd4322 | 523 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 524 | plotx[s] = X + a * cos(-s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 525 | ploty[s] = Y + b * sin(-s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 526 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 527 | } |
la00noix | 0:b87fd8dd4322 | 528 | break; |
la00noix | 0:b87fd8dd4322 | 529 | |
la00noix | 0:b87fd8dd4322 | 530 | case 7://↓→移動 |
la00noix | 0:b87fd8dd4322 | 531 | X=point_x2; |
la00noix | 0:b87fd8dd4322 | 532 | Y=point_y1; |
la00noix | 0:b87fd8dd4322 | 533 | |
la00noix | 0:b87fd8dd4322 | 534 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 535 | plotx[s] = X + a * cos(PI + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 536 | ploty[s] = Y + b * sin(PI + s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 537 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 538 | } |
la00noix | 0:b87fd8dd4322 | 539 | break; |
la00noix | 0:b87fd8dd4322 | 540 | |
la00noix | 0:b87fd8dd4322 | 541 | case 8://→↓移動 |
la00noix | 0:b87fd8dd4322 | 542 | X=point_x1; |
la00noix | 0:b87fd8dd4322 | 543 | Y=point_y2; |
la00noix | 0:b87fd8dd4322 | 544 | |
la00noix | 0:b87fd8dd4322 | 545 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:b87fd8dd4322 | 546 | plotx[s] = X + a * cos(PI/2 - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 547 | ploty[s] = Y + b * sin(PI/2 - s * (PI*theta/180)); |
la00noix | 0:b87fd8dd4322 | 548 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:b87fd8dd4322 | 549 | } |
la00noix | 0:b87fd8dd4322 | 550 | break; |
la00noix | 0:b87fd8dd4322 | 551 | } |
la00noix | 0:b87fd8dd4322 | 552 | |
la00noix | 0:b87fd8dd4322 | 553 | while(1) { |
la00noix | 0:b87fd8dd4322 | 554 | |
la00noix | 0:b87fd8dd4322 | 555 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 556 | if(id1_value[6] != flag)break; |
la00noix | 0:b87fd8dd4322 | 557 | |
la00noix | 0:b87fd8dd4322 | 558 | calc_xy(target_angle,u,v); |
la00noix | 0:b87fd8dd4322 | 559 | |
la00noix | 0:b87fd8dd4322 | 560 | XYRmotorout(plotx[t],ploty[t],plotx[t+1],ploty[t+1],&x_out,&y_out,&r_out,speed,speed); |
la00noix | 0:b87fd8dd4322 | 561 | CalMotorOut(x_out,y_out,r_out); |
la00noix | 0:b87fd8dd4322 | 562 | //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 | 563 | |
yuki0701 | 2:820dcd23c8e3 | 564 | base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),v_base); //m1~m4に代入 |
la00noix | 0:b87fd8dd4322 | 565 | //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 | 566 | |
la00noix | 0:b87fd8dd4322 | 567 | 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 | 568 | |
yuki0701 | 1:26fc1b2f1c42 | 569 | // MaxonControl(m1,m2,m3,m4); //出力 |
yuki0701 | 1:26fc1b2f1c42 | 570 | // 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 | 571 | |
la00noix | 0:b87fd8dd4322 | 572 | if(t == (90/theta))break; |
la00noix | 0:b87fd8dd4322 | 573 | } |
la00noix | 0:b87fd8dd4322 | 574 | } |
la00noix | 0:b87fd8dd4322 | 575 | |
la00noix | 0:b87fd8dd4322 | 576 | void gogo_straight(double u,double v, //直線運動プログラム |
la00noix | 0:b87fd8dd4322 | 577 | double x1_point,double y1_point, |
la00noix | 0:b87fd8dd4322 | 578 | double x2_point,double y2_point, |
la00noix | 0:b87fd8dd4322 | 579 | double speed1,double speed2, |
la00noix | 0:b87fd8dd4322 | 580 | double q_p,double q_d, |
la00noix | 0:b87fd8dd4322 | 581 | double r_p,double r_d, |
la00noix | 0:b87fd8dd4322 | 582 | double r_out_max, |
yuki0701 | 2:820dcd23c8e3 | 583 | double target_angle,double v_base, double q_out_max) |
la00noix | 0:b87fd8dd4322 | 584 | //引数:出発地点の座標(x,y)、目標地点の座標(x,y)、初速度(speed1)、目標速度(speed2)//speed1=speed2 のとき等速運動 |
la00noix | 0:b87fd8dd4322 | 585 | { |
la00noix | 0:b87fd8dd4322 | 586 | //-----PathFollowingのパラメーター設定-----// |
la00noix | 0:b87fd8dd4322 | 587 | q_setPDparam(q_p,q_d); //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:b87fd8dd4322 | 588 | r_setPDparam(r_p,r_d); //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:b87fd8dd4322 | 589 | set_r_out(r_out_max); //旋回時の最大出力値設定関数 |
yuki0701 | 2:820dcd23c8e3 | 590 | set_q_out(q_out_max); |
la00noix | 0:b87fd8dd4322 | 591 | set_target_angle(target_angle); //機体目標角度設定関数 |
la00noix | 0:b87fd8dd4322 | 592 | |
la00noix | 0:b87fd8dd4322 | 593 | while (1) { |
la00noix | 0:b87fd8dd4322 | 594 | |
la00noix | 0:b87fd8dd4322 | 595 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 596 | if(id1_value[6] != flag)break; |
la00noix | 0:b87fd8dd4322 | 597 | |
la00noix | 0:b87fd8dd4322 | 598 | calc_xy(target_angle,u,v); |
la00noix | 0:b87fd8dd4322 | 599 | |
la00noix | 0:b87fd8dd4322 | 600 | XYRmotorout(x1_point,y1_point,x2_point,y2_point,&x_out,&y_out,&r_out,speed1,speed2); |
yuki0701 | 1:26fc1b2f1c42 | 601 | //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 | 602 | |
la00noix | 0:b87fd8dd4322 | 603 | CalMotorOut(x_out,y_out,r_out); |
la00noix | 0:b87fd8dd4322 | 604 | //printf("out1=%lf, out2=%lf, out3=%lf, out4=%lf\n",GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3)); |
la00noix | 0:b87fd8dd4322 | 605 | |
yuki0701 | 2:820dcd23c8e3 | 606 | base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),v_base); |
la00noix | 0:b87fd8dd4322 | 607 | //printf("m1=%d, m2=%d, m3=%d, m4=%d\r\n",m_1,m_2,m_3,m_4); |
la00noix | 0:b87fd8dd4322 | 608 | |
yuki0701 | 1:26fc1b2f1c42 | 609 | // MaxonControl(m1,m2,m3,m4); |
yuki0701 | 1:26fc1b2f1c42 | 610 | // 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 | 611 | 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 | 2:820dcd23c8e3 | 612 | //printf("usw2 = %f usw4 = %f x=%f y=%f angle=%f\n\r",usw_data2,usw_data4,now_x,now_y,now_angle); |
la00noix | 0:b87fd8dd4322 | 613 | |
la00noix | 0:b87fd8dd4322 | 614 | if(((x2_point - now_x)*(x2_point - x1_point) + (y2_point - now_y)*(y2_point - y1_point)) < 0)break; |
la00noix | 0:b87fd8dd4322 | 615 | } |
la00noix | 0:b87fd8dd4322 | 616 | } |
la00noix | 0:b87fd8dd4322 | 617 | |
yuki0701 | 2:820dcd23c8e3 | 618 | |
yuki0701 | 2:820dcd23c8e3 | 619 | |
yuki0701 | 2:820dcd23c8e3 | 620 | double spline_base(int i, int k, double t, int nv[]) //スプライン基底関数を求める関数 |
yuki0701 | 2:820dcd23c8e3 | 621 | { |
yuki0701 | 2:820dcd23c8e3 | 622 | // i:0~(制御点の個数-1) |
yuki0701 | 2:820dcd23c8e3 | 623 | // k:スプライト曲線の次元 |
yuki0701 | 2:820dcd23c8e3 | 624 | // t:0~(ノットベクトルの最大値) |
yuki0701 | 2:820dcd23c8e3 | 625 | // nv[]:ノットベクトル |
yuki0701 | 2:820dcd23c8e3 | 626 | double w1 = 0.0, w2 = 0.0; |
yuki0701 | 2:820dcd23c8e3 | 627 | if (k == 1) { |
yuki0701 | 2:820dcd23c8e3 | 628 | if (t > nv[i] && t <= nv[i + 1]) |
yuki0701 | 2:820dcd23c8e3 | 629 | return 1.0; |
yuki0701 | 2:820dcd23c8e3 | 630 | else |
yuki0701 | 2:820dcd23c8e3 | 631 | return 0.0; |
yuki0701 | 2:820dcd23c8e3 | 632 | } else { |
yuki0701 | 2:820dcd23c8e3 | 633 | if ((nv[i + k] - nv[i + 1]) != 0) { |
yuki0701 | 2:820dcd23c8e3 | 634 | w1 = ((nv[i + k] - t) / (nv[i + k] - nv[i + 1])) * spline_base(i + 1, k - 1, t, nv); |
yuki0701 | 2:820dcd23c8e3 | 635 | //printf("%f\n\r",w1); |
yuki0701 | 2:820dcd23c8e3 | 636 | } |
yuki0701 | 2:820dcd23c8e3 | 637 | if ((nv[i + k - 1] - nv[i]) != 0) { |
yuki0701 | 2:820dcd23c8e3 | 638 | w2 = ((t - nv[i]) / (nv[i + k - 1] - nv[i])) * spline_base(i, k - 1, t, nv); |
yuki0701 | 2:820dcd23c8e3 | 639 | //printf("%f\n\r",w2); |
yuki0701 | 2:820dcd23c8e3 | 640 | } |
yuki0701 | 2:820dcd23c8e3 | 641 | return (w1 + w2); |
yuki0701 | 2:820dcd23c8e3 | 642 | } |
yuki0701 | 2:820dcd23c8e3 | 643 | } |
yuki0701 | 2:820dcd23c8e3 | 644 | |
yuki0701 | 2:820dcd23c8e3 | 645 | |
yuki0701 | 2:820dcd23c8e3 | 646 | void spline_move(double u, double v, |
yuki0701 | 2:820dcd23c8e3 | 647 | double st_x,double st_y,double end_x,double end_y, |
yuki0701 | 2:820dcd23c8e3 | 648 | double cont1_x,double cont1_y,double cont2_x,double cont2_y, |
yuki0701 | 2:820dcd23c8e3 | 649 | double st_speed, double end_speed, |
yuki0701 | 2:820dcd23c8e3 | 650 | double q_p,double q_d, |
yuki0701 | 2:820dcd23c8e3 | 651 | double r_p,double r_d, |
yuki0701 | 2:820dcd23c8e3 | 652 | double r_out_max, |
yuki0701 | 2:820dcd23c8e3 | 653 | double target_angle, double v_base, double q_out_max, int num) |
yuki0701 | 2:820dcd23c8e3 | 654 | { |
yuki0701 | 2:820dcd23c8e3 | 655 | double dx, dy, dr; |
yuki0701 | 2:820dcd23c8e3 | 656 | int nt[] = {0, 0, 0, 1, 2, 2, 2}; //ノットベクトル |
yuki0701 | 2:820dcd23c8e3 | 657 | //dr = (end_angle - st_angle) / num; |
yuki0701 | 2:820dcd23c8e3 | 658 | int ds = (end_speed - st_speed) / num; |
yuki0701 | 2:820dcd23c8e3 | 659 | |
yuki0701 | 2:820dcd23c8e3 | 660 | //-----PathFollowingのパラメーター設定-----// |
yuki0701 | 2:820dcd23c8e3 | 661 | q_setPDparam(q_p,q_d); //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 |
yuki0701 | 2:820dcd23c8e3 | 662 | r_setPDparam(r_p,r_d); //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 |
yuki0701 | 2:820dcd23c8e3 | 663 | set_r_out(r_out_max); //旋回時の最大出力値設定関数 |
yuki0701 | 2:820dcd23c8e3 | 664 | set_q_out(q_out_max); |
yuki0701 | 2:820dcd23c8e3 | 665 | set_target_angle(target_angle); //機体目標角度設定関数 |
yuki0701 | 2:820dcd23c8e3 | 666 | |
yuki0701 | 2:820dcd23c8e3 | 667 | double plotx[num + 1]; //楕円にとるplotのx座標 |
yuki0701 | 2:820dcd23c8e3 | 668 | double ploty[num + 1]; |
yuki0701 | 2:820dcd23c8e3 | 669 | double value_t; |
yuki0701 | 2:820dcd23c8e3 | 670 | int i, j; |
yuki0701 | 2:820dcd23c8e3 | 671 | int t = 0; |
yuki0701 | 2:820dcd23c8e3 | 672 | // for(i = 0; i < 7; i++){ |
yuki0701 | 2:820dcd23c8e3 | 673 | // printf("not_V = %d\n\r",nt[i]); |
yuki0701 | 2:820dcd23c8e3 | 674 | // } |
yuki0701 | 2:820dcd23c8e3 | 675 | for (i = 0; i < num + 1; i++) { |
yuki0701 | 2:820dcd23c8e3 | 676 | plotx[i] = 0.0; |
yuki0701 | 2:820dcd23c8e3 | 677 | ploty[i] = 0.0; |
yuki0701 | 2:820dcd23c8e3 | 678 | } |
yuki0701 | 2:820dcd23c8e3 | 679 | printf("{\n"); |
yuki0701 | 2:820dcd23c8e3 | 680 | for (i = 0; i < num + 1; i++) { |
yuki0701 | 2:820dcd23c8e3 | 681 | value_t = (double)2 * i / num; |
yuki0701 | 2:820dcd23c8e3 | 682 | for (j = 0; j < 4; j++) { |
yuki0701 | 2:820dcd23c8e3 | 683 | if (j == 0) { |
yuki0701 | 2:820dcd23c8e3 | 684 | plotx[i] += st_x * spline_base(j, 3, value_t, nt); |
yuki0701 | 2:820dcd23c8e3 | 685 | ploty[i] += st_y * spline_base(j, 3, value_t, nt); |
yuki0701 | 2:820dcd23c8e3 | 686 | } else if (j == 1) { |
yuki0701 | 2:820dcd23c8e3 | 687 | plotx[i] += cont1_x * spline_base(j, 3, value_t, nt); |
yuki0701 | 2:820dcd23c8e3 | 688 | ploty[i] += cont1_y * spline_base(j, 3, value_t, nt); |
yuki0701 | 2:820dcd23c8e3 | 689 | } else if (j == 2) { |
yuki0701 | 2:820dcd23c8e3 | 690 | plotx[i] += cont2_x * spline_base(j, 3, value_t, nt); |
yuki0701 | 2:820dcd23c8e3 | 691 | ploty[i] += cont2_y * spline_base(j, 3, value_t, nt); |
yuki0701 | 2:820dcd23c8e3 | 692 | } else if (j == 3) { |
yuki0701 | 2:820dcd23c8e3 | 693 | plotx[i] += end_x * spline_base(j, 3, value_t, nt); |
yuki0701 | 2:820dcd23c8e3 | 694 | ploty[i] += end_y * spline_base(j, 3, value_t, nt); |
yuki0701 | 2:820dcd23c8e3 | 695 | } |
yuki0701 | 2:820dcd23c8e3 | 696 | } |
yuki0701 | 2:820dcd23c8e3 | 697 | //printf("plot_x = %f, plot_y = %f\n\r", plotx[i], ploty[i]); |
yuki0701 | 2:820dcd23c8e3 | 698 | } |
yuki0701 | 2:820dcd23c8e3 | 699 | while(1) { |
yuki0701 | 2:820dcd23c8e3 | 700 | |
yuki0701 | 2:820dcd23c8e3 | 701 | if(id1_value[0] != 1)break; |
yuki0701 | 2:820dcd23c8e3 | 702 | if(id1_value[6] != flag)break; |
yuki0701 | 2:820dcd23c8e3 | 703 | |
yuki0701 | 2:820dcd23c8e3 | 704 | calc_xy(target_angle,u,v); |
yuki0701 | 2:820dcd23c8e3 | 705 | |
yuki0701 | 2:820dcd23c8e3 | 706 | XYRmotorout(plotx[t],ploty[t],plotx[t+1],ploty[t+1],&x_out,&y_out,&r_out,st_speed+ds*t,st_speed+ds*(t+1)); |
yuki0701 | 2:820dcd23c8e3 | 707 | CalMotorOut(x_out,y_out,r_out); |
yuki0701 | 2:820dcd23c8e3 | 708 | //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); |
yuki0701 | 2:820dcd23c8e3 | 709 | |
yuki0701 | 2:820dcd23c8e3 | 710 | base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),v_base); //m1~m4に代入 |
yuki0701 | 2:820dcd23c8e3 | 711 | //debug_printf("t=%d (0)=%f (1)=%f (2)=%f (3)=%f\n\r",t,GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3)); |
yuki0701 | 2:820dcd23c8e3 | 712 | |
yuki0701 | 2:820dcd23c8e3 | 713 | if(((plotx[t+1] - now_x)*(plotx[t+1] - plotx[t]) + (ploty[t+1] - now_y)*(ploty[t+1] - ploty[t])) < 0)t++; |
yuki0701 | 2:820dcd23c8e3 | 714 | |
yuki0701 | 2:820dcd23c8e3 | 715 | // MaxonControl(m1,m2,m3,m4); //出力 |
yuki0701 | 2:820dcd23c8e3 | 716 | // 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); |
yuki0701 | 2:820dcd23c8e3 | 717 | 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 | 2:820dcd23c8e3 | 718 | |
yuki0701 | 2:820dcd23c8e3 | 719 | if(t == num)break; |
yuki0701 | 2:820dcd23c8e3 | 720 | } |
yuki0701 | 2:820dcd23c8e3 | 721 | |
yuki0701 | 2:820dcd23c8e3 | 722 | } |
yuki0701 | 2:820dcd23c8e3 | 723 | |
yuki0701 | 2:820dcd23c8e3 | 724 | |
yuki0701 | 2:820dcd23c8e3 | 725 | |
yuki0701 | 2:820dcd23c8e3 | 726 | |
la00noix | 0:b87fd8dd4322 | 727 | /*void pos_correction(double tgt_x, double tgt_y, double tgt_angle, double u, double v) //位置補正(使用前にMaxonControl(0,0,0,0)を入れる) |
la00noix | 0:b87fd8dd4322 | 728 | { |
la00noix | 0:b87fd8dd4322 | 729 | |
la00noix | 0:b87fd8dd4322 | 730 | double r, R=10; // r:一回補正が入るごとの機体の位置と目標位置の距離(ズレ) R:補正終了とみなす目標位置からの機体の位置のズレ |
la00noix | 0:b87fd8dd4322 | 731 | double out; |
la00noix | 0:b87fd8dd4322 | 732 | |
la00noix | 0:b87fd8dd4322 | 733 | calc_xy(tgt_angle, u, v); |
la00noix | 0:b87fd8dd4322 | 734 | |
la00noix | 0:b87fd8dd4322 | 735 | while(1) { //機体の位置を目標領域(目標座標+許容誤差)に収める |
la00noix | 0:b87fd8dd4322 | 736 | 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 | 737 | MaxonControl(0,0,0,0); |
la00noix | 0:b87fd8dd4322 | 738 | |
la00noix | 0:b87fd8dd4322 | 739 | calc_xy(tgt_angle, u, v); |
la00noix | 0:b87fd8dd4322 | 740 | |
la00noix | 0:b87fd8dd4322 | 741 | r=hypot(now_x - tgt_x, now_y - tgt_y); |
la00noix | 0:b87fd8dd4322 | 742 | |
la00noix | 0:b87fd8dd4322 | 743 | if(r < R) break; |
la00noix | 0:b87fd8dd4322 | 744 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 745 | } |
la00noix | 0:b87fd8dd4322 | 746 | |
la00noix | 0:b87fd8dd4322 | 747 | while(1) { |
la00noix | 0:b87fd8dd4322 | 748 | |
la00noix | 0:b87fd8dd4322 | 749 | calc_gyro(); |
la00noix | 0:b87fd8dd4322 | 750 | |
la00noix | 0:b87fd8dd4322 | 751 | out = 10 * (tgt_angle - now_angle); |
la00noix | 0:b87fd8dd4322 | 752 | |
la00noix | 0:b87fd8dd4322 | 753 | if(out > 300) { //0~179°のときは時計回りに回転 |
la00noix | 0:b87fd8dd4322 | 754 | MaxonControl(300,300,300,300); |
la00noix | 0:b87fd8dd4322 | 755 | } else if(out < -300) { |
la00noix | 0:b87fd8dd4322 | 756 | MaxonControl(-300,-300,-300,-300); |
la00noix | 0:b87fd8dd4322 | 757 | } else if(out <= 300 && out > -300) { |
la00noix | 0:b87fd8dd4322 | 758 | MaxonControl(out,out,out,out); |
la00noix | 0:b87fd8dd4322 | 759 | } |
la00noix | 0:b87fd8dd4322 | 760 | |
la00noix | 0:b87fd8dd4322 | 761 | if(tgt_angle - 0.5 < now_angle && now_angle < tgt_angle + 0.5) break; //目標角度からの許容誤差内に機体の角度が収まった時、補正終了 |
la00noix | 0:b87fd8dd4322 | 762 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 763 | } |
la00noix | 0:b87fd8dd4322 | 764 | MaxonControl(0,0,0,0); |
la00noix | 0:b87fd8dd4322 | 765 | }*/ |
la00noix | 0:b87fd8dd4322 | 766 | |
yuki0701 | 2:820dcd23c8e3 | 767 | void pos_correction(double tgt_x, double tgt_y, double tgt_angle, double u, double v, double v_base) //改良版 位置補正(使用前にMaxonControl(0,0,0,0)を入れる) |
la00noix | 0:b87fd8dd4322 | 768 | { |
la00noix | 0:b87fd8dd4322 | 769 | //距離に比例させて補正初速度を増加させる。(最大速度を設定しそれ以上は出ないようにする) |
la00noix | 0:b87fd8dd4322 | 770 | |
yuki0701 | 2:820dcd23c8e3 | 771 | double first_speed, first_speed50 = 10, last_speed = 10, Max_speed = 500, speed5 = 20; |
yuki0701 | 2:820dcd23c8e3 | 772 | double r, R=50; // r:一回補正が入るごとの機体の位置と目標位置の距離(ズレ) R:補正終了とみなす目標位置からの機体の位置のズレ |
la00noix | 0:b87fd8dd4322 | 773 | double out; |
la00noix | 0:b87fd8dd4322 | 774 | |
la00noix | 0:b87fd8dd4322 | 775 | calc_xy(tgt_angle, u, v); |
la00noix | 0:b87fd8dd4322 | 776 | |
la00noix | 0:b87fd8dd4322 | 777 | //r = hypot(now_x - tgt_x, now_y - tgt_y); |
la00noix | 0:b87fd8dd4322 | 778 | |
la00noix | 0:b87fd8dd4322 | 779 | while(1) { //機体の位置を目標領域(目標座標+許容誤差)に収める |
yuki0701 | 1:26fc1b2f1c42 | 780 | //printf("col\n\n\n"); |
la00noix | 0:b87fd8dd4322 | 781 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 782 | if(id1_value[6] != flag)break; |
la00noix | 0:b87fd8dd4322 | 783 | |
la00noix | 0:b87fd8dd4322 | 784 | //first_speed = first_speed50 * r / 50; |
la00noix | 0:b87fd8dd4322 | 785 | |
la00noix | 0:b87fd8dd4322 | 786 | /*if(first_speed > Max_speed){ |
yuki0701 | 2:820dcd23c8e3 | 787 | 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, v_base); |
la00noix | 0:b87fd8dd4322 | 788 | }else{ |
la00noix | 0:b87fd8dd4322 | 789 | 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 | 790 | }*/ |
la00noix | 0:b87fd8dd4322 | 791 | |
yuki0701 | 1:26fc1b2f1c42 | 792 | //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 | 2:820dcd23c8e3 | 793 | |
yuki0701 | 2:820dcd23c8e3 | 794 | int diff_sm = hypot(now_x-tgt_x,now_y-tgt_y); |
yuki0701 | 2:820dcd23c8e3 | 795 | |
yuki0701 | 2:820dcd23c8e3 | 796 | int f_speed = diff_sm / 5 * (speed5 - last_speed); |
yuki0701 | 2:820dcd23c8e3 | 797 | gogo_straight(u,v,now_x,now_y,tgt_x,tgt_y,f_speed,last_speed,0.5,0.05,5,0.05,20,tgt_angle, v_base, 30); |
yuki0701 | 1:26fc1b2f1c42 | 798 | //gogo_straight(1,1,0,0,200,0,50,500,5,0.1,10,0.1,50,0); |
yuki0701 | 1:26fc1b2f1c42 | 799 | //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 | 800 | |
yuki0701 | 1:26fc1b2f1c42 | 801 | // MaxonControl(0,0,0,0); |
yuki0701 | 1:26fc1b2f1c42 | 802 | m1 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 803 | m2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 804 | m3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 805 | m4 = 0; |
la00noix | 0:b87fd8dd4322 | 806 | |
la00noix | 0:b87fd8dd4322 | 807 | calc_xy(tgt_angle, u, v); |
la00noix | 0:b87fd8dd4322 | 808 | |
la00noix | 0:b87fd8dd4322 | 809 | r=hypot(now_x - tgt_x, now_y - tgt_y); |
la00noix | 0:b87fd8dd4322 | 810 | |
la00noix | 0:b87fd8dd4322 | 811 | if(r < R) break; |
la00noix | 0:b87fd8dd4322 | 812 | } |
la00noix | 0:b87fd8dd4322 | 813 | |
la00noix | 0:b87fd8dd4322 | 814 | while(1) { |
la00noix | 0:b87fd8dd4322 | 815 | |
la00noix | 0:b87fd8dd4322 | 816 | if(id1_value[0] != 1)break; |
la00noix | 0:b87fd8dd4322 | 817 | if(id1_value[6] != flag)break; |
la00noix | 0:b87fd8dd4322 | 818 | |
la00noix | 0:b87fd8dd4322 | 819 | //calc_gyro(); |
yuki0701 | 1:26fc1b2f1c42 | 820 | // now_angle=gyro.getAngle(); |
yuki0701 | 1:26fc1b2f1c42 | 821 | now_angle = -gyro.getZ_Angle(); |
yuki0701 | 2:820dcd23c8e3 | 822 | if(tgt_angle - 1 < now_angle && now_angle < tgt_angle + 1) break; //目標角度からの許容誤差内に機体の角度が収まった時、補正終了 |
yuki0701 | 2:820dcd23c8e3 | 823 | else if(now_angle > tgt_angle + 1)out = 5 * (tgt_angle - now_angle + 1); |
yuki0701 | 2:820dcd23c8e3 | 824 | else if(tgt_angle - 1 > now_angle)out = 5 * (tgt_angle - now_angle - 1); |
la00noix | 0:b87fd8dd4322 | 825 | |
yuki0701 | 2:820dcd23c8e3 | 826 | printf("angle = %f out = %f\n\r",now_angle,out); |
la00noix | 0:b87fd8dd4322 | 827 | |
yuki0701 | 2:820dcd23c8e3 | 828 | if(out > 100) { //0~179°のときは時計回りに回転 |
yuki0701 | 1:26fc1b2f1c42 | 829 | // MaxonControl(-300,-300,-300,-300); |
yuki0701 | 2:820dcd23c8e3 | 830 | m1 = -100; |
yuki0701 | 2:820dcd23c8e3 | 831 | m2 = -100; |
yuki0701 | 2:820dcd23c8e3 | 832 | m3 = -100; |
yuki0701 | 2:820dcd23c8e3 | 833 | m4 = -100; |
yuki0701 | 1:26fc1b2f1c42 | 834 | |
yuki0701 | 2:820dcd23c8e3 | 835 | } else if(out < -100) { |
yuki0701 | 1:26fc1b2f1c42 | 836 | // MaxonControl(300,300,300,300); |
yuki0701 | 2:820dcd23c8e3 | 837 | m1 = 100; |
yuki0701 | 2:820dcd23c8e3 | 838 | m2 = 100; |
yuki0701 | 2:820dcd23c8e3 | 839 | m3 = 100; |
yuki0701 | 2:820dcd23c8e3 | 840 | m4 = 100; |
yuki0701 | 2:820dcd23c8e3 | 841 | } else if(out <= 100 && out > -100) { |
yuki0701 | 1:26fc1b2f1c42 | 842 | // MaxonControl(-out,-out,-out,-out); |
yuki0701 | 1:26fc1b2f1c42 | 843 | m1 = -out; |
yuki0701 | 1:26fc1b2f1c42 | 844 | m2 = -out; |
yuki0701 | 1:26fc1b2f1c42 | 845 | m3 = -out; |
yuki0701 | 1:26fc1b2f1c42 | 846 | m4 = -out; |
la00noix | 0:b87fd8dd4322 | 847 | } |
la00noix | 0:b87fd8dd4322 | 848 | |
yuki0701 | 2:820dcd23c8e3 | 849 | |
la00noix | 0:b87fd8dd4322 | 850 | } |
yuki0701 | 1:26fc1b2f1c42 | 851 | // MaxonControl(0,0,0,0); |
yuki0701 | 1:26fc1b2f1c42 | 852 | m1 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 853 | m2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 854 | m3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 855 | m4 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 856 | } |
yuki0701 | 1:26fc1b2f1c42 | 857 | |
yuki0701 | 1:26fc1b2f1c42 | 858 | void mt_stop() |
yuki0701 | 1:26fc1b2f1c42 | 859 | { |
yuki0701 | 1:26fc1b2f1c42 | 860 | m1 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 861 | m2 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 862 | m3 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 863 | m4 = 0; |
yuki0701 | 1:26fc1b2f1c42 | 864 | printf("motor stop\n\r"); |
la00noix | 0:b87fd8dd4322 | 865 | } |