Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: CruizCore_R1370P
Fork of PathFollowing1 by
PathFollowing1.cpp
- Committer:
- yuki0701
- Date:
- 2018-10-31
- Revision:
- 0:3d9101dd0061
- Child:
- 1:9858517eef98
File content as of revision 0:3d9101dd0061:
#include <PathFollowing1.h>
#include <mbed.h>
#include <math.h>
namespace{
R1370P gyro(PC_6,PC_7);
}
//初期座標:A, 目標座標:B、機体位置:C、点Cから直線ABに下ろした垂線の足:H
void pathfollowing1::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はグローバル変数として定義する必要あり
double Vector_P[2] = {(plot_x2 - plot_x1), (plot_y2 - plot_y1)}; //ベクトルAB
double A_Vector_P = hypot(Vector_P[0], Vector_P[1]); //ベクトルABの大きさ(hypot(a,b)で√(a^2+b^2)を計算できる <math.h>))
double UnitVector_P[2] = {Vector_P[0]/A_Vector_P, Vector_P[1]/A_Vector_P}; //ベクトルABの単位ベクトル
double UnitVector_Q[2] = {UnitVector_P[1], -UnitVector_P[0]}; //ベクトルCHの単位ベクトル
double Vector_R[2] = {(now_x - plot_x1), (now_y - plot_y1)}; //ベクトルAC
double diff = UnitVector_P[0]*Vector_R[1] - UnitVector_P[1]*Vector_R[0]; //機体位置と直線ABの距離(外積を用いて計算)
double VectorOut_P[2] = {p_out*UnitVector_P[0], p_out*UnitVector_P[1]}; //ベクトルABに平行方向の出力をx軸方向、y軸方向の出力に分解
///////////////////<XYRmotorout関数内>以下、ベクトルABに垂直な方向の誤差を埋めるPD制御(ベクトルABに垂直方向の出力を求め、x軸方向、y軸方向の出力に分解)//////////////////////
now_timeQ=timer.read();
out_dutyQ=Kvq_p*diff+Kvq_d*(diff-diff_old)/(now_timeQ-old_timeQ); //ベクトルABに垂直方向の出力を決定
diff_old=diff;
if(out_dutyQ>0.1)out_dutyQ=0.1;
if(out_dutyQ<-0.1)out_dutyQ=-0.1;
old_timeQ=now_timeQ;
double VectorOut_Q[2] = {out_dutyQ*UnitVector_Q[0], out_dutyQ*UnitVector_Q[1]}; //ベクトルABに垂直方向の出力をx軸方向、y軸方向の出力に分解
///////////////////////////////<XYRmotorout関数内>以下、機体角度と目標角度の誤差を埋めるPD制御(旋回のための出力値を決定)//////////////////////////////////
now_angle=gyro.getAngle();
now_timeR=timer.read();
diffangle=target_angle-now_angle;
out_dutyR=Kvr_p*diff+Kvr_d*(diffangle-diffangle_old)/(now_timeR-old_timeR);
diffangle_old=diffangle;
if(out_dutyR>r_out)out_dutyR=r_out;
if(out_dutyR<-r_out)out_dutyR=-r_out;
old_timeR=now_timeR;
//////////////////////////<XYRmotorout関数内>以下、x軸方向、y軸方向、旋回の出力をそれぞれad_x_out,ad_y_out,ad_r_outの指すアドレスに書き込む/////////////////////////////
*ad_x_out = VectorOut_P[0]+VectorOut_Q[0];
*ad_y_out = VectorOut_P[1]+VectorOut_Q[1];
*ad_r_out = out_dutyR;
}
////////////////////////////////////////////////////////////<XYRmotorout関数は以上>////////////////////////////////////////////////////////////////
void pathfollowing1::set_p_out(double p){ //ベクトルABに平行方向の出力値設定関数
p_out = p;
}
void pathfollowing1::q_setPDparam(double q_p,double q_d){ //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数
Kvq_p=q_p;
Kvq_d=q_d;
}
void pathfollowing1::r_setPDparam(double r_p,double r_d){ //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数
Kvr_p=r_p;
Kvr_d=r_d;
}
void pathfollowing1::set_r_out(double r){ //旋回時の最大出力値設定関数
r_out = r;
}
void pathfollowing1::set_target_angle(double t){ //機体の目標角度設定関数
target_angle = t;
}
