春ロボ1班(元F3RC4班+) / PathFollowing_2_26
Revision:
7:48af3a9c5bdd
Parent:
6:efe1bc381434
Child:
8:524d86b2073f
--- a/PathFollowing.cpp	Sat Dec 22 02:50:28 2018 +0000
+++ b/PathFollowing.cpp	Thu Jan 10 13:53:05 2019 +0000
@@ -1,4 +1,4 @@
-#include <PathFollowing.h>
+#include "PathFollowing.h"
 #include <mbed.h>
 #include <math.h>
 #include "EC.h"
@@ -40,8 +40,36 @@
 
 static int16_t m_1=0, m_2=0, m_3=0, m_4=0;  //int16bit = int2byte
 
+double xy_type,pm_typeX,pm_typeY,x_base,y_base;
 
 
+///////////////////機体情報をメンバとする構造体"robo_data"と構造体型変数info(←この変数に各センサーにより求めた機体情報(機体位置/機体角度)を格納する)の宣言/////////////////
+
+/*「info.(機体情報の種類).(使用センサーの種類)」に各情報を格納する
+ *状況に応じて、どのセンサーにより算出した情報を信用するかを選択し、その都度now_angle,now_x,now_yに代入する。(何種類かのセンサーの情報を混ぜて使用することも可能)
+ *(ex)
+ *info.nowX.enc → エンコーダにより算出した機体位置のx座標
+ *info.nowY.usw → 超音波センサーにより求めた機体位置のy座標
+*/
+
+typedef struct{ //使用センサーの種類
+    double usw;  //超音波センサー
+    double enc;  //エンコーダ
+    double gyro; //ジャイロ
+    //double line;//ラインセンサー
+}robo_sensor;
+
+typedef struct{ //機体情報の種類
+    robo_sensor angle; //←機体角度は超音波センサーやラインセンサーからも算出可能なので一応格納先を用意したが、ジャイロの値を完全に信用してもいいかも
+    robo_sensor nowX;
+    robo_sensor nowY;
+}robo_data;
+
+robo_data info={{0,0,0},{0,0,0},{0,0,0}}; //全てのデータを0に初期化
+
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
 
 void UserLoopSetting2(){
     
@@ -50,7 +78,6 @@
     EC1.setDiameter_mm(48);
     EC2.setDiameter_mm(48);  //測定輪半径//後で測定
     
-    
 }
 
 //初期座標:A, 目標座標:B、機体位置:C、点Cから直線ABに下ろした垂線の足:H
@@ -202,10 +229,10 @@
 
 void output(double FL,double BL,double BR,double FR)
 {
-    m1=FL;
-    m2=BL;
-    m3=BR;
-    m4=FR;
+    m_1=FL;
+    m_2=BL;
+    m_3=BR;
+    m_4=FR;
 }
 
 void base(double FL,double BL,double BR,double FR,double Max)
@@ -223,7 +250,7 @@
     }
 }
 
