2月25日

Dependencies:   uw_28015 mbed ros_lib_kinetic move4wheel2 EC CruizCore_R6093U CruizCore_R1370P

Committer:
yuki0701
Date:
Tue Feb 25 01:20:43 2020 +0000
Revision:
0:44f9a43e4ab2
a;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yuki0701 0:44f9a43e4ab2 1 #include "PathFollowing.h"
yuki0701 0:44f9a43e4ab2 2 #include "mbed.h"
yuki0701 0:44f9a43e4ab2 3 #include "math.h"
yuki0701 0:44f9a43e4ab2 4
yuki0701 0:44f9a43e4ab2 5 double p_out,r_out_max, q_out_max;
yuki0701 0:44f9a43e4ab2 6 double Kvq_p,Kvq_d,Kvr_p,Kvr_d;
yuki0701 0:44f9a43e4ab2 7 double diff_old,diffangle,diffangle_old;
yuki0701 0:44f9a43e4ab2 8 double out_dutyQ,out_dutyR;
yuki0701 0:44f9a43e4ab2 9 double now_angle,target_angle,old_angle,adj_angle;
yuki0701 0:44f9a43e4ab2 10 double now_timeQ,old_timeQ,now_timeR,old_timeR;
yuki0701 0:44f9a43e4ab2 11 double now_x, now_y;
yuki0701 0:44f9a43e4ab2 12 double diff_st,diff_tgt,diff_st_tgt,p_param;
yuki0701 0:44f9a43e4ab2 13 double usw_data1,usw_data2,usw_data3,usw_data4;
yuki0701 0:44f9a43e4ab2 14
yuki0701 0:44f9a43e4ab2 15 Timer timer;
yuki0701 0:44f9a43e4ab2 16
yuki0701 0:44f9a43e4ab2 17 //初期座標:A, 目標座標:B、機体位置:C、点Cから直線ABに下ろした垂線の足:H
yuki0701 0:44f9a43e4ab2 18 void XYRmotorout(double plot_x1, double plot_y1, double plot_x2, double plot_y2, double *ad_x_out, double *ad_y_out, double *ad_r_out, double speed1, double speed2 ) //プログラム使用時、now_x,now_yはグローバル変数として定義する必要あり
yuki0701 0:44f9a43e4ab2 19 //plot_x1,plot_y1:出発地点の座標
yuki0701 0:44f9a43e4ab2 20 //plot_x2,plot_y2:目標地点の座標
yuki0701 0:44f9a43e4ab2 21 //speed1:初期速度
yuki0701 0:44f9a43e4ab2 22 //speed2:目標速度
yuki0701 0:44f9a43e4ab2 23 {
yuki0701 0:44f9a43e4ab2 24 double Vector_P[2] = {(plot_x2 - plot_x1), (plot_y2 - plot_y1)}; //ベクトルAB
yuki0701 0:44f9a43e4ab2 25 double A_Vector_P = hypot(Vector_P[0], Vector_P[1]); //ベクトルABの大きさ(hypot(a,b)で√(a^2+b^2)を計算できる <math.h>))
yuki0701 0:44f9a43e4ab2 26 double UnitVector_P[2] = {Vector_P[0]/A_Vector_P, Vector_P[1]/A_Vector_P}; //ベクトルABの単位ベクトル
yuki0701 0:44f9a43e4ab2 27 double UnitVector_Q[2] = {UnitVector_P[1], -UnitVector_P[0]}; //ベクトルCHの単位ベクトル
yuki0701 0:44f9a43e4ab2 28 double Vector_R[2] = {(now_x - plot_x1), (now_y - plot_y1)}; //ベクトルAC
yuki0701 0:44f9a43e4ab2 29 double diff = UnitVector_P[0]*Vector_R[1] - UnitVector_P[1]*Vector_R[0]; //機体位置と直線ABの距離(外積を用いて計算)
yuki0701 0:44f9a43e4ab2 30
yuki0701 0:44f9a43e4ab2 31 //double VectorOut_P[2]= {0}; //ベクトルABに平行方向の出力をx軸方向、y軸方向の出力に分解
yuki0701 0:44f9a43e4ab2 32
yuki0701 0:44f9a43e4ab2 33 ///////////////////<XYRmotorout関数内>以下、ベクトルABに垂直な方向の誤差を埋めるPD制御(ベクトルABに垂直方向の出力を求め、x軸方向、y軸方向の出力に分解)//////////////////////
yuki0701 0:44f9a43e4ab2 34
yuki0701 0:44f9a43e4ab2 35 timer.start();
yuki0701 0:44f9a43e4ab2 36 now_timeQ=timer.read();
yuki0701 0:44f9a43e4ab2 37 out_dutyQ=Kvq_p*diff+Kvq_d*(diff-diff_old)/(now_timeQ-old_timeQ); //ベクトルABに垂直方向の出力を決定
yuki0701 0:44f9a43e4ab2 38 diff_old=diff;
yuki0701 0:44f9a43e4ab2 39
yuki0701 0:44f9a43e4ab2 40 if(out_dutyQ>q_out_max)out_dutyQ=q_out_max;
yuki0701 0:44f9a43e4ab2 41 if(out_dutyQ<-q_out_max)out_dutyQ=-q_out_max;
yuki0701 0:44f9a43e4ab2 42
yuki0701 0:44f9a43e4ab2 43 old_timeQ=now_timeQ;
yuki0701 0:44f9a43e4ab2 44
yuki0701 0:44f9a43e4ab2 45 double VectorOut_Q[2] = {out_dutyQ*UnitVector_Q[0], out_dutyQ*UnitVector_Q[1]}; //ベクトルABに垂直方向の出力をx軸方向、y軸方向の出力に分解
yuki0701 0:44f9a43e4ab2 46
yuki0701 0:44f9a43e4ab2 47 ///////////////////////////////<XYRmotorout関数内>以下、機体角度と目標角度の誤差を埋めるPD制御(旋回のための出力値を決定)//////////////////////////////////
yuki0701 0:44f9a43e4ab2 48
yuki0701 0:44f9a43e4ab2 49 now_timeR=timer.read();
yuki0701 0:44f9a43e4ab2 50 diffangle=target_angle-now_angle;
yuki0701 0:44f9a43e4ab2 51 out_dutyR=-(Kvr_p*diffangle+Kvr_d*(diffangle-diffangle_old)/(now_timeR-old_timeR));
yuki0701 0:44f9a43e4ab2 52 diffangle_old=diffangle;
yuki0701 0:44f9a43e4ab2 53
yuki0701 0:44f9a43e4ab2 54 if(out_dutyR>r_out_max)out_dutyR=r_out_max;
yuki0701 0:44f9a43e4ab2 55 if(out_dutyR<-r_out_max)out_dutyR=-r_out_max;
yuki0701 0:44f9a43e4ab2 56
yuki0701 0:44f9a43e4ab2 57 old_timeR=now_timeR;
yuki0701 0:44f9a43e4ab2 58
yuki0701 0:44f9a43e4ab2 59 //////////////////////////<XYRmotorout関数内>以下、x軸方向、y軸方向、旋回の出力をそれぞれad_x_out,ad_y_out,ad_r_outの指すアドレスに書き込む/////////////////////////////
yuki0701 0:44f9a43e4ab2 60 ////////////////////////////////////////////その際、x軸方向、y軸方向の出力はフィールドの座標系から機体の座標系に変換する。///////////////////////////////////////////////
yuki0701 0:44f9a43e4ab2 61
yuki0701 0:44f9a43e4ab2 62 diff_st = hypot(now_x-plot_x1,now_y-plot_y1); //出発座標と機体の位置の距離
yuki0701 0:44f9a43e4ab2 63 diff_tgt = hypot(now_x - plot_x2, now_y - plot_y2); //機体の位置と目標座標の距離
yuki0701 0:44f9a43e4ab2 64 diff_st_tgt = hypot(plot_x1-plot_x2,plot_y1-plot_y2); //出発座標と目標座標の距離
yuki0701 0:44f9a43e4ab2 65
yuki0701 0:44f9a43e4ab2 66 if(speed1 == speed2) { //等速移動
yuki0701 0:44f9a43e4ab2 67
yuki0701 0:44f9a43e4ab2 68 double VectorOut_P[2] = {speed1*UnitVector_P[0], speed1*UnitVector_P[1]};
yuki0701 0:44f9a43e4ab2 69
yuki0701 0:44f9a43e4ab2 70 *ad_x_out = (VectorOut_P[0]+VectorOut_Q[0])*cos(-now_angle*3.141592/180)-(VectorOut_P[1]+VectorOut_Q[1])*sin(-now_angle*3.141592/180);
yuki0701 0:44f9a43e4ab2 71 *ad_y_out = (VectorOut_P[0]+VectorOut_Q[0])*sin(-now_angle*3.141592/180)+(VectorOut_P[1]+VectorOut_Q[1])*cos(-now_angle*3.141592/180);
yuki0701 0:44f9a43e4ab2 72 *ad_r_out = out_dutyR;
yuki0701 0:44f9a43e4ab2 73
yuki0701 0:44f9a43e4ab2 74 } else if(speed2 == 0) { //減速移動(目標速度が0)→ベクトルABに垂直な方向の出力にもP制御をかける。
yuki0701 0:44f9a43e4ab2 75
yuki0701 0:44f9a43e4ab2 76 double VectorOut_P[2] = {speed1*UnitVector_P[0], speed1*UnitVector_P[1]};
yuki0701 0:44f9a43e4ab2 77
yuki0701 0:44f9a43e4ab2 78 if(diff_tgt > diff_st_tgt) {
yuki0701 0:44f9a43e4ab2 79 diff_tgt = diff_st_tgt;
yuki0701 0:44f9a43e4ab2 80 }
yuki0701 0:44f9a43e4ab2 81
yuki0701 0:44f9a43e4ab2 82 p_param=(diff_tgt/diff_st_tgt);
yuki0701 0:44f9a43e4ab2 83
yuki0701 0:44f9a43e4ab2 84 *ad_x_out = p_param*((VectorOut_P[0]+VectorOut_Q[0])*cos(-now_angle*3.141592/180)-(VectorOut_P[1]+VectorOut_Q[1])*sin(-now_angle*3.141592/180));
yuki0701 0:44f9a43e4ab2 85 *ad_y_out = p_param*((VectorOut_P[0]+VectorOut_Q[0])*sin(-now_angle*3.141592/180)+(VectorOut_P[1]+VectorOut_Q[1])*cos(-now_angle*3.141592/180));
yuki0701 0:44f9a43e4ab2 86 *ad_r_out = out_dutyR;
yuki0701 0:44f9a43e4ab2 87
yuki0701 0:44f9a43e4ab2 88 } else if(speed1 > speed2) { //減速移動(目標速度が0でない)
yuki0701 0:44f9a43e4ab2 89
yuki0701 0:44f9a43e4ab2 90 if(diff_tgt > diff_st_tgt) {
yuki0701 0:44f9a43e4ab2 91 diff_tgt = diff_st_tgt;
yuki0701 0:44f9a43e4ab2 92 }
yuki0701 0:44f9a43e4ab2 93
yuki0701 0:44f9a43e4ab2 94 p_param=(diff_tgt/diff_st_tgt);
yuki0701 0:44f9a43e4ab2 95
yuki0701 0:44f9a43e4ab2 96 double speed3 = speed2 + (speed1-speed2)*p_param;
yuki0701 0:44f9a43e4ab2 97
yuki0701 0:44f9a43e4ab2 98 double VectorOut_P[2] = {speed3*UnitVector_P[0], speed3*UnitVector_P[1]};
yuki0701 0:44f9a43e4ab2 99
yuki0701 0:44f9a43e4ab2 100 *ad_x_out = (VectorOut_P[0]+VectorOut_Q[0])*cos(-now_angle*3.141592/180)-(VectorOut_P[1]+VectorOut_Q[1])*sin(-now_angle*3.141592/180);
yuki0701 0:44f9a43e4ab2 101 *ad_y_out = (VectorOut_P[0]+VectorOut_Q[0])*sin(-now_angle*3.141592/180)+(VectorOut_P[1]+VectorOut_Q[1])*cos(-now_angle*3.141592/180);
yuki0701 0:44f9a43e4ab2 102 *ad_r_out = out_dutyR;
yuki0701 0:44f9a43e4ab2 103
yuki0701 0:44f9a43e4ab2 104 } else if(speed1 < speed2) { //加速移動(speed1)
yuki0701 0:44f9a43e4ab2 105
yuki0701 0:44f9a43e4ab2 106 if(diff_st > diff_st_tgt) {
yuki0701 0:44f9a43e4ab2 107 diff_st = diff_st_tgt;
yuki0701 0:44f9a43e4ab2 108 }
yuki0701 0:44f9a43e4ab2 109
yuki0701 0:44f9a43e4ab2 110 p_param=(diff_st/diff_st_tgt);
yuki0701 0:44f9a43e4ab2 111
yuki0701 0:44f9a43e4ab2 112 double speed4 = speed1 + (speed2-speed1)*p_param;
yuki0701 0:44f9a43e4ab2 113
yuki0701 0:44f9a43e4ab2 114 double VectorOut_P[2] = {speed4*UnitVector_P[0], speed4*UnitVector_P[1]};
yuki0701 0:44f9a43e4ab2 115
yuki0701 0:44f9a43e4ab2 116 *ad_x_out = (VectorOut_P[0]+VectorOut_Q[0])*cos(-now_angle*3.141592/180)-(VectorOut_P[1]+VectorOut_Q[1])*sin(-now_angle*3.141592/180);
yuki0701 0:44f9a43e4ab2 117 *ad_y_out = (VectorOut_P[0]+VectorOut_Q[0])*sin(-now_angle*3.141592/180)+(VectorOut_P[1]+VectorOut_Q[1])*cos(-now_angle*3.141592/180);
yuki0701 0:44f9a43e4ab2 118 *ad_r_out = out_dutyR;
yuki0701 0:44f9a43e4ab2 119 }
yuki0701 0:44f9a43e4ab2 120 }
yuki0701 0:44f9a43e4ab2 121
yuki0701 0:44f9a43e4ab2 122 ////////////////////////////////////////////////////////////<XYRmotorout関数は以上>////////////////////////////////////////////////////////////////
yuki0701 0:44f9a43e4ab2 123
yuki0701 0:44f9a43e4ab2 124
yuki0701 0:44f9a43e4ab2 125 /*void set_p_out(double p) //ベクトルABに平行方向の出力値設定関数
yuki0701 0:44f9a43e4ab2 126 {
yuki0701 0:44f9a43e4ab2 127 p_out = p;
yuki0701 0:44f9a43e4ab2 128 }*/
yuki0701 0:44f9a43e4ab2 129
yuki0701 0:44f9a43e4ab2 130 void q_setPDparam(double q_p,double q_d) //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数
yuki0701 0:44f9a43e4ab2 131 {
yuki0701 0:44f9a43e4ab2 132 Kvq_p=q_p;
yuki0701 0:44f9a43e4ab2 133 Kvq_d=q_d;
yuki0701 0:44f9a43e4ab2 134 }
yuki0701 0:44f9a43e4ab2 135
yuki0701 0:44f9a43e4ab2 136 void r_setPDparam(double r_p,double r_d) //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数
yuki0701 0:44f9a43e4ab2 137 {
yuki0701 0:44f9a43e4ab2 138 Kvr_p=r_p;
yuki0701 0:44f9a43e4ab2 139 Kvr_d=r_d;
yuki0701 0:44f9a43e4ab2 140 }
yuki0701 0:44f9a43e4ab2 141
yuki0701 0:44f9a43e4ab2 142 void set_r_out(double r) //旋回時の最大出力値設定関数
yuki0701 0:44f9a43e4ab2 143 {
yuki0701 0:44f9a43e4ab2 144 r_out_max = r;
yuki0701 0:44f9a43e4ab2 145 }
yuki0701 0:44f9a43e4ab2 146
yuki0701 0:44f9a43e4ab2 147 void set_q_out(double q){
yuki0701 0:44f9a43e4ab2 148 q_out_max = q;
yuki0701 0:44f9a43e4ab2 149 }
yuki0701 0:44f9a43e4ab2 150
yuki0701 0:44f9a43e4ab2 151 void set_target_angle(double t) //機体の目標角度設定関数
yuki0701 0:44f9a43e4ab2 152 {
yuki0701 0:44f9a43e4ab2 153 target_angle = t;
yuki0701 0:44f9a43e4ab2 154 }