航空研究会 / Mbed 2 deprecated Autoflight2018_31

Dependencies:   HCSR04_2 MPU6050_2 mbed SDFileSystem3

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