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: HCSR04_2 MPU6050_2 mbed SDFileSystem3
Fork of Autoflight2018_29 by
main.cpp
00001 //mbed 00002 #include "mbed.h" 00003 #include "FATFileSystem.h" 00004 #include "SDFileSystem.h" 00005 //C 00006 #include "math.h" 00007 //sensor 00008 #include "MPU6050_DMP6.h" 00009 //#include "MPU9250.h" 00010 //#include "BMP280.h" 00011 #include "hcsr04.h" 00012 //device 00013 #include "sbus.h" 00014 //config 00015 #include "SkipperSv2.h" 00016 #include "falfalla.h" 00017 //other 00018 #include "pid.h" 00019 00020 #define DEBUG_SEMIAUTO 0 00021 #define DEBUG_PRINT_INLOOP 1 00022 00023 #define KP_ELE 15.0 //2.0 00024 #define KI_ELE 0.0 00025 #define KD_ELE 0.0 //0/0 00026 #define KP_RUD 3.0 00027 #define KI_RUD 0.0 00028 #define KD_RUD 0.0 00029 #define KP_AIL 0.1 00030 #define KI_AIL 0.2 00031 #define KD_AIL 0.2 00032 00033 //#define g_AIL_L_Ratio_rightloop 0.5 00034 00035 #define GAIN_CONTROLVALUE_TO_PWM 3.0 00036 00037 #define RIGHT_ROLL -12.0 00038 #define RIGHT_PITCH -10.0 //5.0 00039 #define LEFT_ROLL 12.0 00040 #define LEFT_PITCH -5.0 00041 #define STRAIGHT_ROLL 4.0 00042 #define STRAIGHT_PITCH 3.0 00043 #define TAKEOFF_THR 0.8 00044 #define LOOP_THR 0.6 00045 00046 //#define g_rightloopRUD 1500 00047 00048 #define RIGHT_ROLL_SHORT -12.0 00049 #define RIGHT_PITCH_SHORT -5.0 00050 #define LEFT_ROLL_SHORT 12.0 00051 #define LEFT_PITCH_SHORT -5.0 00052 00053 /*#define rightloopRUD 1300 //1250 00054 #define rightloopshortRUD 1250 00055 #define leftloopRUD 1500 00056 #define leftloopshortRUD 1500 00057 #define glideloopRUD 1300 00058 */ 00059 #define AIL_R_correctionrightloop 0 00060 #define AIL_L_correctionrightloop 0 00061 #define AIL_L_correctionrightloopshort 0 00062 #define AIL_L_correctionleftloop -0 00063 #define AIL_L_correctionleftloopshort 0 00064 00065 00066 #define RIGHTLOOP_RUD 1250 00067 #define RIGHTLOOPSHORT_RUD 1250 00068 #define LEFTLOOP_RUD 1500 00069 #define LEFTLOOPSHORT_RUD 1500 00070 #define GLIDELOOP_RUD 1300 00071 #define AIL_L_CORRECTION_RIGHTLOOP 0 00072 #define AIL_L_CORRECTION_RIGHTLOOPSHORT 0 00073 #define AIL_L_CORRECTION_LEFTLOOP 0 00074 #define AIL_L_CORRECTION_LEFTLOOPSHORT 0 00075 00076 #define GLIDE_ROLL -12.0 00077 #define GLIDE_PITCH -3.0 00078 00079 00080 #define AIL_L_RatioRising 0.5 00081 #define AIL_L_RatioDescent 2 00082 00083 //コンパスキャリブレーション 00084 //SkipperS2基板 00085 /* 00086 #define MAGBIAS_X -35.0 00087 #define MAGBIAS_Y 535.0 00088 #define MAGBIAS_Z -50.0 00089 */ 00090 //S2v2 1番基板 00091 #define MAGBIAS_X 395.0 00092 #define MAGBIAS_Y 505.0 00093 #define MAGBIAS_Z -725.0 00094 //S2v2 2番基板 00095 /* 00096 #define MAGBIAS_X 185.0 00097 #define MAGBIAS_Y 220.0 00098 #define MAGBIAS_Z -350.0 00099 */ 00100 00101 #define ELEMENT 1 00102 #define LIMIT_STRAIGHT_YAW 5.0 00103 #define THRESHOLD_TURNINGRADIUS_YAW 60.0 00104 #define ALLOWHEIGHT 15 00105 00106 #ifndef PI 00107 #define PI 3.14159265358979 00108 #endif 00109 00110 const int16_t lengthdivpwm = 320; 00111 const int16_t changeModeCount = 6; 00112 00113 00114 SBUS sbus(PA_9, PA_10); //SBUS 00115 00116 PwmOut servo1(PC_6); // TIM3_CH1 //old echo 00117 PwmOut servo2(PC_7); // TIM3_CH2 //PC_7 00118 PwmOut servo3(PB_0); // TIM3_CH3 00119 PwmOut servo4(PB_1); // TIM3_CH4 00120 PwmOut servo5(PB_6); // TIM4_CH1 00121 PwmOut servo6(PB_7); // TIM4_CH2 //old trigger 00122 //PwmOut servo7(PB_8); // TIM4_CH3 //PB_8 new echo 00123 //PwmOut servo8(PB_9); // TIM4_CH4 //new trigger 00124 00125 RawSerial pc(PA_2,PA_3, 115200); //tx,rx.baudrate pin;PA_2=UART2_TX, PA_3=UART2_RX 00126 //RawSerial pc2(PB_6,PB_7, 115200); //sbus確認用 00127 SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd"); 00128 00129 DigitalOut led1(PA_0); //黄色のコネクタ 00130 DigitalOut led2(PA_1); 00131 DigitalOut led3(PB_4); 00132 DigitalOut led4(PB_5); 00133 00134 //InterruptIn switch2(PC_14); 00135 MPU6050DMP6 mpu6050(PC_0,&pc); //割り込みピン,シリアルポインタ i2cのピン指定は MPU6050>>I2Cdev.h 内のdefine 00136 //HCSR04 usensor(PB_9,PB_8); //trig,echo 9,8 00137 00138 PID pid_AIL(g_kpAIL,g_kiAIL,g_kdAIL); 00139 PID pid_ELE(g_kpELE,g_kiELE,g_kdELE); 00140 PID pid_RUD(g_kpRUD,g_kiRUD,g_kdRUD); 00141 00142 enum Channel{AIL_R, ELE, THR, RUD, DROP, AIL_L, Ch7, Ch8}; 00143 enum Angle{ROLL, PITCH, YAW}; //yaw:北を0とした絶対角度 00144 enum OperationMode{StartUp, SemiAuto, RightLoop, LeftLoop, GoStraight, BombwithPC, ZERO, Moebius, Glide}; 00145 enum BombingMode{Takeoff, Chicken, Transition, Approach}; 00146 enum OutputStatus{Manual, Auto}; 00147 00148 static OutputStatus output_status = Manual; 00149 OperationMode operation_mode = StartUp; 00150 BombingMode bombing_mode = Takeoff; 00151 00152 static int16_t autopwm[8] = {1500,1500,1180,1500,1392,1500}; 00153 00154 //1号機 00155 static int16_t trimpwm[6] = {1580,1600,1176,1404,1512,1448}; 00156 int16_t maxpwm[6] = {1796,1936,1848,1740,1820,1856}; 00157 int16_t minpwm[6] = {1182,1265,1176,1068,1180,1176}; 00158 const int16_t reverce[4] = {Reverce_falfalla[0],Reverce_falfalla[1],Reverce_falfalla[2],Reverce_falfalla[3]}; 00159 00160 //2号機 00161 /* 00162 static int16_t trimpwm[6] = {1500,1500,1180,1500,1392,1600}; 00163 int16_t maxpwm[6] = {1820,1820,1820,1820,1820,1820}; 00164 int16_t minpwm[6] = {1180,1180,1180,1180,1180,1180}; 00165 const int16_t reverce[4] = {Reverce_falfalla[0],Reverce_falfalla[1],Reverce_falfalla[2],Reverce_falfalla[3]}; 00166 */ 00167 00168 int16_t oldTHR = 1000; 00169 00170 int16_t g_AIL_L_Ratio_rightloop = 0.5; 00171 00172 00173 static float nowAngle[3] = {0,0,0}; 00174 const float trimAngle[3] = {0.0, 0.0, 0.0}; 00175 const float maxAngle[2] = {90, 90}; 00176 const float minAngle[2] = {-90, -90}; 00177 00178 float FirstROLL = 0.0, FirstPITCH = 0.0 ,FirstYAW = 0.0; 00179 00180 unsigned int g_distance; 00181 //Ticker USsensor; 00182 static char g_buf[16]; 00183 char g_landingcommand='Z'; 00184 float g_SerialTargetYAW; 00185 00186 Timer t; 00187 Timeout RerurnChickenServo1; 00188 Timeout RerurnChickenServo2; 00189 00190 /*-----関数のプロトタイプ宣言-----*/ 00191 void setup(); 00192 void loop(); 00193 00194 void Init_PWM(); 00195 void Init_servo(); //サーボ初期化 00196 void Init_sbus(); //SBUS初期化 00197 void Init_sensors(); 00198 void DisplayClock(); //クロック状態確認 00199 00200 //センサの値取得 00201 void SensingMPU(); 00202 void UpdateDist(); 00203 00204 //void offsetRollPitch(float FirstROLL, float FirstPITCH); 00205 //void TransYaw(float FirstYAW); 00206 float TranslateNewYaw(float beforeYaw, float newzeroYaw); 00207 void UpdateTargetAngle(float targetAngle[3]); 00208 void CalculateControlValue(float targetAngle[3], float controlValue[3]); 00209 void UpdateAutoPWM(float controlValue[3]); 00210 void ConvertPWMintoRAD(float targetAngle[3]); 00211 inline float CalcRatio(float value, float trim, float limit); 00212 bool CheckSW_Up(Channel ch); 00213 int16_t ThresholdMaxMin(int16_t value, int16_t max, int16_t min); 00214 inline int16_t SetTHRinRatio(float ratio); 00215 00216 //sbus割り込み 00217 void Update_PWM(); //マニュアル・自動モードのpwmデータを整形しpwm変数に入力 00218 void Output_PWM(int16_t pwm[6]); //pwmをサーボへ出力 00219 00220 //シリアル割り込み 00221 void getSF_Serial(); 00222 float ConvertByteintoFloat(char high, char low); 00223 00224 00225 //SD設定 00226 int GetParameter(FILE *fp, const char *paramName,char parameter[]); 00227 int SetOptions(float *g_kpELE, float *g_kiELE, float *g_kdELE, 00228 float *g_kpRUD, float *g_kiRUD, float *g_kdRUD, 00229 float *g_rightloopROLL, float *g_rightloopPITCH, 00230 float *g_leftloopROLL, float *g_leftloopPITCH, 00231 float *g_gostraightROLL, float *g_gostraightPITCH, 00232 float *g_takeoffTHR, float *g_loopTHR, 00233 float *g_rightloopROLLshort, float *g_rightloopPITCHshort, 00234 float *g_leftloopROLLshort, float *g_leftloopPITCHshort, 00235 float *g_glideloopROLL, float *g_glideloopPITCH, 00236 float *g_kpAIL, float *g_kiAIL, float *g_kdAIL, 00237 int *g_rightloopRUD, int *g_rightloopshortRUD, 00238 int *g_leftloopRUD, int *g_leftloopshortRUD, 00239 int *g_glideRUD, 00240 int *g_AIL_L_correctionrightloop,int *g_AIL_L_correctionrightloopshort, 00241 int *g_AIL_L_correctionlefttloop,int *g_AIL_L_correctionleftloopshort 00242 ); 00243 //switch2割り込み 00244 void ResetTrim(); 00245 00246 //自動操縦 00247 void UpdateTargetAngle_GoStraight(float targetAngle[3]); 00248 void UpdateTargetAngle_GoStraight_zero(float targetAngle[3]); //着陸時にスロットルが0の時の直進 00249 void UpdateTargetAngle_Rightloop(float targetAngle[3]); 00250 void UpdateTargetAngle_Rightloop_short(float targetAngle[3]); 00251 void UpdateTargetAngle_Rightloop_zero(float targetAngle[3]); //着陸時にスロットルが0の時の右旋回 00252 void UpdateTargetAngle_Leftloop(float targetAngle[3]); 00253 void UpdateTargetAngle_Leftloop_short(float targetAngle[3]); 00254 void UpdateTargetAngle_Leftloop_zero(float targetAngle[3]); //着陸時にスロットルが0の時の左旋回 00255 void UpdateTargetAngle_Moebius(float targetAngle[3]); 00256 void UpdateTargetAngle_Glide(float targetAngle[3]); 00257 void UpdateTargetAngle_Takeoff(float targetAngle[3]); 00258 void UpdateTargetAngle_Approach(float targetAngle[3]); 00259 void Take_off_and_landing(float targetAngle[3]); 00260 00261 int Rotate(float targetAngle[3], float TargetYAW); 00262 00263 //投下 00264 void Chicken_Drop(); 00265 void ReturnChickenServo1(); 00266 void ReturnChickenServo2(); 00267 00268 //超音波による高度補正 00269 void checkHeight(float targetAngle[3]); 00270 void UpdateTargetAngle_NoseUP(float targetAngle[3]); 00271 void UpdateTargetAngle_NoseDOWN(float targetAngle[3]); 00272 00273 //デバッグ用 00274 void Sbusprintf(); 00275 void DebugPrint(); 00276 00277 /*---関数のプロトタイプ宣言終わり---*/ 00278 00279 int main() 00280 { 00281 setup(); 00282 00283 00284 while(1){ 00285 00286 loop(); 00287 00288 00289 //NVIC_DisableIRQ(USART1_IRQn); 00290 if(!CheckSW_Up(Ch7)){ 00291 led3=0; 00292 }else{ 00293 led3=1; 00294 } 00295 //NVIC_EnableIRQ(USART1_IRQn); 00296 } 00297 00298 } 00299 00300 void setup(){ 00301 //buzzer = 0; 00302 led1 = 1; 00303 led2 = 1; 00304 led3 = 1; 00305 led4 = 1; 00306 00307 SetOptions(&g_kpELE, &g_kiELE, &g_kdELE, 00308 &g_kpRUD, &g_kiRUD, &g_kdRUD, 00309 &g_rightloopROLL, &g_rightloopPITCH, 00310 &g_leftloopROLL, &g_leftloopPITCH, 00311 &g_gostraightROLL, &g_gostraightPITCH, 00312 &g_takeoffTHR, &g_loopTHR, 00313 &g_rightloopROLLshort, &g_rightloopPITCHshort, 00314 &g_leftloopROLLshort, &g_leftloopPITCHshort, 00315 &g_glideloopROLL, &g_glideloopPITCH, 00316 &g_kpAIL, &g_kiAIL,&g_kdAIL, 00317 &g_rightloopRUD, &g_rightloopshortRUD, 00318 &g_leftloopRUD, &g_leftloopshortRUD, 00319 &g_glideloopRUD, 00320 &g_AIL_L_correctionrightloop,&g_AIL_L_correctionrightloopshort, 00321 &g_AIL_L_correctionleftloop,&g_AIL_L_correctionleftloopshort 00322 ); 00323 00324 00325 Init_PWM(); 00326 Init_servo(); 00327 Init_sbus(); 00328 Init_sensors(); 00329 //switch2.rise(ResetTrim); 00330 00331 //USsensor.attach(&UpdateDist, 0.05); 00332 00333 NVIC_SetPriority(USART1_IRQn,0); 00334 NVIC_SetPriority(EXTI0_IRQn,1); 00335 NVIC_SetPriority(TIM5_IRQn,2); 00336 NVIC_SetPriority(EXTI9_5_IRQn,3); 00337 DisplayClock(); 00338 t.start(); 00339 00340 00341 pc.printf("MPU calibration start\r\n"); 00342 00343 float offsetstart = t.read(); 00344 while(t.read() - offsetstart < 26){ 00345 SensingMPU(); 00346 for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]); 00347 pc.printf("\r\n"); 00348 led1 = !led1; 00349 led2 = !led2; 00350 led3 = !led3; 00351 led4 = !led4; 00352 } 00353 00354 pc.attach(getSF_Serial, Serial::RxIrq); 00355 NVIC_SetPriority(USART2_IRQn,4); 00356 00357 FirstROLL = nowAngle[ROLL]; 00358 FirstPITCH = nowAngle[PITCH]; 00359 nowAngle[ROLL] -=FirstROLL; 00360 nowAngle[PITCH] -=FirstPITCH; 00361 00362 led1 = 0; 00363 led2 = 0; 00364 led3 = 0; 00365 led4 = 0; 00366 wait(0.2); 00367 00368 00369 pc.printf("All initialized\r\n"); 00370 } 00371 00372 void loop(){ 00373 static float targetAngle[3] = {0.0, 0.0, 0.0}, controlValue[2] = {0.0, 0.0}; 00374 00375 SensingMPU(); 00376 NVIC_DisableIRQ(USART2_IRQn); 00377 UpdateTargetAngle(targetAngle); 00378 //Rotate(targetAngle, 30.0); 00379 CalculateControlValue(targetAngle, controlValue); 00380 NVIC_DisableIRQ(USART1_IRQn); 00381 UpdateAutoPWM(controlValue); 00382 NVIC_EnableIRQ(USART1_IRQn); 00383 NVIC_EnableIRQ(USART2_IRQn); 00384 wait_ms(23); 00385 pc.printf("%c",g_landingcommand); 00386 #if DEBUG_PRINT_INLOOP 00387 //Sbusprintf(); 00388 DebugPrint(); 00389 #endif 00390 } 00391 00392 //サーボ初期化関数 00393 void Init_servo(){ 00394 00395 servo1.period_ms(14); 00396 servo1.pulsewidth_us(trimpwm[AIL_R]); 00397 00398 servo2.period_ms(14); 00399 servo2.pulsewidth_us(trimpwm[ELE]); 00400 00401 servo3.period_ms(14); 00402 servo3.pulsewidth_us(trimpwm[THR]); 00403 00404 servo4.period_ms(14); 00405 servo4.pulsewidth_us(trimpwm[RUD]); 00406 00407 servo5.period_ms(14); 00408 servo5.pulsewidth_us(1392); 00409 00410 servo6.period_ms(14); 00411 servo6.pulsewidth_us(trimpwm[AIL_L]); 00412 00413 pc.printf("servo initialized\r\n"); 00414 } 00415 00416 //Sbus初期化 00417 void Init_sbus(){ 00418 sbus.initialize(); 00419 sbus.setLastfuncPoint(Update_PWM); 00420 sbus.startInterrupt(); 00421 } 00422 00423 void Init_sensors(){ 00424 if(mpu6050.setup() == -1){ 00425 pc.printf("failed initialize\r\n"); 00426 while(1){ 00427 led1 = 1; led2 = 0; led3 = 1; led4 = 0; 00428 wait(1); 00429 led1 = 0; led2 = 1; led3 = 0; led4 = 1; 00430 wait(1); 00431 } 00432 } 00433 } 00434 00435 void Init_PWM(){ 00436 pc.printf("PWM initialized\r\n"); 00437 } 00438 00439 void DisplayClock(){ 00440 pc.printf("System Clock = %d[MHz]\r\n", HAL_RCC_GetSysClockFreq()/1000000); 00441 pc.printf("HCLK Clock = %d[MHz]\r\n", HAL_RCC_GetHCLKFreq()/1000000); 00442 pc.printf("PCLK1 Clock = %d[MHz]\r\n", HAL_RCC_GetPCLK1Freq()/1000000); 00443 pc.printf("PCLK2 Clock = %d[MHz]\r\n", HAL_RCC_GetPCLK2Freq()/1000000); 00444 pc.printf("\r\n"); 00445 } 00446 00447 void UpdateTargetAngle(float targetAngle[3]){ 00448 static int16_t count_op = 0; 00449 #if DEBUG_SEMIAUTO 00450 switch(operation_mode){ 00451 case StartUp: 00452 if(!CheckSW_Up(Ch7) && CheckSW_Up(Ch8)){ 00453 count_op++; 00454 if(count_op > changeModeCount){ 00455 operation_mode = SemiAuto; 00456 pc.printf("Goto SemiAuto mode\r\n"); 00457 count_op = 0; 00458 } 00459 }else count_op = 0; 00460 break; 00461 00462 case SemiAuto: 00463 /* 大会用では以下のif文を入れてoperation_modeを変える 00464 if(CheckSW_Up(Ch6)){ 00465 count_op++; 00466 if(count_op>changeModeCount){ 00467 output_status = XXX; 00468 led2 = 0; 00469 pc.printf("Goto XXX mode\r\n"); 00470 count_op = 0; 00471 }else count_op = 0; 00472 ConvertPWMintoRAD(targetAngle); 00473 } 00474 */ 00475 ConvertPWMintoRAD(targetAngle); 00476 break; 00477 00478 default: 00479 operation_mode = SemiAuto; 00480 break; 00481 } 00482 00483 #else 00484 00485 switch(operation_mode){ 00486 case StartUp: 00487 if(!CheckSW_Up(Ch7) && !CheckSW_Up(Ch8)){ //ch7;自動・手動切り替え ch8;自動操縦モード切替 00488 count_op++; 00489 if(count_op > changeModeCount){ 00490 operation_mode = RightLoop; 00491 pc.printf("Goto RightLoop mode\r\n"); 00492 count_op = 0; 00493 } 00494 }else count_op = 0; 00495 break; 00496 00497 case RightLoop: 00498 if(!CheckSW_Up(Ch7) && CheckSW_Up(Ch8)){ 00499 count_op++; 00500 if(count_op > changeModeCount){ 00501 operation_mode = LeftLoop; 00502 pc.printf("Goto LeftLoop mode\r\n"); 00503 count_op = 0; 00504 } 00505 }else count_op = 0; 00506 UpdateTargetAngle_Rightloop(targetAngle); 00507 00508 break; 00509 00510 case LeftLoop: 00511 if(!CheckSW_Up(Ch7) && !CheckSW_Up(Ch8)){ 00512 count_op++; 00513 if(count_op > changeModeCount){ 00514 operation_mode = GoStraight; 00515 pc.printf("Goto GoStraight mode\r\n"); 00516 count_op = 0; 00517 } 00518 }else count_op = 0; 00519 UpdateTargetAngle_Leftloop(targetAngle); 00520 break; 00521 00522 case GoStraight: 00523 if(!CheckSW_Up(Ch7) && CheckSW_Up(Ch8)){ 00524 count_op++; 00525 if(count_op > changeModeCount){ 00526 operation_mode = Moebius; 00527 pc.printf("Goto Moebius mode\r\n"); 00528 count_op = 0; 00529 } 00530 }else count_op = 0; 00531 UpdateTargetAngle_GoStraight(targetAngle); 00532 break; 00533 00534 case Moebius: 00535 if(!CheckSW_Up(Ch7) && !CheckSW_Up(Ch8)){ 00536 count_op++; 00537 if(count_op > changeModeCount){ 00538 operation_mode = Glide; 00539 pc.printf("Goto Glide mode\r\n"); 00540 count_op = 0; 00541 } 00542 }else count_op = 0; 00543 UpdateTargetAngle_Moebius(targetAngle); 00544 break; 00545 00546 case Glide: 00547 if(!CheckSW_Up(Ch7) && CheckSW_Up(Ch8)){ 00548 count_op++; 00549 if(count_op > changeModeCount){ 00550 operation_mode = BombwithPC; 00551 pc.printf("Goto Bombing mode\r\n"); 00552 count_op = 0; 00553 } 00554 }else count_op = 0; 00555 UpdateTargetAngle_Glide(targetAngle); 00556 break; 00557 00558 case BombwithPC: 00559 if(!CheckSW_Up(Ch7) && !CheckSW_Up(Ch8)){ 00560 count_op++; 00561 if(count_op > changeModeCount){ 00562 operation_mode = RightLoop; 00563 pc.printf("Goto RightLoop mode\r\n"); 00564 count_op = 0; 00565 } 00566 }else count_op = 0; 00567 Take_off_and_landing(targetAngle); 00568 break; 00569 00570 default: 00571 operation_mode = StartUp; 00572 break; 00573 } 00574 #endif 00575 00576 } 00577 00578 int GetParameter(FILE *fp, const char *paramName,char parameter[]){ 00579 int i=0, j=0; 00580 int strmax = 200; 00581 char str[strmax]; 00582 00583 rewind(fp); //ファイル位置を先頭に 00584 while(1){ 00585 if (fgets(str, strmax, fp) == NULL) { 00586 return 0; 00587 } 00588 if (!strncmp(str, paramName, strlen(paramName))) { 00589 while (str[i++] != '=') {} 00590 while (str[i] != '\n') { 00591 parameter[j++] = str[i++]; 00592 } 00593 parameter[j] = '\0'; 00594 return 1; 00595 } 00596 } 00597 return 0; 00598 } 00599 00600 00601 //sdによる設定 00602 int SetOptions(float *g_kpELE, float *g_kiELE, float *g_kdELE, 00603 float *g_kpRUD, float *g_kiRUD, float *g_kdRUD, 00604 float *g_rightloopROLL, float *g_rightloopPITCH, 00605 float *g_leftloopROLL, float *g_leftloopPITCH, 00606 float *g_gostraightROLL, float *g_gostraightPITCH, 00607 float *g_takeoffTHR, float *g_loopTHR, 00608 float *g_rightloopROLLshort, float *g_rightloopPITCHshort, 00609 float *g_leftloopROLLshort, float *g_leftloopPITCHshort, 00610 float *g_glideloopROLL, float *g_glideloopPITCH, 00611 float *g_kpAIL, float *g_kiAIL, float *g_kdAIL, 00612 int *g_rightloopRUD, int *g_rightloopshortRUD, 00613 int *g_leftloopRUD, int *g_leftloopshortRUD, 00614 int *g_glideloopRUD, 00615 int *g_AIL_L_correctionrightloop,int *g_AIL_L_correctionrightloopshort, 00616 int *g_AIL_L_correctionleftloop,int *g_AIL_L_correctionleftloopshort 00617 ){ 00618 00619 pc.printf("SDsetup start.\r\n"); 00620 00621 FILE *fp; 00622 char parameter[40]; //文字列渡す用の配列 00623 int SDerrorcount = 0; //取得できなかった数を返す 00624 const char *paramNames[] = { 00625 "KP_ELEVATOR", 00626 "KI_ELEVATOR", 00627 "KD_ELEVATOR", 00628 "KP_RUDDER", 00629 "KI_RUDDER", 00630 "KD_RUDDER", 00631 "RIGHTLOOP_ROLL", 00632 "RIGHTLOOP_PITCH", 00633 "LEFTLOOP_ROLL", 00634 "LEFTLOOP_PITCH", 00635 "GOSTRAIGHT_ROLL", 00636 "GOSTRAIGHT_PITCH", 00637 "TAKEOFF_THR_RATE", 00638 "LOOP_THR_RATE", 00639 "RIGHTLOOP_ROLL_SHORT", 00640 "RIGHTLOOP_PITCH_SHORT", 00641 "LEFTLOOP_ROLL_SHORT", 00642 "LEFTLOOP_PITCH_SHORT", 00643 "AUTOGLIDE_ROLL", 00644 "AUTOGLIDE PITCH", 00645 "KP_AILERON", 00646 "KI_AILERON", 00647 "KD_AILERON", 00648 "RIGHTLOOP_RUDDER", 00649 "RIGHTLOOPSHORT_RUDDER", 00650 "LEFTLOOP_RUDDER", 00651 "LEFTLOOPSHORT_RUDDER", 00652 "GLIDELOOP_RUDDER", 00653 "AILERON_LEFT_CORRECTION_RIGHTLOOP", 00654 "AILERON_LEFT_CORRECTION_RIGHTLOOPSHORT", 00655 "AILERON_LEFT_CORRECTION_LEFTLOOP", 00656 "AILERON_LEFT_CORRECTION_LEFTLOOPSHORT" 00657 }; 00658 00659 fp = fopen("/sd/option.txt","r"); 00660 00661 if(fp != NULL){ //開けたら 00662 pc.printf("File was openned.\r\n"); 00663 if(GetParameter(fp,paramNames[0],parameter)) *g_kpELE = atof(parameter); 00664 else{ *g_kpELE = KP_ELE; 00665 SDerrorcount++; 00666 } 00667 if(GetParameter(fp,paramNames[1],parameter)) *g_kiELE = atof(parameter); 00668 else{ *g_kiELE = KI_ELE; 00669 SDerrorcount++; 00670 } 00671 if(GetParameter(fp,paramNames[2],parameter)) *g_kdELE = atof(parameter); 00672 else{ *g_kdELE = KD_ELE; 00673 SDerrorcount++; 00674 } 00675 if(GetParameter(fp,paramNames[3],parameter)) *g_kpRUD = atof(parameter); 00676 else{ *g_kpRUD = KP_RUD; 00677 SDerrorcount++; 00678 } 00679 if(GetParameter(fp,paramNames[4],parameter)) *g_kiRUD = atof(parameter); 00680 else{ *g_kiRUD = KI_RUD; 00681 SDerrorcount++; 00682 } 00683 if(GetParameter(fp,paramNames[5],parameter)) *g_kdRUD = atof(parameter); 00684 else{ *g_kdRUD = KD_RUD; 00685 SDerrorcount++; 00686 } 00687 if(GetParameter(fp,paramNames[6],parameter)) *g_rightloopROLL = atof(parameter); 00688 else{ *g_rightloopROLL = RIGHT_ROLL; 00689 SDerrorcount++; 00690 } 00691 if(GetParameter(fp,paramNames[7],parameter)) *g_rightloopPITCH = atof(parameter); 00692 else{ *g_rightloopPITCH = RIGHT_PITCH; 00693 SDerrorcount++; 00694 } 00695 if(GetParameter(fp,paramNames[8],parameter)) *g_leftloopROLL = atof(parameter); 00696 else{ *g_leftloopROLL = LEFT_ROLL; 00697 SDerrorcount++; 00698 } 00699 if(GetParameter(fp,paramNames[9],parameter)) *g_leftloopPITCH = atof(parameter); 00700 else{ *g_leftloopPITCH = LEFT_PITCH; 00701 SDerrorcount++; 00702 } 00703 if(GetParameter(fp,paramNames[10],parameter)) *g_gostraightROLL = atof(parameter); 00704 else{ *g_gostraightROLL = STRAIGHT_ROLL; 00705 SDerrorcount++; 00706 } 00707 if(GetParameter(fp,paramNames[11],parameter)) *g_gostraightPITCH = atof(parameter); 00708 else{ *g_gostraightPITCH = STRAIGHT_PITCH; 00709 SDerrorcount++; 00710 } 00711 if(GetParameter(fp,paramNames[12],parameter)) *g_takeoffTHR = atof(parameter); 00712 else{ *g_takeoffTHR = TAKEOFF_THR; 00713 SDerrorcount++; 00714 } 00715 if(GetParameter(fp,paramNames[13],parameter)) *g_loopTHR = atof(parameter); 00716 else{ *g_loopTHR = LOOP_THR; 00717 SDerrorcount++; 00718 } 00719 if(GetParameter(fp,paramNames[14],parameter)) *g_rightloopROLLshort = atof(parameter); 00720 else{ *g_rightloopROLLshort = RIGHT_ROLL_SHORT; 00721 SDerrorcount++; 00722 } 00723 if(GetParameter(fp,paramNames[15],parameter)) *g_rightloopPITCHshort = atof(parameter); 00724 else{ *g_rightloopPITCHshort = RIGHT_PITCH_SHORT; 00725 SDerrorcount++; 00726 } 00727 if(GetParameter(fp,paramNames[16],parameter)) *g_leftloopROLLshort = atof(parameter); 00728 else{ *g_leftloopROLLshort = LEFT_ROLL_SHORT; 00729 SDerrorcount++; 00730 } 00731 if(GetParameter(fp,paramNames[17],parameter)) *g_leftloopPITCHshort = atof(parameter); 00732 else{ *g_leftloopPITCHshort = LEFT_PITCH_SHORT; 00733 SDerrorcount++; 00734 } 00735 if(GetParameter(fp,paramNames[18],parameter)) *g_glideloopROLL = atof(parameter); 00736 else{ *g_glideloopROLL = GLIDE_ROLL; 00737 SDerrorcount++; 00738 } 00739 if(GetParameter(fp,paramNames[19],parameter)) *g_glideloopPITCH = atof(parameter); 00740 else{ *g_glideloopPITCH = GLIDE_PITCH; 00741 SDerrorcount++; 00742 } 00743 if(GetParameter(fp,paramNames[20],parameter)) *g_kpAIL = atof(parameter); 00744 else{ *g_kpAIL = KP_AIL; 00745 SDerrorcount++; 00746 } 00747 if(GetParameter(fp,paramNames[21],parameter)) *g_kiAIL = atof(parameter); 00748 else{ *g_kiAIL = KI_AIL; 00749 SDerrorcount++; 00750 } 00751 if(GetParameter(fp,paramNames[22],parameter)) *g_kdAIL = atof(parameter); 00752 else{ *g_kdAIL = KP_AIL; 00753 SDerrorcount++; 00754 } 00755 if(GetParameter(fp,paramNames[23],parameter)) *g_rightloopRUD = atof(parameter); 00756 else{ *g_rightloopRUD = RIGHTLOOP_RUD; 00757 SDerrorcount++; 00758 } 00759 if(GetParameter(fp,paramNames[24],parameter)) *g_rightloopshortRUD = atof(parameter); 00760 else{ *g_rightloopshortRUD = RIGHTLOOPSHORT_RUD; 00761 SDerrorcount++; 00762 } 00763 if(GetParameter(fp,paramNames[25],parameter)) *g_leftloopRUD = atof(parameter); 00764 else{ *g_leftloopshortRUD = LEFTLOOP_RUD; 00765 SDerrorcount++; 00766 } 00767 if(GetParameter(fp,paramNames[26],parameter)) *g_leftloopshortRUD = atof(parameter); 00768 else{ *g_leftloopshortRUD = LEFTLOOPSHORT_RUD; 00769 SDerrorcount++; 00770 } 00771 if(GetParameter(fp,paramNames[27],parameter)) *g_glideloopRUD = atof(parameter); 00772 else{ *g_glideloopRUD = GLIDELOOP_RUD; 00773 SDerrorcount++; 00774 } 00775 if(GetParameter(fp,paramNames[28],parameter)) *g_AIL_L_correctionrightloop = atof(parameter); 00776 else{ *g_AIL_L_correctionrightloop = AIL_L_CORRECTION_RIGHTLOOP; 00777 SDerrorcount++; 00778 } 00779 if(GetParameter(fp,paramNames[29],parameter)) *g_AIL_L_correctionrightloopshort = atof(parameter); 00780 else{ *g_AIL_L_correctionrightloopshort = AIL_L_CORRECTION_RIGHTLOOPSHORT; 00781 SDerrorcount++; 00782 } 00783 if(GetParameter(fp,paramNames[30],parameter)) *g_AIL_L_correctionleftloop = atof(parameter); 00784 else{ *g_AIL_L_correctionleftloop = AIL_L_CORRECTION_LEFTLOOP; 00785 SDerrorcount++; 00786 } 00787 if(GetParameter(fp,paramNames[31],parameter)) *g_AIL_L_correctionleftloopshort = atof(parameter); 00788 else{ *g_AIL_L_correctionleftloopshort = AIL_L_CORRECTION_LEFTLOOPSHORT; 00789 SDerrorcount++; 00790 } 00791 00792 fclose(fp); 00793 00794 }else{ //ファイルがなかったら 00795 pc.printf("fp was null.\r\n"); 00796 *g_kpELE = KP_ELE; 00797 *g_kiELE = KI_ELE; 00798 *g_kdELE = KD_ELE; 00799 *g_kpRUD = KP_RUD; 00800 *g_kiRUD = KI_RUD; 00801 *g_kdRUD = KD_RUD; 00802 *g_rightloopROLL = RIGHT_ROLL; 00803 *g_rightloopPITCH = RIGHT_PITCH; 00804 *g_leftloopROLL = LEFT_ROLL; 00805 *g_leftloopPITCH = LEFT_PITCH; 00806 *g_gostraightROLL = STRAIGHT_ROLL; 00807 *g_gostraightPITCH = STRAIGHT_PITCH; 00808 *g_takeoffTHR = TAKEOFF_THR; 00809 *g_loopTHR = LOOP_THR; 00810 *g_kpAIL = KP_AIL; //パラメータ変えるのお忘れなく!! 00811 *g_kiAIL = KI_AIL; 00812 *g_kdAIL = KD_AIL; 00813 *g_rightloopRUD = RIGHTLOOP_RUD; 00814 *g_rightloopshortRUD = RIGHTLOOPSHORT_RUD; 00815 *g_leftloopRUD = LEFTLOOP_RUD; 00816 *g_leftloopshortRUD = LEFTLOOPSHORT_RUD; 00817 *g_glideloopRUD = GLIDELOOP_RUD; 00818 *g_AIL_L_correctionrightloop = AIL_L_CORRECTION_RIGHTLOOP; 00819 *g_AIL_L_correctionrightloopshort = AIL_L_CORRECTION_RIGHTLOOPSHORT; 00820 *g_AIL_L_correctionleftloop = AIL_L_CORRECTION_LEFTLOOP; 00821 *g_AIL_L_correctionleftloopshort = AIL_L_CORRECTION_LEFTLOOPSHORT; 00822 00823 00824 SDerrorcount = -1; 00825 } 00826 pc.printf("SDsetup finished.\r\n"); 00827 if(SDerrorcount == 0) pc.printf("setting option is success\r\n"); 00828 else if(SDerrorcount == -1) pc.printf("ERROR 1. cannot open option\r\n"); 00829 else if(SDerrorcount > 0) pc.printf("ERROR 2. reading parameter is failed[%d]\r\n",SDerrorcount); 00830 00831 pc.printf("kpELE = %f, kiELE = %f, kdELE = %f\r\n", *g_kpRUD, *g_kiRUD, *g_kdRUD); 00832 pc.printf("kpRUD = %f, kiRUD = %f, kdRUD = %f\r\n", *g_kpELE, *g_kiELE, *g_kdELE); 00833 pc.printf("rightloopROLL = %f, rightloopPITCH = %f\r\n", *g_rightloopROLL, *g_rightloopPITCH); 00834 pc.printf("leftloopROLL = %f, g_leftloopPITCH = %f\r\n", *g_leftloopROLL, *g_leftloopPITCH); 00835 pc.printf("gostraightROLL = %f, g_gostraightPITCH = %f\r\n", *g_gostraightROLL, *g_gostraightPITCH); 00836 pc.printf("g_takeoffTHR = %f, g_loopTHR = %f\r\n", *g_takeoffTHR, *g_loopTHR); 00837 pc.printf("rightloopROLLshort = %f, rightloopPITCHshort = %f\r\n", *g_rightloopROLLshort, *g_rightloopPITCHshort); 00838 pc.printf("leftloopROLLshort = %f, g_leftloopPITCHshort = %f\r\n", *g_leftloopROLLshort, *g_leftloopPITCHshort); 00839 pc.printf("glideROLL = %f, glidePITCH = %f\r\n", *g_glideloopROLL, *g_glideloopPITCH); 00840 pc.printf("kpAIL = %f,kiAIL = %f,kdAIL = %f\r\n ",*g_kpAIL,*g_kiAIL,*g_kdAIL); 00841 pc.printf("RIGHTLOOPRUD = %d,RIGHTLOOPSHORTRUD = %d\r\n",*g_rightloopRUD,*g_rightloopshortRUD); 00842 pc.printf("LEFTTLOOPRUD = %d,LEFTLOOPSHORTRUD = %d\r\n",*g_leftloopRUD,*g_leftloopshortRUD); 00843 pc.printf("GLIDELOOPRUD = %d\r\n",*g_glideloopRUD); 00844 pc.printf("AIL_L_CORRECTION_RIGHTLOOP = %d, AIL_L_CORRECTION_RIGHTLOOPSHORT = %d\r\n",*g_AIL_L_correctionrightloop,*g_AIL_L_correctionrightloopshort); 00845 pc.printf("AIL_L_CORRECTION_LEFTLOOP = %d, AIL_L_CORRECTION_LEFTLOOPSHORT = %d\r\n",*g_AIL_L_correctionleftloop,*g_AIL_L_correctionleftloopshort); 00846 return SDerrorcount; 00847 } 00848 00849 void CalculateControlValue(float targetAngle[3], float controlValue[3]){ 00850 static int t_last; 00851 int t_now; 00852 float dt; 00853 00854 t_now = t.read_us(); 00855 dt = (float)((t_now - t_last)/1000000.0f) ; 00856 t_last = t_now; 00857 00858 00859 //controlValue[ROLL] = pid_RUD.calcPID(nowAngle[ROLL], targetAngle[ROLL], dt); 00860 controlValue[ROLL] = pid_AIL.calcPID(nowAngle[ROLL], targetAngle[ROLL], dt); //エルロンでロール制御 00861 controlValue[PITCH] = pid_ELE.calcPID(nowAngle[PITCH], targetAngle[PITCH], dt); 00862 } 00863 00864 void UpdateAutoPWM(float controlValue[3]){ 00865 int16_t addpwm[2]; //-500~500 00866 addpwm[PITCH] = (float)GAIN_CONTROLVALUE_TO_PWM * controlValue[PITCH]; //センサ:機首下げ正 レバー:機首上げ正 00867 addpwm[ROLL] = (float)GAIN_CONTROLVALUE_TO_PWM * controlValue[ROLL]; //センサ:右回転正(8月13日時点;左回転が正!) レバー:右回転正 00868 00869 autopwm[ELE] = trimpwm[ELE] + reverce[ELE] * addpwm[PITCH]; //rewrite 00870 autopwm[AIL_R] = trimpwm[AIL_R] - reverce[AIL_R] * addpwm[ROLL]; 00871 //autopwm[THR] = oldTHR; 00872 00873 autopwm[ELE] = ThresholdMaxMin(autopwm[ELE], maxpwm[ELE], minpwm[ELE]); 00874 autopwm[AIL_R] = ThresholdMaxMin(autopwm[AIL_R], maxpwm[AIL_R], minpwm[AIL_R]); 00875 00876 } 00877 00878 inline float CalcRatio(float value, float trim, float limit){ 00879 return (value - trim) / (limit - trim); 00880 } 00881 00882 bool CheckSW_Up(Channel ch){ 00883 NVIC_DisableIRQ(USART1_IRQn); 00884 if(SWITCH_CHECK < sbus.manualpwm[ch]){ 00885 return true; 00886 }else{ 00887 return false; 00888 } 00889 NVIC_EnableIRQ(USART1_IRQn); 00890 } 00891 00892 int16_t ThresholdMaxMin(int16_t value, int16_t max, int16_t min){ 00893 if(value > max) return max; 00894 if(value < min) return min; 00895 return value; 00896 } 00897 00898 inline int16_t SetTHRinRatio(float ratio){ 00899 return minpwm[THR] + (int16_t)(2 * lengthdivpwm * ratio); 00900 } 00901 00902 00903 00904 /*---SBUS割り込み処理---*/ 00905 00906 //udate_Inputで抽出したpwmデータを整形して各変数に代入する。(マニュアルモード) 00907 //各stabiGylo関数で算出したpwmを各変数に代入する(自動モード) 00908 void Update_PWM() 00909 { 00910 static int16_t pwm[6]; 00911 static int16_t temppwm[6]={trimpwm[0],trimpwm[1],trimpwm[2],trimpwm[3],trimpwm[4],trimpwm[5]}; 00912 if(sbus.flg_ch_update == true){ 00913 NVIC_DisableIRQ(USART1_IRQn); 00914 switch(output_status){ //マニュアルモード,自動モード,自動着陸もモードを切替 00915 case Manual: 00916 for(uint8_t i=0;i<6;i++){ 00917 pwm[i] = sbus.manualpwm[i]; 00918 } 00919 /*pc.printf("%d ,",pwm[0]);//R 00920 pc.printf("%d ,\r\n",pwm[5]);//L*/ 00921 oldTHR = sbus.manualpwm[THR]; 00922 //pc.printf("update_manual\r\n"); 00923 break; 00924 00925 case Auto: 00926 pwm[AIL_R] = autopwm[AIL_R]; //sbus.manualpwm[AIL]; 00927 pwm[ELE] = autopwm[ELE]; 00928 pwm[THR] = autopwm[THR]; 00929 pwm[RUD] = autopwm[RUD]; 00930 pwm[DROP] = autopwm[DROP]; 00931 pwm[AIL_L] = autopwm[AIL_L]; 00932 //pc.printf("%d ,",pwm[AIL_R]);//R 00933 //pc.printf("%d ,\r\n",pwm[AIL_L]);//L 00934 //pc.printf("update_auto\r\n"); 00935 00936 break; 00937 00938 default: 00939 for(uint8_t i=0;i<6;i++){ 00940 pwm[i] = sbus.manualpwm[i]; 00941 } //pc.printf("update_manual\r\n"); 00942 break; 00943 NVIC_EnableIRQ(USART1_IRQn); 00944 } 00945 for(uint8_t i=0;i<6;i++){ 00946 if(pwm[i]<1000 || pwm[i]>2000) pwm[i]=temppwm[i]; 00947 temppwm[i]=pwm[i]; 00948 } 00949 }else{ 00950 pc.printf("0\r\n"); 00951 } 00952 sbus.flg_ch_update = false; 00953 Output_PWM(pwm); 00954 } 00955 00956 00957 //pwmをサーボに出力。 00958 void Output_PWM(int16_t pwm[5]) 00959 { 00960 NVIC_DisableIRQ(USART1_IRQn); 00961 servo1.pulsewidth_us(pwm[0]); 00962 servo2.pulsewidth_us(pwm[1]); 00963 servo3.pulsewidth_us(pwm[2]); 00964 servo4.pulsewidth_us(pwm[3]); 00965 servo5.pulsewidth_us(pwm[4]); 00966 servo6.pulsewidth_us(pwm[5]); 00967 NVIC_EnableIRQ(USART1_IRQn); 00968 00969 } 00970 00971 void ResetTrim(){ 00972 for(uint8_t i=0; i<6; i++){ //i=4から書き換え_投下サーボは入ってない模様 00973 trimpwm[i] = sbus.manualpwm[i]; 00974 } 00975 pc.printf("reset PWM trim\r\n"); 00976 } 00977 00978 00979 void SensingMPU(){ 00980 //static int16_t deltaT = 0, t_start = 0; 00981 //t_start = t.read_us(); 00982 00983 float rpy[3] = {0}, oldrpy[3] = {0}; 00984 static uint16_t count_changeRPY = 0; 00985 static bool flg_checkoutlier = false; 00986 NVIC_DisableIRQ(USART1_IRQn); 00987 NVIC_DisableIRQ(USART2_IRQn); 00988 NVIC_DisableIRQ(TIM5_IRQn); 00989 NVIC_DisableIRQ(EXTI0_IRQn);//MPU割り込み禁止 00990 NVIC_DisableIRQ(EXTI9_5_IRQn);//超音波割り込み禁止 00991 00992 mpu6050.getRollPitchYaw_Skipper(rpy); 00993 00994 NVIC_EnableIRQ(USART1_IRQn); 00995 NVIC_EnableIRQ(USART2_IRQn); 00996 NVIC_EnableIRQ(TIM5_IRQn); 00997 NVIC_EnableIRQ(EXTI0_IRQn); 00998 NVIC_EnableIRQ(EXTI9_5_IRQn); 00999 01000 01001 //外れ値対策 01002 for(uint8_t i=0; i<3; i++) rpy[i] *= 180.0f/PI; 01003 rpy[ROLL] -= FirstROLL; 01004 rpy[PITCH] -= FirstPITCH; 01005 rpy[YAW] -= FirstYAW; 01006 01007 for(uint8_t i=0; i<3; i++) {if(rpy[i] < nowAngle[i]-10 || rpy[i] > nowAngle[i]+10) {flg_checkoutlier = true;}} 01008 if(!flg_checkoutlier || count_changeRPY >= 2){ 01009 for(uint8_t i=0; i<3; i++){ 01010 nowAngle[i] = (rpy[i] + nowAngle[i])/2.0f; //2つの移動平均 01011 } 01012 count_changeRPY = 0; 01013 }else count_changeRPY++; 01014 flg_checkoutlier = false; 01015 01016 } 01017 01018 float TranslateNewYaw(float beforeYaw, float newzeroYaw){ 01019 float newYaw = beforeYaw - newzeroYaw; 01020 01021 if(newYaw<-180.0f) newYaw += 360.0f; 01022 else if(newYaw>180.0f) newYaw -= 360.0f; 01023 return newYaw; 01024 } 01025 01026 01027 void getSF_Serial(){ 01028 01029 static char SFbuf[16]; 01030 static int bufcounter=0; 01031 01032 if(pc.readable()) { // 受信確認 01033 SFbuf[bufcounter] = pc.getc(); // 1文字取り出し 01034 } 01035 01036 01037 pc.printf("%c",SFbuf[bufcounter]); 01038 01039 if(SFbuf[0]=='S'&&bufcounter<5) bufcounter++; 01040 01041 if(bufcounter==5 && SFbuf[4]=='F'){ 01042 g_landingcommand = SFbuf[1]; 01043 wait_ms(20); 01044 if(g_landingcommand=='Y')g_SerialTargetYAW = ConvertByteintoFloat(SFbuf[2], SFbuf[3]); 01045 bufcounter = 0; 01046 memset(SFbuf, 0, strlen(SFbuf)); 01047 //pc.printf("command = %c, commandYAW = %f\r\n", g_landingcommand, g_SerialTargetYAW); 01048 } 01049 else if(bufcounter>=5 ){ 01050 pc.printf("Communication Falsed.\r\n"); 01051 bufcounter = 0; 01052 } 01053 01054 } 01055 01056 float ConvertByteintoFloat(char high, char low){ 01057 01058 //int16_t intvalue = (int16_t)high*256 + (int16_t)low; 01059 int16_t intvalue = (int16_t)(((int16_t)high << 8) | low); // Turn the MSB and LSB into a signed 16-bit value 01060 float floatvalue = (float)intvalue; 01061 return floatvalue; 01062 } 01063 01064 01065 //超音波割り込み 01066 /*void UpdateDist(){ 01067 g_distance = usensor.get_dist_cm(); 01068 usensor.start(); 01069 }*/ 01070 01071 //8の字旋回 01072 void UpdateTargetAngle_Moebius(float targetAngle[3]){ 01073 static uint8_t RotateCounter=0; 01074 static bool flg_setInStartAuto = false; 01075 static float FirstYAW_Moebius = 0.0; 01076 float newYaw_Moebius; 01077 01078 if(!flg_setInStartAuto && CheckSW_Up(Ch7)){ 01079 FirstYAW_Moebius = nowAngle[YAW]; 01080 RotateCounter = 0; 01081 flg_setInStartAuto = true; 01082 }else if(!CheckSW_Up(Ch7)){ 01083 flg_setInStartAuto = false; 01084 led2 = 0; 01085 } 01086 autopwm[THR]=oldTHR; 01087 01088 newYaw_Moebius = TranslateNewYaw(nowAngle[YAW], FirstYAW_Moebius); 01089 01090 if(RotateCounter == 0 && newYaw_Moebius >90.0 && newYaw_Moebius < 180.0) {RotateCounter++; led2 = 1; pc.printf("Rotate 90\r\n");} 01091 if(RotateCounter == 1 && newYaw_Moebius >-180.0 && newYaw_Moebius < -90.0) {RotateCounter++; led2 = 0; pc.printf("Rotate 180\r\n");} 01092 if(RotateCounter == 2 && newYaw_Moebius >-90.0 && newYaw_Moebius <-10.0) {RotateCounter++; led2 = 1; pc.printf("Rotate 270\r\n");} 01093 if(RotateCounter == 3 && newYaw_Moebius >0.0 && newYaw_Moebius < 90.0) {RotateCounter++; led2 = 0; pc.printf("Change Rotate direction\r\n");} 01094 01095 01096 if(RotateCounter <= 3) UpdateTargetAngle_Rightloop_short(targetAngle); 01097 else UpdateTargetAngle_Leftloop_short(targetAngle); //左旋回 01098 01099 } 01100 01101 //自動滑空 01102 void UpdateTargetAngle_Glide(float targetAngle[3]){ 01103 static int THRcount = 0; 01104 static int t_start = 0; 01105 static bool flg_tstart = false; 01106 int t_diff = 0; 01107 static int groundcount = 0; 01108 01109 targetAngle[ROLL] = g_glideloopROLL; 01110 targetAngle[PITCH] = g_glideloopPITCH; 01111 01112 autopwm[RUD]=g_glideloopRUD; 01113 // autopwm[THR]=oldTHR; 01114 01115 01116 01117 //時間計測開始設定 01118 if(!flg_tstart && CheckSW_Up(Ch7)){ 01119 t_start = t.read(); 01120 flg_tstart = true; 01121 pc.printf("timer start\r\n"); 01122 }else if(!CheckSW_Up(Ch7)){ 01123 t_start = 0; 01124 flg_tstart = false; 01125 } 01126 01127 01128 //フラグが偽であれば計測は行わない 01129 if(flg_tstart){ 01130 t_diff = t.read() - t_start; 01131 //一定高度or15秒でled点灯 01132 NVIC_DisableIRQ(EXTI9_5_IRQn); 01133 if((groundcount>5 && g_distance>0) || t_diff > 15){ 01134 led2 = 1; 01135 //pc.printf("Call [Stop!] calling!\r\n"); 01136 } 01137 01138 if(g_distance<180 && g_distance > 0) groundcount++; 01139 NVIC_EnableIRQ(EXTI9_5_IRQn); 01140 }else{ 01141 t_diff = 0; 01142 groundcount = 0; 01143 led2 = 0; 01144 } 01145 01146 if(t_diff > 17){ 01147 autopwm[THR] = SetTHRinRatio(0.5); 01148 }else{ 01149 NVIC_DisableIRQ(EXTI9_5_IRQn); 01150 if(g_distance<150 && g_distance>0 ){ 01151 NVIC_EnableIRQ(EXTI9_5_IRQn); 01152 THRcount++; 01153 if(THRcount>5){ 01154 autopwm[THR] = SetTHRinRatio(0.6); 01155 //pc.printf("throttle ON\r\n"); 01156 } 01157 }else{ 01158 autopwm[THR] = 1180; 01159 THRcount = 0; 01160 } 01161 } 01162 } 01163 //離陸-投下-着陸一連 01164 void Take_off_and_landing(float targetAngle[3]){ 01165 /* 01166 if(!CheckSW_Up(Ch7)) bombing_mode = Takeoff; 01167 01168 switch(bombing_mode){ 01169 case Takeoff: 01170 static bool flg_setFirstYaw = false; 01171 static int TakeoffCount = 0; 01172 01173 if(!flg_setFirstYaw && CheckSW_Up(Ch7)){ 01174 FirstYAW = nowAngle[YAW]; 01175 flg_setFirstYaw = true; 01176 }else if(flg_setFirstYaw && !CheckSW_Up(Ch7)){ 01177 flg_setFirstYaw = false; 01178 } 01179 01180 UpdateTargetAngle_Takeoff(targetAngle); 01181 01182 if(g_distance>150) TakeoffCount++; 01183 else TakeoffCount = 0; 01184 if(TakeoffCount>5){ 01185 autopwm[THR] = 1180+320*2*0.5; 01186 pc.printf("Now go to Approach mode!!"); 01187 bombing_mode = Approach; 01188 } 01189 break; 01190 01191 //case Chicken: 01192 //break; 01193 01194 case Transition: 01195 static int ApproachCount = 0; 01196 targetAngle[YAW]=180.0; 01197 int Judge = Rotate(targetAngle, g_SerialTargetYAW); 01198 01199 if(Judge==0) ApproachCount++; 01200 if(ApproachCount>5) bombing_mode = Approach; 01201 break; 01202 01203 case Approach: 01204 */ 01205 autopwm[THR] = SetTHRinRatio(g_loopTHR); //0.7;スロットルの割合 01206 UpdateTargetAngle_Approach(targetAngle); 01207 /* 01208 break; 01209 01210 default: 01211 bombing_mode = Takeoff; 01212 break; 01213 } 01214 */ 01215 } 01216 01217 //離陸モード 01218 void UpdateTargetAngle_Takeoff(float targetAngle[3]){ 01219 //pc.printf("%d \r\n",g_distance); 01220 targetAngle[ROLL] = g_gostraightROLL; 01221 targetAngle[PITCH] = g_loopTHR; 01222 autopwm[THR] = SetTHRinRatio(g_takeoffTHR); //0.7;スロットルの割合 01223 } 01224 01225 //ヨーを目標値にして許容角度になるまで水平旋回 01226 int Rotate(float targetAngle[3], float TargetYAW){ 01227 float diffYaw = TranslateNewYaw(TargetYAW, nowAngle[YAW]); 01228 01229 if(diffYaw > LIMIT_STRAIGHT_YAW){ 01230 /* 01231 if(diffYaw > THRESHOLD_TURNINGRADIUS_YAW) UpdateTargetAngle_Rightloop_short(targetAngle); 01232 else UpdateTargetAngle_Rightloop(targetAngle); 01233 */ 01234 UpdateTargetAngle_Rightloop_short(targetAngle); 01235 return 1; 01236 }else if(diffYaw < -LIMIT_STRAIGHT_YAW){ 01237 UpdateTargetAngle_Leftloop_short(targetAngle); 01238 /* 01239 if(diffYaw < -THRESHOLD_TURNINGRADIUS_YAW) UpdateTargetAngle_Leftloop_short(targetAngle); 01240 else UpdateTargetAngle_Leftloop(targetAngle); 01241 */ 01242 return 1; 01243 }else{ 01244 UpdateTargetAngle_GoStraight(targetAngle); 01245 return 0; 01246 } 01247 } 01248 01249 //チキラー投下 01250 void Chicken_Drop(){ 01251 if(CheckSW_Up(Ch7)){ 01252 autopwm[DROP] = 1512; 01253 pc.printf("Bombed!\r\n"); 01254 RerurnChickenServo1.attach(&ReturnChickenServo1, 3); 01255 //operation_mode = Approach; 01256 //buzzer = 0; 01257 pc.printf("Goto LeftLoop mode\r\n"); 01258 } 01259 } 01260 01261 void ReturnChickenServo1(){ 01262 autopwm[DROP] = 1344; 01263 pc.printf("first reverse\r\n"); 01264 RerurnChickenServo2.attach(&ReturnChickenServo2, 1); 01265 } 01266 01267 void ReturnChickenServo2(){ 01268 autopwm[DROP] = 1392; 01269 pc.printf("second reverse\r\n"); 01270 } 01271 01272 //着陸モード(PCからの指令に従う) 01273 void UpdateTargetAngle_Approach(float targetAngle[3]){ 01274 01275 static bool zeroTHR=true; 01276 01277 if(CheckSW_Up(Ch7)){ 01278 output_status = Auto; 01279 led1 = 1; 01280 }else{ 01281 output_status = Manual; 01282 led1 = 0; 01283 zeroTHR=true; 01284 } 01285 01286 NVIC_DisableIRQ(USART2_IRQn); 01287 01288 switch(g_landingcommand){ 01289 case 'R': //右旋回セヨ 01290 if(zeroTHR==false){ 01291 UpdateTargetAngle_Rightloop_zero(targetAngle); 01292 } 01293 else{ 01294 targetAngle[ROLL] = g_rightloopROLL; 01295 targetAngle[PITCH] = g_rightloopPITCH ; 01296 autopwm[RUD]=g_rightloopRUD; //RUD固定 01297 if(autopwm[AIL_R]<trimpwm[AIL_R]){ //エルロン上がりやすさ調節 01298 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising; 01299 } 01300 else {autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent; 01301 autopwm[AIL_R]=autopwm[AIL_R]+AIL_R_correctionrightloop; 01302 } 01303 } 01304 break; 01305 01306 case 'L': //左旋回セヨ 01307 if(zeroTHR==false){ 01308 UpdateTargetAngle_Leftloop_zero(targetAngle); 01309 } 01310 else{ 01311 targetAngle[ROLL] = g_leftloopROLL; 01312 targetAngle[PITCH] = g_leftloopPITCH; 01313 autopwm[RUD]=g_leftloopRUD; 01314 if(autopwm[AIL_R]<trimpwm[AIL_R]){ 01315 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising; 01316 } 01317 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent; 01318 } 01319 break; 01320 01321 case 'G': //直進セヨ 01322 if(zeroTHR==false){ 01323 UpdateTargetAngle_GoStraight_zero(targetAngle); 01324 } 01325 else{ 01326 targetAngle[ROLL] = g_gostraightROLL; 01327 targetAngle[PITCH] = g_gostraightPITCH; 01328 } 01329 01330 break; 01331 01332 case 'Y': //指定ノヨー方向ニ移動セヨ 01333 Rotate(targetAngle, g_SerialTargetYAW); 01334 break; 01335 01336 case 'B': //ブザーヲ鳴ラセ 01337 //buzzer = 1; 01338 break; 01339 01340 case 'D': //物資ヲ落トセ 01341 Chicken_Drop(); 01342 break; 01343 01344 case 'C': //停止セヨ 01345 targetAngle[ROLL] = 0.0; 01346 targetAngle[PITCH] = -3.0; 01347 autopwm[THR] = minpwm[THR]; 01348 zeroTHR=false; 01349 break; 01350 01351 } 01352 NVIC_EnableIRQ(USART2_IRQn); 01353 } 01354 01355 void checkHeight(float targetAngle[3]){ 01356 01357 static int targetHeight = 200; 01358 NVIC_DisableIRQ(EXTI9_5_IRQn); 01359 if(g_distance < targetHeight + ALLOWHEIGHT){ 01360 UpdateTargetAngle_NoseUP(targetAngle); 01361 if(CheckSW_Up(Ch7)) led2 = 1; 01362 } 01363 else if(g_distance > targetHeight - ALLOWHEIGHT){ 01364 UpdateTargetAngle_NoseDOWN(targetAngle); 01365 if(CheckSW_Up(Ch7)) led2 = 1; 01366 } 01367 else led2=0; 01368 NVIC_EnableIRQ(EXTI9_5_IRQn); 01369 } 01370 01371 void UpdateTargetAngle_NoseUP(float targetAngle[3]){ 01372 01373 //targetAngle[PITCH] += 2.0f; 01374 //if(nowAngle[PITCH]<targetAngle[PITCH]) autopwm[THR] = SetTHRinRatio(0.6); 01375 autopwm[THR] = SetTHRinRatio(g_loopTHR+0.05); 01376 //pc.printf("nose UP"); 01377 } 01378 01379 void UpdateTargetAngle_NoseDOWN(float targetAngle[3]){ 01380 01381 //targetAngle[PITCH] -= 2.0f; 01382 autopwm[THR] = SetTHRinRatio(g_loopTHR-0.05); 01383 //pc.printf("nose DOWN"); 01384 } 01385 01386 //直進 01387 void UpdateTargetAngle_GoStraight(float targetAngle[3]){ 01388 01389 autopwm[RUD] = trimpwm[RUD]; 01390 targetAngle[ROLL] = g_gostraightROLL; 01391 targetAngle[PITCH] = g_gostraightPITCH; 01392 autopwm[THR] = SetTHRinRatio(g_loopTHR); 01393 01394 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]); 01395 } 01396 01397 //直進(着陸時スロットル0のとき) 01398 void UpdateTargetAngle_GoStraight_zero(float targetAngle[3]){ 01399 01400 autopwm[RUD] = trimpwm[RUD]; 01401 targetAngle[ROLL] = g_gostraightROLL; 01402 targetAngle[PITCH] = g_gostraightPITCH; 01403 autopwm[THR] = minpwm[THR]; 01404 01405 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]); 01406 } 01407 01408 //右旋回 01409 void UpdateTargetAngle_Rightloop(float targetAngle[3]){ //右旋回 01410 01411 targetAngle[ROLL] = g_rightloopROLL; 01412 targetAngle[PITCH] = g_rightloopPITCH ; 01413 autopwm[RUD]=g_rightloopRUD; //RUD固定 01414 autopwm[THR] = SetTHRinRatio(0.5); //手動スロットル記憶 01415 if(autopwm[AIL_R]<trimpwm[AIL_R]){ //エルロン上がりやすさ調節 01416 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising; 01417 } 01418 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent; 01419 autopwm[AIL_R]=autopwm[AIL_R]+AIL_R_correctionrightloop; 01420 01421 //checkHeight(targetAngle); 01422 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]); 01423 } 01424 01425 //右旋回(着陸時スロットル0の時) 01426 void UpdateTargetAngle_Rightloop_zero(float targetAngle[3]){ //右旋回 01427 autopwm[THR]=minpwm[THR]; 01428 targetAngle[ROLL] = g_rightloopROLL; 01429 targetAngle[PITCH] = g_rightloopPITCH ; 01430 autopwm[RUD]=g_rightloopRUD; //RUD固定 01431 if(autopwm[AIL_R]<trimpwm[AIL_R]){ //エルロン上がりやすさ調節 01432 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising; 01433 } 01434 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent; 01435 autopwm[AIL_R]=autopwm[AIL_R]+AIL_R_correctionrightloop; 01436 01437 //checkHeight(targetAngle); 01438 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]); 01439 } 01440 01441 void UpdateTargetAngle_Rightloop_short(float targetAngle[3]){ //右旋回 01442 01443 targetAngle[ROLL] = g_rightloopROLLshort; 01444 targetAngle[PITCH] = g_rightloopPITCHshort; 01445 autopwm[RUD]=g_rightloopshortRUD; 01446 autopwm[THR] = SetTHRinRatio(0.5); 01447 if(autopwm[AIL_R]<trimpwm[AIL_R]){ 01448 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising; 01449 } 01450 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent; 01451 01452 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]); 01453 } 01454 01455 //左旋回 01456 void UpdateTargetAngle_Leftloop(float targetAngle[3]){ 01457 01458 targetAngle[ROLL] = g_leftloopROLL; 01459 targetAngle[PITCH] = g_leftloopPITCH; 01460 autopwm[RUD]=g_leftloopRUD; 01461 autopwm[THR] = SetTHRinRatio(0.5); 01462 if(autopwm[AIL_R]<trimpwm[AIL_R]){ 01463 autopwm[AIL_L]=trimpwm[AIL_L]+g_AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising; 01464 } 01465 else autopwm[AIL_L]=trimpwm[AIL_L]+g_AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent; 01466 //checkHeight(targetAngle); 01467 01468 //printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[], targetAngle[PITCH], autopwm[THR]); 01469 } 01470 01471 //左旋回(着陸時スロットル0のとき) 01472 void UpdateTargetAngle_Leftloop_zero(float targetAngle[3]){ 01473 01474 targetAngle[ROLL] = g_leftloopROLL; 01475 targetAngle[PITCH] = g_leftloopPITCH; 01476 autopwm[RUD]=g_leftloopRUD; 01477 autopwm[THR] = minpwm[THR]; 01478 if(autopwm[AIL_R]<trimpwm[AIL_R]){ 01479 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising; 01480 } 01481 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent; 01482 //autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_Ratio_leftloop; 01483 //checkHeight(targetAngle); 01484 01485 //printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[], targetAngle[PITCH], autopwm[THR]); 01486 } 01487 01488 void UpdateTargetAngle_Leftloop_short(float targetAngle[3]){ 01489 01490 targetAngle[ROLL] = g_leftloopROLLshort; 01491 targetAngle[PITCH] = g_leftloopPITCHshort; 01492 autopwm[RUD]=g_leftloopRUD; 01493 autopwm[THR] = SetTHRinRatio(0.5); 01494 if(autopwm[AIL_R]<trimpwm[AIL_R]){ 01495 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising; 01496 } 01497 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent; 01498 01499 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]); 01500 } 01501 01502 void Sbusprintf(){ 01503 01504 for(uint8_t i=0; i<8; i++) pc.printf("ch.%d = %d ",i+1,sbus.manualpwm[i]); 01505 pc.printf("\r\n"); 01506 01507 } 01508 01509 01510 //デバッグ用 01511 void DebugPrint(){ 01512 /* 01513 static int16_t deltaT = 0, t_start = 0; 01514 deltaT = t.read_u2s() - t_start; 01515 pc.printf("t:%d us, ",deltaT); 01516 pc.printf("\r\n"); 01517 t_start = t.read_us(); 01518 */ 01519 //for(uint8_t i=0; i<8; i++) pc.printf("%d ",sbus.manualpwm[i]); 01520 //for(uint8_t i=1; i<4; i++) pc.printf("%d ",sbus.manualpwm[i]); 01521 //pc.printf("\r\n"); 01522 //for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]); 01523 //for(uint8_t i=0; i<2; i++) pc.printf("%3.2f\t",nowAngle[i]); //ロール、ピッチのみ 小数点以下2ケタ 01524 //pc.printf("%d\t",autopwm[AIL_L]); // pc.printf("%d\t",autopwm[RUD]); 01525 //pc.printf("%d",g_distance); 01526 01527 //pc.printf("Mode: %c: ",g_buf[0]); 01528 //if(g_buf[0] == 'Y') pc.printf(" %3.1f",g_SerialTargetYAW); 01529 //pc.printf("\r\n"); 01530 }
Generated on Thu Jul 21 2022 13:53:53 by
1.7.2
