harurobo_mbed_undercarriage_sub
PathFollowing.cpp@0:591749f315ac, 2018-11-16 (annotated)
- Committer:
- la00noix
- Date:
- Fri Nov 16 23:16:59 2018 +0000
- Revision:
- 0:591749f315ac
- Child:
- 1:d438093cb464
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
la00noix | 0:591749f315ac | 1 | #include <PathFollowing.h> |
la00noix | 0:591749f315ac | 2 | #include <mbed.h> |
la00noix | 0:591749f315ac | 3 | #include <math.h> |
la00noix | 0:591749f315ac | 4 | |
la00noix | 0:591749f315ac | 5 | double p_out,r_out; |
la00noix | 0:591749f315ac | 6 | double Kvq_p,Kvq_d,Kvr_p,Kvr_d; |
la00noix | 0:591749f315ac | 7 | double diff_old,diffangle,diffangle_old; |
la00noix | 0:591749f315ac | 8 | double out_dutyQ,out_dutyR; |
la00noix | 0:591749f315ac | 9 | double now_angle,target_angle; |
la00noix | 0:591749f315ac | 10 | double now_timeQ,old_timeQ,now_timeR,old_timeR; |
la00noix | 0:591749f315ac | 11 | double now_x, now_y; |
la00noix | 0:591749f315ac | 12 | |
la00noix | 0:591749f315ac | 13 | Timer timer; |
la00noix | 0:591749f315ac | 14 | |
la00noix | 0:591749f315ac | 15 | //初期座標:A, 目標座標:B、機体位置:C、点Cから直線ABに下ろした垂線の足:H |
la00noix | 0:591749f315ac | 16 | 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) //プログラム使用時、now_x,now_yはグローバル変数として定義する必要あり |
la00noix | 0:591749f315ac | 17 | { |
la00noix | 0:591749f315ac | 18 | double Vector_P[2] = {(plot_x2 - plot_x1), (plot_y2 - plot_y1)}; //ベクトルAB |
la00noix | 0:591749f315ac | 19 | double A_Vector_P = hypot(Vector_P[0], Vector_P[1]); //ベクトルABの大きさ(hypot(a,b)で√(a^2+b^2)を計算できる <math.h>)) |
la00noix | 0:591749f315ac | 20 | double UnitVector_P[2] = {Vector_P[0]/A_Vector_P, Vector_P[1]/A_Vector_P}; //ベクトルABの単位ベクトル |
la00noix | 0:591749f315ac | 21 | double UnitVector_Q[2] = {UnitVector_P[1], -UnitVector_P[0]}; //ベクトルCHの単位ベクトル |
la00noix | 0:591749f315ac | 22 | double Vector_R[2] = {(now_x - plot_x1), (now_y - plot_y1)}; //ベクトルAC |
la00noix | 0:591749f315ac | 23 | double diff = UnitVector_P[0]*Vector_R[1] - UnitVector_P[1]*Vector_R[0]; //機体位置と直線ABの距離(外積を用いて計算) |
la00noix | 0:591749f315ac | 24 | |
la00noix | 0:591749f315ac | 25 | double VectorOut_P[2] = {p_out*UnitVector_P[0], p_out*UnitVector_P[1]}; //ベクトルABに平行方向の出力をx軸方向、y軸方向の出力に分解 |
la00noix | 0:591749f315ac | 26 | |
la00noix | 0:591749f315ac | 27 | ///////////////////<XYRmotorout関数内>以下、ベクトルABに垂直な方向の誤差を埋めるPD制御(ベクトルABに垂直方向の出力を求め、x軸方向、y軸方向の出力に分解)////////////////////// |
la00noix | 0:591749f315ac | 28 | |
la00noix | 0:591749f315ac | 29 | timer.start(); |
la00noix | 0:591749f315ac | 30 | now_timeQ=timer.read(); |
la00noix | 0:591749f315ac | 31 | out_dutyQ=Kvq_p*diff+Kvq_d*(diff-diff_old)/(now_timeQ-old_timeQ); //ベクトルABに垂直方向の出力を決定 |
la00noix | 0:591749f315ac | 32 | diff_old=diff; |
la00noix | 0:591749f315ac | 33 | |
la00noix | 0:591749f315ac | 34 | if(out_dutyQ>0.1)out_dutyQ=0.1; |
la00noix | 0:591749f315ac | 35 | if(out_dutyQ<-0.1)out_dutyQ=-0.1; |
la00noix | 0:591749f315ac | 36 | |
la00noix | 0:591749f315ac | 37 | old_timeQ=now_timeQ; |
la00noix | 0:591749f315ac | 38 | |
la00noix | 0:591749f315ac | 39 | double VectorOut_Q[2] = {out_dutyQ*UnitVector_Q[0], out_dutyQ*UnitVector_Q[1]}; //ベクトルABに垂直方向の出力をx軸方向、y軸方向の出力に分解 |
la00noix | 0:591749f315ac | 40 | |
la00noix | 0:591749f315ac | 41 | ///////////////////////////////<XYRmotorout関数内>以下、機体角度と目標角度の誤差を埋めるPD制御(旋回のための出力値を決定)////////////////////////////////// |
la00noix | 0:591749f315ac | 42 | |
la00noix | 0:591749f315ac | 43 | now_timeR=timer.read(); |
la00noix | 0:591749f315ac | 44 | diffangle=target_angle-now_angle; |
la00noix | 0:591749f315ac | 45 | out_dutyR=Kvr_p*diff+Kvr_d*(diffangle-diffangle_old)/(now_timeR-old_timeR); |
la00noix | 0:591749f315ac | 46 | diffangle_old=diffangle; |
la00noix | 0:591749f315ac | 47 | |
la00noix | 0:591749f315ac | 48 | if(out_dutyR>r_out)out_dutyR=r_out; |
la00noix | 0:591749f315ac | 49 | if(out_dutyR<-r_out)out_dutyR=-r_out; |
la00noix | 0:591749f315ac | 50 | |
la00noix | 0:591749f315ac | 51 | old_timeR=now_timeR; |
la00noix | 0:591749f315ac | 52 | |
la00noix | 0:591749f315ac | 53 | //////////////////////////<XYRmotorout関数内>以下、x軸方向、y軸方向、旋回の出力をそれぞれad_x_out,ad_y_out,ad_r_outの指すアドレスに書き込む///////////////////////////// |
la00noix | 0:591749f315ac | 54 | ////////////////////////////////////////////その際、x軸方向、y軸方向の出力はフィールドの座標系から機体の座標系に変換する。/////////////////////////////////////////////// |
la00noix | 0:591749f315ac | 55 | |
la00noix | 0:591749f315ac | 56 | *ad_x_out = (VectorOut_P[0]+VectorOut_Q[0])*cos(now_angle)-(VectorOut_P[1]+VectorOut_Q[1])*sin(now_angle); |
la00noix | 0:591749f315ac | 57 | *ad_y_out = (VectorOut_P[0]+VectorOut_Q[0])*sin(now_angle)+(VectorOut_P[1]+VectorOut_Q[1])*cos(now_angle); |
la00noix | 0:591749f315ac | 58 | *ad_r_out = out_dutyR; |
la00noix | 0:591749f315ac | 59 | } |
la00noix | 0:591749f315ac | 60 | |
la00noix | 0:591749f315ac | 61 | ////////////////////////////////////////////////////////////<XYRmotorout関数は以上>//////////////////////////////////////////////////////////////// |
la00noix | 0:591749f315ac | 62 | |
la00noix | 0:591749f315ac | 63 | |
la00noix | 0:591749f315ac | 64 | void set_p_out(double p) //ベクトルABに平行方向の出力値設定関数 |
la00noix | 0:591749f315ac | 65 | { |
la00noix | 0:591749f315ac | 66 | p_out = p; |
la00noix | 0:591749f315ac | 67 | } |
la00noix | 0:591749f315ac | 68 | |
la00noix | 0:591749f315ac | 69 | void q_setPDparam(double q_p,double q_d) //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:591749f315ac | 70 | { |
la00noix | 0:591749f315ac | 71 | Kvq_p=q_p; |
la00noix | 0:591749f315ac | 72 | Kvq_d=q_d; |
la00noix | 0:591749f315ac | 73 | } |
la00noix | 0:591749f315ac | 74 | |
la00noix | 0:591749f315ac | 75 | void r_setPDparam(double r_p,double r_d) //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 |
la00noix | 0:591749f315ac | 76 | { |
la00noix | 0:591749f315ac | 77 | Kvr_p=r_p; |
la00noix | 0:591749f315ac | 78 | Kvr_d=r_d; |
la00noix | 0:591749f315ac | 79 | } |
la00noix | 0:591749f315ac | 80 | |
la00noix | 0:591749f315ac | 81 | void set_r_out(double r) //旋回時の最大出力値設定関数 |
la00noix | 0:591749f315ac | 82 | { |
la00noix | 0:591749f315ac | 83 | r_out = r; |
la00noix | 0:591749f315ac | 84 | } |
la00noix | 0:591749f315ac | 85 | |
la00noix | 0:591749f315ac | 86 | void set_target_angle(double t) //機体の目標角度設定関数 |
la00noix | 0:591749f315ac | 87 | { |
la00noix | 0:591749f315ac | 88 | target_angle = t; |
la00noix | 0:591749f315ac | 89 | } |