航空研究会 / Mbed 2 deprecated Autoflight2018_22_MODSERIAL

Dependencies:   HCSR04_2 MODSERIAL MPU6050_2 mbed SDFileSystem3

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