3/1

Dependencies:   mbed move4wheel2 EC CruizCore_R1370P

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers movement.cpp Source File

movement.cpp

00001 #include "EC.h"
00002 #include "R1370P.h"
00003 #include "move4wheel.h"
00004 #include "mbed.h"
00005 #include "math.h"
00006 #include "PathFollowing.h"
00007 #include "movement.h"
00008 #include "maxonsetting.h"
00009 #include "manual.h"
00010 #include "can.h"
00011 
00012 #define PI 3.141592
00013 
00014 char ashi_data[4]={0};
00015 
00016 Ec EC2(p26,p8,NC,500,0.05);
00017 Ec EC1(p22,p21,NC,500,0.05);
00018 Ticker ec_ticker;  //ec角速度計算用ticker
00019 
00020 R1370P gyro(p28,p27);
00021 
00022 double new_dist1=0,new_dist2=0;
00023 double old_dist1=0,old_dist2=0;
00024 double d_dist1=0,d_dist2=0;  //座標計算用関数
00025 double d_x,d_y;
00026 //現在地X,y座標、現在角度については、PathFollowingでnow_x,now_y,now_angleを定義済
00027 double start_x=0,start_y=0;  //スタート位置
00028 double x_out,y_out,r_out;  //出力値
00029 
00030 int16_t m1=0, m2=0, m3=0, m4=0;  //int16bit = int2byte
00031 
00032 double xy_type,pm_typeX,pm_typeY,x_base,y_base;
00033 
00034 ///////////////////機体情報をメンバとする構造体"robo_data"と構造体型変数info(←この変数に各センサーにより求めた機体情報(機体位置/機体角度)を格納する)の宣言/////////////////
00035 
00036 /*「info.(機体情報の種類).(使用センサーの種類)」に各情報を格納する
00037  *状況に応じて、どのセンサーにより算出した情報を信用するかを選択し、その都度now_angle,now_x,now_yに代入する。(何種類かのセンサーの情報を混ぜて使用することも可能)
00038  *(ex)
00039  *info.nowX.enc → エンコーダにより算出した機体位置のx座標
00040  *info.nowY.usw → 超音波センサーにより求めた機体位置のy座標
00041 */
00042 
00043 typedef struct{ //使用センサーの種類
00044     double usw;  //超音波センサー
00045     double enc;  //エンコーダ
00046     double gyro; //ジャイロ
00047     //double line;//ラインセンサー
00048 }robo_sensor;
00049 
00050 typedef struct{ //機体情報の種類
00051     robo_sensor angle; //←機体角度は超音波センサーやラインセンサーからも算出可能なので一応格納先を用意したが、ジャイロの値を完全に信用してもいいかも
00052     robo_sensor nowX;
00053     robo_sensor nowY;
00054 }robo_data;
00055 
00056 robo_data info={{0,0,0},{0,0,0},{0,0,0}}; //全てのデータを0に初期化
00057 
00058 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00059 
00060 void UserLoopSetting_sensor(){
00061     
00062     gyro.initialize();
00063     ec_ticker.attach(&calOmega,0.05);  //0.05秒間隔で角速度を計算
00064     EC1.setDiameter_mm(25.5);
00065     EC2.setDiameter_mm(25.5);  //測定輪半径//後で測定
00066     info.nowX.enc = -2962; //初期位置の設定
00067     info.nowY.enc = 3500;
00068 }
00069 
00070 void calOmega()  //角速度計算関数
00071 {
00072     EC1.CalOmega();
00073     EC2.CalOmega();
00074 }
00075 
00076 void output(double FL,double BL,double BR,double FR)
00077 {
00078     m1=FL;
00079     m2=BL;
00080     m3=BR;
00081     m4=FR;
00082 }
00083 
00084 void base(double FL,double BL,double BR,double FR,double Max)
00085 //いろんな加算をしても最大OR最小がMaxになるような補正//絶対値が一番でかいやつで除算
00086 //DCモーターならMax=1、マクソンは-4095~4095だからMax=4095にする
00087 {
00088     if(fabs(FL)>=Max||fabs(BL)>=Max||fabs(BR)>=Max||fabs(FR)>=Max) {
00089 
00090         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));
00091         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));
00092         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));
00093         else                                                               output(Max*FL/fabs(FR),Max*BL/fabs(FR),Max*BR/fabs(FR),Max*FR/fabs(FR));
00094       } else {
00095         output(FL,BL,BR,FR);
00096     }
00097 }
00098 
00099 void ashi_led(){
00100     
00101     if(now_angle > -1 && now_angle < 1){
00102         ashi_data[0] = 1;
00103     }else{
00104         ashi_data[0] = 0;
00105     }
00106     
00107     if(now_angle > 350){
00108         ashi_data[1] = 1;
00109     }else{
00110         ashi_data[1] = 0;
00111     }
00112     
00113     if(now_x > -1 && now_x < 1){
00114         
00115         ashi_data[2] = 1;
00116     }else{
00117         ashi_data[2] = 0;
00118     }
00119     
00120     if(now_y > -1 && now_y < 1){
00121         ashi_data[3] = 1;
00122     }else{
00123         ashi_data[3] = 0;
00124     }
00125 }
00126 
00127 void calc_gyro(){
00128     now_angle=gyro.getAngle();  //ジャイロの値読み込み
00129 }
00130 
00131 void calc_xy_enc()  //エンコーダ&ジャイロによる座標計算
00132 {
00133     now_angle=gyro.getAngle();  //ジャイロの値読み込み
00134 
00135     new_dist1=EC1.getDistance_mm();
00136     new_dist2=EC2.getDistance_mm();
00137     d_dist1=new_dist1-old_dist1;
00138     d_dist2=new_dist2-old_dist2;
00139     old_dist1=new_dist1;
00140     old_dist2=new_dist2;  //微小時間当たりのエンコーダ読み込み
00141 
00142     d_x=d_dist2*sin(now_angle*PI/180)-d_dist1*cos(now_angle*PI/180);
00143     d_y=d_dist2*cos(now_angle*PI/180)+d_dist1*sin(now_angle*PI/180);  //微小時間毎の座標変化
00144     info.nowX.enc = info.nowX.enc + d_x;
00145     info.nowY.enc = info.nowY.enc - d_y;  //微小時間毎に座標に加算
00146 }
00147 
00148 void set_cond(int t, int px, double bx, int py, double by){  //超音波センサーを使用するときの条件設定関数
00149 //引数の詳細は関数"calc_xy_usw"参照
00150   
00151    xy_type = t;
00152    
00153    pm_typeX = px;
00154    x_base = bx;
00155    
00156    pm_typeY = py;
00157    y_base = by;   
00158 }
00159 
00160 void calc_xy_usw(double tgt_angle){  //超音波センサーによる座標計算(機体が旋回する場合はこの方法による座標計算は出来ない)
00161 //tgt_angle:機体の目標角度(運動初期角度と同じ/今大会では0,90,180のみ)
00162 //xy_type:(0:Y軸平行の壁を読む/1:X軸平行の壁を読む/2:X,Y軸平行の壁を共に読む)
00163 //pm_typeX,pm_typeY:(0:各軸正方向側の壁を読む/1:各軸負方向側の壁を読む)
00164 //x_base,y_base:超音波センサーで読む壁の座標(y軸並行の壁のx座標/x軸平行の壁のy座標)
00165     
00166     double R1=240,R2=240,R3=240,R4=240; //機体の中心から各超音波センサーが付いている面までの距離
00167     double D1=30,D2=30,D3=30,D4=30; //各超音波センサーが付いている面の中心から各超音波センサーまでの距離
00168     
00169     now_angle=gyro.getAngle();  //ジャイロの値読み込み
00170     
00171     if(tgt_angle==0){
00172       if((xy_type==0 || xy_type==2) && pm_typeX==0){
00173         
00174         info.nowX.usw = x_base - (usw_data4 + R4*cos(now_angle*PI/180) + D4*sin(now_angle*PI/180));
00175            
00176       }else if((xy_type==0 || xy_type==2) && pm_typeX==1){
00177          
00178         info.nowX.usw = x_base + (usw_data3 + R3*cos(now_angle*PI/180) + D3*sin(now_angle*PI/180));
00179           
00180       }
00181       if((xy_type==1 || xy_type==2) && pm_typeY==0){
00182          
00183         info.nowY.usw = y_base - (usw_data2 + R2*cos(now_angle*PI/180) + D2*sin(now_angle*PI/180));
00184           
00185       }else if((xy_type==1 || xy_type==2) && pm_typeY==1){
00186          
00187         info.nowY.usw = y_base + (usw_data1 + R1*cos(now_angle*PI/180) + D1*sin(now_angle*PI/180));
00188           
00189       }
00190     
00191     }else if(tgt_angle==90){
00192       if((xy_type==0 || xy_type==2) && pm_typeX==0){
00193         
00194         info.nowX.usw = x_base - (usw_data1 + R1*cos(now_angle*PI/180) + D1*sin(now_angle*PI/180));
00195            
00196       }else if((xy_type==0 || xy_type==2) && pm_typeX==1){
00197          
00198         info.nowX.usw = x_base + (usw_data2 + R2*cos(now_angle*PI/180) + D2*sin(now_angle*PI/180));
00199           
00200       }
00201       if((xy_type==1 || xy_type==2) && pm_typeY==0){
00202          
00203         info.nowY.usw = y_base - (usw_data4 + R4*cos(now_angle*PI/180) + D4*sin(now_angle*PI/180));
00204           
00205       }else if((xy_type==1 || xy_type==2) && pm_typeY==1){
00206          
00207         info.nowY.usw = y_base + (usw_data3 + R3*cos(now_angle*PI/180) + D3*sin(now_angle*PI/180));
00208           
00209       }
00210         
00211     }else if(tgt_angle==180){    
00212        if((xy_type==0 || xy_type==2) && pm_typeX==0){
00213         
00214         info.nowX.usw = x_base - (usw_data3 + R3*cos(now_angle*PI/180) + D3*sin(now_angle*PI/180));
00215            
00216       }else if((xy_type==0 || xy_type==2) && pm_typeX==1){
00217          
00218         info.nowX.usw = x_base + (usw_data4 + R4*cos(now_angle*PI/180) + D4*sin(now_angle*PI/180));
00219           
00220       }
00221       if((xy_type==1 || xy_type==2) && pm_typeY==0){
00222          
00223         info.nowY.usw = y_base - (usw_data1+ R1*cos(now_angle*PI/180) + D1*sin(now_angle*PI/180));
00224           
00225       }else if((xy_type==1 || xy_type==2) && pm_typeY==1){
00226          
00227         info.nowY.usw = y_base + (usw_data2 + R2*cos(now_angle*PI/180) + D2*sin(now_angle*PI/180));
00228           
00229       }
00230     }
00231 }
00232 
00233 void calc_xy(double target_angle, double u,double v){
00234 //エンコーダにより求めた機体の座標と超音波センサーにより求めた機体の座標を(エンコーダ : 超音波 = u : 1-u / v : 1-v)の割合で混ぜて now_x,now_y に代入する
00235     
00236    calc_xy_enc();
00237    
00238    if(u != 1 || v != 1){
00239       calc_xy_usw(target_angle);  //エンコーダの値しか使用しない場合は超音波センサーによる座標計算は行わずに計算量を減らす。
00240    }
00241     
00242     now_x = u * info.nowX.enc + (1-u) * info.nowX.usw;
00243     now_y = v * info.nowY.enc + (1-v) * info.nowY.usw;
00244     
00245     /*if(now_x >-1 && now_x <1 && now_y >-1 && now_y <1){  //スタート時の0合わせ用
00246         ec_led = 1;
00247     }else{
00248         ec_led = 0;
00249     }
00250     
00251     if(now_angle >-0.5 && now_angle <0.5){
00252         gyro_led = 1;
00253     }else{
00254         gyro_led = 0;
00255     }*/
00256 }
00257 
00258 //ここからそれぞれのプログラム/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00259 //now_x(現在のx座標),now_y(現在のy座標),now_angle(機体角度(ラジアンではない)(0~360や-180~180とは限らない))(反時計回りが正)
00260 //ジャイロの出力は角度だが三角関数はラジアンとして計算する
00261 //通常の移動+座標のずれ補正+機体の角度補正(+必要に応じさらに別補正)
00262 //ジャイロの仕様上、角度補正をするときに計算式内で角度はそのままよりsinをとったほうがいいかもね
00263 
00264 void purecurve(int type,double u,double v,       //正面を変えずに円弧or楕円を描いて曲がる
00265                double point_x1,double point_y1,
00266                double point_x2,double point_y2,
00267                int theta,
00268                double speed,
00269                double q_p,double q_d,
00270                double r_p,double r_d,
00271                double r_out_max,
00272                double target_angle)
00273 //type:動きの種類(8パターン) point_x1,point_y1=出発地点の座標 point_x2,point_x2=目標地点の座標 theta=plotの間隔(0~90°) speed=速度
00274 {
00275     //-----PathFollowingのパラメーター設定-----//
00276     q_setPDparam(q_p,q_d);  //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数
00277     r_setPDparam(r_p,r_d);  //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数
00278     set_r_out(r_out_max);  //旋回時の最大出力値設定関数
00279     set_target_angle(target_angle);  //機体目標角度設定関数
00280 
00281     int s;
00282     int t = 0;
00283     double X,Y;//X=楕円の中心座標、Y=楕円の中心座標
00284     double a,b; //a=楕円のx軸方向の幅の半分,b=楕円のy軸方向の幅の半分
00285     double plotx[(90/theta)+1];  //楕円にとるplotのx座標
00286     double ploty[(90/theta)+1];
00287 
00288     double x_out,y_out,r_out;
00289 
00290     a=fabs(point_x1-point_x2);
00291     b=fabs(point_y1-point_y2);
00292 
00293     switch(type) {
00294 
00295         case 1://→↑移動
00296             X=point_x1;
00297             Y=point_y2;
00298 
00299             for(s=0; s<((90/theta)+1); s++) {
00300                 plotx[s] = X + a * cos(-PI/2 + s * (PI*theta/180));
00301                 ploty[s] = Y + b * sin(-PI/2 + s * (PI*theta/180));
00302                 //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]);
00303             }
00304             break;
00305 
00306         case 2://↑→移動
00307             X=point_x2;
00308             Y=point_y1;
00309 
00310             for(s=0; s<((90/theta)+1); s++) {
00311                 plotx[s] = X + a * cos(PI - s * (PI*theta/180));
00312                 ploty[s] = Y + b * sin(PI - s * (PI*theta/180));
00313                 //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]);
00314             }
00315             break;
00316 
00317         case 3://↑←移動
00318             X=point_x2;
00319             Y=point_y1;
00320 
00321             for(s=0; s<((90/theta)+1); s++) {
00322                 plotx[s] = X + a * cos(s * (PI*theta/180));
00323                 ploty[s] = Y + b * sin(s * (PI*theta/180));
00324                 //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]);
00325             }
00326             break;
00327 
00328         case 4://←↑移動
00329             X=point_x1;
00330             Y=point_y2;
00331 
00332             for(s=0; s<((90/theta)+1); s++) {
00333                 plotx[s] = X + a * cos(-PI/2 - s * (PI*theta/180));
00334                 ploty[s] = Y + b * sin(-PI/2 - s * (PI*theta/180));
00335                 //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]);
00336             }
00337             break;
00338 
00339         case 5://←↓移動
00340             X=point_x1;
00341             Y=point_y2;
00342 
00343             for(s=0; s<((90/theta)+1); s++) {
00344                 plotx[s] = X + a * cos(PI/2 + s * (PI*theta/180));
00345                 ploty[s] = Y + b * sin(PI/2 + s * (PI*theta/180));
00346                 //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]);
00347             }
00348             break;
00349 
00350         case 6://↓←移動
00351             X=point_x2;
00352             Y=point_y1;
00353 
00354             for(s=0; s<((90/theta)+1); s++) {
00355                 plotx[s] = X + a * cos(-s * (PI*theta/180));
00356                 ploty[s] = Y + b * sin(-s * (PI*theta/180));
00357                 //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]);
00358             }
00359             break;
00360 
00361         case 7://↓→移動
00362             X=point_x2;
00363             Y=point_y1;
00364 
00365             for(s=0; s<((90/theta)+1); s++) {
00366                 plotx[s] = X + a * cos(PI + s * (PI*theta/180));
00367                 ploty[s] = Y + b * sin(PI + s * (PI*theta/180));
00368                 //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]);
00369             }
00370             break;
00371 
00372         case 8://→↓移動
00373             X=point_x1;
00374             Y=point_y2;
00375 
00376             for(s=0; s<((90/theta)+1); s++) {
00377                 plotx[s] = X + a * cos(PI/2 - s * (PI*theta/180));
00378                 ploty[s] = Y + b * sin(PI/2 - s * (PI*theta/180));
00379                 //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]);
00380             }
00381             break;
00382     }
00383 
00384     while(1) {
00385 
00386         calc_xy(target_angle,u,v);
00387 
00388         XYRmotorout(plotx[t],ploty[t],plotx[t+1],ploty[t+1],&x_out,&y_out,&r_out,speed,speed);
00389         CalMotorOut(x_out,y_out,r_out);
00390         //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);
00391 
00392         base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),4095);  //m1~m4に代入
00393         //debug_printf("t=%d (0)=%f (1)=%f (2)=%f (3)=%f\n\r",t,GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3));
00394         
00395         if(((plotx[t+1] - now_x)*(plotx[t+1] - plotx[t]) + (ploty[t+1] - now_y)*(ploty[t+1] - ploty[t])) < 0)t++;
00396 
00397         MaxonControl(m1,m2,m3,m4);  //出力
00398         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);
00399         
00400         if(t == (90/theta))break;
00401         if(id1_value[0] != 1)break;
00402     }
00403 }
00404 
00405 void gogo_straight(double u,double v,                //直線運動プログラム
00406                    double x1_point,double y1_point,
00407                    double x2_point,double y2_point,
00408                    double speed1,double speed2,
00409                    double q_p,double q_d,
00410                    double r_p,double r_d,
00411                    double r_out_max,
00412                    double target_angle)
00413 //引数:出発地点の座標(x,y)、目標地点の座標(x,y)、初速度(speed1)、目標速度(speed2)//speed1=speed2 のとき等速運動
00414 {
00415     //-----PathFollowingのパラメーター設定-----//
00416     q_setPDparam(q_p,q_d);  //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数
00417     r_setPDparam(r_p,r_d);  //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数
00418     set_r_out(r_out_max);  //旋回時の最大出力値設定関数
00419     set_target_angle(target_angle);  //機体目標角度設定関数
00420     
00421     while (1) {
00422 
00423         calc_xy(target_angle,u,v);
00424 
00425         XYRmotorout(x1_point,y1_point,x2_point,y2_point,&x_out,&y_out,&r_out,speed1,speed2);
00426         //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);
00427 
00428         CalMotorOut(x_out,y_out,r_out);
00429         //printf("out1=%lf, out2=%lf, out3=%lf, out4=%lf\n",GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3));
00430 
00431         base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),4095);
00432         //printf("m1=%d, m2=%d, m3=%d, m4=%d\r\n",m_1,m_2,m_3,m_4);
00433         
00434         MaxonControl(m1,m2,m3,m4);
00435         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);
00436 
00437         if(((x2_point - now_x)*(x2_point - x1_point) + (y2_point - now_y)*(y2_point - y1_point)) < 0)break;
00438         if(id1_value[0] != 1)break; 
00439     }
00440 }
00441 
00442 void pos_correction(double tgt_x, double tgt_y, double tgt_angle, double u, double v){  //位置補正(使用前にMaxonControl(0,0,0,0)を入れる)
00443     
00444     double r, R=10;  // r:一回補正が入るごとの機体の位置と目標位置の距離(ズレ) R:補正終了とみなす目標位置からの機体の位置のズレ
00445     double out;
00446    
00447     calc_xy(tgt_angle, u, v);
00448  
00449     while(1){  //機体の位置を目標領域(目標座標+許容誤差)に収める
00450         gogo_straight(u,v,now_x,now_y,tgt_x,tgt_y,200,50,5,0.1,10,0.1,500,tgt_angle);
00451         MaxonControl(0,0,0,0);
00452     
00453         calc_xy(tgt_angle, u, v);
00454     
00455         r=hypot(now_x - tgt_x, now_y - tgt_y);
00456     
00457         if(r < R) break;
00458     
00459     }
00460    
00461     while(1){
00462     
00463         calc_gyro();
00464        
00465         out = 10 * (tgt_angle - now_angle);
00466  
00467         if(out > 300) {  //0~179°のときは時計回りに回転
00468             MaxonControl(300,300,300,300);
00469         }else if(out < -300){
00470             MaxonControl(-300,-300,-300,-300);   
00471         }else if(out <= 300 && out > -300) {
00472             MaxonControl(out,out,out,out);
00473         }
00474  
00475         if(tgt_angle - 0.5 < now_angle && now_angle < tgt_angle + 0.5) break;  //目標角度からの許容誤差内に機体の角度が収まった時、補正終了
00476         
00477     }
00478     MaxonControl(0,0,0,0);
00479 }