harurobo_mbed_undercarriage_sub
Revision 6:efe1bc381434, committed 2018-12-22
- Comitter:
- yuki0701
- Date:
- Sat Dec 22 02:50:28 2018 +0000
- Parent:
- 5:babe249ce482
- Commit message:
- a; ;
Changed in this revision
PathFollowing.cpp | Show annotated file Show diff for this revision Revisions of this file |
PathFollowing.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r babe249ce482 -r efe1bc381434 PathFollowing.cpp --- a/PathFollowing.cpp Sat Dec 15 13:22:52 2018 +0000 +++ b/PathFollowing.cpp Sat Dec 22 02:50:28 2018 +0000 @@ -1,6 +1,14 @@ #include <PathFollowing.h> #include <mbed.h> #include <math.h> +#include "EC.h" +#include "R1370P.h" +#include "move4wheel.h" +#include <stdarg.h> +#include "Maxon_setting.h" + +#define PI 3.141592 + double p_out,r_out_max; double Kvq_p,Kvq_d,Kvr_p,Kvr_d; @@ -10,10 +18,41 @@ double now_timeQ,old_timeQ,now_timeR,old_timeR; double now_x, now_y; double diff_st,diff_tgt,diff_st_tgt,p_param; +double usw_data1,usw_data2,usw_data3,usw_data4; +Ticker motor_tick; //角速度計算用ticker +//Ticker ticker; //for enc Timer timer; +Ec EC1(p8,p26,NC,500,0.05); +Ec EC2(p21,p22,NC,500,0.05); +R1370P gyro(p28,p27); + +double new_dist1=0,new_dist2=0; +double old_dist1=0,old_dist2=0; +double d_dist1=0,d_dist2=0; //座標計算用関数 +double d_x,d_y; +//現在地X,y座標、現在角度については、PathFollowingでnow_x,now_y,now_angleを定義済 +double start_x=0,start_y=0; //スタート位置 + +double x_out,y_out,r_out; //出力値 + +static int16_t m_1=0, m_2=0, m_3=0, m_4=0; //int16bit = int2byte + + + + +void UserLoopSetting2(){ + + gyro.initialize(); + // motor_tick.attach(&calOmega,0.05); //0.05秒間隔で角速度を計算 + EC1.setDiameter_mm(48); + EC2.setDiameter_mm(48); //測定輪半径//後で測定 + + +} + //初期座標:A, 目標座標:B、機体位置:C、点Cから直線ABに下ろした垂線の足:H 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はグローバル変数として定義する必要あり //plot_x1,plot_y1:出発地点の座標 @@ -149,3 +188,237 @@ { target_angle = t; } + + +void UserLoopSetting(); // initialize setting +void DAC_Write(int16_t data, DigitalOut* DAC_cs); +void MotorControl(int16_t val_md1, int16_t val_md2, int16_t val_md3, int16_t val_md4); + +void calOmega() //角速度計算関数 +{ + EC1.CalOmega(); + EC2.CalOmega(); +} + +void output(double FL,double BL,double BR,double FR) +{ + m1=FL; + m2=BL; + m3=BR; + m4=FR; +} + +void base(double FL,double BL,double BR,double FR,double Max) +//いろんな加算をしても最大OR最小がMaxになるような補正//絶対値が一番でかいやつで除算 +//DCモーターならMax=1、マクソンは-4095~4095だからMax=4095にする +{ + if(fabs(FL)>=Max||fabs(BL)>=Max||fabs(BR)>=Max||fabs(FR)>=Max) { + + 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)); + 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)); + 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)); + else output(Max*FL/fabs(FR),Max*BL/fabs(FR),Max*BR/fabs(FR),Max*FR/fabs(FR)); + } else { + output(FL,BL,BR,FR); + } +} + +void calc_xy() +{ + now_angle=gyro.getAngle(); //ジャイロの値読み込み + + new_dist1=EC1.getDistance_mm(); + new_dist2=EC2.getDistance_mm(); + d_dist1=new_dist1-old_dist1; + d_dist2=new_dist2-old_dist2; + old_dist1=new_dist1; + old_dist2=new_dist2; //微小時間当たりのエンコーダ読み込み + + 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; //微小時間毎に座標に加算 +} + +//ここからそれぞれのプログラム///////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//now_x(現在のx座標),now_y(現在のy座標),now_angle(機体角度(ラジアンではない)(0~360や-180~180とは限らない))(反時計回りが正) +//ジャイロの出力は角度だが三角関数はラジアンとして計算する +//通常の移動+座標のずれ補正+機体の角度補正(+必要に応じさらに別補正) +//ジャイロの仕様上、角度補正をするときに計算式内で角度はそのままよりsinをとったほうがいいかもね + +void purecurve2(int type, //正面を変えずに円弧or楕円を描いて曲がる + double point_x1,double point_y1, + double point_x2,double point_y2, + int theta, + double speed, + double q_p,double q_d, + double r_p,double r_d, + double r_out_max, + double target_angle) +//type:動きの種類(8パターン) point_x1,point_y1=出発地点の座標 point_x2,point_x2=目標地点の座標 theta=plotの間隔(0~90°) speed=速度 +{ + //-----PathFollowingのパラメーター設定-----// + q_setPDparam(q_p,q_d); //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 + r_setPDparam(r_p,r_d); //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 + set_r_out(r_out_max); //旋回時の最大出力値設定関数 + set_target_angle(target_angle); //機体目標角度設定関数 + + int s; + int t = 0; + double X,Y;//X=楕円の中心座標、Y=楕円の中心座標 + double a,b; //a=楕円のx軸方向の幅の半分,b=楕円のy軸方向の幅の半分 + double plotx[(90/theta)+1]; //楕円にとるplotのx座標 + double ploty[(90/theta)+1]; + + double x_out,y_out,r_out; + + a=fabs(point_x1-point_x2); + b=fabs(point_y1-point_y2); + + switch(type) { + + case 1://→↑移動 + X=point_x1; + Y=point_y2; + + for(s=0; s<((90/theta)+1); s++) { + plotx[s] = X + a * cos(-PI/2 + s * (PI*theta/180)); + ploty[s] = Y + b * sin(-PI/2 + s * (PI*theta/180)); + //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); + } + break; + + case 2://↑→移動 + X=point_x2; + Y=point_y1; + + for(s=0; s<((90/theta)+1); s++) { + plotx[s] = X + a * cos(PI - s * (PI*theta/180)); + ploty[s] = Y + b * sin(PI - s * (PI*theta/180)); + //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); + } + break; + + case 3://↑←移動 + X=point_x2; + Y=point_y1; + + for(s=0; s<((90/theta)+1); s++) { + plotx[s] = X + a * cos(s * (PI*theta/180)); + ploty[s] = Y + b * sin(s * (PI*theta/180)); + //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); + } + break; + + case 4://←↑移動 + X=point_x1; + Y=point_y2; + + for(s=0; s<((90/theta)+1); s++) { + plotx[s] = X + a * cos(-PI/2 - s * (PI*theta/180)); + ploty[s] = Y + b * sin(-PI/2 - s * (PI*theta/180)); + //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); + } + break; + + case 5://←↓移動 + X=point_x1; + Y=point_y2; + + for(s=0; s<((90/theta)+1); s++) { + plotx[s] = X + a * cos(PI/2 + s * (PI*theta/180)); + ploty[s] = Y + b * sin(PI/2 + s * (PI*theta/180)); + //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); + } + break; + + case 6://↓←移動 + X=point_x2; + Y=point_y1; + + for(s=0; s<((90/theta)+1); s++) { + plotx[s] = X + a * cos(-s * (PI*theta/180)); + ploty[s] = Y + b * sin(-s * (PI*theta/180)); + //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); + } + break; + + case 7://↓→移動 + X=point_x2; + Y=point_y1; + + for(s=0; s<((90/theta)+1); s++) { + plotx[s] = X + a * cos(PI + s * (PI*theta/180)); + ploty[s] = Y + b * sin(PI + s * (PI*theta/180)); + //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); + } + break; + + case 8://→↓移動 + X=point_x1; + Y=point_y2; + + for(s=0; s<((90/theta)+1); s++) { + plotx[s] = X + a * cos(PI/2 - s * (PI*theta/180)); + ploty[s] = Y + b * sin(PI/2 - s * (PI*theta/180)); + //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); + } + break; + } + + while(1) { + + calc_xy(); + + 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); + //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); + + base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),4095); //m1~m4に代入 + //debug_printf("t=%d (0)=%f (1)=%f (2)=%f (3)=%f\n\r",t,GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3)); + + if(((plotx[t+1] - now_x)*(plotx[t+1] - plotx[t]) + (ploty[t+1] - now_y)*(ploty[t+1] - ploty[t])) < 0)t++; + + MotorControl(m_1,m_2,m_3,m_4); //出力 + debug_printf("t=%d m1=%d m2=%d m3=%d m4=%d x=%f y=%f angle=%f\n\r",t,m_1,m_2,m_3,m_4,now_x,now_y,now_angle); + + if(t == (90/theta))break; + } +} + + +void gogo_straight(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) +//引数:出発地点の座標(x,y)、目標地点の座標(x,y)、初速度(speed1)、目標速度(speed2)//speed1=speed2 のとき等速運動 +{ + //-----PathFollowingのパラメーター設定-----// + q_setPDparam(q_p,q_d); //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 + r_setPDparam(r_p,r_d); //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 + set_r_out(r_out_max); //旋回時の最大出力値設定関数 + set_target_angle(target_angle); //機体目標角度設定関数 + + while (1) { + + calc_xy(); + + 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); + + CalMotorOut(x_out,y_out,r_out); + //printf("out1=%lf, out2=%lf, out3=%lf, out4=%lf\n",GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3)); + + 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); + 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; + } +} +
diff -r babe249ce482 -r efe1bc381434 PathFollowing.h --- a/PathFollowing.h Sat Dec 15 13:22:52 2018 +0000 +++ b/PathFollowing.h Sat Dec 22 02:50:28 2018 +0000 @@ -2,10 +2,14 @@ #define HARUROBO2019_PATHFOLLOWING_H #include <EC.h> + extern double now_x,now_y,now_angle; //main.cppにこれらの値の読み込みを書くこと extern double now_timeQ,now_timeR; +extern double usw_data1,usw_data2,usw_data3,usw_data4; +void UserLoopSetting2(); + 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); //出発地点、目標地点の座標から機体のx軸方向、y軸方向、旋回の出力を算出する関数(エンコーダ/ジャイロ使用) /* @@ -31,6 +35,39 @@ void set_target_angle(double t); //機体目標角度設定関数 +void calOmega(); + +void output(double FL,double BL,double BR,double FR); + +void base(double FL,double BL,double BR,double FR,double Max); + +void calc_xy_enc(); + +void set_cond(int t, int px, double bx, int py, double by); + +void calc_xy_usw(double tgt_angle); + +void calc_xy(double tgt_angle, double u, double v); + +void purecurve2(int type,double u, double v, //正面を変えずに円弧or楕円を描いて曲がる + double point_x1,double point_y1, + double point_x2,double point_y2, + int theta, + double speed, + double q_p,double q_d, + double r_p,double r_d, + double r_out_max, + double target_angle); + +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); + + #endif \ No newline at end of file