3/2

Dependencies:   mbed move4wheel2 EC CruizCore_R1370P

Committer:
yuki0701
Date:
Sat Mar 02 07:18:38 2019 +0000
Revision:
5:6cebe1c458a9
Parent:
4:317c53a674fa
a

Who changed what in which revision?

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