-void calc_xy()
+void calc_xy_enc() //エンコーダによる座標計算
 {
     now_angle=gyro.getAngle();  //ジャイロの値読み込み
 
@@ -236,8 +263,113 @@
 
     d_x=d_dist2*sin(now_angle*PI/180)-d_dist1*cos(now_angle*PI/180);
     d_y=d_dist2*cos(now_angle*PI/180)+d_dist1*sin(now_angle*PI/180);  //微小時間毎の座標変化
-    now_x=now_x+d_x;
-    now_y=now_y-d_y;  //微小時間毎に座標に加算
+    info.nowX.enc = info.nowX.enc + d_x;
+    info.nowY.enc = info.nowY.enc - d_y;  //微小時間毎に座標に加算
+}
+
+void set_cond(int t, int px, double bx, int py, double by){ //超音波センサーを使用するときの条件設定関数
+//引数の詳細は関数"calc_xy_usw"
+  
+   xy_type = t;
+   
+   pm_typeX = px;
+   x_base = bx;
+   
+   pm_typeY = py;
+   y_base = by;   
+       
+}
+
+
+void calc_xy_usw(double tgt_angle){ //超音波センサーによる座標計算(機体が旋回する場合はこの方法による座標計算は出来ない)
+//tgt_angle:機体の目標角度(運動初期角度と同じ/今大会では0,90,180のみ)
+//xy_type:(0:Y軸平行の壁を読む/1:X軸平行の壁を読む/2:X,Y軸平行の壁を共に読む)
+//pm_typeX,pm_typeY:(0:各軸正方向側の壁を読む/1:各軸負方向側の壁を読む)
+//x_base,y_base:超音波センサーで読む壁の座標(y軸並行の壁のx座標/x軸平行の壁のy座標)
+    
+    double R1=240,R2=240,R3=240,R4=240; //機体の中心から各超音波センサーが付いている面までの距離
+    double D1=30,D2=30,D3=30,D4=30; //各超音波センサーが付いている面の中心から各超音波センサーまでの距離
+    
+    now_angle=gyro.getAngle();
+    
+    if(tgt_angle==0){
+      if((xy_type==0 || xy_type==2) && pm_typeX==0){
+        
+        info.nowX.usw = x_base - (usw_data4 + R4*cos(now_angle*PI/180) + D4*sin(now_angle*PI/180));
+           
+      }else if((xy_type==0 || xy_type==2) && pm_typeX==1){
+         
+        info.nowX.usw = x_base + (usw_data3 + R3*cos(now_angle*PI/180) + D3*sin(now_angle*PI/180));
+          
+      }
+      if((xy_type==1 || xy_type==2) && pm_typeY==0){
+         
+        info.nowY.usw = y_base - (usw_data2 + R2*cos(now_angle*PI/180) + D2*sin(now_angle*PI/180));
+          
+      }else if((xy_type==1 || xy_type==2) && pm_typeY==1){
+         
+        info.nowY.usw = y_base + (usw_data1 + R1*cos(now_angle*PI/180) + D1*sin(now_angle*PI/180));
+          
+      }
+    
+    }else if(tgt_angle==90){
+      if((xy_type==0 || xy_type==2) && pm_typeX==0){
+        
+        info.nowX.usw = x_base - (usw_data1 + R1*cos(now_angle*PI/180) + D1*sin(now_angle*PI/180));
+           
+      }else if((xy_type==0 || xy_type==2) && pm_typeX==1){
+         
+        info.nowX.usw = x_base + (usw_data2 + R2*cos(now_angle*PI/180) + D2*sin(now_angle*PI/180));
+          
+      }
+      if((xy_type==1 || xy_type==2) && pm_typeY==0){
+         
+        info.nowY.usw = y_base - (usw_data4 + R4*cos(now_angle*PI/180) + D4*sin(now_angle*PI/180));
+          
+      }else if((xy_type==1 || xy_type==2) && pm_typeY==1){
+         
+        info.nowY.usw = y_base + (usw_data3 + R3*cos(now_angle*PI/180) + D3*sin(now_angle*PI/180));
+          
+      }
+        
+    }else if(tgt_angle==180){    
+       if((xy_type==0 || xy_type==2) && pm_typeX==0){
+        
+        info.nowX.usw = x_base - (usw_data3 + R3*cos(now_angle*PI/180) + D3*sin(now_angle*PI/180));
+           
+      }else if((xy_type==0 || xy_type==2) && pm_typeX==1){
+         
+        info.nowX.usw = x_base + (usw_data4 + R4*cos(now_angle*PI/180) + D4*sin(now_angle*PI/180));
+          
+      }
+      if((xy_type==1 || xy_type==2) && pm_typeY==0){
+         
+        info.nowY.usw = y_base - (usw_data1+ R1*cos(now_angle*PI/180) + D1*sin(now_angle*PI/180));
+          
+      }else if((xy_type==1 || xy_type==2) && pm_typeY==1){
+         
+        info.nowY.usw = y_base + (usw_data2 + R2*cos(now_angle*PI/180) + D2*sin(now_angle*PI/180));
+          
+      }
+    
+    }
+    
+}
+
+void calc_xy(double target_angle, double u,double v){ //エンコーダにより求めた機体の座標と超音波センサーにより求めた機体の座標を(エンコーダ : 超音波 = u : i-u / v : i-v)の割合で混ぜて now_x,now_y に代入する。
+    
+   calc_xy_enc();
+   
+   if(u != 1){
+      calc_xy_usw(target_angle); //エンコーダの値しか使用しない場合は超音波センサーによる座標計算は行わずに計算量を減らす。
+   }
+   if(v != 1){
+      calc_xy_usw(target_angle);
+    }
+    
+    now_x = u * info.nowX.enc + (1-u) * info.nowX.usw;
+    now_y = v * info.nowY.enc + (1-v) * info.nowY.usw;
+    
 }
 
 //ここからそれぞれのプログラム/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -246,7 +378,7 @@
 //通常の移動+座標のずれ補正+機体の角度補正(+必要に応じさらに別補正)
 //ジャイロの仕様上、角度補正をするときに計算式内で角度はそのままよりsinをとったほうがいいかもね
 
-void purecurve2(int type,                         //正面を変えずに円弧or楕円を描いて曲がる
+void purecurve2(int type,double u,double v,                         //正面を変えずに円弧or楕円を描いて曲がる
                 double point_x1,double point_y1,
                 double point_x2,double point_y2,
                 int theta,
@@ -368,7 +500,7 @@
 
     while(1) {
 
-        calc_xy();
+        calc_xy(target_angle,u,v);
 
         XYRmotorout(plotx[t],ploty[t],plotx[t+1],ploty[t+1],&x_out,&y_out,&r_out,speed,speed);
         CalMotorOut(x_out,y_out,r_out);
@@ -387,13 +519,14 @@
 }
 
 
-void gogo_straight(double x1_point,double y1_point,  //直線運動プログラム
+void gogo_straight(double u,double v,
+                   double x1_point,double y1_point,  //直線運動プログラム
                    double x2_point,double y2_point,
                    double speed1,double speed2,
                    double q_p,double q_d,
                    double r_p,double r_d,
                    double r_out_max,
-                   double target_angle)   
+                   double target_angle)
 //引数:出発地点の座標(x,y)、目標地点の座標(x,y)、初速度(speed1)、目標速度(speed2)//speed1=speed2 のとき等速運動
 {
     //-----PathFollowingのパラメーター設定-----//
@@ -404,7 +537,7 @@
     
     while (1) {
 
-        calc_xy();
+        calc_xy(target_angle,u,v);
 
         XYRmotorout(x1_point,y1_point,x2_point,y2_point,&x_out,&y_out,&r_out,speed1,speed2);
         //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);
@@ -415,7 +548,7 @@
         base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),4095);
         //printf("m1=%d, m2=%d, m3=%d, m4=%d\r\n",m_1,m_2,m_3,m_4);
         
-        MotorControl(m1,m2,m3,m4);
+        MotorControl(m_1,m_2,m_3,m_4);
         debug_printf("m1=%d m2=%d m3=%d m4=%d x=%f y=%f angle=%f\n\r",m_1,m_2,m_3,m_4,now_x,now_y,now_angle);
 
         if(((x2_point - now_x)*(x2_point - x1_point) + (y2_point - now_y)*(y2_point - y1_point)) < 0)break;