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: mbed EC PathFollowing-ver10 CruizCore_R1370P
main.cpp@3:e696a6dd4254, 2018-11-24 (annotated)
- Committer:
- yuki0701
- Date:
- Sat Nov 24 14:45:01 2018 +0000
- Revision:
- 3:e696a6dd4254
- Parent:
- 2:e04e6b5d6584
- Child:
- 5:7493649d098b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
la00noix | 0:f5992b0c6e00 | 1 | #include "mbed.h" |
la00noix | 0:f5992b0c6e00 | 2 | #include "EC.h" |
la00noix | 0:f5992b0c6e00 | 3 | #include "R1370P.h" |
la00noix | 0:f5992b0c6e00 | 4 | #include "move4wheel.h" |
la00noix | 0:f5992b0c6e00 | 5 | #include "PathFollowing.h" |
la00noix | 0:f5992b0c6e00 | 6 | #include <stdarg.h> |
la00noix | 0:f5992b0c6e00 | 7 | |
la00noix | 0:f5992b0c6e00 | 8 | #define PI 3.141592 |
la00noix | 0:f5992b0c6e00 | 9 | |
la00noix | 0:f5992b0c6e00 | 10 | #define DEBUG_MODE // compile as debug mode (comment out if you don't use) |
la00noix | 0:f5992b0c6e00 | 11 | #ifdef DEBUG_MODE |
la00noix | 0:f5992b0c6e00 | 12 | #define DEBUG_PRINT // enable debug_printf |
la00noix | 0:f5992b0c6e00 | 13 | #endif |
la00noix | 0:f5992b0c6e00 | 14 | |
la00noix | 0:f5992b0c6e00 | 15 | Serial pc(USBTX,USBRX); |
la00noix | 0:f5992b0c6e00 | 16 | void debug_printf(const char* format,...); // work as printf in debug |
la00noix | 0:f5992b0c6e00 | 17 | void Debug_Control(); // control by PC keybord |
la00noix | 0:f5992b0c6e00 | 18 | |
la00noix | 0:f5992b0c6e00 | 19 | #define SPI_FREQ 1000000 // 1MHz |
la00noix | 0:f5992b0c6e00 | 20 | #define SPI_BITS 16 |
la00noix | 0:f5992b0c6e00 | 21 | #define SPI_MODE 0 |
la00noix | 0:f5992b0c6e00 | 22 | #define SPI_WAIT_US 1 // 1us |
yuki0701 | 3:e696a6dd4254 | 23 | //SPI spi(PB_5,PB_4,PB_3); //Nucleo |
yuki0701 | 3:e696a6dd4254 | 24 | SPI spi(p5,p6,p7); //mbed |
la00noix | 0:f5992b0c6e00 | 25 | |
yuki0701 | 3:e696a6dd4254 | 26 | /*DigitalOut ss_md1(PB_15); //エスコンの設定 |
la00noix | 0:f5992b0c6e00 | 27 | DigitalOut ss_md2(PB_14); |
la00noix | 0:f5992b0c6e00 | 28 | DigitalOut ss_md3(PB_13); |
la00noix | 0:f5992b0c6e00 | 29 | DigitalOut ss_md4(PC_4); |
la00noix | 0:f5992b0c6e00 | 30 | |
la00noix | 0:f5992b0c6e00 | 31 | DigitalOut md_enable(PA_13); // do all motor driver enable |
la00noix | 0:f5992b0c6e00 | 32 | //DigitalIn md_ch_enable(p10); // check enable switch is open or close |
la00noix | 0:f5992b0c6e00 | 33 | //Timer md_disable; |
la00noix | 0:f5992b0c6e00 | 34 | DigitalOut md_stop(PA_14); // stop all motor |
yuki0701 | 3:e696a6dd4254 | 35 | DigitalIn md_check(PB_7); // check error of all motor driver //とりあえず使わない*/ |
yuki0701 | 3:e696a6dd4254 | 36 | |
yuki0701 | 3:e696a6dd4254 | 37 | DigitalOut ss_md1(p15); //エスコンの設定 |
yuki0701 | 3:e696a6dd4254 | 38 | DigitalOut ss_md2(p16); |
yuki0701 | 3:e696a6dd4254 | 39 | DigitalOut ss_md3(p17); |
yuki0701 | 3:e696a6dd4254 | 40 | DigitalOut ss_md4(p18); |
yuki0701 | 3:e696a6dd4254 | 41 | |
yuki0701 | 3:e696a6dd4254 | 42 | DigitalOut md_enable(p25); |
yuki0701 | 3:e696a6dd4254 | 43 | //Timer md_disable; |
yuki0701 | 3:e696a6dd4254 | 44 | DigitalOut md_stop(p24); // stop all motor |
yuki0701 | 3:e696a6dd4254 | 45 | DigitalIn md_check(p23); // check error of all motor driver //とりあえず使わない |
yuki0701 | 3:e696a6dd4254 | 46 | |
la00noix | 0:f5992b0c6e00 | 47 | |
la00noix | 0:f5992b0c6e00 | 48 | /*モーターの配置 |
la00noix | 0:f5992b0c6e00 | 49 | * md1//---F---\\md4 |
la00noix | 0:f5992b0c6e00 | 50 | * | | |
la00noix | 0:f5992b0c6e00 | 51 | * L + R |
la00noix | 0:f5992b0c6e00 | 52 | * | | |
la00noix | 0:f5992b0c6e00 | 53 | * md2\\---B---//md3 |
la00noix | 0:f5992b0c6e00 | 54 | */ |
la00noix | 0:f5992b0c6e00 | 55 | |
la00noix | 0:f5992b0c6e00 | 56 | |
yuki0701 | 3:e696a6dd4254 | 57 | //Ec EC1(PC_6,PC_8,NC,500,0.05); |
yuki0701 | 3:e696a6dd4254 | 58 | //Ec EC2(PB_1,PB_12,NC,500,0.05); //Nucleo |
yuki0701 | 3:e696a6dd4254 | 59 | |
yuki0701 | 3:e696a6dd4254 | 60 | Ec EC1(p21,p22,NC,500,0.05); |
yuki0701 | 3:e696a6dd4254 | 61 | Ec EC2(p8,p26,NC,500,0.05); //←mbad |
la00noix | 0:f5992b0c6e00 | 62 | Ticker motor_tick; //角速度計算用ticker |
la00noix | 0:f5992b0c6e00 | 63 | Ticker ticker; //for enc |
la00noix | 0:f5992b0c6e00 | 64 | |
yuki0701 | 3:e696a6dd4254 | 65 | //R1370P gyro(PC_6,PC_7); //ジャイロ |
yuki0701 | 3:e696a6dd4254 | 66 | R1370P gyro(p28,p27); |
la00noix | 0:f5992b0c6e00 | 67 | |
la00noix | 0:f5992b0c6e00 | 68 | //DigitalOut can_led(LED1); //if can enable -> toggle |
la00noix | 0:f5992b0c6e00 | 69 | DigitalOut debug_led(LED2); //if debugmode -> on |
la00noix | 0:f5992b0c6e00 | 70 | DigitalOut md_stop_led(LED3); //if motor stop -> on |
la00noix | 0:f5992b0c6e00 | 71 | DigitalOut md_err_led(LED4); //if driver error -> on //とりあえず使わない |
la00noix | 0:f5992b0c6e00 | 72 | |
la00noix | 0:f5992b0c6e00 | 73 | double new_dist1=0,new_dist2=0; |
la00noix | 0:f5992b0c6e00 | 74 | double old_dist1=0,old_dist2=0; |
la00noix | 0:f5992b0c6e00 | 75 | double d_dist1=0,d_dist2=0; //座標計算用関数 |
la00noix | 0:f5992b0c6e00 | 76 | double d_x,d_y; |
la00noix | 0:f5992b0c6e00 | 77 | //現在地X,y座標、現在角度については、PathFollowingでnow_x,now_y,now_angleを定義済 |
la00noix | 0:f5992b0c6e00 | 78 | double start_x=0,start_y=0; //スタート位置 |
la00noix | 0:f5992b0c6e00 | 79 | |
yuki0701 | 3:e696a6dd4254 | 80 | double x_out,y_out,r_out;//出力値 |
yuki0701 | 3:e696a6dd4254 | 81 | |
la00noix | 0:f5992b0c6e00 | 82 | static int16_t m1=0, m2=0, m3=0, m4=0; //int16bit = int2byte |
la00noix | 0:f5992b0c6e00 | 83 | |
yuki0701 | 3:e696a6dd4254 | 84 | ///////////////////////////////////////////////////関数のプロトタイプ宣言//////////////////////////////////////////////////// |
la00noix | 0:f5992b0c6e00 | 85 | void UserLoopSetting(); // initialize setting |
la00noix | 0:f5992b0c6e00 | 86 | void DAC_Write(int16_t data, DigitalOut* DAC_cs); |
la00noix | 0:f5992b0c6e00 | 87 | void MotorControl(int16_t val_md1, int16_t val_md2, int16_t val_md3, int16_t val_md4); |
la00noix | 0:f5992b0c6e00 | 88 | |
la00noix | 0:f5992b0c6e00 | 89 | void calOmega() //角速度計算関数 |
la00noix | 0:f5992b0c6e00 | 90 | { |
la00noix | 0:f5992b0c6e00 | 91 | EC1.CalOmega(); |
la00noix | 0:f5992b0c6e00 | 92 | EC2.CalOmega(); |
la00noix | 0:f5992b0c6e00 | 93 | } |
la00noix | 0:f5992b0c6e00 | 94 | |
la00noix | 0:f5992b0c6e00 | 95 | void output(double FL,double BL,double BR,double FR) |
la00noix | 0:f5992b0c6e00 | 96 | { |
la00noix | 0:f5992b0c6e00 | 97 | m1=FL; |
la00noix | 0:f5992b0c6e00 | 98 | m2=BL; |
la00noix | 0:f5992b0c6e00 | 99 | m3=BR; |
la00noix | 0:f5992b0c6e00 | 100 | m4=FR; |
la00noix | 0:f5992b0c6e00 | 101 | } |
la00noix | 0:f5992b0c6e00 | 102 | |
la00noix | 0:f5992b0c6e00 | 103 | void base(double FL,double BL,double BR,double FR,double Max) |
la00noix | 0:f5992b0c6e00 | 104 | //いろんな加算をしても最大OR最小が1になるような補正(?)//絶対値が一番でかいやつで除算//double Max(0~1) |
la00noix | 0:f5992b0c6e00 | 105 | //マクソンは-4095~4095だからMax=4095にする//最速スピードを出すための関数になってる |
la00noix | 0:f5992b0c6e00 | 106 | { |
yuki0701 | 3:e696a6dd4254 | 107 | 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)); |
yuki0701 | 3:e696a6dd4254 | 108 | 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)); |
yuki0701 | 3:e696a6dd4254 | 109 | 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)); |
yuki0701 | 3:e696a6dd4254 | 110 | else output(Max*FL/fabs(FR),Max*BL/fabs(FR),Max*BR/fabs(FR),Max*FR/fabs(FR)); |
yuki0701 | 3:e696a6dd4254 | 111 | } |
yuki0701 | 3:e696a6dd4254 | 112 | |
yuki0701 | 3:e696a6dd4254 | 113 | void calc_xy() |
yuki0701 | 3:e696a6dd4254 | 114 | { |
yuki0701 | 3:e696a6dd4254 | 115 | now_angle=gyro.getAngle(); //ジャイロの値読み込み |
yuki0701 | 3:e696a6dd4254 | 116 | |
yuki0701 | 3:e696a6dd4254 | 117 | new_dist1=EC1.getDistance_mm(); |
yuki0701 | 3:e696a6dd4254 | 118 | new_dist2=EC2.getDistance_mm(); |
yuki0701 | 3:e696a6dd4254 | 119 | d_dist1=new_dist1-old_dist1; |
yuki0701 | 3:e696a6dd4254 | 120 | d_dist2=new_dist2-old_dist2; |
yuki0701 | 3:e696a6dd4254 | 121 | old_dist1=new_dist1; |
yuki0701 | 3:e696a6dd4254 | 122 | old_dist2=new_dist2; //微小時間当たりのエンコーダ読み込み |
yuki0701 | 3:e696a6dd4254 | 123 | |
yuki0701 | 3:e696a6dd4254 | 124 | d_x=d_dist2*sin(now_angle*PI/180)-d_dist1*cos(now_angle*PI/180); |
yuki0701 | 3:e696a6dd4254 | 125 | d_y=d_dist2*cos(now_angle*PI/180)+d_dist1*sin(now_angle*PI/180); //微小時間毎の座標変化 |
yuki0701 | 3:e696a6dd4254 | 126 | now_x=now_x+d_x; |
yuki0701 | 3:e696a6dd4254 | 127 | now_y=now_y-d_y; //微小時間毎に座標に加算 |
yuki0701 | 3:e696a6dd4254 | 128 | |
la00noix | 0:f5992b0c6e00 | 129 | } |
la00noix | 0:f5992b0c6e00 | 130 | |
la00noix | 0:f5992b0c6e00 | 131 | //ここからそれぞれのプログラム////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
la00noix | 0:f5992b0c6e00 | 132 | //now_x(現在のx座標),now_y(現在のy座標),now_angle(機体角度(ラジアンではない)(0~360や-180~180とは限らない))(反時計回りが正) |
la00noix | 0:f5992b0c6e00 | 133 | //ジャイロの出力は角度だが三角関数はラジアンとして計算する |
la00noix | 0:f5992b0c6e00 | 134 | //通常の移動+座標のずれ補正+機体の角度補正(+必要に応じさらに別補正) |
la00noix | 0:f5992b0c6e00 | 135 | //ジャイロの仕様上、角度補正をするときに計算式内で角度はそのままよりsinをとったほうがいいかもね |
la00noix | 0:f5992b0c6e00 | 136 | |
la00noix | 0:f5992b0c6e00 | 137 | void purecurve(int type,double X,double Y,double r,int theta,double speed/*,double v*/) |
la00noix | 0:f5992b0c6e00 | 138 | { |
la00noix | 0:f5992b0c6e00 | 139 | //正面を変えずに円弧を描いて90°曲がる |
la00noix | 0:f5992b0c6e00 | 140 | //X=円弧の中心座標、Y=円弧の中心座標、r=円弧の半径、theta=plotの間隔(0~90°)、v=目標速度 |
la00noix | 0:f5992b0c6e00 | 141 | |
la00noix | 0:f5992b0c6e00 | 142 | int s; |
yuki0701 | 3:e696a6dd4254 | 143 | int t = 0; |
la00noix | 0:f5992b0c6e00 | 144 | double plotx[(90/theta)+1]; //円弧にとるplotのx座標 |
la00noix | 0:f5992b0c6e00 | 145 | double ploty[(90/theta)+1]; |
la00noix | 0:f5992b0c6e00 | 146 | //double plotvx[(90/theta)+1]; //各plotにおける速度 |
la00noix | 0:f5992b0c6e00 | 147 | //double plotvy[(90/theta)+1]; |
la00noix | 0:f5992b0c6e00 | 148 | |
la00noix | 0:f5992b0c6e00 | 149 | double x_out,y_out,r_out; |
la00noix | 0:f5992b0c6e00 | 150 | |
la00noix | 0:f5992b0c6e00 | 151 | switch(type) { |
la00noix | 0:f5992b0c6e00 | 152 | case 1://↑から→ |
la00noix | 0:f5992b0c6e00 | 153 | |
la00noix | 0:f5992b0c6e00 | 154 | for(s=0; s<((90/theta)+1); s++) { |
yuki0701 | 3:e696a6dd4254 | 155 | plotx[s] = X + r * cos(PI - s * (PI*theta/180)); |
la00noix | 0:f5992b0c6e00 | 156 | ploty[s] = Y + r * sin(PI - s * (PI*theta/180)); |
la00noix | 0:f5992b0c6e00 | 157 | //plotvx[s] = -v * cos(PI - s * (PI*theta/180)); |
la00noix | 0:f5992b0c6e00 | 158 | //plotvy[s] = v * sin(PI - s * (PI*theta/180)); |
yuki0701 | 3:e696a6dd4254 | 159 | //debug_printf("plotx[%d]=%f ploty[%d]=%f\n\r",s,plotx[s],s,ploty[s]); |
la00noix | 0:f5992b0c6e00 | 160 | } |
la00noix | 0:f5992b0c6e00 | 161 | |
la00noix | 0:f5992b0c6e00 | 162 | while(1) { |
la00noix | 0:f5992b0c6e00 | 163 | now_angle=gyro.getAngle(); //ジャイロの値読み込み |
la00noix | 0:f5992b0c6e00 | 164 | |
yuki0701 | 3:e696a6dd4254 | 165 | calc_xy(); |
la00noix | 0:f5992b0c6e00 | 166 | |
la00noix | 0:f5992b0c6e00 | 167 | XYRmotorout(plotx[t],ploty[t],plotx[t+1],ploty[t+1],&x_out,&y_out,&r_out); |
la00noix | 0:f5992b0c6e00 | 168 | CalMotorOut(x_out,y_out,r_out); //move4wheel内のモーター番号定義または成分分解が違うかも? |
la00noix | 0:f5992b0c6e00 | 169 | //CalMotorOut(plotvx[t], plotvy[t],0); |
la00noix | 0:f5992b0c6e00 | 170 | |
yuki0701 | 3:e696a6dd4254 | 171 | //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); |
yuki0701 | 3:e696a6dd4254 | 172 | //debug_printf("t=%d (0)=%f (1)=%f (2)=%f (3)=%f\n\r",t,GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3)); |
la00noix | 0:f5992b0c6e00 | 173 | |
yuki0701 | 3:e696a6dd4254 | 174 | base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),1000); //m1~m4に代入 |
la00noix | 0:f5992b0c6e00 | 175 | |
yuki0701 | 3:e696a6dd4254 | 176 | if(((plotx[t+1] - now_x)*(plotx[t+1] - plotx[t]) + (ploty[t+1] - now_y)*(ploty[t+1] - ploty[t])) < 0)t++; |
la00noix | 0:f5992b0c6e00 | 177 | if(t == (90/theta))break; |
la00noix | 2:e04e6b5d6584 | 178 | |
la00noix | 0:f5992b0c6e00 | 179 | MotorControl(m1,m2,m3,m4); //出力 |
la00noix | 2:e04e6b5d6584 | 180 | |
yuki0701 | 3:e696a6dd4254 | 181 | debug_printf("t=%d m1=%d m2=%d m3=%d m4=%d x=%f y=%f, angle = %f\n\r",t,m1,m2,m3,m4,now_x,now_y,now_angle); |
la00noix | 0:f5992b0c6e00 | 182 | |
la00noix | 0:f5992b0c6e00 | 183 | } |
la00noix | 0:f5992b0c6e00 | 184 | |
la00noix | 0:f5992b0c6e00 | 185 | case 2://↑から← //まだ編集してない |
la00noix | 0:f5992b0c6e00 | 186 | |
la00noix | 0:f5992b0c6e00 | 187 | for(s=0; s<((90/theta)+1); s++) { |
la00noix | 0:f5992b0c6e00 | 188 | plotx[s] = X + r * cos(s * (PI*theta/180)); |
la00noix | 0:f5992b0c6e00 | 189 | ploty[s] = Y + r * sin(s * (PI*theta/180)); |
la00noix | 0:f5992b0c6e00 | 190 | } |
la00noix | 0:f5992b0c6e00 | 191 | |
la00noix | 0:f5992b0c6e00 | 192 | while(1) { |
la00noix | 0:f5992b0c6e00 | 193 | |
la00noix | 0:f5992b0c6e00 | 194 | now_angle=gyro.getAngle(); //ジャイロの値読み込み |
yuki0701 | 3:e696a6dd4254 | 195 | |
yuki0701 | 3:e696a6dd4254 | 196 | calc_xy(); |
la00noix | 0:f5992b0c6e00 | 197 | |
la00noix | 0:f5992b0c6e00 | 198 | XYRmotorout(plotx[t],ploty[t],plotx[t+1],ploty[t+1],&x_out,&y_out,&r_out); |
la00noix | 0:f5992b0c6e00 | 199 | CalMotorOut(x_out,y_out,r_out); |
yuki0701 | 3:e696a6dd4254 | 200 | base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),1000); |
yuki0701 | 3:e696a6dd4254 | 201 | if(((plotx[t+1] - now_x)*(plotx[t+1] - plotx[t]) + (ploty[t+1] - now_y)*(ploty[t+1] - ploty[t])) < 0)t++; |
la00noix | 0:f5992b0c6e00 | 202 | if(t == (90/theta))break; |
la00noix | 0:f5992b0c6e00 | 203 | |
yuki0701 | 3:e696a6dd4254 | 204 | MotorControl(m1,m2,m3,m4); |
la00noix | 0:f5992b0c6e00 | 205 | } |
la00noix | 0:f5992b0c6e00 | 206 | } |
la00noix | 0:f5992b0c6e00 | 207 | } |
yuki0701 | 3:e696a6dd4254 | 208 | |
yuki0701 | 3:e696a6dd4254 | 209 | void gogo_straight(double x1_point,double y1_point,double x2_point,double y2_point)//直線運動プログラム(引数:出発地点の座標(x,y)、目標地点の座標(x,y)) |
yuki0701 | 3:e696a6dd4254 | 210 | { |
yuki0701 | 3:e696a6dd4254 | 211 | while (1) { |
yuki0701 | 3:e696a6dd4254 | 212 | |
yuki0701 | 3:e696a6dd4254 | 213 | //now_angle=gyro.getAngle(); |
yuki0701 | 3:e696a6dd4254 | 214 | |
yuki0701 | 3:e696a6dd4254 | 215 | calc_xy(); |
yuki0701 | 3:e696a6dd4254 | 216 | printf("x = %f, y = %f, angle = %f\r\n",now_x,now_y,now_angle); |
yuki0701 | 3:e696a6dd4254 | 217 | |
yuki0701 | 3:e696a6dd4254 | 218 | |
yuki0701 | 3:e696a6dd4254 | 219 | //Debug_Control(); |
yuki0701 | 3:e696a6dd4254 | 220 | XYRmotorout(x1_point,y1_point,x2_point,y2_point,&x_out,&y_out,&r_out); |
yuki0701 | 3:e696a6dd4254 | 221 | //printf("x=%lf, y=%lf, r=%lf",x_out, y_out,r_out); |
yuki0701 | 3:e696a6dd4254 | 222 | |
yuki0701 | 3:e696a6dd4254 | 223 | CalMotorOut(x_out,y_out,r_out); |
yuki0701 | 3:e696a6dd4254 | 224 | //printf("out1=%lf, out2=%lf, out3=%lf, out4=%lf\n",GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3)); |
yuki0701 | 3:e696a6dd4254 | 225 | |
yuki0701 | 3:e696a6dd4254 | 226 | base(GetMotorOut(0),GetMotorOut(1),GetMotorOut(2),GetMotorOut(3),1000); |
yuki0701 | 3:e696a6dd4254 | 227 | //printf("m1=%d, m2=%d, m3=%d, m4=%d\r\n",m1,m2,m3,m4); |
yuki0701 | 3:e696a6dd4254 | 228 | MotorControl(m1,m2,m3,m4); |
yuki0701 | 3:e696a6dd4254 | 229 | |
yuki0701 | 3:e696a6dd4254 | 230 | |
yuki0701 | 3:e696a6dd4254 | 231 | |
yuki0701 | 3:e696a6dd4254 | 232 | if(((x2_point - now_x)*(x2_point - x1_point) + (y2_point - now_y)*(y2_point - y1_point)) < 0) break; |
yuki0701 | 3:e696a6dd4254 | 233 | |
yuki0701 | 3:e696a6dd4254 | 234 | } |
yuki0701 | 3:e696a6dd4254 | 235 | |
yuki0701 | 3:e696a6dd4254 | 236 | MotorControl(0,0,0,0); |
yuki0701 | 3:e696a6dd4254 | 237 | } |
yuki0701 | 3:e696a6dd4254 | 238 | |
yuki0701 | 3:e696a6dd4254 | 239 | void go_straight(int type,double goal_x,double goal_y,double speed,double front)//移動パターン(1,2,3,4)、目標X、目標Y、最高速度(0~1)、正面角度 |
yuki0701 | 3:e696a6dd4254 | 240 | { |
yuki0701 | 3:e696a6dd4254 | 241 | double y_hosei=(now_y-goal_y)*0.001;//Y座標(mm単位)にP処理 |
yuki0701 | 3:e696a6dd4254 | 242 | double x_hosei=(now_x-goal_x)*0.001;//X座標(mm単位)にP処理 |
yuki0701 | 3:e696a6dd4254 | 243 | double incl_hosei=sin(now_angle-front)*(PI/180)*0.1;//機体角度(sin(数度→ラジアンに変換))にP処理 |
yuki0701 | 3:e696a6dd4254 | 244 | |
yuki0701 | 3:e696a6dd4254 | 245 | switch(type) { |
yuki0701 | 3:e696a6dd4254 | 246 | case 1://Y座標一定の正方向横移動 |
yuki0701 | 3:e696a6dd4254 | 247 | while(now_x<goal_x){ |
yuki0701 | 3:e696a6dd4254 | 248 | base(-1-y_hosei-incl_hosei,-1+y_hosei-incl_hosei,1+y_hosei-incl_hosei,1-y_hosei-incl_hosei,speed); |
yuki0701 | 3:e696a6dd4254 | 249 | } |
yuki0701 | 3:e696a6dd4254 | 250 | break; |
yuki0701 | 3:e696a6dd4254 | 251 | |
yuki0701 | 3:e696a6dd4254 | 252 | case 2://Y座標一定の負方向横移動 |
yuki0701 | 3:e696a6dd4254 | 253 | while(now_x>goal_x){ |
yuki0701 | 3:e696a6dd4254 | 254 | base(1-y_hosei-incl_hosei,1+y_hosei-incl_hosei,-1+y_hosei-incl_hosei,-1-y_hosei-incl_hosei,speed); |
yuki0701 | 3:e696a6dd4254 | 255 | } |
yuki0701 | 3:e696a6dd4254 | 256 | break; |
yuki0701 | 3:e696a6dd4254 | 257 | |
yuki0701 | 3:e696a6dd4254 | 258 | case 3://Y座標一定の正方向横移動 |
yuki0701 | 3:e696a6dd4254 | 259 | while(now_y<goal_y){ |
yuki0701 | 3:e696a6dd4254 | 260 | base(1+x_hosei-incl_hosei,-1+x_hosei-incl_hosei,-1-x_hosei-incl_hosei,1-x_hosei-incl_hosei,speed); |
yuki0701 | 3:e696a6dd4254 | 261 | } |
yuki0701 | 3:e696a6dd4254 | 262 | break; |
yuki0701 | 3:e696a6dd4254 | 263 | |
yuki0701 | 3:e696a6dd4254 | 264 | case 4://X座標一定の負方向横移動 |
yuki0701 | 3:e696a6dd4254 | 265 | while(now_y>goal_y){ |
yuki0701 | 3:e696a6dd4254 | 266 | base(-1+x_hosei-incl_hosei,1+x_hosei-incl_hosei,1-x_hosei-incl_hosei,-1-x_hosei-incl_hosei,speed); |
yuki0701 | 3:e696a6dd4254 | 267 | } |
yuki0701 | 3:e696a6dd4254 | 268 | break; |
yuki0701 | 3:e696a6dd4254 | 269 | } |
yuki0701 | 3:e696a6dd4254 | 270 | } |
yuki0701 | 3:e696a6dd4254 | 271 | |
yuki0701 | 3:e696a6dd4254 | 272 | //////////////////////////////////////////////////////////////以下main文//////////////////////////////////////////////////////////////////////// |
la00noix | 0:f5992b0c6e00 | 273 | |
la00noix | 0:f5992b0c6e00 | 274 | int main() |
la00noix | 0:f5992b0c6e00 | 275 | { |
la00noix | 0:f5992b0c6e00 | 276 | UserLoopSetting(); |
la00noix | 0:f5992b0c6e00 | 277 | |
yuki0701 | 3:e696a6dd4254 | 278 | void reset(); |
la00noix | 0:f5992b0c6e00 | 279 | EC1.reset(); |
yuki0701 | 3:e696a6dd4254 | 280 | EC2.reset(); |
la00noix | 0:f5992b0c6e00 | 281 | |
la00noix | 0:f5992b0c6e00 | 282 | now_x=start_x; |
la00noix | 0:f5992b0c6e00 | 283 | now_y=start_y; |
yuki0701 | 3:e696a6dd4254 | 284 | |
yuki0701 | 3:e696a6dd4254 | 285 | |
yuki0701 | 3:e696a6dd4254 | 286 | // purecurve(1,1000,0,1000,9,1000); |
yuki0701 | 3:e696a6dd4254 | 287 | // MotorControl(0,0,0,0); |
yuki0701 | 3:e696a6dd4254 | 288 | |
yuki0701 | 3:e696a6dd4254 | 289 | |
yuki0701 | 3:e696a6dd4254 | 290 | //gogo_straight(0,0,1500,0); |
la00noix | 0:f5992b0c6e00 | 291 | |
la00noix | 0:f5992b0c6e00 | 292 | } |
yuki0701 | 3:e696a6dd4254 | 293 | ///////////////////////////////////////////////////////////////////////以下マクソン関連/////////////////////////////////////////////////////////////////////////// |
la00noix | 0:f5992b0c6e00 | 294 | |
la00noix | 0:f5992b0c6e00 | 295 | void UserLoopSetting() |
la00noix | 0:f5992b0c6e00 | 296 | { |
la00noix | 0:f5992b0c6e00 | 297 | //-----エスコンの初期設定-----// |
la00noix | 0:f5992b0c6e00 | 298 | spi.format(SPI_BITS, SPI_MODE); |
la00noix | 0:f5992b0c6e00 | 299 | spi.frequency(SPI_FREQ); |
la00noix | 0:f5992b0c6e00 | 300 | ss_md1 = 1; |
la00noix | 0:f5992b0c6e00 | 301 | ss_md2 = 1; |
la00noix | 0:f5992b0c6e00 | 302 | ss_md3 = 1; |
la00noix | 0:f5992b0c6e00 | 303 | ss_md4 = 1; |
la00noix | 0:f5992b0c6e00 | 304 | md_enable = 1; //enable on |
la00noix | 0:f5992b0c6e00 | 305 | md_err_led = 0; |
la00noix | 0:f5992b0c6e00 | 306 | md_stop = 1; |
la00noix | 0:f5992b0c6e00 | 307 | md_stop_led = 1; |
la00noix | 0:f5992b0c6e00 | 308 | //-----センサーの初期設定-----// |
la00noix | 0:f5992b0c6e00 | 309 | gyro.initialize(); |
la00noix | 0:f5992b0c6e00 | 310 | motor_tick.attach(&calOmega,0.05); //0.05秒間隔で角速度を計算 |
la00noix | 0:f5992b0c6e00 | 311 | EC1.setDiameter_mm(48); |
la00noix | 0:f5992b0c6e00 | 312 | EC2.setDiameter_mm(48); //測定輪半径 |
la00noix | 0:f5992b0c6e00 | 313 | //-----PathFollowingのパラメーター設定-----// |
yuki0701 | 3:e696a6dd4254 | 314 | set_p_out(800); //ベクトルABに平行方向の出力値設定関数(カーブを曲がる速度) |
yuki0701 | 3:e696a6dd4254 | 315 | q_setPDparam(0.1,0.1); //ベクトルABに垂直な方向の誤差を埋めるPD制御のパラメータ設定関数 |
yuki0701 | 3:e696a6dd4254 | 316 | r_setPDparam(10,0.1); //機体角度と目標角度の誤差を埋めるPD制御のパラメータ設定関数 |
yuki0701 | 3:e696a6dd4254 | 317 | set_r_out(500); //旋回時の最大出力値設定関数 |
yuki0701 | 3:e696a6dd4254 | 318 | // set_target_angle(0); //機体目標角度設定関数 |
la00noix | 0:f5992b0c6e00 | 319 | |
la00noix | 0:f5992b0c6e00 | 320 | #ifdef DEBUG_MODE |
la00noix | 0:f5992b0c6e00 | 321 | debug_led = 1; |
la00noix | 0:f5992b0c6e00 | 322 | pc.attach(Debug_Control, Serial::RxIrq); |
la00noix | 0:f5992b0c6e00 | 323 | #else |
la00noix | 0:f5992b0c6e00 | 324 | debug_led = 0; |
la00noix | 0:f5992b0c6e00 | 325 | #endif |
la00noix | 0:f5992b0c6e00 | 326 | } |
la00noix | 0:f5992b0c6e00 | 327 | |
la00noix | 0:f5992b0c6e00 | 328 | #define MCP4922_AB (1<<15) |
la00noix | 0:f5992b0c6e00 | 329 | #define MCP4922_BUF (1<<14) |
la00noix | 0:f5992b0c6e00 | 330 | #define MCP4922_GA (1<<13) |
la00noix | 0:f5992b0c6e00 | 331 | #define MCP4922_SHDN (1<<12) |
la00noix | 0:f5992b0c6e00 | 332 | |
la00noix | 0:f5992b0c6e00 | 333 | #define MCP4922_SET_OUTA (0x3000) //( MCP4922_GA || MCP4922_SHDN ) //12288 |
la00noix | 0:f5992b0c6e00 | 334 | #define MCP4922_SET_OUTB (0xB000) //( MCP4922_AB || MCP4922_GA || MCP4922_SHDN ) //45056 |
la00noix | 0:f5992b0c6e00 | 335 | #define MCP4922_MASKSET (0x0FFF) //4095 |
la00noix | 0:f5992b0c6e00 | 336 | |
la00noix | 0:f5992b0c6e00 | 337 | void DAC_Write(int16_t data, DigitalOut* DAC_cs) //(出力,出力場所) |
la00noix | 0:f5992b0c6e00 | 338 | { |
la00noix | 0:f5992b0c6e00 | 339 | static uint16_t dataA; //送るデータ |
la00noix | 0:f5992b0c6e00 | 340 | static uint16_t dataB; |
la00noix | 0:f5992b0c6e00 | 341 | |
la00noix | 0:f5992b0c6e00 | 342 | dataA = MCP4922_SET_OUTA; |
la00noix | 0:f5992b0c6e00 | 343 | dataB = MCP4922_SET_OUTB; |
la00noix | 0:f5992b0c6e00 | 344 | |
la00noix | 0:f5992b0c6e00 | 345 | if(data >= 0) { |
la00noix | 0:f5992b0c6e00 | 346 | if(data > 4095) { |
la00noix | 0:f5992b0c6e00 | 347 | data = 4095; |
la00noix | 0:f5992b0c6e00 | 348 | } |
la00noix | 0:f5992b0c6e00 | 349 | dataA += (MCP4922_MASKSET & (uint16_t)(data)); |
la00noix | 0:f5992b0c6e00 | 350 | } else { |
la00noix | 0:f5992b0c6e00 | 351 | if(data < -4095) { |
la00noix | 0:f5992b0c6e00 | 352 | data = -4095; |
la00noix | 0:f5992b0c6e00 | 353 | } |
la00noix | 0:f5992b0c6e00 | 354 | dataB += (MCP4922_MASKSET & (uint16_t)(-data)); |
la00noix | 0:f5992b0c6e00 | 355 | } |
la00noix | 0:f5992b0c6e00 | 356 | |
la00noix | 0:f5992b0c6e00 | 357 | //Aの出力設定 |
la00noix | 0:f5992b0c6e00 | 358 | (DigitalOut)(*DAC_cs)=0; |
la00noix | 0:f5992b0c6e00 | 359 | wait_us(SPI_WAIT_US); |
la00noix | 0:f5992b0c6e00 | 360 | spi.write(dataA); |
la00noix | 0:f5992b0c6e00 | 361 | wait_us(SPI_WAIT_US); |
la00noix | 0:f5992b0c6e00 | 362 | (DigitalOut)(*DAC_cs)=1; |
la00noix | 0:f5992b0c6e00 | 363 | wait_us(SPI_WAIT_US); |
la00noix | 0:f5992b0c6e00 | 364 | |
la00noix | 0:f5992b0c6e00 | 365 | //Bの出力設定 |
la00noix | 0:f5992b0c6e00 | 366 | (DigitalOut)(*DAC_cs)=0; |
la00noix | 0:f5992b0c6e00 | 367 | wait_us(SPI_WAIT_US); |
la00noix | 0:f5992b0c6e00 | 368 | spi.write(dataB); |
la00noix | 0:f5992b0c6e00 | 369 | wait_us(SPI_WAIT_US); |
la00noix | 0:f5992b0c6e00 | 370 | (DigitalOut)(*DAC_cs)=1; |
la00noix | 0:f5992b0c6e00 | 371 | |
la00noix | 0:f5992b0c6e00 | 372 | } |
la00noix | 0:f5992b0c6e00 | 373 | |
la00noix | 0:f5992b0c6e00 | 374 | void MotorControl(int16_t val_md1, int16_t val_md2, int16_t val_md3, int16_t val_md4) //出力 |
la00noix | 0:f5992b0c6e00 | 375 | { |
la00noix | 0:f5992b0c6e00 | 376 | static int16_t zero_check; |
la00noix | 0:f5992b0c6e00 | 377 | |
la00noix | 0:f5992b0c6e00 | 378 | DAC_Write(val_md1, &ss_md1); |
la00noix | 0:f5992b0c6e00 | 379 | DAC_Write(val_md2, &ss_md2); |
la00noix | 0:f5992b0c6e00 | 380 | DAC_Write(val_md3, &ss_md3); |
la00noix | 0:f5992b0c6e00 | 381 | DAC_Write(val_md4, &ss_md4); |
la00noix | 0:f5992b0c6e00 | 382 | |
la00noix | 0:f5992b0c6e00 | 383 | zero_check = (val_md1 | val_md2 | val_md3 | val_md4); //すべての出力が0なら強制停止 |
la00noix | 0:f5992b0c6e00 | 384 | if(zero_check == 0) { |
la00noix | 0:f5992b0c6e00 | 385 | md_stop = 1; |
la00noix | 0:f5992b0c6e00 | 386 | md_stop_led = 1; |
la00noix | 0:f5992b0c6e00 | 387 | } else { |
la00noix | 0:f5992b0c6e00 | 388 | md_stop = 0; |
la00noix | 0:f5992b0c6e00 | 389 | md_stop_led = 0; |
la00noix | 0:f5992b0c6e00 | 390 | } |
la00noix | 0:f5992b0c6e00 | 391 | } |
la00noix | 0:f5992b0c6e00 | 392 | |
la00noix | 0:f5992b0c6e00 | 393 | #ifdef DEBUG_MODE |
la00noix | 0:f5992b0c6e00 | 394 | void Debug_Control() |
la00noix | 0:f5992b0c6e00 | 395 | { |
la00noix | 0:f5992b0c6e00 | 396 | static char pc_command = '\0'; |
la00noix | 0:f5992b0c6e00 | 397 | |
la00noix | 0:f5992b0c6e00 | 398 | pc_command = pc.getc(); |
la00noix | 0:f5992b0c6e00 | 399 | |
la00noix | 1:86eae1cf26d2 | 400 | if(pc_command == 'w') { //前進 |
la00noix | 0:f5992b0c6e00 | 401 | m1+=500; |
la00noix | 0:f5992b0c6e00 | 402 | m2+=500; |
la00noix | 0:f5992b0c6e00 | 403 | m3-=500; |
la00noix | 0:f5992b0c6e00 | 404 | m4-=500; |
la00noix | 1:86eae1cf26d2 | 405 | } else if(pc_command == 's') { //後進 |
la00noix | 0:f5992b0c6e00 | 406 | m1-=500; |
la00noix | 0:f5992b0c6e00 | 407 | m2-=500; |
la00noix | 0:f5992b0c6e00 | 408 | m3+=500; |
la00noix | 0:f5992b0c6e00 | 409 | m4+=500; |
la00noix | 1:86eae1cf26d2 | 410 | } else if(pc_command == 'd') { //右回り |
la00noix | 0:f5992b0c6e00 | 411 | m1+=500; |
la00noix | 0:f5992b0c6e00 | 412 | m2+=500; |
la00noix | 0:f5992b0c6e00 | 413 | m3+=500; |
la00noix | 0:f5992b0c6e00 | 414 | m4+=500; |
la00noix | 1:86eae1cf26d2 | 415 | } else if(pc_command == 'a') { //左回り |
la00noix | 0:f5992b0c6e00 | 416 | m1-=500; |
la00noix | 0:f5992b0c6e00 | 417 | m2-=500; |
la00noix | 0:f5992b0c6e00 | 418 | m3-=500; |
la00noix | 0:f5992b0c6e00 | 419 | m4-=500; |
la00noix | 0:f5992b0c6e00 | 420 | } else { |
la00noix | 0:f5992b0c6e00 | 421 | m1=0; |
la00noix | 0:f5992b0c6e00 | 422 | m2=0; |
la00noix | 0:f5992b0c6e00 | 423 | m3=0; |
la00noix | 0:f5992b0c6e00 | 424 | m4=0; |
la00noix | 0:f5992b0c6e00 | 425 | } |
la00noix | 0:f5992b0c6e00 | 426 | |
la00noix | 0:f5992b0c6e00 | 427 | if(m1>4095) { //最大値を超えないように |
la00noix | 0:f5992b0c6e00 | 428 | m1=4095; |
la00noix | 0:f5992b0c6e00 | 429 | } else if(m1<-4095) { |
la00noix | 0:f5992b0c6e00 | 430 | m1=-4095; |
la00noix | 0:f5992b0c6e00 | 431 | } |
la00noix | 0:f5992b0c6e00 | 432 | if(m2>4095) { |
la00noix | 0:f5992b0c6e00 | 433 | m2=4095; |
la00noix | 0:f5992b0c6e00 | 434 | } else if(m2<-4095) { |
la00noix | 0:f5992b0c6e00 | 435 | m2=-4095; |
la00noix | 0:f5992b0c6e00 | 436 | } |
la00noix | 0:f5992b0c6e00 | 437 | if(m3>4095) { |
la00noix | 0:f5992b0c6e00 | 438 | m3=4095; |
la00noix | 0:f5992b0c6e00 | 439 | } else if(m3<-4095) { |
la00noix | 0:f5992b0c6e00 | 440 | m3=-4095; |
la00noix | 0:f5992b0c6e00 | 441 | } |
la00noix | 0:f5992b0c6e00 | 442 | if(m4>4095) { |
la00noix | 0:f5992b0c6e00 | 443 | m4=4095; |
la00noix | 0:f5992b0c6e00 | 444 | } else if(m4<-4095) { |
la00noix | 0:f5992b0c6e00 | 445 | m4=-4095; |
la00noix | 0:f5992b0c6e00 | 446 | } |
la00noix | 0:f5992b0c6e00 | 447 | |
la00noix | 0:f5992b0c6e00 | 448 | debug_printf("%d %d %d %d\r\n",m1,m2,m3,m4); |
la00noix | 0:f5992b0c6e00 | 449 | MotorControl(m1,m2,m3,m4); |
la00noix | 0:f5992b0c6e00 | 450 | pc_command = '\0'; |
la00noix | 0:f5992b0c6e00 | 451 | } |
la00noix | 0:f5992b0c6e00 | 452 | #endif |
la00noix | 0:f5992b0c6e00 | 453 | |
la00noix | 0:f5992b0c6e00 | 454 | #ifdef DEBUG_PRINT |
la00noix | 0:f5992b0c6e00 | 455 | void debug_printf(const char* format,...) |
la00noix | 0:f5992b0c6e00 | 456 | { |
la00noix | 0:f5992b0c6e00 | 457 | va_list arg; |
la00noix | 0:f5992b0c6e00 | 458 | va_start(arg, format); |
la00noix | 0:f5992b0c6e00 | 459 | vprintf(format, arg); |
la00noix | 0:f5992b0c6e00 | 460 | va_end(arg); |
la00noix | 0:f5992b0c6e00 | 461 | } |
la00noix | 0:f5992b0c6e00 | 462 | #endif |