航空研究会 / Mbed 2 deprecated Autoflight2018_24

Dependencies:   HCSR04_2 MPU6050_2 mbed SDFileSystem3

Fork of Autoflight2018_23 by 航空研究会

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 int zeroTHR=1;//着陸時のスロットルが0かの判断用
00173 
00174 
00175 static float nowAngle[3] = {0,0,0};
00176 const float trimAngle[3] = {0.0, 0.0, 0.0};
00177 const float maxAngle[2] = {90, 90};
00178 const float minAngle[2] = {-90, -90};
00179 
00180 float FirstROLL = 0.0, FirstPITCH = 0.0 ,FirstYAW = 0.0;
00181 
00182 unsigned int g_distance;
00183 //Ticker USsensor;
00184 static char g_buf[16];
00185 char g_landingcommand='Z';
00186 float g_SerialTargetYAW;
00187 
00188 Timer t;
00189 Timeout RerurnChickenServo1;
00190 Timeout RerurnChickenServo2;
00191 
00192 /*-----関数のプロトタイプ宣言-----*/
00193 void setup();
00194 void loop();
00195 
00196 void Init_PWM();
00197 void Init_servo();              //サーボ初期化
00198 void Init_sbus();               //SBUS初期化
00199 void Init_sensors();
00200 void DisplayClock();            //クロック状態確認
00201 
00202 //センサの値取得
00203 void SensingMPU();
00204 void UpdateDist();
00205 
00206 //void offsetRollPitch(float FirstROLL, float FirstPITCH);
00207 //void TransYaw(float FirstYAW);
00208 float TranslateNewYaw(float beforeYaw,  float newzeroYaw);
00209 void UpdateTargetAngle(float targetAngle[3]);
00210 void CalculateControlValue(float targetAngle[3], float controlValue[3]);
00211 void UpdateAutoPWM(float controlValue[3]);
00212 void ConvertPWMintoRAD(float targetAngle[3]);
00213 inline float CalcRatio(float value, float trim, float limit);
00214 bool CheckSW_Up(Channel ch);
00215 int16_t ThresholdMaxMin(int16_t value, int16_t max, int16_t min);
00216 inline int16_t SetTHRinRatio(float ratio);
00217 
00218 //sbus割り込み
00219 void Update_PWM();              //マニュアル・自動モードのpwmデータを整形しpwm変数に入力
00220 void Output_PWM(int16_t pwm[6]);    //pwmをサーボへ出力
00221 
00222 //シリアル割り込み
00223 void getSF_Serial();
00224 float ConvertByteintoFloat(char high, char low);
00225 
00226 
00227 //SD設定
00228 int GetParameter(FILE *fp, const char *paramName,char parameter[]);
00229 int SetOptions(float *g_kpELE, float *g_kiELE, float *g_kdELE,
00230                float *g_kpRUD, float *g_kiRUD, float *g_kdRUD,
00231                float *g_rightloopROLL, float *g_rightloopPITCH,
00232                float *g_leftloopROLL, float *g_leftloopPITCH,
00233                float *g_gostraightROLL, float *g_gostraightPITCH,
00234                float *g_takeoffTHR, float *g_loopTHR,
00235                float *g_rightloopROLLshort, float *g_rightloopPITCHshort,
00236                float *g_leftloopROLLshort, float *g_leftloopPITCHshort,
00237                float *g_glideloopROLL, float *g_glideloopPITCH,
00238                float *g_kpAIL, float *g_kiAIL, float *g_kdAIL,
00239                int *g_rightloopRUD, int *g_rightloopshortRUD,
00240                int *g_leftloopRUD, int *g_leftloopshortRUD,
00241                int *g_glideRUD,
00242                int *g_AIL_L_correctionrightloop,int *g_AIL_L_correctionrightloopshort,
00243                int *g_AIL_L_correctionlefttloop,int *g_AIL_L_correctionleftloopshort
00244                );
00245 //switch2割り込み
00246 void ResetTrim();
00247 
00248 //自動操縦
00249 void UpdateTargetAngle_GoStraight(float targetAngle[3]);
00250 void UpdateTargetAngle_GoStraight_zero(float targetAngle[3]);       //着陸時にスロットルが0の時の直進
00251 void UpdateTargetAngle_Rightloop(float targetAngle[3]);
00252 void UpdateTargetAngle_Rightloop_short(float targetAngle[3]);
00253 void UpdateTargetAngle_Rightloop_zero(float targetAngle[3]);      //着陸時にスロットルが0の時の右旋回
00254 void UpdateTargetAngle_Leftloop(float targetAngle[3]);
00255 void UpdateTargetAngle_Leftloop_short(float targetAngle[3]);
00256 void UpdateTargetAngle_Leftloop_zero(float targetAngle[3]);     //着陸時にスロットルが0の時の左旋回
00257 void UpdateTargetAngle_Moebius(float targetAngle[3]);
00258 void UpdateTargetAngle_Glide(float targetAngle[3]);
00259 void UpdateTargetAngle_Takeoff(float targetAngle[3]);
00260 void UpdateTargetAngle_Approach(float targetAngle[3]);
00261 void Take_off_and_landing(float targetAngle[3]);
00262 
00263 int Rotate(float targetAngle[3], float TargetYAW);
00264 
00265 //投下
00266 void Chicken_Drop();
00267 void ReturnChickenServo1();
00268 void ReturnChickenServo2();
00269 
00270 //超音波による高度補正
00271 void checkHeight(float targetAngle[3]);
00272 void UpdateTargetAngle_NoseUP(float targetAngle[3]);
00273 void UpdateTargetAngle_NoseDOWN(float targetAngle[3]);
00274 
00275 //デバッグ用
00276 void Sbusprintf();
00277 void DebugPrint();
00278 
00279 /*---関数のプロトタイプ宣言終わり---*/
00280 
00281 int main()
00282 {   
00283     setup();
00284     
00285     
00286     while(1){
00287         
00288         loop();
00289         
00290         
00291         NVIC_DisableIRQ(USART1_IRQn);
00292         if(!CheckSW_Up(Ch7)){
00293             led3=0;
00294         }else{
00295             led3=1;
00296         }
00297         NVIC_EnableIRQ(USART1_IRQn);
00298     }
00299     
00300 }
00301 
00302 void setup(){
00303     //buzzer = 0;
00304     led1 = 1;
00305     led2 = 1;
00306     led3 = 1;
00307     led4 = 1;
00308     
00309     SetOptions(&g_kpELE, &g_kiELE, &g_kdELE,
00310                &g_kpRUD, &g_kiRUD, &g_kdRUD,
00311                &g_rightloopROLL, &g_rightloopPITCH,
00312                &g_leftloopROLL, &g_leftloopPITCH,
00313                &g_gostraightROLL, &g_gostraightPITCH,
00314                &g_takeoffTHR, &g_loopTHR,
00315                &g_rightloopROLLshort, &g_rightloopPITCHshort,
00316                &g_leftloopROLLshort, &g_leftloopPITCHshort,
00317                &g_glideloopROLL, &g_glideloopPITCH,
00318                &g_kpAIL, &g_kiAIL,&g_kdAIL,
00319                &g_rightloopRUD, &g_rightloopshortRUD,
00320                &g_leftloopRUD, &g_leftloopshortRUD,
00321                &g_glideloopRUD,
00322                &g_AIL_L_correctionrightloop,&g_AIL_L_correctionrightloopshort,
00323                &g_AIL_L_correctionleftloop,&g_AIL_L_correctionleftloopshort
00324                );    
00325     
00326         
00327     Init_PWM();
00328     Init_servo();
00329     Init_sbus();    
00330     Init_sensors();
00331     //switch2.rise(ResetTrim);
00332     pc.attach(getSF_Serial, Serial::RxIrq);
00333     //USsensor.attach(&UpdateDist, 0.05);
00334     
00335     NVIC_SetPriority(USART1_IRQn,0);
00336     NVIC_SetPriority(EXTI0_IRQn,1);
00337     NVIC_SetPriority(TIM5_IRQn,2);
00338     NVIC_SetPriority(EXTI9_5_IRQn,3);
00339     NVIC_SetPriority(USART2_IRQn,4);
00340     DisplayClock();
00341     t.start();
00342     
00343     
00344     pc.printf("MPU calibration start\r\n");
00345     
00346     float offsetstart = t.read();
00347     while(t.read() - offsetstart < 26){
00348         SensingMPU();
00349         for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]);
00350         pc.printf("\r\n");
00351         led1 = !led1;
00352         led2 = !led2;
00353         led3 = !led3;
00354         led4 = !led4;
00355     }
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     if(CheckSW_Up(Ch7)){
00577         output_status = Auto;
00578         led1 = 1;
00579     }else{
00580         output_status = Manual;
00581         led1 = 0;
00582     }
00583 }
00584 
00585 int GetParameter(FILE *fp, const char *paramName,char parameter[]){
00586     int i=0, j=0;
00587     int strmax = 200;
00588     char str[strmax];
00589 
00590     rewind(fp); //ファイル位置を先頭に
00591     while(1){
00592         if (fgets(str, strmax, fp) == NULL) {
00593             return 0;
00594         }
00595         if (!strncmp(str, paramName, strlen(paramName))) {
00596             while (str[i++] != '=') {}
00597             while (str[i] != '\n') {
00598                     parameter[j++] = str[i++];
00599             }
00600             parameter[j] = '\0';
00601             return 1;
00602         }
00603     }
00604     return 0;
00605 }
00606 
00607 
00608 //sdによる設定
00609 int SetOptions(float *g_kpELE, float *g_kiELE, float *g_kdELE,
00610                float *g_kpRUD, float *g_kiRUD, float *g_kdRUD,
00611                float *g_rightloopROLL, float *g_rightloopPITCH,
00612                float *g_leftloopROLL, float *g_leftloopPITCH,
00613                float *g_gostraightROLL, float *g_gostraightPITCH,
00614                float *g_takeoffTHR, float *g_loopTHR,
00615                float *g_rightloopROLLshort, float *g_rightloopPITCHshort,
00616                float *g_leftloopROLLshort, float *g_leftloopPITCHshort,
00617                float *g_glideloopROLL, float *g_glideloopPITCH,
00618                float *g_kpAIL, float *g_kiAIL, float *g_kdAIL,
00619                int *g_rightloopRUD, int *g_rightloopshortRUD,
00620                int *g_leftloopRUD, int *g_leftloopshortRUD,
00621                int *g_glideloopRUD,
00622                int *g_AIL_L_correctionrightloop,int *g_AIL_L_correctionrightloopshort,
00623                int *g_AIL_L_correctionleftloop,int *g_AIL_L_correctionleftloopshort
00624                ){
00625 
00626     pc.printf("SDsetup start.\r\n");    
00627     
00628     FILE *fp;
00629     char parameter[40]; //文字列渡す用の配列
00630     int SDerrorcount = 0;  //取得できなかった数を返す
00631     const char *paramNames[] = { 
00632         "KP_ELEVATOR",
00633         "KI_ELEVATOR",
00634         "KD_ELEVATOR",
00635         "KP_RUDDER",
00636         "KI_RUDDER",
00637         "KD_RUDDER",
00638         "RIGHTLOOP_ROLL",
00639         "RIGHTLOOP_PITCH",
00640         "LEFTLOOP_ROLL",
00641         "LEFTLOOP_PITCH",
00642         "GOSTRAIGHT_ROLL",
00643         "GOSTRAIGHT_PITCH",
00644         "TAKEOFF_THR_RATE",
00645         "LOOP_THR_RATE",
00646         "RIGHTLOOP_ROLL_SHORT",
00647         "RIGHTLOOP_PITCH_SHORT",
00648         "LEFTLOOP_ROLL_SHORT",
00649         "LEFTLOOP_PITCH_SHORT",
00650         "AUTOGLIDE_ROLL",
00651         "AUTOGLIDE PITCH",
00652         "KP_AILERON",
00653         "KI_AILERON",
00654         "KD_AILERON",
00655         "RIGHTLOOP_RUDDER",
00656         "RIGHTLOOPSHORT_RUDDER",
00657         "LEFTLOOP_RUDDER",
00658         "LEFTLOOPSHORT_RUDDER",
00659         "GLIDELOOP_RUDDER",
00660         "AILERON_LEFT_CORRECTION_RIGHTLOOP",        
00661         "AILERON_LEFT_CORRECTION_RIGHTLOOPSHORT",        
00662         "AILERON_LEFT_CORRECTION_LEFTLOOP",        
00663         "AILERON_LEFT_CORRECTION_LEFTLOOPSHORT"            
00664     };
00665 
00666     fp = fopen("/sd/option.txt","r");
00667     
00668     if(fp != NULL){ //開けたら
00669         pc.printf("File was openned.\r\n");
00670         if(GetParameter(fp,paramNames[0],parameter)) *g_kpELE = atof(parameter);
00671         else{                                        *g_kpELE = KP_ELE;
00672                                                      SDerrorcount++;
00673         }
00674         if(GetParameter(fp,paramNames[1],parameter)) *g_kiELE = atof(parameter);
00675         else{                                        *g_kiELE = KI_ELE;
00676                                                       SDerrorcount++;
00677         }
00678         if(GetParameter(fp,paramNames[2],parameter)) *g_kdELE = atof(parameter);
00679         else{                                        *g_kdELE = KD_ELE;
00680                                                      SDerrorcount++;
00681         }
00682         if(GetParameter(fp,paramNames[3],parameter)) *g_kpRUD = atof(parameter);
00683         else{                                        *g_kpRUD = KP_RUD;
00684                                                       SDerrorcount++;
00685         }
00686         if(GetParameter(fp,paramNames[4],parameter)) *g_kiRUD = atof(parameter);
00687         else{                                        *g_kiRUD = KI_RUD;
00688                                                       SDerrorcount++;
00689         }
00690         if(GetParameter(fp,paramNames[5],parameter)) *g_kdRUD = atof(parameter);
00691         else{                                        *g_kdRUD = KD_RUD;
00692                                                       SDerrorcount++;
00693         }
00694         if(GetParameter(fp,paramNames[6],parameter)) *g_rightloopROLL = atof(parameter);
00695         else{                                        *g_rightloopROLL = RIGHT_ROLL;
00696                                                       SDerrorcount++;
00697         }
00698         if(GetParameter(fp,paramNames[7],parameter)) *g_rightloopPITCH = atof(parameter);
00699         else{                                        *g_rightloopPITCH = RIGHT_PITCH;
00700                                                       SDerrorcount++;
00701         }
00702         if(GetParameter(fp,paramNames[8],parameter)) *g_leftloopROLL = atof(parameter);
00703         else{                                        *g_leftloopROLL = LEFT_ROLL;
00704                                                       SDerrorcount++;
00705         }
00706         if(GetParameter(fp,paramNames[9],parameter)) *g_leftloopPITCH = atof(parameter);
00707         else{                                         *g_leftloopPITCH = LEFT_PITCH;
00708                                                       SDerrorcount++;
00709         }
00710         if(GetParameter(fp,paramNames[10],parameter)) *g_gostraightROLL = atof(parameter);
00711         else{                                         *g_gostraightROLL = STRAIGHT_ROLL;
00712                                                       SDerrorcount++;
00713         }
00714         if(GetParameter(fp,paramNames[11],parameter)) *g_gostraightPITCH = atof(parameter);
00715         else{                                         *g_gostraightPITCH = STRAIGHT_PITCH;
00716                                                       SDerrorcount++;
00717         }
00718         if(GetParameter(fp,paramNames[12],parameter)) *g_takeoffTHR = atof(parameter);
00719         else{                                         *g_takeoffTHR = TAKEOFF_THR;
00720                                                       SDerrorcount++;
00721         }
00722         if(GetParameter(fp,paramNames[13],parameter)) *g_loopTHR = atof(parameter);
00723         else{                                         *g_loopTHR = LOOP_THR;
00724                                                       SDerrorcount++;
00725         }
00726         if(GetParameter(fp,paramNames[14],parameter)) *g_rightloopROLLshort = atof(parameter);
00727         else{                                         *g_rightloopROLLshort = RIGHT_ROLL_SHORT;
00728                                                       SDerrorcount++;
00729         }
00730         if(GetParameter(fp,paramNames[15],parameter)) *g_rightloopPITCHshort = atof(parameter);
00731         else{                                         *g_rightloopPITCHshort = RIGHT_PITCH_SHORT;
00732                                                       SDerrorcount++;
00733         }
00734         if(GetParameter(fp,paramNames[16],parameter)) *g_leftloopROLLshort = atof(parameter);
00735         else{                                         *g_leftloopROLLshort = LEFT_ROLL_SHORT;
00736                                                       SDerrorcount++;
00737         }
00738         if(GetParameter(fp,paramNames[17],parameter)) *g_leftloopPITCHshort = atof(parameter);
00739         else{                                         *g_leftloopPITCHshort = LEFT_PITCH_SHORT;
00740                                                       SDerrorcount++;
00741         }
00742         if(GetParameter(fp,paramNames[18],parameter)) *g_glideloopROLL = atof(parameter);
00743         else{                                         *g_glideloopROLL = GLIDE_ROLL;
00744                                                       SDerrorcount++;
00745         }
00746         if(GetParameter(fp,paramNames[19],parameter)) *g_glideloopPITCH = atof(parameter);
00747         else{                                         *g_glideloopPITCH = GLIDE_PITCH;
00748                                                       SDerrorcount++;
00749         }
00750         if(GetParameter(fp,paramNames[20],parameter)) *g_kpAIL = atof(parameter);
00751         else{                                         *g_kpAIL = KP_AIL;
00752                                                       SDerrorcount++;
00753         }
00754         if(GetParameter(fp,paramNames[21],parameter)) *g_kiAIL = atof(parameter);
00755         else{                                         *g_kiAIL = KI_AIL;
00756                                                       SDerrorcount++;
00757         }
00758         if(GetParameter(fp,paramNames[22],parameter)) *g_kdAIL = atof(parameter);
00759         else{                                         *g_kdAIL = KP_AIL;
00760                                                       SDerrorcount++;
00761         }
00762         if(GetParameter(fp,paramNames[23],parameter)) *g_rightloopRUD = atof(parameter);
00763         else{                                         *g_rightloopRUD = RIGHTLOOP_RUD;
00764                                                       SDerrorcount++;
00765         }
00766         if(GetParameter(fp,paramNames[24],parameter)) *g_rightloopshortRUD = atof(parameter);
00767         else{                                         *g_rightloopshortRUD = RIGHTLOOPSHORT_RUD;
00768                                                       SDerrorcount++;
00769         }
00770         if(GetParameter(fp,paramNames[25],parameter)) *g_leftloopRUD = atof(parameter);
00771         else{                                         *g_leftloopshortRUD = LEFTLOOP_RUD;
00772                                                       SDerrorcount++;
00773         }
00774         if(GetParameter(fp,paramNames[26],parameter)) *g_leftloopshortRUD = atof(parameter);
00775         else{                                         *g_leftloopshortRUD = LEFTLOOPSHORT_RUD;
00776                                                       SDerrorcount++;
00777         }
00778         if(GetParameter(fp,paramNames[27],parameter)) *g_glideloopRUD = atof(parameter);
00779         else{                                         *g_glideloopRUD = GLIDELOOP_RUD;
00780                                                       SDerrorcount++;
00781         }
00782         if(GetParameter(fp,paramNames[28],parameter)) *g_AIL_L_correctionrightloop = atof(parameter);
00783         else{                                         *g_AIL_L_correctionrightloop = AIL_L_CORRECTION_RIGHTLOOP;
00784                                                       SDerrorcount++;
00785         }
00786         if(GetParameter(fp,paramNames[29],parameter)) *g_AIL_L_correctionrightloopshort = atof(parameter);
00787         else{                                         *g_AIL_L_correctionrightloopshort = AIL_L_CORRECTION_RIGHTLOOPSHORT;
00788                                                       SDerrorcount++;
00789         }
00790         if(GetParameter(fp,paramNames[30],parameter)) *g_AIL_L_correctionleftloop = atof(parameter);
00791         else{                                         *g_AIL_L_correctionleftloop = AIL_L_CORRECTION_LEFTLOOP;
00792                                                       SDerrorcount++;
00793         }
00794         if(GetParameter(fp,paramNames[31],parameter)) *g_AIL_L_correctionleftloopshort = atof(parameter);
00795         else{                                         *g_AIL_L_correctionleftloopshort = AIL_L_CORRECTION_LEFTLOOPSHORT;
00796                                                       SDerrorcount++;
00797         }
00798         
00799         fclose(fp);
00800 
00801     }else{  //ファイルがなかったら
00802         pc.printf("fp was null.\r\n");
00803         *g_kpELE = KP_ELE;
00804         *g_kiELE = KI_ELE;
00805         *g_kdELE = KD_ELE;
00806         *g_kpRUD = KP_RUD;
00807         *g_kiRUD = KI_RUD;
00808         *g_kdRUD = KD_RUD;
00809         *g_rightloopROLL = RIGHT_ROLL;
00810         *g_rightloopPITCH = RIGHT_PITCH;
00811         *g_leftloopROLL = LEFT_ROLL;
00812         *g_leftloopPITCH = LEFT_PITCH;
00813         *g_gostraightROLL = STRAIGHT_ROLL;
00814         *g_gostraightPITCH = STRAIGHT_PITCH;
00815         *g_takeoffTHR = TAKEOFF_THR;
00816         *g_loopTHR = LOOP_THR;
00817         *g_kpAIL = KP_AIL; //パラメータ変えるのお忘れなく!!
00818         *g_kiAIL = KI_AIL;
00819         *g_kdAIL = KD_AIL;
00820         *g_rightloopRUD = RIGHTLOOP_RUD;
00821         *g_rightloopshortRUD = RIGHTLOOPSHORT_RUD;
00822         *g_leftloopRUD = LEFTLOOP_RUD;
00823         *g_leftloopshortRUD = LEFTLOOPSHORT_RUD;
00824         *g_glideloopRUD = GLIDELOOP_RUD;
00825         *g_AIL_L_correctionrightloop = AIL_L_CORRECTION_RIGHTLOOP;
00826         *g_AIL_L_correctionrightloopshort = AIL_L_CORRECTION_RIGHTLOOPSHORT;
00827         *g_AIL_L_correctionleftloop = AIL_L_CORRECTION_LEFTLOOP;
00828         *g_AIL_L_correctionleftloopshort = AIL_L_CORRECTION_LEFTLOOPSHORT;
00829         
00830         
00831         SDerrorcount = -1;
00832     }
00833     pc.printf("SDsetup finished.\r\n");
00834     if(SDerrorcount == 0)  pc.printf("setting option is success\r\n");
00835     else if(SDerrorcount == -1) pc.printf("ERROR 1. cannot open option\r\n");
00836     else if(SDerrorcount > 0)  pc.printf("ERROR 2. reading parameter is failed[%d]\r\n",SDerrorcount); 
00837     
00838     pc.printf("kpELE = %f, kiELE = %f, kdELE = %f\r\n", *g_kpRUD, *g_kiRUD, *g_kdRUD);
00839     pc.printf("kpRUD = %f, kiRUD = %f, kdRUD = %f\r\n", *g_kpELE, *g_kiELE, *g_kdELE);
00840     pc.printf("rightloopROLL = %f, rightloopPITCH = %f\r\n", *g_rightloopROLL, *g_rightloopPITCH);
00841     pc.printf("leftloopROLL = %f, g_leftloopPITCH =  %f\r\n", *g_leftloopROLL, *g_leftloopPITCH);
00842     pc.printf("gostraightROLL = %f, g_gostraightPITCH = %f\r\n", *g_gostraightROLL, *g_gostraightPITCH);
00843     pc.printf("g_takeoffTHR = %f, g_loopTHR = %f\r\n", *g_takeoffTHR, *g_loopTHR);
00844     pc.printf("rightloopROLLshort = %f, rightloopPITCHshort = %f\r\n", *g_rightloopROLLshort, *g_rightloopPITCHshort);
00845     pc.printf("leftloopROLLshort = %f, g_leftloopPITCHshort =  %f\r\n", *g_leftloopROLLshort, *g_leftloopPITCHshort);
00846     pc.printf("glideROLL = %f, glidePITCH = %f\r\n", *g_glideloopROLL, *g_glideloopPITCH);
00847     pc.printf("kpAIL = %f,kiAIL = %f,kdAIL = %f\r\n ",*g_kpAIL,*g_kiAIL,*g_kdAIL);
00848     pc.printf("RIGHTLOOPRUD = %d,RIGHTLOOPSHORTRUD = %d\r\n",*g_rightloopRUD,*g_rightloopshortRUD);
00849     pc.printf("LEFTTLOOPRUD = %d,LEFTLOOPSHORTRUD = %d\r\n",*g_leftloopRUD,*g_leftloopshortRUD);
00850     pc.printf("GLIDELOOPRUD = %d\r\n",*g_glideloopRUD);
00851     pc.printf("AIL_L_CORRECTION_RIGHTLOOP = %d, AIL_L_CORRECTION_RIGHTLOOPSHORT = %d\r\n",*g_AIL_L_correctionrightloop,*g_AIL_L_correctionrightloopshort);
00852     pc.printf("AIL_L_CORRECTION_LEFTLOOP = %d, AIL_L_CORRECTION_LEFTLOOPSHORT = %d\r\n",*g_AIL_L_correctionleftloop,*g_AIL_L_correctionleftloopshort);
00853     return SDerrorcount;
00854 }                  
00855 
00856 void CalculateControlValue(float targetAngle[3], float controlValue[3]){
00857     static int t_last;
00858     int t_now;
00859     float dt;
00860 
00861     t_now = t.read_us();
00862     dt = (float)((t_now - t_last)/1000000.0f) ;
00863     t_last = t_now;
00864 
00865 
00866     //controlValue[ROLL] = pid_RUD.calcPID(nowAngle[ROLL], targetAngle[ROLL], dt);
00867     controlValue[ROLL] = pid_AIL.calcPID(nowAngle[ROLL], targetAngle[ROLL], dt);       //エルロンでロール制御   
00868     controlValue[PITCH] = pid_ELE.calcPID(nowAngle[PITCH], targetAngle[PITCH], dt);
00869 }
00870 
00871 void UpdateAutoPWM(float controlValue[3]){    
00872     int16_t addpwm[2]; //-500~500
00873     addpwm[PITCH] = (float)GAIN_CONTROLVALUE_TO_PWM * controlValue[PITCH];    //センサ:機首下げ正    レバー:機首上げ正
00874     addpwm[ROLL] = (float)GAIN_CONTROLVALUE_TO_PWM * controlValue[ROLL];           //センサ:右回転正(8月13日時点;左回転が正!)     レバー:右回転正
00875     
00876     autopwm[ELE] = trimpwm[ELE] + reverce[ELE] * addpwm[PITCH];                 //rewrite
00877     autopwm[AIL_R] = trimpwm[AIL_R] - reverce[AIL_R] * addpwm[ROLL];
00878     //autopwm[THR] = oldTHR;
00879 
00880     autopwm[ELE] = ThresholdMaxMin(autopwm[ELE], maxpwm[ELE], minpwm[ELE]);
00881     autopwm[AIL_R] = ThresholdMaxMin(autopwm[AIL_R], maxpwm[AIL_R], minpwm[AIL_R]);
00882     
00883 }
00884 
00885 inline float CalcRatio(float value, float trim, float limit){
00886     return  (value - trim) / (limit - trim);
00887 }
00888 
00889 bool CheckSW_Up(Channel ch){
00890     if(SWITCH_CHECK < sbus.manualpwm[ch]){
00891         return true;
00892     }else{
00893         return false;
00894     }
00895 }
00896 
00897 int16_t ThresholdMaxMin(int16_t value, int16_t max, int16_t min){
00898     if(value > max) return max;
00899     if(value < min) return min;
00900     return value;
00901 }
00902 
00903 inline int16_t SetTHRinRatio(float ratio){
00904     return minpwm[THR] + (int16_t)(2 * lengthdivpwm * ratio);
00905 }
00906 
00907 
00908 
00909 /*---SBUS割り込み処理---*/
00910 
00911 //udate_Inputで抽出したpwmデータを整形して各変数に代入する。(マニュアルモード)
00912 //各stabiGylo関数で算出したpwmを各変数に代入する(自動モード)
00913 void Update_PWM()
00914 {    
00915     static int16_t pwm[6];
00916     static int16_t temppwm[6]={trimpwm[0],trimpwm[1],trimpwm[2],trimpwm[3],trimpwm[4],trimpwm[5]};
00917     if(sbus.flg_ch_update == true){
00918         NVIC_DisableIRQ(USART1_IRQn);
00919         switch(output_status){    //マニュアルモード,自動モード,自動着陸もモードを切替
00920             case Manual: 
00921                 for(uint8_t i=0;i<6;i++){
00922                     pwm[i] = sbus.manualpwm[i];  
00923                 }
00924                 /*pc.printf("%d ,",pwm[0]);//R
00925                 pc.printf("%d ,\r\n",pwm[5]);//L*/
00926                 oldTHR = sbus.manualpwm[THR];
00927                 //pc.printf("update_manual\r\n");
00928                 break;
00929         
00930             case Auto:
00931                 pwm[AIL_R] = autopwm[AIL_R];               //sbus.manualpwm[AIL];
00932                 pwm[ELE] = autopwm[ELE];
00933                 pwm[THR] = autopwm[THR];
00934                 pwm[RUD] = autopwm[RUD];
00935                 pwm[DROP] = autopwm[DROP];
00936                 pwm[AIL_L] = autopwm[AIL_L];
00937                 //pc.printf("%d ,",pwm[AIL_R]);//R
00938                 //pc.printf("%d ,\r\n",pwm[AIL_L]);//L
00939                 //pc.printf("update_auto\r\n");
00940                 
00941                 break;
00942                 
00943             default:
00944                 for(uint8_t i=0;i<6;i++){
00945                     pwm[i] = sbus.manualpwm[i];
00946                 } //pc.printf("update_manual\r\n");
00947                 break;
00948             NVIC_EnableIRQ(USART1_IRQn);
00949         }
00950         for(uint8_t i=0;i<6;i++){
00951         if(pwm[i]<1000 || pwm[i]>2000) pwm[i]=temppwm[i];
00952         temppwm[i]=pwm[i];
00953         }
00954     }else{
00955         pc.printf("0\r\n");
00956     }
00957     sbus.flg_ch_update = false;   
00958     Output_PWM(pwm);  
00959 }
00960 
00961 
00962 //pwmをサーボに出力。
00963 void Output_PWM(int16_t pwm[5])
00964 {
00965     NVIC_DisableIRQ(USART1_IRQn);
00966     servo1.pulsewidth_us(pwm[0]);
00967     servo2.pulsewidth_us(pwm[1]);
00968     servo3.pulsewidth_us(pwm[2]);
00969     servo4.pulsewidth_us(pwm[3]);
00970     servo5.pulsewidth_us(pwm[4]);
00971     servo6.pulsewidth_us(pwm[5]);
00972     NVIC_EnableIRQ(USART1_IRQn);
00973 
00974 }
00975 
00976 void ResetTrim(){
00977     for(uint8_t i=0; i<6; i++){            //i=4から書き換え_投下サーボは入ってない模様
00978         trimpwm[i] = sbus.manualpwm[i];
00979     }
00980     pc.printf("reset PWM trim\r\n");
00981 }
00982 
00983 
00984 void SensingMPU(){
00985     //static int16_t deltaT = 0, t_start = 0;
00986     //t_start = t.read_us();
00987     
00988     float rpy[3] = {0}, oldrpy[3] = {0};
00989     static uint16_t count_changeRPY = 0;
00990     static bool flg_checkoutlier = false;
00991     NVIC_DisableIRQ(USART1_IRQn);
00992     NVIC_DisableIRQ(USART2_IRQn);
00993     NVIC_DisableIRQ(TIM5_IRQn);
00994     NVIC_DisableIRQ(EXTI0_IRQn);//MPU割り込み禁止
00995     NVIC_DisableIRQ(EXTI9_5_IRQn);//超音波割り込み禁止
00996 
00997     mpu6050.getRollPitchYaw_Skipper(rpy);
00998  
00999     NVIC_EnableIRQ(USART1_IRQn);
01000     NVIC_EnableIRQ(USART2_IRQn);
01001     NVIC_EnableIRQ(TIM5_IRQn);
01002     NVIC_EnableIRQ(EXTI0_IRQn);
01003     NVIC_EnableIRQ(EXTI9_5_IRQn);
01004     
01005     
01006     //外れ値対策
01007     for(uint8_t i=0; i<3; i++) rpy[i] *= 180.0f/PI;
01008     rpy[ROLL] -= FirstROLL;
01009     rpy[PITCH] -= FirstPITCH;
01010     rpy[YAW] -= FirstYAW;
01011     
01012     for(uint8_t i=0; i<3; i++) {if(rpy[i] < nowAngle[i]-10 || rpy[i] > nowAngle[i]+10) {flg_checkoutlier = true;}}
01013     if(!flg_checkoutlier || count_changeRPY >= 2){
01014         for(uint8_t i=0; i<3; i++){
01015             nowAngle[i] = (rpy[i] + nowAngle[i])/2.0f;  //2つの移動平均
01016         }
01017         count_changeRPY = 0;
01018     }else   count_changeRPY++;
01019     flg_checkoutlier = false;
01020     
01021 }
01022 
01023 float TranslateNewYaw(float beforeYaw,  float newzeroYaw){
01024     float newYaw = beforeYaw - newzeroYaw;
01025     
01026     if(newYaw<-180.0f) newYaw += 360.0f;
01027     else if(newYaw>180.0f) newYaw -= 360.0f;
01028     return newYaw;
01029 }
01030 
01031 
01032 void getSF_Serial(){
01033     
01034         static char SFbuf[16];
01035         static int bufcounter=0;
01036         
01037         if(pc.readable()) {    // 受信確認
01038             SFbuf[bufcounter] = pc.getc();    // 1文字取り出し
01039         }
01040         
01041         
01042         pc.printf("%c",SFbuf[bufcounter]);
01043         
01044         if(SFbuf[0]=='S'&&bufcounter<5) bufcounter++;
01045             
01046         if(bufcounter==5 && SFbuf[4]=='F'){
01047             g_landingcommand = SFbuf[1];
01048             wait_ms(8);
01049             if(g_landingcommand=='Y')g_SerialTargetYAW = ConvertByteintoFloat(SFbuf[2], SFbuf[3]);
01050             bufcounter = 0;
01051             memset(SFbuf, 0, strlen(SFbuf));
01052             //pc.printf("command = %c, commandYAW = %f\r\n", g_landingcommand, g_SerialTargetYAW); 
01053         }
01054             else if(bufcounter>=5 ){
01055                 pc.printf("Communication Falsed.\r\n");
01056                 bufcounter = 0;
01057             }
01058             
01059     }
01060     
01061 float ConvertByteintoFloat(char high, char low){
01062 
01063         //int16_t intvalue = (int16_t)high*256 + (int16_t)low;
01064         int16_t intvalue = (int16_t)(((int16_t)high << 8) | low);  // Turn the MSB and LSB into a signed 16-bit value
01065         float floatvalue = (float)intvalue;
01066         return floatvalue;
01067 }
01068 
01069 
01070 //超音波割り込み
01071 /*void UpdateDist(){
01072     g_distance = usensor.get_dist_cm();
01073     usensor.start();
01074 }*/
01075 
01076 //8の字旋回
01077 void UpdateTargetAngle_Moebius(float targetAngle[3]){
01078     static uint8_t RotateCounter=0;
01079     static bool flg_setInStartAuto = false;
01080     static float FirstYAW_Moebius = 0.0;
01081     float newYaw_Moebius;
01082 
01083     if(!flg_setInStartAuto && CheckSW_Up(Ch7)){
01084         FirstYAW_Moebius = nowAngle[YAW];
01085         RotateCounter = 0;
01086         flg_setInStartAuto = true;
01087     }else if(!CheckSW_Up(Ch7)){
01088         flg_setInStartAuto = false;
01089         led2 = 0;
01090     }
01091     autopwm[THR]=oldTHR;
01092 
01093     newYaw_Moebius = TranslateNewYaw(nowAngle[YAW], FirstYAW_Moebius);
01094 
01095     if(RotateCounter == 0 && newYaw_Moebius >90.0 && newYaw_Moebius < 180.0)    {RotateCounter++; led2 = 1; pc.printf("Rotate 90\r\n");}
01096     if(RotateCounter == 1 && newYaw_Moebius >-180.0 && newYaw_Moebius < -90.0)  {RotateCounter++; led2 = 0; pc.printf("Rotate 180\r\n");}
01097     if(RotateCounter == 2 && newYaw_Moebius >-90.0 && newYaw_Moebius <-10.0)   {RotateCounter++; led2 = 1; pc.printf("Rotate 270\r\n");}
01098     if(RotateCounter == 3 && newYaw_Moebius >0.0 && newYaw_Moebius < 90.0)     {RotateCounter++; led2 = 0; pc.printf("Change Rotate direction\r\n");}
01099 
01100 
01101     if(RotateCounter <= 3) UpdateTargetAngle_Rightloop_short(targetAngle);    
01102     else UpdateTargetAngle_Leftloop_short(targetAngle);   //左旋回
01103 
01104 }
01105 
01106 //自動滑空
01107 void UpdateTargetAngle_Glide(float targetAngle[3]){
01108     static int THRcount = 0;
01109     static int t_start = 0;
01110     static bool flg_tstart = false;
01111     int t_diff = 0;
01112     static int groundcount = 0;
01113     
01114     targetAngle[ROLL] = g_glideloopROLL;
01115     targetAngle[PITCH] = g_glideloopPITCH;
01116     
01117     autopwm[RUD]=g_glideloopRUD;
01118    // autopwm[THR]=oldTHR;
01119 
01120     
01121     
01122     //時間計測開始設定
01123     if(!flg_tstart && CheckSW_Up(Ch7)){
01124         t_start = t.read();
01125         flg_tstart = true;
01126         pc.printf("timer start\r\n");
01127     }else if(!CheckSW_Up(Ch7)){
01128         t_start = 0;
01129         flg_tstart = false;
01130     }
01131 
01132 
01133     //フラグが偽であれば計測は行わない    
01134     if(flg_tstart){
01135         t_diff = t.read() - t_start;
01136         //一定高度or15秒でled点灯
01137         NVIC_DisableIRQ(EXTI9_5_IRQn);
01138         if((groundcount>5 && g_distance>0) || t_diff > 15){
01139             led2 = 1;
01140             //pc.printf("Call [Stop!] calling!\r\n");
01141         }
01142         
01143         if(g_distance<180 && g_distance > 0) groundcount++;
01144         NVIC_EnableIRQ(EXTI9_5_IRQn);
01145     }else{
01146         t_diff = 0;
01147         groundcount = 0;
01148         led2 = 0;
01149     }
01150     
01151     if(t_diff > 17){
01152         autopwm[THR] = SetTHRinRatio(0.5);
01153     }else{
01154         NVIC_DisableIRQ(EXTI9_5_IRQn);
01155         if(g_distance<150 && g_distance>0 ){
01156         NVIC_EnableIRQ(EXTI9_5_IRQn);    
01157             THRcount++;
01158             if(THRcount>5){
01159                 autopwm[THR] = SetTHRinRatio(0.6);
01160                 //pc.printf("throttle ON\r\n");
01161             }
01162         }else{
01163             autopwm[THR] = 1180;
01164             THRcount = 0;
01165         }
01166     }
01167 }
01168 //離陸-投下-着陸一連
01169 void Take_off_and_landing(float targetAngle[3]){
01170     /*
01171     if(!CheckSW_Up(Ch7)) bombing_mode = Takeoff;
01172     
01173     switch(bombing_mode){
01174         case Takeoff:
01175             static bool flg_setFirstYaw = false;
01176             static int TakeoffCount = 0;
01177 
01178             if(!flg_setFirstYaw && CheckSW_Up(Ch7)){
01179                 FirstYAW = nowAngle[YAW];
01180                 flg_setFirstYaw = true;
01181             }else if(flg_setFirstYaw && !CheckSW_Up(Ch7)){
01182                 flg_setFirstYaw = false;
01183             }
01184             
01185             UpdateTargetAngle_Takeoff(targetAngle);
01186             
01187             if(g_distance>150) TakeoffCount++;
01188             else TakeoffCount = 0;
01189             if(TakeoffCount>5){
01190                 autopwm[THR] = 1180+320*2*0.5;
01191                 pc.printf("Now go to Approach mode!!");
01192                 bombing_mode = Approach;
01193             }
01194             break;
01195            
01196         //case Chicken:    
01197             //break;
01198             
01199         case Transition:
01200             static int ApproachCount = 0;
01201             targetAngle[YAW]=180.0;
01202             int Judge = Rotate(targetAngle, g_SerialTargetYAW);
01203             
01204             if(Judge==0) ApproachCount++;
01205             if(ApproachCount>5) bombing_mode = Approach;
01206             break;
01207             
01208         case Approach:
01209         */
01210             autopwm[THR] = SetTHRinRatio(g_loopTHR); //0.7;スロットルの割合
01211             UpdateTargetAngle_Approach(targetAngle);
01212             /*
01213             break;
01214             
01215         default:
01216             bombing_mode = Takeoff;
01217             break;    
01218     }
01219     */
01220 }
01221 
01222 //離陸モード
01223 void UpdateTargetAngle_Takeoff(float targetAngle[3]){
01224     //pc.printf("%d \r\n",g_distance);
01225     targetAngle[ROLL] = g_gostraightROLL;
01226     targetAngle[PITCH] = g_loopTHR;
01227     autopwm[THR] = SetTHRinRatio(g_takeoffTHR); //0.7;スロットルの割合
01228 }
01229 
01230 //ヨーを目標値にして許容角度になるまで水平旋回
01231 int Rotate(float targetAngle[3], float TargetYAW){
01232     float diffYaw = TranslateNewYaw(TargetYAW, nowAngle[YAW]);
01233 
01234     if(diffYaw > LIMIT_STRAIGHT_YAW){
01235         /*
01236         if(diffYaw > THRESHOLD_TURNINGRADIUS_YAW) UpdateTargetAngle_Rightloop_short(targetAngle);
01237         else UpdateTargetAngle_Rightloop(targetAngle);
01238         */
01239         UpdateTargetAngle_Rightloop_short(targetAngle);
01240         return 1;
01241     }else if(diffYaw < -LIMIT_STRAIGHT_YAW){
01242         UpdateTargetAngle_Leftloop_short(targetAngle);
01243         /*
01244         if(diffYaw < -THRESHOLD_TURNINGRADIUS_YAW) UpdateTargetAngle_Leftloop_short(targetAngle);
01245         else UpdateTargetAngle_Leftloop(targetAngle);
01246         */
01247         return 1;
01248     }else{
01249         UpdateTargetAngle_GoStraight(targetAngle);
01250         return 0;
01251     }
01252 }
01253 
01254 //チキラー投下
01255 void Chicken_Drop(){
01256     if(CheckSW_Up(Ch7)){
01257         autopwm[DROP] = 1512;
01258         pc.printf("Bombed!\r\n");
01259         RerurnChickenServo1.attach(&ReturnChickenServo1, 3);
01260         //operation_mode = Approach;
01261         //buzzer = 0;
01262         pc.printf("Goto LeftLoop mode\r\n");
01263     }
01264 }
01265 
01266 void ReturnChickenServo1(){
01267     autopwm[DROP] = 1344;
01268     pc.printf("first reverse\r\n");
01269     RerurnChickenServo2.attach(&ReturnChickenServo2, 1);
01270 }
01271 
01272 void ReturnChickenServo2(){
01273     autopwm[DROP] = 1392;
01274     pc.printf("second reverse\r\n");
01275     }
01276     
01277     //着陸モード(PCからの指令に従う)
01278     void UpdateTargetAngle_Approach(float targetAngle[3]){
01279        
01280         NVIC_DisableIRQ(USART2_IRQn);
01281        
01282         switch(g_landingcommand){
01283             case 'R':   //右旋回セヨ
01284             if(zeroTHR==0){
01285                 UpdateTargetAngle_Rightloop_zero(targetAngle);
01286                 }
01287                 else{
01288                 targetAngle[ROLL] = g_rightloopROLL;
01289         targetAngle[PITCH] = g_rightloopPITCH ;
01290         autopwm[RUD]=g_rightloopRUD;              //RUD固定
01291         if(autopwm[AIL_R]>trimpwm[AIL_R]){                                                                                  //エルロン上がりやすさ調節
01292             autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
01293             }
01294         else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
01295         autopwm[AIL_R]=autopwm[AIL_R]+AIL_R_correctionrightloop;
01296         
01297                 break;
01298                 }
01299                 case 'L':   //左旋回セヨ
01300                 if(zeroTHR==0){
01301                     UpdateTargetAngle_Leftloop_zero(targetAngle);
01302                     }
01303                     else{
01304                     targetAngle[ROLL] = g_leftloopROLL;
01305             targetAngle[PITCH] = g_leftloopPITCH;
01306             autopwm[RUD]=g_leftloopRUD;
01307             if(autopwm[AIL_R]>trimpwm[AIL_R]){
01308                 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
01309                 }
01310             else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
01311         }
01312                     break;
01313                     
01314                 case 'G':   //直進セヨ
01315                 if(zeroTHR==0){
01316                     UpdateTargetAngle_GoStraight_zero(targetAngle);
01317                     }
01318                     else{
01319                     targetAngle[ROLL] = g_gostraightROLL;
01320                     targetAngle[PITCH] = g_gostraightPITCH;
01321     }
01322     
01323                     break;
01324                 
01325                 case 'Y':   //指定ノヨー方向ニ移動セヨ
01326                     Rotate(targetAngle, g_SerialTargetYAW);
01327                     break;
01328                 
01329                 case 'B':   //ブザーヲ鳴ラセ
01330                     //buzzer = 1;
01331                     break;
01332                     
01333                 case 'D':   //物資ヲ落トセ
01334                     Chicken_Drop();
01335                     break;
01336                             
01337                 case 'C':   //停止セヨ
01338                     targetAngle[ROLL] = 0.0;
01339                     targetAngle[PITCH] = -3.0;
01340                     autopwm[THR] = minpwm[THR];
01341                     zeroTHR=0;
01342                     break;
01343             
01344             }
01345             NVIC_EnableIRQ(USART2_IRQn);
01346 }
01347         
01348 void checkHeight(float targetAngle[3]){
01349         
01350         static int targetHeight = 200; 
01351         NVIC_DisableIRQ(EXTI9_5_IRQn);
01352         if(g_distance < targetHeight + ALLOWHEIGHT){
01353             UpdateTargetAngle_NoseUP(targetAngle);
01354             if(CheckSW_Up(Ch7)) led2 = 1;
01355         }
01356         else if(g_distance > targetHeight - ALLOWHEIGHT){
01357             UpdateTargetAngle_NoseDOWN(targetAngle);
01358             if(CheckSW_Up(Ch7)) led2 = 1;
01359         }
01360         else led2=0;
01361         NVIC_EnableIRQ(EXTI9_5_IRQn);
01362     }
01363     
01364     void UpdateTargetAngle_NoseUP(float targetAngle[3]){
01365     
01366     //targetAngle[PITCH] += 2.0f;
01367     //if(nowAngle[PITCH]<targetAngle[PITCH]) autopwm[THR] = SetTHRinRatio(0.6);
01368     autopwm[THR] = SetTHRinRatio(g_loopTHR+0.05);
01369     //pc.printf("nose UP");
01370 }
01371 
01372 void UpdateTargetAngle_NoseDOWN(float targetAngle[3]){
01373     
01374     //targetAngle[PITCH] -= 2.0f;
01375     autopwm[THR] = SetTHRinRatio(g_loopTHR-0.05);
01376     //pc.printf("nose DOWN");
01377 }
01378 
01379 //直進
01380 void UpdateTargetAngle_GoStraight(float targetAngle[3]){
01381 
01382     autopwm[RUD] = trimpwm[RUD];
01383     targetAngle[ROLL] = g_gostraightROLL;
01384     targetAngle[PITCH] = g_gostraightPITCH;
01385     autopwm[THR] = SetTHRinRatio(g_loopTHR);
01386     
01387     //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
01388 }
01389 
01390 //直進(着陸時スロットル0のとき)
01391 void UpdateTargetAngle_GoStraight_zero(float targetAngle[3]){
01392 
01393     autopwm[RUD] =  trimpwm[RUD];
01394     targetAngle[ROLL] = g_gostraightROLL;
01395     targetAngle[PITCH] = g_gostraightPITCH;
01396     autopwm[THR] = minpwm[THR];
01397     
01398     //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
01399 }
01400 
01401 //右旋回
01402 void UpdateTargetAngle_Rightloop(float targetAngle[3]){ //右旋回
01403 
01404     targetAngle[ROLL] = g_rightloopROLL;
01405     targetAngle[PITCH] = g_rightloopPITCH ;
01406     autopwm[RUD]=g_rightloopRUD;              //RUD固定
01407     autopwm[THR] = SetTHRinRatio(0.5);                  //手動スロットル記憶
01408     if(autopwm[AIL_R]>trimpwm[AIL_R]){                                                                                  //エルロン上がりやすさ調節
01409         autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
01410         }
01411     else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
01412     autopwm[AIL_R]=autopwm[AIL_R]+AIL_R_correctionrightloop;
01413     
01414     //checkHeight(targetAngle);
01415     //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
01416 }
01417 
01418 //右旋回(着陸時スロットル0の時)
01419 void UpdateTargetAngle_Rightloop_zero(float targetAngle[3]){ //右旋回
01420 
01421     targetAngle[ROLL] = g_rightloopROLL;
01422     targetAngle[PITCH] = g_rightloopPITCH ;
01423     autopwm[RUD]=g_rightloopRUD;
01424     autopwm[THR] = minpwm[THR];//SetTHRinRatio(g_loopTHR);
01425     if(autopwm[AIL_R]>trimpwm[AIL_R]){
01426         autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
01427         }
01428     else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
01429 
01430     //checkHeight(targetAngle);
01431     //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
01432 }
01433 
01434 void UpdateTargetAngle_Rightloop_short(float targetAngle[3]){ //右旋回
01435 
01436     targetAngle[ROLL] = g_rightloopROLLshort;
01437     targetAngle[PITCH] = g_rightloopPITCHshort;
01438     autopwm[RUD]=g_rightloopshortRUD;
01439     autopwm[THR] = SetTHRinRatio(0.5);
01440     if(autopwm[AIL_R]>trimpwm[AIL_R]){
01441         autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
01442         }
01443     else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
01444     
01445     //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
01446 }
01447 
01448 //左旋回
01449 void UpdateTargetAngle_Leftloop(float targetAngle[3]){
01450     
01451     targetAngle[ROLL] = g_leftloopROLL;
01452     targetAngle[PITCH] = g_leftloopPITCH;
01453     autopwm[RUD]=g_leftloopRUD;
01454     autopwm[THR] = SetTHRinRatio(0.5);
01455     if(autopwm[AIL_R]>trimpwm[AIL_R]){
01456         autopwm[AIL_L]=trimpwm[AIL_L]+g_AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
01457         }
01458     else autopwm[AIL_L]=trimpwm[AIL_L]+g_AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
01459     //checkHeight(targetAngle);
01460 
01461     //printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[], targetAngle[PITCH], autopwm[THR]);
01462 }
01463 
01464 //左旋回(着陸時スロットル0のとき)
01465 void UpdateTargetAngle_Leftloop_zero(float targetAngle[3]){
01466     
01467     targetAngle[ROLL] = g_leftloopROLL;
01468     targetAngle[PITCH] = g_leftloopPITCH;
01469     autopwm[RUD]=g_leftloopRUD;
01470     autopwm[THR] = minpwm[THR];
01471     if(autopwm[AIL_R]>trimpwm[AIL_R]){
01472         autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
01473         }
01474     else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
01475     //autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_Ratio_leftloop;
01476     //checkHeight(targetAngle);
01477 
01478     //printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[], targetAngle[PITCH], autopwm[THR]);
01479 }
01480 
01481 void UpdateTargetAngle_Leftloop_short(float targetAngle[3]){
01482     
01483     targetAngle[ROLL] = g_leftloopROLLshort;
01484     targetAngle[PITCH] = g_leftloopPITCHshort;
01485     autopwm[RUD]=g_leftloopRUD;
01486     autopwm[THR] = SetTHRinRatio(0.5);
01487     if(autopwm[AIL_R]>trimpwm[AIL_R]){
01488         autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
01489         }
01490     else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
01491 
01492     //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
01493 }
01494 
01495 void Sbusprintf(){
01496     
01497     for(uint8_t i=0; i<8; i++) pc.printf("ch.%d = %d ",i+1,sbus.manualpwm[i]);
01498     pc.printf("\r\n");
01499     
01500     }
01501 
01502 
01503 //デバッグ用
01504 void DebugPrint(){    
01505     /*
01506     static int16_t deltaT = 0, t_start = 0;
01507     deltaT = t.read_u2s() - t_start;
01508     pc.printf("t:%d us, ",deltaT);
01509     pc.printf("\r\n");
01510     t_start = t.read_us();
01511     */
01512     //for(uint8_t i=0; i<8; i++) pc.printf("%d ",sbus.manualpwm[i]);
01513     //for(uint8_t i=1; i<4; i++) pc.printf("%d ",sbus.manualpwm[i]);
01514     //pc.printf("\r\n");
01515     //for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]);
01516     //for(uint8_t i=0; i<2; i++) pc.printf("%3.2f\t",nowAngle[i]); //ロール、ピッチのみ 小数点以下2ケタ
01517     //pc.printf("%d\t",autopwm[AIL_L]); // pc.printf("%d\t",autopwm[RUD]);
01518     //pc.printf("%d",g_distance);
01519 
01520     //pc.printf("Mode: %c: ",g_buf[0]);
01521     //if(g_buf[0] == 'Y') pc.printf(" %3.1f",g_SerialTargetYAW);
01522     //pc.printf("\r\n");
01523 }