割り込みテスト用(28は間違い)

Dependencies:   HCSR04_2 MPU6050_2 mbed SDFileSystem3

Fork of Autoflight2018_27_test by 航空研究会

Committer:
taknokolat
Date:
Thu Sep 20 07:12:24 2018 +0000
Revision:
24:3e7dcf16a6a7
Parent:
23:0f3b1441b08c
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HARUKIDELTA 0:17f575135219 1 //mbed
HARUKIDELTA 0:17f575135219 2 #include "mbed.h"
HARUKIDELTA 0:17f575135219 3 #include "FATFileSystem.h"
HARUKIDELTA 0:17f575135219 4 #include "SDFileSystem.h"
HARUKIDELTA 0:17f575135219 5 //C
HARUKIDELTA 0:17f575135219 6 #include "math.h"
HARUKIDELTA 0:17f575135219 7 //sensor
HARUKIDELTA 0:17f575135219 8 #include "MPU6050_DMP6.h"
HARUKIDELTA 0:17f575135219 9 //#include "MPU9250.h"
HARUKIDELTA 0:17f575135219 10 //#include "BMP280.h"
HARUKIDELTA 0:17f575135219 11 #include "hcsr04.h"
HARUKIDELTA 0:17f575135219 12 //device
HARUKIDELTA 0:17f575135219 13 #include "sbus.h"
HARUKIDELTA 0:17f575135219 14 //config
HARUKIDELTA 0:17f575135219 15 #include "SkipperSv2.h"
HARUKIDELTA 0:17f575135219 16 #include "falfalla.h"
HARUKIDELTA 0:17f575135219 17 //other
HARUKIDELTA 0:17f575135219 18 #include "pid.h"
HARUKIDELTA 0:17f575135219 19
HARUKIDELTA 0:17f575135219 20 #define DEBUG_SEMIAUTO 0
taknokolat 23:0f3b1441b08c 21 #define DEBUG_PRINT_INLOOP 0
HARUKIDELTA 0:17f575135219 22
TUATBM 9:f6367b7fd7be 23 #define KP_ELE 15.0 //2.0
HARUKIDELTA 0:17f575135219 24 #define KI_ELE 0.0
TUATBM 9:f6367b7fd7be 25 #define KD_ELE 0.0 //0/0
HARUKIDELTA 0:17f575135219 26 #define KP_RUD 3.0
HARUKIDELTA 0:17f575135219 27 #define KI_RUD 0.0
HARUKIDELTA 0:17f575135219 28 #define KD_RUD 0.0
HARUKIDELTA 2:23daa5fa28b4 29 #define KP_AIL 0.1
HARUKIDELTA 2:23daa5fa28b4 30 #define KI_AIL 0.2
HARUKIDELTA 2:23daa5fa28b4 31 #define KD_AIL 0.2
taknokolat 1:f383708a5a52 32
HARUKIDELTA 2:23daa5fa28b4 33 //#define g_AIL_L_Ratio_rightloop 0.5
HARUKIDELTA 0:17f575135219 34
HARUKIDELTA 0:17f575135219 35 #define GAIN_CONTROLVALUE_TO_PWM 3.0
HARUKIDELTA 0:17f575135219 36
HARUKIDELTA 0:17f575135219 37 #define RIGHT_ROLL -12.0
TUATBM 9:f6367b7fd7be 38 #define RIGHT_PITCH -10.0 //5.0
HARUKIDELTA 0:17f575135219 39 #define LEFT_ROLL 12.0
HARUKIDELTA 0:17f575135219 40 #define LEFT_PITCH -5.0
HARUKIDELTA 0:17f575135219 41 #define STRAIGHT_ROLL 4.0
HARUKIDELTA 0:17f575135219 42 #define STRAIGHT_PITCH 3.0
HARUKIDELTA 0:17f575135219 43 #define TAKEOFF_THR 0.8
HARUKIDELTA 0:17f575135219 44 #define LOOP_THR 0.6
HARUKIDELTA 0:17f575135219 45
HARUKIDELTA 13:77b13d5f4d78 46 //#define g_rightloopRUD 1500
HARUKIDELTA 2:23daa5fa28b4 47
HARUKIDELTA 0:17f575135219 48 #define RIGHT_ROLL_SHORT -12.0
HARUKIDELTA 0:17f575135219 49 #define RIGHT_PITCH_SHORT -5.0
HARUKIDELTA 0:17f575135219 50 #define LEFT_ROLL_SHORT 12.0
HARUKIDELTA 0:17f575135219 51 #define LEFT_PITCH_SHORT -5.0
HARUKIDELTA 0:17f575135219 52
HARUKIDELTA 17:55249ea37dff 53 /*#define rightloopRUD 1300 //1250
HARUKIDELTA 17:55249ea37dff 54 #define rightloopshortRUD 1250
HARUKIDELTA 17:55249ea37dff 55 #define leftloopRUD 1500
HARUKIDELTA 17:55249ea37dff 56 #define leftloopshortRUD 1500
HARUKIDELTA 17:55249ea37dff 57 #define glideloopRUD 1300
HARUKIDELTA 17:55249ea37dff 58 */
TUATBM 9:f6367b7fd7be 59 #define AIL_R_correctionrightloop 0
HARUKIDELTA 18:cce82f3374fc 60 #define AIL_L_correctionrightloop 0
HARUKIDELTA 3:206b17251f5b 61 #define AIL_L_correctionrightloopshort 0
HARUKIDELTA 3:206b17251f5b 62 #define AIL_L_correctionleftloop -0
HARUKIDELTA 3:206b17251f5b 63 #define AIL_L_correctionleftloopshort 0
HARUKIDELTA 3:206b17251f5b 64
HARUKIDELTA 19:0955311b0db6 65
HARUKIDELTA 8:66bba39b95a9 66 #define RIGHTLOOP_RUD 1250
HARUKIDELTA 8:66bba39b95a9 67 #define RIGHTLOOPSHORT_RUD 1250
HARUKIDELTA 8:66bba39b95a9 68 #define LEFTLOOP_RUD 1500
HARUKIDELTA 8:66bba39b95a9 69 #define LEFTLOOPSHORT_RUD 1500
HARUKIDELTA 8:66bba39b95a9 70 #define GLIDELOOP_RUD 1300
HARUKIDELTA 8:66bba39b95a9 71 #define AIL_L_CORRECTION_RIGHTLOOP 0
HARUKIDELTA 8:66bba39b95a9 72 #define AIL_L_CORRECTION_RIGHTLOOPSHORT 0
HARUKIDELTA 8:66bba39b95a9 73 #define AIL_L_CORRECTION_LEFTLOOP 0
HARUKIDELTA 8:66bba39b95a9 74 #define AIL_L_CORRECTION_LEFTLOOPSHORT 0
HARUKIDELTA 3:206b17251f5b 75
HARUKIDELTA 0:17f575135219 76 #define GLIDE_ROLL -12.0
HARUKIDELTA 0:17f575135219 77 #define GLIDE_PITCH -3.0
HARUKIDELTA 0:17f575135219 78
HARUKIDELTA 3:206b17251f5b 79
HARUKIDELTA 3:206b17251f5b 80 #define AIL_L_RatioRising 0.5
HARUKIDELTA 3:206b17251f5b 81 #define AIL_L_RatioDescent 2
HARUKIDELTA 3:206b17251f5b 82
HARUKIDELTA 0:17f575135219 83 //コンパスキャリブレーション
HARUKIDELTA 0:17f575135219 84 //SkipperS2基板
HARUKIDELTA 0:17f575135219 85 /*
HARUKIDELTA 0:17f575135219 86 #define MAGBIAS_X -35.0
HARUKIDELTA 0:17f575135219 87 #define MAGBIAS_Y 535.0
HARUKIDELTA 0:17f575135219 88 #define MAGBIAS_Z -50.0
HARUKIDELTA 0:17f575135219 89 */
HARUKIDELTA 0:17f575135219 90 //S2v2 1番基板
HARUKIDELTA 0:17f575135219 91 #define MAGBIAS_X 395.0
HARUKIDELTA 0:17f575135219 92 #define MAGBIAS_Y 505.0
HARUKIDELTA 0:17f575135219 93 #define MAGBIAS_Z -725.0
HARUKIDELTA 0:17f575135219 94 //S2v2 2番基板
HARUKIDELTA 0:17f575135219 95 /*
HARUKIDELTA 0:17f575135219 96 #define MAGBIAS_X 185.0
HARUKIDELTA 0:17f575135219 97 #define MAGBIAS_Y 220.0
HARUKIDELTA 0:17f575135219 98 #define MAGBIAS_Z -350.0
HARUKIDELTA 0:17f575135219 99 */
HARUKIDELTA 0:17f575135219 100
HARUKIDELTA 0:17f575135219 101 #define ELEMENT 1
HARUKIDELTA 0:17f575135219 102 #define LIMIT_STRAIGHT_YAW 5.0
HARUKIDELTA 0:17f575135219 103 #define THRESHOLD_TURNINGRADIUS_YAW 60.0
HARUKIDELTA 0:17f575135219 104 #define ALLOWHEIGHT 15
HARUKIDELTA 0:17f575135219 105
HARUKIDELTA 0:17f575135219 106 #ifndef PI
HARUKIDELTA 0:17f575135219 107 #define PI 3.14159265358979
HARUKIDELTA 0:17f575135219 108 #endif
HARUKIDELTA 0:17f575135219 109
HARUKIDELTA 0:17f575135219 110 const int16_t lengthdivpwm = 320;
HARUKIDELTA 0:17f575135219 111 const int16_t changeModeCount = 6;
HARUKIDELTA 0:17f575135219 112
HARUKIDELTA 0:17f575135219 113
HARUKIDELTA 0:17f575135219 114 SBUS sbus(PA_9, PA_10); //SBUS
HARUKIDELTA 0:17f575135219 115
HARUKIDELTA 0:17f575135219 116 PwmOut servo1(PC_6); // TIM3_CH1 //old echo
HARUKIDELTA 0:17f575135219 117 PwmOut servo2(PC_7); // TIM3_CH2 //PC_7
HARUKIDELTA 0:17f575135219 118 PwmOut servo3(PB_0); // TIM3_CH3
HARUKIDELTA 0:17f575135219 119 PwmOut servo4(PB_1); // TIM3_CH4
HARUKIDELTA 0:17f575135219 120 PwmOut servo5(PB_6); // TIM4_CH1
HARUKIDELTA 0:17f575135219 121 PwmOut servo6(PB_7); // TIM4_CH2 //old trigger
HARUKIDELTA 0:17f575135219 122 //PwmOut servo7(PB_8); // TIM4_CH3 //PB_8 new echo
HARUKIDELTA 0:17f575135219 123 //PwmOut servo8(PB_9); // TIM4_CH4 //new trigger
HARUKIDELTA 0:17f575135219 124
HARUKIDELTA 0:17f575135219 125 RawSerial pc(PA_2,PA_3, 115200); //tx,rx.baudrate pin;PA_2=UART2_TX, PA_3=UART2_RX
taknokolat 1:f383708a5a52 126 //RawSerial pc2(PB_6,PB_7, 115200); //sbus確認用
HARUKIDELTA 0:17f575135219 127 SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd");
HARUKIDELTA 0:17f575135219 128
HARUKIDELTA 0:17f575135219 129 DigitalOut led1(PA_0); //黄色のコネクタ
HARUKIDELTA 0:17f575135219 130 DigitalOut led2(PA_1);
HARUKIDELTA 0:17f575135219 131 DigitalOut led3(PB_4);
HARUKIDELTA 0:17f575135219 132 DigitalOut led4(PB_5);
HARUKIDELTA 0:17f575135219 133
HARUKIDELTA 0:17f575135219 134 //InterruptIn switch2(PC_14);
HARUKIDELTA 0:17f575135219 135 MPU6050DMP6 mpu6050(PC_0,&pc); //割り込みピン,シリアルポインタ i2cのピン指定は MPU6050>>I2Cdev.h 内のdefine
taknokolat 10:652071c20bf6 136 //HCSR04 usensor(PB_9,PB_8); //trig,echo 9,8
HARUKIDELTA 0:17f575135219 137
taknokolat 1:f383708a5a52 138 PID pid_AIL(g_kpAIL,g_kiAIL,g_kdAIL);
HARUKIDELTA 0:17f575135219 139 PID pid_ELE(g_kpELE,g_kiELE,g_kdELE);
HARUKIDELTA 0:17f575135219 140 PID pid_RUD(g_kpRUD,g_kiRUD,g_kdRUD);
HARUKIDELTA 0:17f575135219 141
HARUKIDELTA 0:17f575135219 142 enum Channel{AIL_R, ELE, THR, RUD, DROP, AIL_L, Ch7, Ch8};
HARUKIDELTA 0:17f575135219 143 enum Angle{ROLL, PITCH, YAW}; //yaw:北を0とした絶対角度
HARUKIDELTA 0:17f575135219 144 enum OperationMode{StartUp, SemiAuto, RightLoop, LeftLoop, GoStraight, BombwithPC, ZERO, Moebius, Glide};
HARUKIDELTA 0:17f575135219 145 enum BombingMode{Takeoff, Chicken, Transition, Approach};
HARUKIDELTA 0:17f575135219 146 enum OutputStatus{Manual, Auto};
HARUKIDELTA 0:17f575135219 147
HARUKIDELTA 0:17f575135219 148 static OutputStatus output_status = Manual;
HARUKIDELTA 0:17f575135219 149 OperationMode operation_mode = StartUp;
HARUKIDELTA 0:17f575135219 150 BombingMode bombing_mode = Takeoff;
TUATBM 15:43427b0241d9 151
HARUKIDELTA 0:17f575135219 152 static int16_t autopwm[8] = {1500,1500,1180,1500,1392,1500};
TUATBM 15:43427b0241d9 153
TUATBM 15:43427b0241d9 154 //1号機
HARUKIDELTA 18:cce82f3374fc 155 static int16_t trimpwm[6] = {1580,1600,1176,1404,1512,1448};
HARUKIDELTA 18:cce82f3374fc 156 int16_t maxpwm[6] = {1796,1936,1848,1740,1820,1856};
HARUKIDELTA 18:cce82f3374fc 157 int16_t minpwm[6] = {1182,1265,1176,1068,1180,1176};
TUATBM 15:43427b0241d9 158 const int16_t reverce[4] = {Reverce_falfalla[0],Reverce_falfalla[1],Reverce_falfalla[2],Reverce_falfalla[3]};
TUATBM 15:43427b0241d9 159
TUATBM 15:43427b0241d9 160 //2号機
TUATBM 15:43427b0241d9 161 /*
TUATBM 15:43427b0241d9 162 static int16_t trimpwm[6] = {1500,1500,1180,1500,1392,1600};
TUATBM 15:43427b0241d9 163 int16_t maxpwm[6] = {1820,1820,1820,1820,1820,1820};
TUATBM 15:43427b0241d9 164 int16_t minpwm[6] = {1180,1180,1180,1180,1180,1180};
TUATBM 15:43427b0241d9 165 const int16_t reverce[4] = {Reverce_falfalla[0],Reverce_falfalla[1],Reverce_falfalla[2],Reverce_falfalla[3]};
TUATBM 15:43427b0241d9 166 */
TUATBM 15:43427b0241d9 167
HARUKIDELTA 0:17f575135219 168 int16_t oldTHR = 1000;
HARUKIDELTA 0:17f575135219 169
HARUKIDELTA 2:23daa5fa28b4 170 int16_t g_AIL_L_Ratio_rightloop = 0.5;
HARUKIDELTA 2:23daa5fa28b4 171
HARUKIDELTA 19:0955311b0db6 172 int zeroTHR=1;//着陸時のスロットルが0かの判断用
HARUKIDELTA 19:0955311b0db6 173
HARUKIDELTA 2:23daa5fa28b4 174
HARUKIDELTA 0:17f575135219 175 static float nowAngle[3] = {0,0,0};
HARUKIDELTA 0:17f575135219 176 const float trimAngle[3] = {0.0, 0.0, 0.0};
HARUKIDELTA 0:17f575135219 177 const float maxAngle[2] = {90, 90};
HARUKIDELTA 0:17f575135219 178 const float minAngle[2] = {-90, -90};
HARUKIDELTA 0:17f575135219 179
HARUKIDELTA 0:17f575135219 180 float FirstROLL = 0.0, FirstPITCH = 0.0 ,FirstYAW = 0.0;
HARUKIDELTA 0:17f575135219 181
HARUKIDELTA 0:17f575135219 182 unsigned int g_distance;
taknokolat 10:652071c20bf6 183 //Ticker USsensor;
HARUKIDELTA 0:17f575135219 184 static char g_buf[16];
taknokolat 10:652071c20bf6 185 char g_landingcommand='Z';
HARUKIDELTA 0:17f575135219 186 float g_SerialTargetYAW;
HARUKIDELTA 0:17f575135219 187
HARUKIDELTA 0:17f575135219 188 Timer t;
HARUKIDELTA 0:17f575135219 189 Timeout RerurnChickenServo1;
HARUKIDELTA 0:17f575135219 190 Timeout RerurnChickenServo2;
HARUKIDELTA 0:17f575135219 191
HARUKIDELTA 0:17f575135219 192 /*-----関数のプロトタイプ宣言-----*/
HARUKIDELTA 0:17f575135219 193 void setup();
HARUKIDELTA 0:17f575135219 194 void loop();
HARUKIDELTA 0:17f575135219 195
HARUKIDELTA 0:17f575135219 196 void Init_PWM();
HARUKIDELTA 0:17f575135219 197 void Init_servo(); //サーボ初期化
HARUKIDELTA 0:17f575135219 198 void Init_sbus(); //SBUS初期化
HARUKIDELTA 0:17f575135219 199 void Init_sensors();
HARUKIDELTA 0:17f575135219 200 void DisplayClock(); //クロック状態確認
HARUKIDELTA 0:17f575135219 201
HARUKIDELTA 0:17f575135219 202 //センサの値取得
HARUKIDELTA 0:17f575135219 203 void SensingMPU();
HARUKIDELTA 0:17f575135219 204 void UpdateDist();
HARUKIDELTA 0:17f575135219 205
HARUKIDELTA 0:17f575135219 206 //void offsetRollPitch(float FirstROLL, float FirstPITCH);
HARUKIDELTA 0:17f575135219 207 //void TransYaw(float FirstYAW);
HARUKIDELTA 0:17f575135219 208 float TranslateNewYaw(float beforeYaw, float newzeroYaw);
HARUKIDELTA 0:17f575135219 209 void UpdateTargetAngle(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 210 void CalculateControlValue(float targetAngle[3], float controlValue[3]);
HARUKIDELTA 0:17f575135219 211 void UpdateAutoPWM(float controlValue[3]);
HARUKIDELTA 0:17f575135219 212 void ConvertPWMintoRAD(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 213 inline float CalcRatio(float value, float trim, float limit);
HARUKIDELTA 0:17f575135219 214 bool CheckSW_Up(Channel ch);
HARUKIDELTA 0:17f575135219 215 int16_t ThresholdMaxMin(int16_t value, int16_t max, int16_t min);
HARUKIDELTA 0:17f575135219 216 inline int16_t SetTHRinRatio(float ratio);
HARUKIDELTA 0:17f575135219 217
HARUKIDELTA 0:17f575135219 218 //sbus割り込み
HARUKIDELTA 0:17f575135219 219 void Update_PWM(); //マニュアル・自動モードのpwmデータを整形しpwm変数に入力
HARUKIDELTA 0:17f575135219 220 void Output_PWM(int16_t pwm[6]); //pwmをサーボへ出力
HARUKIDELTA 0:17f575135219 221
HARUKIDELTA 0:17f575135219 222 //シリアル割り込み
HARUKIDELTA 0:17f575135219 223 void getSF_Serial();
HARUKIDELTA 0:17f575135219 224 float ConvertByteintoFloat(char high, char low);
TUATBM 15:43427b0241d9 225
HARUKIDELTA 0:17f575135219 226
HARUKIDELTA 0:17f575135219 227 //SD設定
HARUKIDELTA 0:17f575135219 228 int GetParameter(FILE *fp, const char *paramName,char parameter[]);
HARUKIDELTA 0:17f575135219 229 int SetOptions(float *g_kpELE, float *g_kiELE, float *g_kdELE,
HARUKIDELTA 0:17f575135219 230 float *g_kpRUD, float *g_kiRUD, float *g_kdRUD,
HARUKIDELTA 0:17f575135219 231 float *g_rightloopROLL, float *g_rightloopPITCH,
HARUKIDELTA 0:17f575135219 232 float *g_leftloopROLL, float *g_leftloopPITCH,
HARUKIDELTA 0:17f575135219 233 float *g_gostraightROLL, float *g_gostraightPITCH,
HARUKIDELTA 0:17f575135219 234 float *g_takeoffTHR, float *g_loopTHR,
HARUKIDELTA 0:17f575135219 235 float *g_rightloopROLLshort, float *g_rightloopPITCHshort,
HARUKIDELTA 0:17f575135219 236 float *g_leftloopROLLshort, float *g_leftloopPITCHshort,
HARUKIDELTA 12:763bf416ba4b 237 float *g_glideloopROLL, float *g_glideloopPITCH,
HARUKIDELTA 13:77b13d5f4d78 238 float *g_kpAIL, float *g_kiAIL, float *g_kdAIL,
HARUKIDELTA 13:77b13d5f4d78 239 int *g_rightloopRUD, int *g_rightloopshortRUD,
HARUKIDELTA 13:77b13d5f4d78 240 int *g_leftloopRUD, int *g_leftloopshortRUD,
HARUKIDELTA 13:77b13d5f4d78 241 int *g_glideRUD,
HARUKIDELTA 13:77b13d5f4d78 242 int *g_AIL_L_correctionrightloop,int *g_AIL_L_correctionrightloopshort,
HARUKIDELTA 13:77b13d5f4d78 243 int *g_AIL_L_correctionlefttloop,int *g_AIL_L_correctionleftloopshort
HARUKIDELTA 13:77b13d5f4d78 244 );
HARUKIDELTA 0:17f575135219 245 //switch2割り込み
HARUKIDELTA 0:17f575135219 246 void ResetTrim();
HARUKIDELTA 0:17f575135219 247
HARUKIDELTA 0:17f575135219 248 //自動操縦
HARUKIDELTA 0:17f575135219 249 void UpdateTargetAngle_GoStraight(float targetAngle[3]);
HARUKIDELTA 19:0955311b0db6 250 void UpdateTargetAngle_GoStraight_zero(float targetAngle[3]); //着陸時にスロットルが0の時の直進
HARUKIDELTA 0:17f575135219 251 void UpdateTargetAngle_Rightloop(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 252 void UpdateTargetAngle_Rightloop_short(float targetAngle[3]);
HARUKIDELTA 19:0955311b0db6 253 void UpdateTargetAngle_Rightloop_zero(float targetAngle[3]); //着陸時にスロットルが0の時の右旋回
HARUKIDELTA 0:17f575135219 254 void UpdateTargetAngle_Leftloop(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 255 void UpdateTargetAngle_Leftloop_short(float targetAngle[3]);
HARUKIDELTA 19:0955311b0db6 256 void UpdateTargetAngle_Leftloop_zero(float targetAngle[3]); //着陸時にスロットルが0の時の左旋回
HARUKIDELTA 0:17f575135219 257 void UpdateTargetAngle_Moebius(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 258 void UpdateTargetAngle_Glide(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 259 void UpdateTargetAngle_Takeoff(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 260 void UpdateTargetAngle_Approach(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 261 void Take_off_and_landing(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 262
HARUKIDELTA 0:17f575135219 263 int Rotate(float targetAngle[3], float TargetYAW);
HARUKIDELTA 0:17f575135219 264
HARUKIDELTA 0:17f575135219 265 //投下
HARUKIDELTA 0:17f575135219 266 void Chicken_Drop();
HARUKIDELTA 0:17f575135219 267 void ReturnChickenServo1();
HARUKIDELTA 0:17f575135219 268 void ReturnChickenServo2();
HARUKIDELTA 0:17f575135219 269
HARUKIDELTA 0:17f575135219 270 //超音波による高度補正
HARUKIDELTA 0:17f575135219 271 void checkHeight(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 272 void UpdateTargetAngle_NoseUP(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 273 void UpdateTargetAngle_NoseDOWN(float targetAngle[3]);
HARUKIDELTA 0:17f575135219 274
HARUKIDELTA 0:17f575135219 275 //デバッグ用
HARUKIDELTA 18:cce82f3374fc 276 void Sbusprintf();
HARUKIDELTA 0:17f575135219 277 void DebugPrint();
HARUKIDELTA 0:17f575135219 278
HARUKIDELTA 0:17f575135219 279 /*---関数のプロトタイプ宣言終わり---*/
HARUKIDELTA 0:17f575135219 280
HARUKIDELTA 0:17f575135219 281 int main()
HARUKIDELTA 0:17f575135219 282 {
HARUKIDELTA 0:17f575135219 283 setup();
HARUKIDELTA 0:17f575135219 284
HARUKIDELTA 0:17f575135219 285
HARUKIDELTA 0:17f575135219 286 while(1){
HARUKIDELTA 0:17f575135219 287
HARUKIDELTA 0:17f575135219 288 loop();
HARUKIDELTA 0:17f575135219 289
taknokolat 10:652071c20bf6 290
HARUKIDELTA 18:cce82f3374fc 291 NVIC_DisableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 292 if(!CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 293 led3=0;
HARUKIDELTA 0:17f575135219 294 }else{
HARUKIDELTA 0:17f575135219 295 led3=1;
HARUKIDELTA 0:17f575135219 296 }
HARUKIDELTA 18:cce82f3374fc 297 NVIC_EnableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 298 }
HARUKIDELTA 0:17f575135219 299
HARUKIDELTA 0:17f575135219 300 }
HARUKIDELTA 0:17f575135219 301
HARUKIDELTA 0:17f575135219 302 void setup(){
HARUKIDELTA 0:17f575135219 303 //buzzer = 0;
HARUKIDELTA 0:17f575135219 304 led1 = 1;
HARUKIDELTA 0:17f575135219 305 led2 = 1;
HARUKIDELTA 0:17f575135219 306 led3 = 1;
HARUKIDELTA 0:17f575135219 307 led4 = 1;
HARUKIDELTA 0:17f575135219 308
HARUKIDELTA 0:17f575135219 309 SetOptions(&g_kpELE, &g_kiELE, &g_kdELE,
HARUKIDELTA 0:17f575135219 310 &g_kpRUD, &g_kiRUD, &g_kdRUD,
HARUKIDELTA 0:17f575135219 311 &g_rightloopROLL, &g_rightloopPITCH,
HARUKIDELTA 0:17f575135219 312 &g_leftloopROLL, &g_leftloopPITCH,
HARUKIDELTA 0:17f575135219 313 &g_gostraightROLL, &g_gostraightPITCH,
HARUKIDELTA 0:17f575135219 314 &g_takeoffTHR, &g_loopTHR,
HARUKIDELTA 0:17f575135219 315 &g_rightloopROLLshort, &g_rightloopPITCHshort,
HARUKIDELTA 0:17f575135219 316 &g_leftloopROLLshort, &g_leftloopPITCHshort,
HARUKIDELTA 12:763bf416ba4b 317 &g_glideloopROLL, &g_glideloopPITCH,
HARUKIDELTA 13:77b13d5f4d78 318 &g_kpAIL, &g_kiAIL,&g_kdAIL,
HARUKIDELTA 13:77b13d5f4d78 319 &g_rightloopRUD, &g_rightloopshortRUD,
HARUKIDELTA 13:77b13d5f4d78 320 &g_leftloopRUD, &g_leftloopshortRUD,
HARUKIDELTA 13:77b13d5f4d78 321 &g_glideloopRUD,
HARUKIDELTA 13:77b13d5f4d78 322 &g_AIL_L_correctionrightloop,&g_AIL_L_correctionrightloopshort,
HARUKIDELTA 13:77b13d5f4d78 323 &g_AIL_L_correctionleftloop,&g_AIL_L_correctionleftloopshort
HARUKIDELTA 13:77b13d5f4d78 324 );
HARUKIDELTA 0:17f575135219 325
HARUKIDELTA 0:17f575135219 326
HARUKIDELTA 0:17f575135219 327 Init_PWM();
HARUKIDELTA 0:17f575135219 328 Init_servo();
HARUKIDELTA 0:17f575135219 329 Init_sbus();
HARUKIDELTA 0:17f575135219 330 Init_sensors();
HARUKIDELTA 0:17f575135219 331 //switch2.rise(ResetTrim);
taknokolat 21:18fe7bf9e187 332
taknokolat 10:652071c20bf6 333 //USsensor.attach(&UpdateDist, 0.05);
HARUKIDELTA 0:17f575135219 334
HARUKIDELTA 0:17f575135219 335 NVIC_SetPriority(USART1_IRQn,0);
HARUKIDELTA 0:17f575135219 336 NVIC_SetPriority(EXTI0_IRQn,1);
taknokolat 24:3e7dcf16a6a7 337 NVIC_SetPriority(TIM5_IRQn,2);
taknokolat 24:3e7dcf16a6a7 338 NVIC_SetPriority(EXTI9_5_IRQn,3);
HARUKIDELTA 0:17f575135219 339 DisplayClock();
HARUKIDELTA 0:17f575135219 340 t.start();
HARUKIDELTA 0:17f575135219 341
HARUKIDELTA 0:17f575135219 342
HARUKIDELTA 0:17f575135219 343 pc.printf("MPU calibration start\r\n");
HARUKIDELTA 0:17f575135219 344
HARUKIDELTA 0:17f575135219 345 float offsetstart = t.read();
HARUKIDELTA 0:17f575135219 346 while(t.read() - offsetstart < 26){
HARUKIDELTA 0:17f575135219 347 SensingMPU();
HARUKIDELTA 0:17f575135219 348 for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]);
HARUKIDELTA 0:17f575135219 349 pc.printf("\r\n");
HARUKIDELTA 0:17f575135219 350 led1 = !led1;
HARUKIDELTA 0:17f575135219 351 led2 = !led2;
HARUKIDELTA 0:17f575135219 352 led3 = !led3;
HARUKIDELTA 0:17f575135219 353 led4 = !led4;
HARUKIDELTA 0:17f575135219 354 }
HARUKIDELTA 0:17f575135219 355
taknokolat 21:18fe7bf9e187 356 pc.attach(getSF_Serial, Serial::RxIrq);
taknokolat 24:3e7dcf16a6a7 357 NVIC_SetPriority(USART2_IRQn,4);
taknokolat 21:18fe7bf9e187 358
HARUKIDELTA 0:17f575135219 359 FirstROLL = nowAngle[ROLL];
HARUKIDELTA 0:17f575135219 360 FirstPITCH = nowAngle[PITCH];
HARUKIDELTA 0:17f575135219 361 nowAngle[ROLL] -=FirstROLL;
HARUKIDELTA 0:17f575135219 362 nowAngle[PITCH] -=FirstPITCH;
HARUKIDELTA 0:17f575135219 363
HARUKIDELTA 0:17f575135219 364 led1 = 0;
HARUKIDELTA 0:17f575135219 365 led2 = 0;
HARUKIDELTA 0:17f575135219 366 led3 = 0;
HARUKIDELTA 0:17f575135219 367 led4 = 0;
HARUKIDELTA 0:17f575135219 368 wait(0.2);
HARUKIDELTA 0:17f575135219 369
HARUKIDELTA 0:17f575135219 370
HARUKIDELTA 0:17f575135219 371 pc.printf("All initialized\r\n");
HARUKIDELTA 0:17f575135219 372 }
HARUKIDELTA 0:17f575135219 373
HARUKIDELTA 0:17f575135219 374 void loop(){
HARUKIDELTA 0:17f575135219 375 static float targetAngle[3] = {0.0, 0.0, 0.0}, controlValue[2] = {0.0, 0.0};
taknokolat 23:0f3b1441b08c 376 pc.printf("0\r\n");
HARUKIDELTA 0:17f575135219 377 SensingMPU();
taknokolat 23:0f3b1441b08c 378 pc.printf("1\r\n");
taknokolat 10:652071c20bf6 379 NVIC_DisableIRQ(USART2_IRQn);
HARUKIDELTA 0:17f575135219 380 UpdateTargetAngle(targetAngle);
taknokolat 23:0f3b1441b08c 381 pc.printf("2\r\n ");
HARUKIDELTA 0:17f575135219 382 //Rotate(targetAngle, 30.0);
HARUKIDELTA 0:17f575135219 383 CalculateControlValue(targetAngle, controlValue);
taknokolat 23:0f3b1441b08c 384 pc.printf("3\r\n");
taknokolat 10:652071c20bf6 385 NVIC_DisableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 386 UpdateAutoPWM(controlValue);
taknokolat 23:0f3b1441b08c 387 pc.printf("4\r\n");
taknokolat 10:652071c20bf6 388 NVIC_EnableIRQ(USART1_IRQn);
taknokolat 10:652071c20bf6 389 NVIC_EnableIRQ(USART2_IRQn);
taknokolat 23:0f3b1441b08c 390 pc.printf("5\r\n");
taknokolat 24:3e7dcf16a6a7 391
taknokolat 24:3e7dcf16a6a7 392 NVIC_SetPriority(TIM5_IRQn,4);
taknokolat 24:3e7dcf16a6a7 393 NVIC_SetPriority(USART2_IRQn,2);
taknokolat 24:3e7dcf16a6a7 394
taknokolat 11:c0b9ad25d3db 395 wait_ms(23);
taknokolat 24:3e7dcf16a6a7 396
taknokolat 24:3e7dcf16a6a7 397 NVIC_SetPriority(TIM5_IRQn,2);
taknokolat 24:3e7dcf16a6a7 398 NVIC_SetPriority(USART2_IRQn,4);
taknokolat 24:3e7dcf16a6a7 399
taknokolat 24:3e7dcf16a6a7 400
taknokolat 23:0f3b1441b08c 401 pc.printf("6\r\n");
taknokolat 23:0f3b1441b08c 402 //NVIC_DisableIRQ(USART2_IRQn);
taknokolat 23:0f3b1441b08c 403 //pc.printf("%c",g_landingcommand);
taknokolat 23:0f3b1441b08c 404 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:17f575135219 405 #if DEBUG_PRINT_INLOOP
HARUKIDELTA 18:cce82f3374fc 406 //Sbusprintf();
HARUKIDELTA 0:17f575135219 407 DebugPrint();
HARUKIDELTA 0:17f575135219 408 #endif
HARUKIDELTA 0:17f575135219 409 }
HARUKIDELTA 0:17f575135219 410
HARUKIDELTA 0:17f575135219 411 //サーボ初期化関数
HARUKIDELTA 0:17f575135219 412 void Init_servo(){
HARUKIDELTA 0:17f575135219 413
HARUKIDELTA 0:17f575135219 414 servo1.period_ms(14);
HARUKIDELTA 0:17f575135219 415 servo1.pulsewidth_us(trimpwm[AIL_R]);
HARUKIDELTA 0:17f575135219 416
HARUKIDELTA 0:17f575135219 417 servo2.period_ms(14);
HARUKIDELTA 0:17f575135219 418 servo2.pulsewidth_us(trimpwm[ELE]);
HARUKIDELTA 0:17f575135219 419
HARUKIDELTA 0:17f575135219 420 servo3.period_ms(14);
HARUKIDELTA 0:17f575135219 421 servo3.pulsewidth_us(trimpwm[THR]);
HARUKIDELTA 0:17f575135219 422
HARUKIDELTA 0:17f575135219 423 servo4.period_ms(14);
HARUKIDELTA 0:17f575135219 424 servo4.pulsewidth_us(trimpwm[RUD]);
HARUKIDELTA 0:17f575135219 425
HARUKIDELTA 0:17f575135219 426 servo5.period_ms(14);
HARUKIDELTA 0:17f575135219 427 servo5.pulsewidth_us(1392);
HARUKIDELTA 0:17f575135219 428
HARUKIDELTA 0:17f575135219 429 servo6.period_ms(14);
HARUKIDELTA 0:17f575135219 430 servo6.pulsewidth_us(trimpwm[AIL_L]);
HARUKIDELTA 0:17f575135219 431
HARUKIDELTA 0:17f575135219 432 pc.printf("servo initialized\r\n");
HARUKIDELTA 0:17f575135219 433 }
HARUKIDELTA 0:17f575135219 434
HARUKIDELTA 0:17f575135219 435 //Sbus初期化
HARUKIDELTA 0:17f575135219 436 void Init_sbus(){
HARUKIDELTA 0:17f575135219 437 sbus.initialize();
HARUKIDELTA 0:17f575135219 438 sbus.setLastfuncPoint(Update_PWM);
HARUKIDELTA 0:17f575135219 439 sbus.startInterrupt();
HARUKIDELTA 0:17f575135219 440 }
HARUKIDELTA 0:17f575135219 441
HARUKIDELTA 0:17f575135219 442 void Init_sensors(){
HARUKIDELTA 0:17f575135219 443 if(mpu6050.setup() == -1){
HARUKIDELTA 0:17f575135219 444 pc.printf("failed initialize\r\n");
HARUKIDELTA 0:17f575135219 445 while(1){
HARUKIDELTA 0:17f575135219 446 led1 = 1; led2 = 0; led3 = 1; led4 = 0;
HARUKIDELTA 0:17f575135219 447 wait(1);
HARUKIDELTA 0:17f575135219 448 led1 = 0; led2 = 1; led3 = 0; led4 = 1;
HARUKIDELTA 0:17f575135219 449 wait(1);
HARUKIDELTA 0:17f575135219 450 }
HARUKIDELTA 0:17f575135219 451 }
HARUKIDELTA 0:17f575135219 452 }
HARUKIDELTA 0:17f575135219 453
HARUKIDELTA 0:17f575135219 454 void Init_PWM(){
HARUKIDELTA 0:17f575135219 455 pc.printf("PWM initialized\r\n");
HARUKIDELTA 0:17f575135219 456 }
HARUKIDELTA 0:17f575135219 457
HARUKIDELTA 0:17f575135219 458 void DisplayClock(){
HARUKIDELTA 0:17f575135219 459 pc.printf("System Clock = %d[MHz]\r\n", HAL_RCC_GetSysClockFreq()/1000000);
HARUKIDELTA 0:17f575135219 460 pc.printf("HCLK Clock = %d[MHz]\r\n", HAL_RCC_GetHCLKFreq()/1000000);
HARUKIDELTA 0:17f575135219 461 pc.printf("PCLK1 Clock = %d[MHz]\r\n", HAL_RCC_GetPCLK1Freq()/1000000);
HARUKIDELTA 0:17f575135219 462 pc.printf("PCLK2 Clock = %d[MHz]\r\n", HAL_RCC_GetPCLK2Freq()/1000000);
HARUKIDELTA 0:17f575135219 463 pc.printf("\r\n");
HARUKIDELTA 0:17f575135219 464 }
HARUKIDELTA 0:17f575135219 465
HARUKIDELTA 0:17f575135219 466 void UpdateTargetAngle(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 467 static int16_t count_op = 0;
HARUKIDELTA 0:17f575135219 468 #if DEBUG_SEMIAUTO
HARUKIDELTA 0:17f575135219 469 switch(operation_mode){
HARUKIDELTA 0:17f575135219 470 case StartUp:
HARUKIDELTA 0:17f575135219 471 if(!CheckSW_Up(Ch7) && CheckSW_Up(Ch8)){
HARUKIDELTA 0:17f575135219 472 count_op++;
HARUKIDELTA 0:17f575135219 473 if(count_op > changeModeCount){
HARUKIDELTA 0:17f575135219 474 operation_mode = SemiAuto;
HARUKIDELTA 0:17f575135219 475 pc.printf("Goto SemiAuto mode\r\n");
HARUKIDELTA 0:17f575135219 476 count_op = 0;
HARUKIDELTA 0:17f575135219 477 }
HARUKIDELTA 0:17f575135219 478 }else count_op = 0;
HARUKIDELTA 0:17f575135219 479 break;
HARUKIDELTA 0:17f575135219 480
HARUKIDELTA 0:17f575135219 481 case SemiAuto:
HARUKIDELTA 0:17f575135219 482 /* 大会用では以下のif文を入れてoperation_modeを変える
HARUKIDELTA 0:17f575135219 483 if(CheckSW_Up(Ch6)){
HARUKIDELTA 0:17f575135219 484 count_op++;
HARUKIDELTA 0:17f575135219 485 if(count_op>changeModeCount){
HARUKIDELTA 0:17f575135219 486 output_status = XXX;
HARUKIDELTA 0:17f575135219 487 led2 = 0;
HARUKIDELTA 0:17f575135219 488 pc.printf("Goto XXX mode\r\n");
HARUKIDELTA 0:17f575135219 489 count_op = 0;
HARUKIDELTA 0:17f575135219 490 }else count_op = 0;
HARUKIDELTA 0:17f575135219 491 ConvertPWMintoRAD(targetAngle);
HARUKIDELTA 0:17f575135219 492 }
HARUKIDELTA 0:17f575135219 493 */
HARUKIDELTA 0:17f575135219 494 ConvertPWMintoRAD(targetAngle);
HARUKIDELTA 0:17f575135219 495 break;
HARUKIDELTA 0:17f575135219 496
HARUKIDELTA 0:17f575135219 497 default:
HARUKIDELTA 0:17f575135219 498 operation_mode = SemiAuto;
HARUKIDELTA 0:17f575135219 499 break;
HARUKIDELTA 0:17f575135219 500 }
HARUKIDELTA 0:17f575135219 501
HARUKIDELTA 0:17f575135219 502 #else
HARUKIDELTA 0:17f575135219 503
HARUKIDELTA 0:17f575135219 504 switch(operation_mode){
HARUKIDELTA 0:17f575135219 505 case StartUp:
HARUKIDELTA 0:17f575135219 506 if(!CheckSW_Up(Ch7) && !CheckSW_Up(Ch8)){ //ch7;自動・手動切り替え ch8;自動操縦モード切替
HARUKIDELTA 0:17f575135219 507 count_op++;
HARUKIDELTA 0:17f575135219 508 if(count_op > changeModeCount){
HARUKIDELTA 0:17f575135219 509 operation_mode = RightLoop;
HARUKIDELTA 0:17f575135219 510 pc.printf("Goto RightLoop mode\r\n");
HARUKIDELTA 0:17f575135219 511 count_op = 0;
HARUKIDELTA 0:17f575135219 512 }
HARUKIDELTA 0:17f575135219 513 }else count_op = 0;
HARUKIDELTA 0:17f575135219 514 break;
HARUKIDELTA 0:17f575135219 515
HARUKIDELTA 0:17f575135219 516 case RightLoop:
HARUKIDELTA 0:17f575135219 517 if(!CheckSW_Up(Ch7) && CheckSW_Up(Ch8)){
HARUKIDELTA 0:17f575135219 518 count_op++;
HARUKIDELTA 0:17f575135219 519 if(count_op > changeModeCount){
HARUKIDELTA 0:17f575135219 520 operation_mode = LeftLoop;
HARUKIDELTA 0:17f575135219 521 pc.printf("Goto LeftLoop mode\r\n");
HARUKIDELTA 0:17f575135219 522 count_op = 0;
HARUKIDELTA 0:17f575135219 523 }
HARUKIDELTA 0:17f575135219 524 }else count_op = 0;
HARUKIDELTA 0:17f575135219 525 UpdateTargetAngle_Rightloop(targetAngle);
HARUKIDELTA 0:17f575135219 526
HARUKIDELTA 0:17f575135219 527 break;
HARUKIDELTA 0:17f575135219 528
HARUKIDELTA 0:17f575135219 529 case LeftLoop:
HARUKIDELTA 0:17f575135219 530 if(!CheckSW_Up(Ch7) && !CheckSW_Up(Ch8)){
HARUKIDELTA 0:17f575135219 531 count_op++;
HARUKIDELTA 0:17f575135219 532 if(count_op > changeModeCount){
HARUKIDELTA 0:17f575135219 533 operation_mode = GoStraight;
HARUKIDELTA 0:17f575135219 534 pc.printf("Goto GoStraight mode\r\n");
HARUKIDELTA 0:17f575135219 535 count_op = 0;
HARUKIDELTA 0:17f575135219 536 }
HARUKIDELTA 0:17f575135219 537 }else count_op = 0;
HARUKIDELTA 0:17f575135219 538 UpdateTargetAngle_Leftloop(targetAngle);
HARUKIDELTA 0:17f575135219 539 break;
HARUKIDELTA 0:17f575135219 540
HARUKIDELTA 0:17f575135219 541 case GoStraight:
HARUKIDELTA 0:17f575135219 542 if(!CheckSW_Up(Ch7) && CheckSW_Up(Ch8)){
HARUKIDELTA 0:17f575135219 543 count_op++;
HARUKIDELTA 0:17f575135219 544 if(count_op > changeModeCount){
HARUKIDELTA 0:17f575135219 545 operation_mode = Moebius;
HARUKIDELTA 0:17f575135219 546 pc.printf("Goto Moebius mode\r\n");
HARUKIDELTA 0:17f575135219 547 count_op = 0;
HARUKIDELTA 0:17f575135219 548 }
HARUKIDELTA 0:17f575135219 549 }else count_op = 0;
HARUKIDELTA 0:17f575135219 550 UpdateTargetAngle_GoStraight(targetAngle);
HARUKIDELTA 0:17f575135219 551 break;
HARUKIDELTA 0:17f575135219 552
HARUKIDELTA 0:17f575135219 553 case Moebius:
HARUKIDELTA 0:17f575135219 554 if(!CheckSW_Up(Ch7) && !CheckSW_Up(Ch8)){
HARUKIDELTA 0:17f575135219 555 count_op++;
HARUKIDELTA 0:17f575135219 556 if(count_op > changeModeCount){
HARUKIDELTA 0:17f575135219 557 operation_mode = Glide;
HARUKIDELTA 0:17f575135219 558 pc.printf("Goto Glide mode\r\n");
HARUKIDELTA 0:17f575135219 559 count_op = 0;
HARUKIDELTA 0:17f575135219 560 }
HARUKIDELTA 0:17f575135219 561 }else count_op = 0;
HARUKIDELTA 0:17f575135219 562 UpdateTargetAngle_Moebius(targetAngle);
HARUKIDELTA 0:17f575135219 563 break;
HARUKIDELTA 0:17f575135219 564
HARUKIDELTA 0:17f575135219 565 case Glide:
HARUKIDELTA 0:17f575135219 566 if(!CheckSW_Up(Ch7) && CheckSW_Up(Ch8)){
HARUKIDELTA 0:17f575135219 567 count_op++;
HARUKIDELTA 0:17f575135219 568 if(count_op > changeModeCount){
HARUKIDELTA 0:17f575135219 569 operation_mode = BombwithPC;
HARUKIDELTA 0:17f575135219 570 pc.printf("Goto Bombing mode\r\n");
HARUKIDELTA 0:17f575135219 571 count_op = 0;
HARUKIDELTA 0:17f575135219 572 }
HARUKIDELTA 0:17f575135219 573 }else count_op = 0;
HARUKIDELTA 0:17f575135219 574 UpdateTargetAngle_Glide(targetAngle);
HARUKIDELTA 0:17f575135219 575 break;
HARUKIDELTA 0:17f575135219 576
HARUKIDELTA 0:17f575135219 577 case BombwithPC:
HARUKIDELTA 0:17f575135219 578 if(!CheckSW_Up(Ch7) && !CheckSW_Up(Ch8)){
HARUKIDELTA 0:17f575135219 579 count_op++;
HARUKIDELTA 0:17f575135219 580 if(count_op > changeModeCount){
HARUKIDELTA 0:17f575135219 581 operation_mode = RightLoop;
HARUKIDELTA 0:17f575135219 582 pc.printf("Goto RightLoop mode\r\n");
HARUKIDELTA 0:17f575135219 583 count_op = 0;
HARUKIDELTA 0:17f575135219 584 }
HARUKIDELTA 0:17f575135219 585 }else count_op = 0;
HARUKIDELTA 0:17f575135219 586 Take_off_and_landing(targetAngle);
HARUKIDELTA 0:17f575135219 587 break;
HARUKIDELTA 0:17f575135219 588
HARUKIDELTA 0:17f575135219 589 default:
HARUKIDELTA 0:17f575135219 590 operation_mode = StartUp;
HARUKIDELTA 0:17f575135219 591 break;
HARUKIDELTA 0:17f575135219 592 }
HARUKIDELTA 0:17f575135219 593 #endif
HARUKIDELTA 0:17f575135219 594
HARUKIDELTA 0:17f575135219 595 if(CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 596 output_status = Auto;
HARUKIDELTA 0:17f575135219 597 led1 = 1;
HARUKIDELTA 0:17f575135219 598 }else{
HARUKIDELTA 0:17f575135219 599 output_status = Manual;
HARUKIDELTA 0:17f575135219 600 led1 = 0;
HARUKIDELTA 22:438bedf24707 601 zeroTHR=1;
HARUKIDELTA 0:17f575135219 602 }
HARUKIDELTA 0:17f575135219 603 }
HARUKIDELTA 0:17f575135219 604
HARUKIDELTA 0:17f575135219 605 int GetParameter(FILE *fp, const char *paramName,char parameter[]){
HARUKIDELTA 0:17f575135219 606 int i=0, j=0;
HARUKIDELTA 0:17f575135219 607 int strmax = 200;
HARUKIDELTA 0:17f575135219 608 char str[strmax];
HARUKIDELTA 0:17f575135219 609
HARUKIDELTA 0:17f575135219 610 rewind(fp); //ファイル位置を先頭に
HARUKIDELTA 0:17f575135219 611 while(1){
HARUKIDELTA 0:17f575135219 612 if (fgets(str, strmax, fp) == NULL) {
HARUKIDELTA 0:17f575135219 613 return 0;
HARUKIDELTA 0:17f575135219 614 }
HARUKIDELTA 0:17f575135219 615 if (!strncmp(str, paramName, strlen(paramName))) {
HARUKIDELTA 0:17f575135219 616 while (str[i++] != '=') {}
HARUKIDELTA 0:17f575135219 617 while (str[i] != '\n') {
HARUKIDELTA 0:17f575135219 618 parameter[j++] = str[i++];
HARUKIDELTA 0:17f575135219 619 }
HARUKIDELTA 0:17f575135219 620 parameter[j] = '\0';
HARUKIDELTA 0:17f575135219 621 return 1;
HARUKIDELTA 0:17f575135219 622 }
HARUKIDELTA 0:17f575135219 623 }
HARUKIDELTA 0:17f575135219 624 return 0;
HARUKIDELTA 0:17f575135219 625 }
HARUKIDELTA 0:17f575135219 626
HARUKIDELTA 0:17f575135219 627
HARUKIDELTA 0:17f575135219 628 //sdによる設定
HARUKIDELTA 0:17f575135219 629 int SetOptions(float *g_kpELE, float *g_kiELE, float *g_kdELE,
HARUKIDELTA 0:17f575135219 630 float *g_kpRUD, float *g_kiRUD, float *g_kdRUD,
HARUKIDELTA 0:17f575135219 631 float *g_rightloopROLL, float *g_rightloopPITCH,
HARUKIDELTA 0:17f575135219 632 float *g_leftloopROLL, float *g_leftloopPITCH,
HARUKIDELTA 0:17f575135219 633 float *g_gostraightROLL, float *g_gostraightPITCH,
HARUKIDELTA 0:17f575135219 634 float *g_takeoffTHR, float *g_loopTHR,
HARUKIDELTA 0:17f575135219 635 float *g_rightloopROLLshort, float *g_rightloopPITCHshort,
HARUKIDELTA 0:17f575135219 636 float *g_leftloopROLLshort, float *g_leftloopPITCHshort,
HARUKIDELTA 12:763bf416ba4b 637 float *g_glideloopROLL, float *g_glideloopPITCH,
HARUKIDELTA 13:77b13d5f4d78 638 float *g_kpAIL, float *g_kiAIL, float *g_kdAIL,
HARUKIDELTA 13:77b13d5f4d78 639 int *g_rightloopRUD, int *g_rightloopshortRUD,
HARUKIDELTA 13:77b13d5f4d78 640 int *g_leftloopRUD, int *g_leftloopshortRUD,
HARUKIDELTA 13:77b13d5f4d78 641 int *g_glideloopRUD,
HARUKIDELTA 13:77b13d5f4d78 642 int *g_AIL_L_correctionrightloop,int *g_AIL_L_correctionrightloopshort,
HARUKIDELTA 13:77b13d5f4d78 643 int *g_AIL_L_correctionleftloop,int *g_AIL_L_correctionleftloopshort
HARUKIDELTA 0:17f575135219 644 ){
HARUKIDELTA 0:17f575135219 645
HARUKIDELTA 0:17f575135219 646 pc.printf("SDsetup start.\r\n");
HARUKIDELTA 0:17f575135219 647
HARUKIDELTA 0:17f575135219 648 FILE *fp;
HARUKIDELTA 13:77b13d5f4d78 649 char parameter[40]; //文字列渡す用の配列
HARUKIDELTA 0:17f575135219 650 int SDerrorcount = 0; //取得できなかった数を返す
HARUKIDELTA 0:17f575135219 651 const char *paramNames[] = {
HARUKIDELTA 0:17f575135219 652 "KP_ELEVATOR",
HARUKIDELTA 0:17f575135219 653 "KI_ELEVATOR",
HARUKIDELTA 0:17f575135219 654 "KD_ELEVATOR",
HARUKIDELTA 0:17f575135219 655 "KP_RUDDER",
HARUKIDELTA 0:17f575135219 656 "KI_RUDDER",
HARUKIDELTA 0:17f575135219 657 "KD_RUDDER",
HARUKIDELTA 0:17f575135219 658 "RIGHTLOOP_ROLL",
HARUKIDELTA 0:17f575135219 659 "RIGHTLOOP_PITCH",
HARUKIDELTA 0:17f575135219 660 "LEFTLOOP_ROLL",
HARUKIDELTA 0:17f575135219 661 "LEFTLOOP_PITCH",
HARUKIDELTA 0:17f575135219 662 "GOSTRAIGHT_ROLL",
HARUKIDELTA 0:17f575135219 663 "GOSTRAIGHT_PITCH",
HARUKIDELTA 0:17f575135219 664 "TAKEOFF_THR_RATE",
HARUKIDELTA 0:17f575135219 665 "LOOP_THR_RATE",
HARUKIDELTA 0:17f575135219 666 "RIGHTLOOP_ROLL_SHORT",
HARUKIDELTA 0:17f575135219 667 "RIGHTLOOP_PITCH_SHORT",
HARUKIDELTA 0:17f575135219 668 "LEFTLOOP_ROLL_SHORT",
HARUKIDELTA 0:17f575135219 669 "LEFTLOOP_PITCH_SHORT",
HARUKIDELTA 0:17f575135219 670 "AUTOGLIDE_ROLL",
HARUKIDELTA 12:763bf416ba4b 671 "AUTOGLIDE PITCH",
HARUKIDELTA 12:763bf416ba4b 672 "KP_AILERON",
HARUKIDELTA 12:763bf416ba4b 673 "KI_AILERON",
HARUKIDELTA 13:77b13d5f4d78 674 "KD_AILERON",
HARUKIDELTA 13:77b13d5f4d78 675 "RIGHTLOOP_RUDDER",
HARUKIDELTA 13:77b13d5f4d78 676 "RIGHTLOOPSHORT_RUDDER",
HARUKIDELTA 13:77b13d5f4d78 677 "LEFTLOOP_RUDDER",
HARUKIDELTA 13:77b13d5f4d78 678 "LEFTLOOPSHORT_RUDDER",
HARUKIDELTA 13:77b13d5f4d78 679 "GLIDELOOP_RUDDER",
HARUKIDELTA 13:77b13d5f4d78 680 "AILERON_LEFT_CORRECTION_RIGHTLOOP",
HARUKIDELTA 13:77b13d5f4d78 681 "AILERON_LEFT_CORRECTION_RIGHTLOOPSHORT",
HARUKIDELTA 13:77b13d5f4d78 682 "AILERON_LEFT_CORRECTION_LEFTLOOP",
HARUKIDELTA 13:77b13d5f4d78 683 "AILERON_LEFT_CORRECTION_LEFTLOOPSHORT"
HARUKIDELTA 0:17f575135219 684 };
HARUKIDELTA 0:17f575135219 685
HARUKIDELTA 0:17f575135219 686 fp = fopen("/sd/option.txt","r");
HARUKIDELTA 0:17f575135219 687
HARUKIDELTA 0:17f575135219 688 if(fp != NULL){ //開けたら
HARUKIDELTA 0:17f575135219 689 pc.printf("File was openned.\r\n");
HARUKIDELTA 0:17f575135219 690 if(GetParameter(fp,paramNames[0],parameter)) *g_kpELE = atof(parameter);
HARUKIDELTA 0:17f575135219 691 else{ *g_kpELE = KP_ELE;
HARUKIDELTA 0:17f575135219 692 SDerrorcount++;
HARUKIDELTA 0:17f575135219 693 }
HARUKIDELTA 0:17f575135219 694 if(GetParameter(fp,paramNames[1],parameter)) *g_kiELE = atof(parameter);
HARUKIDELTA 0:17f575135219 695 else{ *g_kiELE = KI_ELE;
HARUKIDELTA 0:17f575135219 696 SDerrorcount++;
HARUKIDELTA 0:17f575135219 697 }
HARUKIDELTA 0:17f575135219 698 if(GetParameter(fp,paramNames[2],parameter)) *g_kdELE = atof(parameter);
HARUKIDELTA 0:17f575135219 699 else{ *g_kdELE = KD_ELE;
HARUKIDELTA 0:17f575135219 700 SDerrorcount++;
HARUKIDELTA 0:17f575135219 701 }
HARUKIDELTA 0:17f575135219 702 if(GetParameter(fp,paramNames[3],parameter)) *g_kpRUD = atof(parameter);
HARUKIDELTA 0:17f575135219 703 else{ *g_kpRUD = KP_RUD;
HARUKIDELTA 0:17f575135219 704 SDerrorcount++;
HARUKIDELTA 0:17f575135219 705 }
HARUKIDELTA 0:17f575135219 706 if(GetParameter(fp,paramNames[4],parameter)) *g_kiRUD = atof(parameter);
HARUKIDELTA 0:17f575135219 707 else{ *g_kiRUD = KI_RUD;
HARUKIDELTA 0:17f575135219 708 SDerrorcount++;
HARUKIDELTA 0:17f575135219 709 }
HARUKIDELTA 0:17f575135219 710 if(GetParameter(fp,paramNames[5],parameter)) *g_kdRUD = atof(parameter);
HARUKIDELTA 0:17f575135219 711 else{ *g_kdRUD = KD_RUD;
HARUKIDELTA 0:17f575135219 712 SDerrorcount++;
HARUKIDELTA 0:17f575135219 713 }
HARUKIDELTA 0:17f575135219 714 if(GetParameter(fp,paramNames[6],parameter)) *g_rightloopROLL = atof(parameter);
HARUKIDELTA 0:17f575135219 715 else{ *g_rightloopROLL = RIGHT_ROLL;
HARUKIDELTA 0:17f575135219 716 SDerrorcount++;
HARUKIDELTA 0:17f575135219 717 }
HARUKIDELTA 0:17f575135219 718 if(GetParameter(fp,paramNames[7],parameter)) *g_rightloopPITCH = atof(parameter);
HARUKIDELTA 0:17f575135219 719 else{ *g_rightloopPITCH = RIGHT_PITCH;
HARUKIDELTA 0:17f575135219 720 SDerrorcount++;
HARUKIDELTA 0:17f575135219 721 }
HARUKIDELTA 0:17f575135219 722 if(GetParameter(fp,paramNames[8],parameter)) *g_leftloopROLL = atof(parameter);
HARUKIDELTA 0:17f575135219 723 else{ *g_leftloopROLL = LEFT_ROLL;
HARUKIDELTA 0:17f575135219 724 SDerrorcount++;
HARUKIDELTA 0:17f575135219 725 }
HARUKIDELTA 0:17f575135219 726 if(GetParameter(fp,paramNames[9],parameter)) *g_leftloopPITCH = atof(parameter);
HARUKIDELTA 0:17f575135219 727 else{ *g_leftloopPITCH = LEFT_PITCH;
HARUKIDELTA 0:17f575135219 728 SDerrorcount++;
HARUKIDELTA 0:17f575135219 729 }
HARUKIDELTA 0:17f575135219 730 if(GetParameter(fp,paramNames[10],parameter)) *g_gostraightROLL = atof(parameter);
HARUKIDELTA 0:17f575135219 731 else{ *g_gostraightROLL = STRAIGHT_ROLL;
HARUKIDELTA 0:17f575135219 732 SDerrorcount++;
HARUKIDELTA 0:17f575135219 733 }
HARUKIDELTA 0:17f575135219 734 if(GetParameter(fp,paramNames[11],parameter)) *g_gostraightPITCH = atof(parameter);
HARUKIDELTA 0:17f575135219 735 else{ *g_gostraightPITCH = STRAIGHT_PITCH;
HARUKIDELTA 0:17f575135219 736 SDerrorcount++;
HARUKIDELTA 0:17f575135219 737 }
HARUKIDELTA 0:17f575135219 738 if(GetParameter(fp,paramNames[12],parameter)) *g_takeoffTHR = atof(parameter);
HARUKIDELTA 0:17f575135219 739 else{ *g_takeoffTHR = TAKEOFF_THR;
HARUKIDELTA 0:17f575135219 740 SDerrorcount++;
HARUKIDELTA 0:17f575135219 741 }
HARUKIDELTA 0:17f575135219 742 if(GetParameter(fp,paramNames[13],parameter)) *g_loopTHR = atof(parameter);
HARUKIDELTA 0:17f575135219 743 else{ *g_loopTHR = LOOP_THR;
HARUKIDELTA 0:17f575135219 744 SDerrorcount++;
HARUKIDELTA 0:17f575135219 745 }
HARUKIDELTA 0:17f575135219 746 if(GetParameter(fp,paramNames[14],parameter)) *g_rightloopROLLshort = atof(parameter);
HARUKIDELTA 0:17f575135219 747 else{ *g_rightloopROLLshort = RIGHT_ROLL_SHORT;
HARUKIDELTA 0:17f575135219 748 SDerrorcount++;
HARUKIDELTA 0:17f575135219 749 }
HARUKIDELTA 0:17f575135219 750 if(GetParameter(fp,paramNames[15],parameter)) *g_rightloopPITCHshort = atof(parameter);
HARUKIDELTA 0:17f575135219 751 else{ *g_rightloopPITCHshort = RIGHT_PITCH_SHORT;
HARUKIDELTA 0:17f575135219 752 SDerrorcount++;
HARUKIDELTA 0:17f575135219 753 }
HARUKIDELTA 0:17f575135219 754 if(GetParameter(fp,paramNames[16],parameter)) *g_leftloopROLLshort = atof(parameter);
HARUKIDELTA 0:17f575135219 755 else{ *g_leftloopROLLshort = LEFT_ROLL_SHORT;
HARUKIDELTA 0:17f575135219 756 SDerrorcount++;
HARUKIDELTA 0:17f575135219 757 }
HARUKIDELTA 0:17f575135219 758 if(GetParameter(fp,paramNames[17],parameter)) *g_leftloopPITCHshort = atof(parameter);
HARUKIDELTA 0:17f575135219 759 else{ *g_leftloopPITCHshort = LEFT_PITCH_SHORT;
HARUKIDELTA 0:17f575135219 760 SDerrorcount++;
HARUKIDELTA 0:17f575135219 761 }
HARUKIDELTA 0:17f575135219 762 if(GetParameter(fp,paramNames[18],parameter)) *g_glideloopROLL = atof(parameter);
HARUKIDELTA 0:17f575135219 763 else{ *g_glideloopROLL = GLIDE_ROLL;
HARUKIDELTA 0:17f575135219 764 SDerrorcount++;
HARUKIDELTA 0:17f575135219 765 }
HARUKIDELTA 0:17f575135219 766 if(GetParameter(fp,paramNames[19],parameter)) *g_glideloopPITCH = atof(parameter);
HARUKIDELTA 0:17f575135219 767 else{ *g_glideloopPITCH = GLIDE_PITCH;
HARUKIDELTA 0:17f575135219 768 SDerrorcount++;
HARUKIDELTA 0:17f575135219 769 }
HARUKIDELTA 18:cce82f3374fc 770 if(GetParameter(fp,paramNames[20],parameter)) *g_kpAIL = atof(parameter);
HARUKIDELTA 12:763bf416ba4b 771 else{ *g_kpAIL = KP_AIL;
HARUKIDELTA 12:763bf416ba4b 772 SDerrorcount++;
HARUKIDELTA 12:763bf416ba4b 773 }
HARUKIDELTA 12:763bf416ba4b 774 if(GetParameter(fp,paramNames[21],parameter)) *g_kiAIL = atof(parameter);
HARUKIDELTA 12:763bf416ba4b 775 else{ *g_kiAIL = KI_AIL;
HARUKIDELTA 12:763bf416ba4b 776 SDerrorcount++;
HARUKIDELTA 12:763bf416ba4b 777 }
HARUKIDELTA 12:763bf416ba4b 778 if(GetParameter(fp,paramNames[22],parameter)) *g_kdAIL = atof(parameter);
HARUKIDELTA 12:763bf416ba4b 779 else{ *g_kdAIL = KP_AIL;
HARUKIDELTA 12:763bf416ba4b 780 SDerrorcount++;
HARUKIDELTA 12:763bf416ba4b 781 }
HARUKIDELTA 13:77b13d5f4d78 782 if(GetParameter(fp,paramNames[23],parameter)) *g_rightloopRUD = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 783 else{ *g_rightloopRUD = RIGHTLOOP_RUD;
HARUKIDELTA 13:77b13d5f4d78 784 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 785 }
HARUKIDELTA 13:77b13d5f4d78 786 if(GetParameter(fp,paramNames[24],parameter)) *g_rightloopshortRUD = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 787 else{ *g_rightloopshortRUD = RIGHTLOOPSHORT_RUD;
HARUKIDELTA 13:77b13d5f4d78 788 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 789 }
HARUKIDELTA 13:77b13d5f4d78 790 if(GetParameter(fp,paramNames[25],parameter)) *g_leftloopRUD = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 791 else{ *g_leftloopshortRUD = LEFTLOOP_RUD;
HARUKIDELTA 13:77b13d5f4d78 792 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 793 }
HARUKIDELTA 13:77b13d5f4d78 794 if(GetParameter(fp,paramNames[26],parameter)) *g_leftloopshortRUD = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 795 else{ *g_leftloopshortRUD = LEFTLOOPSHORT_RUD;
HARUKIDELTA 13:77b13d5f4d78 796 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 797 }
HARUKIDELTA 13:77b13d5f4d78 798 if(GetParameter(fp,paramNames[27],parameter)) *g_glideloopRUD = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 799 else{ *g_glideloopRUD = GLIDELOOP_RUD;
HARUKIDELTA 13:77b13d5f4d78 800 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 801 }
HARUKIDELTA 13:77b13d5f4d78 802 if(GetParameter(fp,paramNames[28],parameter)) *g_AIL_L_correctionrightloop = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 803 else{ *g_AIL_L_correctionrightloop = AIL_L_CORRECTION_RIGHTLOOP;
HARUKIDELTA 13:77b13d5f4d78 804 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 805 }
HARUKIDELTA 13:77b13d5f4d78 806 if(GetParameter(fp,paramNames[29],parameter)) *g_AIL_L_correctionrightloopshort = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 807 else{ *g_AIL_L_correctionrightloopshort = AIL_L_CORRECTION_RIGHTLOOPSHORT;
HARUKIDELTA 13:77b13d5f4d78 808 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 809 }
HARUKIDELTA 13:77b13d5f4d78 810 if(GetParameter(fp,paramNames[30],parameter)) *g_AIL_L_correctionleftloop = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 811 else{ *g_AIL_L_correctionleftloop = AIL_L_CORRECTION_LEFTLOOP;
HARUKIDELTA 13:77b13d5f4d78 812 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 813 }
HARUKIDELTA 13:77b13d5f4d78 814 if(GetParameter(fp,paramNames[31],parameter)) *g_AIL_L_correctionleftloopshort = atof(parameter);
HARUKIDELTA 13:77b13d5f4d78 815 else{ *g_AIL_L_correctionleftloopshort = AIL_L_CORRECTION_LEFTLOOPSHORT;
HARUKIDELTA 13:77b13d5f4d78 816 SDerrorcount++;
HARUKIDELTA 13:77b13d5f4d78 817 }
HARUKIDELTA 13:77b13d5f4d78 818
HARUKIDELTA 0:17f575135219 819 fclose(fp);
HARUKIDELTA 0:17f575135219 820
HARUKIDELTA 0:17f575135219 821 }else{ //ファイルがなかったら
HARUKIDELTA 0:17f575135219 822 pc.printf("fp was null.\r\n");
HARUKIDELTA 0:17f575135219 823 *g_kpELE = KP_ELE;
HARUKIDELTA 0:17f575135219 824 *g_kiELE = KI_ELE;
HARUKIDELTA 0:17f575135219 825 *g_kdELE = KD_ELE;
HARUKIDELTA 0:17f575135219 826 *g_kpRUD = KP_RUD;
HARUKIDELTA 0:17f575135219 827 *g_kiRUD = KI_RUD;
HARUKIDELTA 0:17f575135219 828 *g_kdRUD = KD_RUD;
HARUKIDELTA 0:17f575135219 829 *g_rightloopROLL = RIGHT_ROLL;
HARUKIDELTA 0:17f575135219 830 *g_rightloopPITCH = RIGHT_PITCH;
HARUKIDELTA 0:17f575135219 831 *g_leftloopROLL = LEFT_ROLL;
HARUKIDELTA 0:17f575135219 832 *g_leftloopPITCH = LEFT_PITCH;
HARUKIDELTA 0:17f575135219 833 *g_gostraightROLL = STRAIGHT_ROLL;
HARUKIDELTA 0:17f575135219 834 *g_gostraightPITCH = STRAIGHT_PITCH;
HARUKIDELTA 0:17f575135219 835 *g_takeoffTHR = TAKEOFF_THR;
HARUKIDELTA 0:17f575135219 836 *g_loopTHR = LOOP_THR;
HARUKIDELTA 12:763bf416ba4b 837 *g_kpAIL = KP_AIL; //パラメータ変えるのお忘れなく!!
HARUKIDELTA 12:763bf416ba4b 838 *g_kiAIL = KI_AIL;
HARUKIDELTA 12:763bf416ba4b 839 *g_kdAIL = KD_AIL;
HARUKIDELTA 13:77b13d5f4d78 840 *g_rightloopRUD = RIGHTLOOP_RUD;
HARUKIDELTA 13:77b13d5f4d78 841 *g_rightloopshortRUD = RIGHTLOOPSHORT_RUD;
HARUKIDELTA 13:77b13d5f4d78 842 *g_leftloopRUD = LEFTLOOP_RUD;
HARUKIDELTA 13:77b13d5f4d78 843 *g_leftloopshortRUD = LEFTLOOPSHORT_RUD;
HARUKIDELTA 13:77b13d5f4d78 844 *g_glideloopRUD = GLIDELOOP_RUD;
HARUKIDELTA 13:77b13d5f4d78 845 *g_AIL_L_correctionrightloop = AIL_L_CORRECTION_RIGHTLOOP;
HARUKIDELTA 13:77b13d5f4d78 846 *g_AIL_L_correctionrightloopshort = AIL_L_CORRECTION_RIGHTLOOPSHORT;
HARUKIDELTA 13:77b13d5f4d78 847 *g_AIL_L_correctionleftloop = AIL_L_CORRECTION_LEFTLOOP;
HARUKIDELTA 13:77b13d5f4d78 848 *g_AIL_L_correctionleftloopshort = AIL_L_CORRECTION_LEFTLOOPSHORT;
HARUKIDELTA 13:77b13d5f4d78 849
HARUKIDELTA 13:77b13d5f4d78 850
HARUKIDELTA 0:17f575135219 851 SDerrorcount = -1;
HARUKIDELTA 0:17f575135219 852 }
HARUKIDELTA 0:17f575135219 853 pc.printf("SDsetup finished.\r\n");
HARUKIDELTA 0:17f575135219 854 if(SDerrorcount == 0) pc.printf("setting option is success\r\n");
HARUKIDELTA 0:17f575135219 855 else if(SDerrorcount == -1) pc.printf("ERROR 1. cannot open option\r\n");
HARUKIDELTA 0:17f575135219 856 else if(SDerrorcount > 0) pc.printf("ERROR 2. reading parameter is failed[%d]\r\n",SDerrorcount);
HARUKIDELTA 0:17f575135219 857
HARUKIDELTA 0:17f575135219 858 pc.printf("kpELE = %f, kiELE = %f, kdELE = %f\r\n", *g_kpRUD, *g_kiRUD, *g_kdRUD);
HARUKIDELTA 0:17f575135219 859 pc.printf("kpRUD = %f, kiRUD = %f, kdRUD = %f\r\n", *g_kpELE, *g_kiELE, *g_kdELE);
HARUKIDELTA 0:17f575135219 860 pc.printf("rightloopROLL = %f, rightloopPITCH = %f\r\n", *g_rightloopROLL, *g_rightloopPITCH);
HARUKIDELTA 0:17f575135219 861 pc.printf("leftloopROLL = %f, g_leftloopPITCH = %f\r\n", *g_leftloopROLL, *g_leftloopPITCH);
HARUKIDELTA 0:17f575135219 862 pc.printf("gostraightROLL = %f, g_gostraightPITCH = %f\r\n", *g_gostraightROLL, *g_gostraightPITCH);
HARUKIDELTA 0:17f575135219 863 pc.printf("g_takeoffTHR = %f, g_loopTHR = %f\r\n", *g_takeoffTHR, *g_loopTHR);
HARUKIDELTA 0:17f575135219 864 pc.printf("rightloopROLLshort = %f, rightloopPITCHshort = %f\r\n", *g_rightloopROLLshort, *g_rightloopPITCHshort);
HARUKIDELTA 0:17f575135219 865 pc.printf("leftloopROLLshort = %f, g_leftloopPITCHshort = %f\r\n", *g_leftloopROLLshort, *g_leftloopPITCHshort);
HARUKIDELTA 0:17f575135219 866 pc.printf("glideROLL = %f, glidePITCH = %f\r\n", *g_glideloopROLL, *g_glideloopPITCH);
HARUKIDELTA 16:7fca5b938da6 867 pc.printf("kpAIL = %f,kiAIL = %f,kdAIL = %f\r\n ",*g_kpAIL,*g_kiAIL,*g_kdAIL);
HARUKIDELTA 16:7fca5b938da6 868 pc.printf("RIGHTLOOPRUD = %d,RIGHTLOOPSHORTRUD = %d\r\n",*g_rightloopRUD,*g_rightloopshortRUD);
HARUKIDELTA 16:7fca5b938da6 869 pc.printf("LEFTTLOOPRUD = %d,LEFTLOOPSHORTRUD = %d\r\n",*g_leftloopRUD,*g_leftloopshortRUD);
HARUKIDELTA 16:7fca5b938da6 870 pc.printf("GLIDELOOPRUD = %d\r\n",*g_glideloopRUD);
HARUKIDELTA 16:7fca5b938da6 871 pc.printf("AIL_L_CORRECTION_RIGHTLOOP = %d, AIL_L_CORRECTION_RIGHTLOOPSHORT = %d\r\n",*g_AIL_L_correctionrightloop,*g_AIL_L_correctionrightloopshort);
HARUKIDELTA 16:7fca5b938da6 872 pc.printf("AIL_L_CORRECTION_LEFTLOOP = %d, AIL_L_CORRECTION_LEFTLOOPSHORT = %d\r\n",*g_AIL_L_correctionleftloop,*g_AIL_L_correctionleftloopshort);
HARUKIDELTA 0:17f575135219 873 return SDerrorcount;
HARUKIDELTA 0:17f575135219 874 }
HARUKIDELTA 0:17f575135219 875
HARUKIDELTA 0:17f575135219 876 void CalculateControlValue(float targetAngle[3], float controlValue[3]){
HARUKIDELTA 0:17f575135219 877 static int t_last;
HARUKIDELTA 0:17f575135219 878 int t_now;
HARUKIDELTA 0:17f575135219 879 float dt;
HARUKIDELTA 0:17f575135219 880
HARUKIDELTA 0:17f575135219 881 t_now = t.read_us();
HARUKIDELTA 0:17f575135219 882 dt = (float)((t_now - t_last)/1000000.0f) ;
HARUKIDELTA 0:17f575135219 883 t_last = t_now;
HARUKIDELTA 0:17f575135219 884
taknokolat 1:f383708a5a52 885
taknokolat 1:f383708a5a52 886 //controlValue[ROLL] = pid_RUD.calcPID(nowAngle[ROLL], targetAngle[ROLL], dt);
taknokolat 1:f383708a5a52 887 controlValue[ROLL] = pid_AIL.calcPID(nowAngle[ROLL], targetAngle[ROLL], dt); //エルロンでロール制御
HARUKIDELTA 0:17f575135219 888 controlValue[PITCH] = pid_ELE.calcPID(nowAngle[PITCH], targetAngle[PITCH], dt);
HARUKIDELTA 0:17f575135219 889 }
HARUKIDELTA 0:17f575135219 890
HARUKIDELTA 0:17f575135219 891 void UpdateAutoPWM(float controlValue[3]){
HARUKIDELTA 0:17f575135219 892 int16_t addpwm[2]; //-500~500
HARUKIDELTA 0:17f575135219 893 addpwm[PITCH] = (float)GAIN_CONTROLVALUE_TO_PWM * controlValue[PITCH]; //センサ:機首下げ正 レバー:機首上げ正
HARUKIDELTA 0:17f575135219 894 addpwm[ROLL] = (float)GAIN_CONTROLVALUE_TO_PWM * controlValue[ROLL]; //センサ:右回転正(8月13日時点;左回転が正!) レバー:右回転正
HARUKIDELTA 0:17f575135219 895
HARUKIDELTA 2:23daa5fa28b4 896 autopwm[ELE] = trimpwm[ELE] + reverce[ELE] * addpwm[PITCH]; //rewrite
HARUKIDELTA 8:66bba39b95a9 897 autopwm[AIL_R] = trimpwm[AIL_R] - reverce[AIL_R] * addpwm[ROLL];
HARUKIDELTA 0:17f575135219 898 //autopwm[THR] = oldTHR;
HARUKIDELTA 0:17f575135219 899
HARUKIDELTA 0:17f575135219 900 autopwm[ELE] = ThresholdMaxMin(autopwm[ELE], maxpwm[ELE], minpwm[ELE]);
taknokolat 1:f383708a5a52 901 autopwm[AIL_R] = ThresholdMaxMin(autopwm[AIL_R], maxpwm[AIL_R], minpwm[AIL_R]);
HARUKIDELTA 2:23daa5fa28b4 902
HARUKIDELTA 0:17f575135219 903 }
HARUKIDELTA 0:17f575135219 904
HARUKIDELTA 0:17f575135219 905 inline float CalcRatio(float value, float trim, float limit){
HARUKIDELTA 0:17f575135219 906 return (value - trim) / (limit - trim);
HARUKIDELTA 0:17f575135219 907 }
HARUKIDELTA 0:17f575135219 908
HARUKIDELTA 0:17f575135219 909 bool CheckSW_Up(Channel ch){
HARUKIDELTA 0:17f575135219 910 if(SWITCH_CHECK < sbus.manualpwm[ch]){
HARUKIDELTA 0:17f575135219 911 return true;
HARUKIDELTA 0:17f575135219 912 }else{
HARUKIDELTA 0:17f575135219 913 return false;
HARUKIDELTA 0:17f575135219 914 }
HARUKIDELTA 0:17f575135219 915 }
HARUKIDELTA 0:17f575135219 916
HARUKIDELTA 0:17f575135219 917 int16_t ThresholdMaxMin(int16_t value, int16_t max, int16_t min){
HARUKIDELTA 0:17f575135219 918 if(value > max) return max;
HARUKIDELTA 0:17f575135219 919 if(value < min) return min;
HARUKIDELTA 0:17f575135219 920 return value;
HARUKIDELTA 0:17f575135219 921 }
HARUKIDELTA 0:17f575135219 922
HARUKIDELTA 0:17f575135219 923 inline int16_t SetTHRinRatio(float ratio){
HARUKIDELTA 0:17f575135219 924 return minpwm[THR] + (int16_t)(2 * lengthdivpwm * ratio);
HARUKIDELTA 0:17f575135219 925 }
HARUKIDELTA 0:17f575135219 926
HARUKIDELTA 2:23daa5fa28b4 927
HARUKIDELTA 2:23daa5fa28b4 928
HARUKIDELTA 0:17f575135219 929 /*---SBUS割り込み処理---*/
HARUKIDELTA 0:17f575135219 930
HARUKIDELTA 0:17f575135219 931 //udate_Inputで抽出したpwmデータを整形して各変数に代入する。(マニュアルモード)
HARUKIDELTA 0:17f575135219 932 //各stabiGylo関数で算出したpwmを各変数に代入する(自動モード)
HARUKIDELTA 0:17f575135219 933 void Update_PWM()
HARUKIDELTA 0:17f575135219 934 {
HARUKIDELTA 0:17f575135219 935 static int16_t pwm[6];
HARUKIDELTA 0:17f575135219 936 static int16_t temppwm[6]={trimpwm[0],trimpwm[1],trimpwm[2],trimpwm[3],trimpwm[4],trimpwm[5]};
HARUKIDELTA 0:17f575135219 937 if(sbus.flg_ch_update == true){
taknokolat 10:652071c20bf6 938 NVIC_DisableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 939 switch(output_status){ //マニュアルモード,自動モード,自動着陸もモードを切替
HARUKIDELTA 0:17f575135219 940 case Manual:
HARUKIDELTA 0:17f575135219 941 for(uint8_t i=0;i<6;i++){
taknokolat 1:f383708a5a52 942 pwm[i] = sbus.manualpwm[i];
HARUKIDELTA 0:17f575135219 943 }
HARUKIDELTA 2:23daa5fa28b4 944 /*pc.printf("%d ,",pwm[0]);//R
HARUKIDELTA 2:23daa5fa28b4 945 pc.printf("%d ,\r\n",pwm[5]);//L*/
HARUKIDELTA 0:17f575135219 946 oldTHR = sbus.manualpwm[THR];
HARUKIDELTA 0:17f575135219 947 //pc.printf("update_manual\r\n");
HARUKIDELTA 0:17f575135219 948 break;
HARUKIDELTA 0:17f575135219 949
HARUKIDELTA 0:17f575135219 950 case Auto:
HARUKIDELTA 0:17f575135219 951 pwm[AIL_R] = autopwm[AIL_R]; //sbus.manualpwm[AIL];
HARUKIDELTA 0:17f575135219 952 pwm[ELE] = autopwm[ELE];
HARUKIDELTA 0:17f575135219 953 pwm[THR] = autopwm[THR];
HARUKIDELTA 0:17f575135219 954 pwm[RUD] = autopwm[RUD];
HARUKIDELTA 0:17f575135219 955 pwm[DROP] = autopwm[DROP];
HARUKIDELTA 0:17f575135219 956 pwm[AIL_L] = autopwm[AIL_L];
taknokolat 10:652071c20bf6 957 //pc.printf("%d ,",pwm[AIL_R]);//R
taknokolat 10:652071c20bf6 958 //pc.printf("%d ,\r\n",pwm[AIL_L]);//L
HARUKIDELTA 0:17f575135219 959 //pc.printf("update_auto\r\n");
HARUKIDELTA 2:23daa5fa28b4 960
HARUKIDELTA 0:17f575135219 961 break;
HARUKIDELTA 0:17f575135219 962
HARUKIDELTA 0:17f575135219 963 default:
HARUKIDELTA 0:17f575135219 964 for(uint8_t i=0;i<6;i++){
HARUKIDELTA 0:17f575135219 965 pwm[i] = sbus.manualpwm[i];
HARUKIDELTA 0:17f575135219 966 } //pc.printf("update_manual\r\n");
HARUKIDELTA 0:17f575135219 967 break;
taknokolat 10:652071c20bf6 968 NVIC_EnableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 969 }
HARUKIDELTA 0:17f575135219 970 for(uint8_t i=0;i<6;i++){
HARUKIDELTA 0:17f575135219 971 if(pwm[i]<1000 || pwm[i]>2000) pwm[i]=temppwm[i];
HARUKIDELTA 0:17f575135219 972 temppwm[i]=pwm[i];
HARUKIDELTA 0:17f575135219 973 }
HARUKIDELTA 0:17f575135219 974 }else{
HARUKIDELTA 0:17f575135219 975 pc.printf("0\r\n");
HARUKIDELTA 0:17f575135219 976 }
HARUKIDELTA 0:17f575135219 977 sbus.flg_ch_update = false;
HARUKIDELTA 0:17f575135219 978 Output_PWM(pwm);
HARUKIDELTA 0:17f575135219 979 }
HARUKIDELTA 0:17f575135219 980
HARUKIDELTA 0:17f575135219 981
HARUKIDELTA 0:17f575135219 982 //pwmをサーボに出力。
HARUKIDELTA 0:17f575135219 983 void Output_PWM(int16_t pwm[5])
HARUKIDELTA 0:17f575135219 984 {
taknokolat 10:652071c20bf6 985 NVIC_DisableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 986 servo1.pulsewidth_us(pwm[0]);
HARUKIDELTA 0:17f575135219 987 servo2.pulsewidth_us(pwm[1]);
HARUKIDELTA 0:17f575135219 988 servo3.pulsewidth_us(pwm[2]);
HARUKIDELTA 0:17f575135219 989 servo4.pulsewidth_us(pwm[3]);
HARUKIDELTA 0:17f575135219 990 servo5.pulsewidth_us(pwm[4]);
HARUKIDELTA 0:17f575135219 991 servo6.pulsewidth_us(pwm[5]);
taknokolat 10:652071c20bf6 992 NVIC_EnableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 993
HARUKIDELTA 0:17f575135219 994 }
HARUKIDELTA 0:17f575135219 995
HARUKIDELTA 0:17f575135219 996 void ResetTrim(){
HARUKIDELTA 0:17f575135219 997 for(uint8_t i=0; i<6; i++){ //i=4から書き換え_投下サーボは入ってない模様
HARUKIDELTA 0:17f575135219 998 trimpwm[i] = sbus.manualpwm[i];
HARUKIDELTA 0:17f575135219 999 }
HARUKIDELTA 0:17f575135219 1000 pc.printf("reset PWM trim\r\n");
HARUKIDELTA 0:17f575135219 1001 }
HARUKIDELTA 0:17f575135219 1002
HARUKIDELTA 0:17f575135219 1003
HARUKIDELTA 0:17f575135219 1004 void SensingMPU(){
HARUKIDELTA 0:17f575135219 1005 //static int16_t deltaT = 0, t_start = 0;
HARUKIDELTA 0:17f575135219 1006 //t_start = t.read_us();
HARUKIDELTA 0:17f575135219 1007
HARUKIDELTA 0:17f575135219 1008 float rpy[3] = {0}, oldrpy[3] = {0};
HARUKIDELTA 0:17f575135219 1009 static uint16_t count_changeRPY = 0;
HARUKIDELTA 0:17f575135219 1010 static bool flg_checkoutlier = false;
HARUKIDELTA 0:17f575135219 1011 NVIC_DisableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 1012 NVIC_DisableIRQ(USART2_IRQn);
HARUKIDELTA 0:17f575135219 1013 NVIC_DisableIRQ(TIM5_IRQn);
taknokolat 10:652071c20bf6 1014 NVIC_DisableIRQ(EXTI0_IRQn);//MPU割り込み禁止
taknokolat 10:652071c20bf6 1015 NVIC_DisableIRQ(EXTI9_5_IRQn);//超音波割り込み禁止
HARUKIDELTA 0:17f575135219 1016
HARUKIDELTA 0:17f575135219 1017 mpu6050.getRollPitchYaw_Skipper(rpy);
HARUKIDELTA 0:17f575135219 1018
HARUKIDELTA 0:17f575135219 1019 NVIC_EnableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 1020 NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:17f575135219 1021 NVIC_EnableIRQ(TIM5_IRQn);
HARUKIDELTA 0:17f575135219 1022 NVIC_EnableIRQ(EXTI0_IRQn);
HARUKIDELTA 0:17f575135219 1023 NVIC_EnableIRQ(EXTI9_5_IRQn);
HARUKIDELTA 0:17f575135219 1024
HARUKIDELTA 0:17f575135219 1025
HARUKIDELTA 0:17f575135219 1026 //外れ値対策
HARUKIDELTA 0:17f575135219 1027 for(uint8_t i=0; i<3; i++) rpy[i] *= 180.0f/PI;
HARUKIDELTA 0:17f575135219 1028 rpy[ROLL] -= FirstROLL;
HARUKIDELTA 0:17f575135219 1029 rpy[PITCH] -= FirstPITCH;
HARUKIDELTA 0:17f575135219 1030 rpy[YAW] -= FirstYAW;
HARUKIDELTA 0:17f575135219 1031
HARUKIDELTA 0:17f575135219 1032 for(uint8_t i=0; i<3; i++) {if(rpy[i] < nowAngle[i]-10 || rpy[i] > nowAngle[i]+10) {flg_checkoutlier = true;}}
HARUKIDELTA 0:17f575135219 1033 if(!flg_checkoutlier || count_changeRPY >= 2){
HARUKIDELTA 0:17f575135219 1034 for(uint8_t i=0; i<3; i++){
HARUKIDELTA 0:17f575135219 1035 nowAngle[i] = (rpy[i] + nowAngle[i])/2.0f; //2つの移動平均
HARUKIDELTA 0:17f575135219 1036 }
HARUKIDELTA 0:17f575135219 1037 count_changeRPY = 0;
HARUKIDELTA 0:17f575135219 1038 }else count_changeRPY++;
HARUKIDELTA 0:17f575135219 1039 flg_checkoutlier = false;
HARUKIDELTA 0:17f575135219 1040
HARUKIDELTA 0:17f575135219 1041 }
HARUKIDELTA 0:17f575135219 1042
HARUKIDELTA 0:17f575135219 1043 float TranslateNewYaw(float beforeYaw, float newzeroYaw){
HARUKIDELTA 0:17f575135219 1044 float newYaw = beforeYaw - newzeroYaw;
HARUKIDELTA 0:17f575135219 1045
HARUKIDELTA 0:17f575135219 1046 if(newYaw<-180.0f) newYaw += 360.0f;
HARUKIDELTA 0:17f575135219 1047 else if(newYaw>180.0f) newYaw -= 360.0f;
HARUKIDELTA 0:17f575135219 1048 return newYaw;
HARUKIDELTA 0:17f575135219 1049 }
HARUKIDELTA 0:17f575135219 1050
HARUKIDELTA 0:17f575135219 1051
HARUKIDELTA 0:17f575135219 1052 void getSF_Serial(){
taknokolat 23:0f3b1441b08c 1053 NVIC_DisableIRQ(USART1_IRQn);
taknokolat 23:0f3b1441b08c 1054 NVIC_DisableIRQ(EXTI0_IRQn);
taknokolat 24:3e7dcf16a6a7 1055 //NVIC_DisableIRQ(TIM5_IRQn);
HARUKIDELTA 0:17f575135219 1056
HARUKIDELTA 0:17f575135219 1057 static char SFbuf[16];
HARUKIDELTA 0:17f575135219 1058 static int bufcounter=0;
HARUKIDELTA 0:17f575135219 1059
taknokolat 14:bbd5ff31fb67 1060 if(pc.readable()) { // 受信確認
taknokolat 14:bbd5ff31fb67 1061 SFbuf[bufcounter] = pc.getc(); // 1文字取り出し
taknokolat 14:bbd5ff31fb67 1062 }
taknokolat 11:c0b9ad25d3db 1063
taknokolat 11:c0b9ad25d3db 1064
taknokolat 11:c0b9ad25d3db 1065 pc.printf("%c",SFbuf[bufcounter]);
taknokolat 11:c0b9ad25d3db 1066
HARUKIDELTA 0:17f575135219 1067 if(SFbuf[0]=='S'&&bufcounter<5) bufcounter++;
HARUKIDELTA 0:17f575135219 1068
HARUKIDELTA 0:17f575135219 1069 if(bufcounter==5 && SFbuf[4]=='F'){
HARUKIDELTA 0:17f575135219 1070 g_landingcommand = SFbuf[1];
taknokolat 23:0f3b1441b08c 1071 //wait_ms(20);
HARUKIDELTA 0:17f575135219 1072 if(g_landingcommand=='Y')g_SerialTargetYAW = ConvertByteintoFloat(SFbuf[2], SFbuf[3]);
HARUKIDELTA 0:17f575135219 1073 bufcounter = 0;
HARUKIDELTA 0:17f575135219 1074 memset(SFbuf, 0, strlen(SFbuf));
taknokolat 10:652071c20bf6 1075 //pc.printf("command = %c, commandYAW = %f\r\n", g_landingcommand, g_SerialTargetYAW);
HARUKIDELTA 0:17f575135219 1076 }
taknokolat 10:652071c20bf6 1077 else if(bufcounter>=5 ){
HARUKIDELTA 0:17f575135219 1078 pc.printf("Communication Falsed.\r\n");
HARUKIDELTA 0:17f575135219 1079 bufcounter = 0;
HARUKIDELTA 0:17f575135219 1080 }
taknokolat 24:3e7dcf16a6a7 1081 //NVIC_EnableIRQ(TIM5_IRQn);
taknokolat 23:0f3b1441b08c 1082 NVIC_EnableIRQ(EXTI0_IRQn);
taknokolat 24:3e7dcf16a6a7 1083 NVIC_EnableIRQ(USART1_IRQn);
HARUKIDELTA 0:17f575135219 1084 }
HARUKIDELTA 0:17f575135219 1085
HARUKIDELTA 0:17f575135219 1086 float ConvertByteintoFloat(char high, char low){
HARUKIDELTA 0:17f575135219 1087
HARUKIDELTA 0:17f575135219 1088 //int16_t intvalue = (int16_t)high*256 + (int16_t)low;
HARUKIDELTA 0:17f575135219 1089 int16_t intvalue = (int16_t)(((int16_t)high << 8) | low); // Turn the MSB and LSB into a signed 16-bit value
HARUKIDELTA 0:17f575135219 1090 float floatvalue = (float)intvalue;
HARUKIDELTA 0:17f575135219 1091 return floatvalue;
HARUKIDELTA 0:17f575135219 1092 }
HARUKIDELTA 0:17f575135219 1093
HARUKIDELTA 0:17f575135219 1094
HARUKIDELTA 0:17f575135219 1095 //超音波割り込み
taknokolat 10:652071c20bf6 1096 /*void UpdateDist(){
HARUKIDELTA 0:17f575135219 1097 g_distance = usensor.get_dist_cm();
HARUKIDELTA 0:17f575135219 1098 usensor.start();
taknokolat 10:652071c20bf6 1099 }*/
HARUKIDELTA 0:17f575135219 1100
HARUKIDELTA 0:17f575135219 1101 //8の字旋回
HARUKIDELTA 0:17f575135219 1102 void UpdateTargetAngle_Moebius(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 1103 static uint8_t RotateCounter=0;
HARUKIDELTA 0:17f575135219 1104 static bool flg_setInStartAuto = false;
HARUKIDELTA 0:17f575135219 1105 static float FirstYAW_Moebius = 0.0;
HARUKIDELTA 0:17f575135219 1106 float newYaw_Moebius;
HARUKIDELTA 0:17f575135219 1107
HARUKIDELTA 0:17f575135219 1108 if(!flg_setInStartAuto && CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 1109 FirstYAW_Moebius = nowAngle[YAW];
HARUKIDELTA 0:17f575135219 1110 RotateCounter = 0;
HARUKIDELTA 0:17f575135219 1111 flg_setInStartAuto = true;
HARUKIDELTA 0:17f575135219 1112 }else if(!CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 1113 flg_setInStartAuto = false;
HARUKIDELTA 0:17f575135219 1114 led2 = 0;
HARUKIDELTA 0:17f575135219 1115 }
HARUKIDELTA 6:ed61ed8b8fab 1116 autopwm[THR]=oldTHR;
HARUKIDELTA 6:ed61ed8b8fab 1117
HARUKIDELTA 0:17f575135219 1118 newYaw_Moebius = TranslateNewYaw(nowAngle[YAW], FirstYAW_Moebius);
HARUKIDELTA 0:17f575135219 1119
HARUKIDELTA 0:17f575135219 1120 if(RotateCounter == 0 && newYaw_Moebius >90.0 && newYaw_Moebius < 180.0) {RotateCounter++; led2 = 1; pc.printf("Rotate 90\r\n");}
HARUKIDELTA 0:17f575135219 1121 if(RotateCounter == 1 && newYaw_Moebius >-180.0 && newYaw_Moebius < -90.0) {RotateCounter++; led2 = 0; pc.printf("Rotate 180\r\n");}
HARUKIDELTA 6:ed61ed8b8fab 1122 if(RotateCounter == 2 && newYaw_Moebius >-90.0 && newYaw_Moebius <-10.0) {RotateCounter++; led2 = 1; pc.printf("Rotate 270\r\n");}
HARUKIDELTA 6:ed61ed8b8fab 1123 if(RotateCounter == 3 && newYaw_Moebius >0.0 && newYaw_Moebius < 90.0) {RotateCounter++; led2 = 0; pc.printf("Change Rotate direction\r\n");}
HARUKIDELTA 0:17f575135219 1124
HARUKIDELTA 0:17f575135219 1125
HARUKIDELTA 0:17f575135219 1126 if(RotateCounter <= 3) UpdateTargetAngle_Rightloop_short(targetAngle);
HARUKIDELTA 0:17f575135219 1127 else UpdateTargetAngle_Leftloop_short(targetAngle); //左旋回
HARUKIDELTA 0:17f575135219 1128
HARUKIDELTA 0:17f575135219 1129 }
HARUKIDELTA 0:17f575135219 1130
HARUKIDELTA 0:17f575135219 1131 //自動滑空
HARUKIDELTA 0:17f575135219 1132 void UpdateTargetAngle_Glide(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 1133 static int THRcount = 0;
HARUKIDELTA 0:17f575135219 1134 static int t_start = 0;
HARUKIDELTA 0:17f575135219 1135 static bool flg_tstart = false;
HARUKIDELTA 0:17f575135219 1136 int t_diff = 0;
taknokolat 1:f383708a5a52 1137 static int groundcount = 0;
HARUKIDELTA 0:17f575135219 1138
HARUKIDELTA 0:17f575135219 1139 targetAngle[ROLL] = g_glideloopROLL;
HARUKIDELTA 0:17f575135219 1140 targetAngle[PITCH] = g_glideloopPITCH;
HARUKIDELTA 0:17f575135219 1141
HARUKIDELTA 16:7fca5b938da6 1142 autopwm[RUD]=g_glideloopRUD;
HARUKIDELTA 7:53b0eb6f6bd3 1143 // autopwm[THR]=oldTHR;
taknokolat 10:652071c20bf6 1144
taknokolat 1:f383708a5a52 1145
HARUKIDELTA 7:53b0eb6f6bd3 1146
HARUKIDELTA 0:17f575135219 1147 //時間計測開始設定
HARUKIDELTA 0:17f575135219 1148 if(!flg_tstart && CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 1149 t_start = t.read();
HARUKIDELTA 0:17f575135219 1150 flg_tstart = true;
HARUKIDELTA 0:17f575135219 1151 pc.printf("timer start\r\n");
taknokolat 1:f383708a5a52 1152 }else if(!CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 1153 t_start = 0;
HARUKIDELTA 0:17f575135219 1154 flg_tstart = false;
HARUKIDELTA 0:17f575135219 1155 }
HARUKIDELTA 0:17f575135219 1156
taknokolat 1:f383708a5a52 1157
HARUKIDELTA 0:17f575135219 1158 //フラグが偽であれば計測は行わない
HARUKIDELTA 0:17f575135219 1159 if(flg_tstart){
HARUKIDELTA 0:17f575135219 1160 t_diff = t.read() - t_start;
HARUKIDELTA 0:17f575135219 1161 //一定高度or15秒でled点灯
taknokolat 10:652071c20bf6 1162 NVIC_DisableIRQ(EXTI9_5_IRQn);
taknokolat 1:f383708a5a52 1163 if((groundcount>5 && g_distance>0) || t_diff > 15){
HARUKIDELTA 0:17f575135219 1164 led2 = 1;
HARUKIDELTA 0:17f575135219 1165 //pc.printf("Call [Stop!] calling!\r\n");
HARUKIDELTA 0:17f575135219 1166 }
taknokolat 10:652071c20bf6 1167
taknokolat 1:f383708a5a52 1168 if(g_distance<180 && g_distance > 0) groundcount++;
taknokolat 10:652071c20bf6 1169 NVIC_EnableIRQ(EXTI9_5_IRQn);
HARUKIDELTA 0:17f575135219 1170 }else{
HARUKIDELTA 0:17f575135219 1171 t_diff = 0;
taknokolat 1:f383708a5a52 1172 groundcount = 0;
HARUKIDELTA 0:17f575135219 1173 led2 = 0;
HARUKIDELTA 0:17f575135219 1174 }
HARUKIDELTA 0:17f575135219 1175
taknokolat 1:f383708a5a52 1176 if(t_diff > 17){
HARUKIDELTA 0:17f575135219 1177 autopwm[THR] = SetTHRinRatio(0.5);
HARUKIDELTA 0:17f575135219 1178 }else{
taknokolat 10:652071c20bf6 1179 NVIC_DisableIRQ(EXTI9_5_IRQn);
HARUKIDELTA 0:17f575135219 1180 if(g_distance<150 && g_distance>0 ){
taknokolat 10:652071c20bf6 1181 NVIC_EnableIRQ(EXTI9_5_IRQn);
HARUKIDELTA 0:17f575135219 1182 THRcount++;
HARUKIDELTA 0:17f575135219 1183 if(THRcount>5){
taknokolat 1:f383708a5a52 1184 autopwm[THR] = SetTHRinRatio(0.6);
HARUKIDELTA 0:17f575135219 1185 //pc.printf("throttle ON\r\n");
HARUKIDELTA 0:17f575135219 1186 }
HARUKIDELTA 0:17f575135219 1187 }else{
HARUKIDELTA 0:17f575135219 1188 autopwm[THR] = 1180;
HARUKIDELTA 0:17f575135219 1189 THRcount = 0;
HARUKIDELTA 0:17f575135219 1190 }
HARUKIDELTA 7:53b0eb6f6bd3 1191 }
HARUKIDELTA 0:17f575135219 1192 }
HARUKIDELTA 0:17f575135219 1193 //離陸-投下-着陸一連
HARUKIDELTA 0:17f575135219 1194 void Take_off_and_landing(float targetAngle[3]){
taknokolat 10:652071c20bf6 1195 /*
HARUKIDELTA 0:17f575135219 1196 if(!CheckSW_Up(Ch7)) bombing_mode = Takeoff;
HARUKIDELTA 0:17f575135219 1197
HARUKIDELTA 0:17f575135219 1198 switch(bombing_mode){
HARUKIDELTA 0:17f575135219 1199 case Takeoff:
HARUKIDELTA 0:17f575135219 1200 static bool flg_setFirstYaw = false;
HARUKIDELTA 0:17f575135219 1201 static int TakeoffCount = 0;
HARUKIDELTA 0:17f575135219 1202
HARUKIDELTA 0:17f575135219 1203 if(!flg_setFirstYaw && CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 1204 FirstYAW = nowAngle[YAW];
HARUKIDELTA 0:17f575135219 1205 flg_setFirstYaw = true;
HARUKIDELTA 0:17f575135219 1206 }else if(flg_setFirstYaw && !CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 1207 flg_setFirstYaw = false;
HARUKIDELTA 0:17f575135219 1208 }
HARUKIDELTA 0:17f575135219 1209
HARUKIDELTA 0:17f575135219 1210 UpdateTargetAngle_Takeoff(targetAngle);
HARUKIDELTA 0:17f575135219 1211
HARUKIDELTA 0:17f575135219 1212 if(g_distance>150) TakeoffCount++;
HARUKIDELTA 0:17f575135219 1213 else TakeoffCount = 0;
HARUKIDELTA 0:17f575135219 1214 if(TakeoffCount>5){
HARUKIDELTA 0:17f575135219 1215 autopwm[THR] = 1180+320*2*0.5;
HARUKIDELTA 0:17f575135219 1216 pc.printf("Now go to Approach mode!!");
HARUKIDELTA 0:17f575135219 1217 bombing_mode = Approach;
HARUKIDELTA 0:17f575135219 1218 }
HARUKIDELTA 0:17f575135219 1219 break;
taknokolat 10:652071c20bf6 1220
taknokolat 10:652071c20bf6 1221 //case Chicken:
taknokolat 10:652071c20bf6 1222 //break;
taknokolat 10:652071c20bf6 1223
HARUKIDELTA 0:17f575135219 1224 case Transition:
HARUKIDELTA 0:17f575135219 1225 static int ApproachCount = 0;
HARUKIDELTA 0:17f575135219 1226 targetAngle[YAW]=180.0;
HARUKIDELTA 0:17f575135219 1227 int Judge = Rotate(targetAngle, g_SerialTargetYAW);
HARUKIDELTA 0:17f575135219 1228
HARUKIDELTA 0:17f575135219 1229 if(Judge==0) ApproachCount++;
HARUKIDELTA 0:17f575135219 1230 if(ApproachCount>5) bombing_mode = Approach;
HARUKIDELTA 0:17f575135219 1231 break;
HARUKIDELTA 0:17f575135219 1232
HARUKIDELTA 0:17f575135219 1233 case Approach:
taknokolat 10:652071c20bf6 1234 */
taknokolat 10:652071c20bf6 1235 autopwm[THR] = SetTHRinRatio(g_loopTHR); //0.7;スロットルの割合
HARUKIDELTA 0:17f575135219 1236 UpdateTargetAngle_Approach(targetAngle);
taknokolat 10:652071c20bf6 1237 /*
HARUKIDELTA 0:17f575135219 1238 break;
HARUKIDELTA 0:17f575135219 1239
HARUKIDELTA 0:17f575135219 1240 default:
HARUKIDELTA 0:17f575135219 1241 bombing_mode = Takeoff;
HARUKIDELTA 0:17f575135219 1242 break;
HARUKIDELTA 0:17f575135219 1243 }
taknokolat 10:652071c20bf6 1244 */
HARUKIDELTA 0:17f575135219 1245 }
HARUKIDELTA 0:17f575135219 1246
HARUKIDELTA 0:17f575135219 1247 //離陸モード
HARUKIDELTA 0:17f575135219 1248 void UpdateTargetAngle_Takeoff(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 1249 //pc.printf("%d \r\n",g_distance);
HARUKIDELTA 0:17f575135219 1250 targetAngle[ROLL] = g_gostraightROLL;
HARUKIDELTA 0:17f575135219 1251 targetAngle[PITCH] = g_loopTHR;
HARUKIDELTA 0:17f575135219 1252 autopwm[THR] = SetTHRinRatio(g_takeoffTHR); //0.7;スロットルの割合
HARUKIDELTA 0:17f575135219 1253 }
HARUKIDELTA 0:17f575135219 1254
HARUKIDELTA 0:17f575135219 1255 //ヨーを目標値にして許容角度になるまで水平旋回
HARUKIDELTA 0:17f575135219 1256 int Rotate(float targetAngle[3], float TargetYAW){
HARUKIDELTA 0:17f575135219 1257 float diffYaw = TranslateNewYaw(TargetYAW, nowAngle[YAW]);
HARUKIDELTA 0:17f575135219 1258
HARUKIDELTA 0:17f575135219 1259 if(diffYaw > LIMIT_STRAIGHT_YAW){
HARUKIDELTA 0:17f575135219 1260 /*
HARUKIDELTA 0:17f575135219 1261 if(diffYaw > THRESHOLD_TURNINGRADIUS_YAW) UpdateTargetAngle_Rightloop_short(targetAngle);
HARUKIDELTA 0:17f575135219 1262 else UpdateTargetAngle_Rightloop(targetAngle);
HARUKIDELTA 0:17f575135219 1263 */
HARUKIDELTA 0:17f575135219 1264 UpdateTargetAngle_Rightloop_short(targetAngle);
HARUKIDELTA 0:17f575135219 1265 return 1;
HARUKIDELTA 0:17f575135219 1266 }else if(diffYaw < -LIMIT_STRAIGHT_YAW){
HARUKIDELTA 0:17f575135219 1267 UpdateTargetAngle_Leftloop_short(targetAngle);
HARUKIDELTA 0:17f575135219 1268 /*
HARUKIDELTA 0:17f575135219 1269 if(diffYaw < -THRESHOLD_TURNINGRADIUS_YAW) UpdateTargetAngle_Leftloop_short(targetAngle);
HARUKIDELTA 0:17f575135219 1270 else UpdateTargetAngle_Leftloop(targetAngle);
HARUKIDELTA 0:17f575135219 1271 */
HARUKIDELTA 0:17f575135219 1272 return 1;
HARUKIDELTA 0:17f575135219 1273 }else{
HARUKIDELTA 0:17f575135219 1274 UpdateTargetAngle_GoStraight(targetAngle);
HARUKIDELTA 0:17f575135219 1275 return 0;
HARUKIDELTA 0:17f575135219 1276 }
HARUKIDELTA 0:17f575135219 1277 }
HARUKIDELTA 0:17f575135219 1278
HARUKIDELTA 0:17f575135219 1279 //チキラー投下
HARUKIDELTA 0:17f575135219 1280 void Chicken_Drop(){
HARUKIDELTA 0:17f575135219 1281 if(CheckSW_Up(Ch7)){
HARUKIDELTA 0:17f575135219 1282 autopwm[DROP] = 1512;
HARUKIDELTA 0:17f575135219 1283 pc.printf("Bombed!\r\n");
HARUKIDELTA 0:17f575135219 1284 RerurnChickenServo1.attach(&ReturnChickenServo1, 3);
HARUKIDELTA 0:17f575135219 1285 //operation_mode = Approach;
HARUKIDELTA 0:17f575135219 1286 //buzzer = 0;
HARUKIDELTA 0:17f575135219 1287 pc.printf("Goto LeftLoop mode\r\n");
HARUKIDELTA 0:17f575135219 1288 }
HARUKIDELTA 0:17f575135219 1289 }
HARUKIDELTA 0:17f575135219 1290
HARUKIDELTA 0:17f575135219 1291 void ReturnChickenServo1(){
HARUKIDELTA 0:17f575135219 1292 autopwm[DROP] = 1344;
HARUKIDELTA 0:17f575135219 1293 pc.printf("first reverse\r\n");
HARUKIDELTA 0:17f575135219 1294 RerurnChickenServo2.attach(&ReturnChickenServo2, 1);
HARUKIDELTA 0:17f575135219 1295 }
HARUKIDELTA 0:17f575135219 1296
HARUKIDELTA 0:17f575135219 1297 void ReturnChickenServo2(){
HARUKIDELTA 0:17f575135219 1298 autopwm[DROP] = 1392;
HARUKIDELTA 0:17f575135219 1299 pc.printf("second reverse\r\n");
taknokolat 10:652071c20bf6 1300 }
taknokolat 10:652071c20bf6 1301
taknokolat 10:652071c20bf6 1302 //着陸モード(PCからの指令に従う)
taknokolat 10:652071c20bf6 1303 void UpdateTargetAngle_Approach(float targetAngle[3]){
taknokolat 10:652071c20bf6 1304
taknokolat 10:652071c20bf6 1305 NVIC_DisableIRQ(USART2_IRQn);
taknokolat 10:652071c20bf6 1306
taknokolat 10:652071c20bf6 1307 switch(g_landingcommand){
taknokolat 10:652071c20bf6 1308 case 'R': //右旋回セヨ
HARUKIDELTA 19:0955311b0db6 1309 if(zeroTHR==0){
HARUKIDELTA 19:0955311b0db6 1310 UpdateTargetAngle_Rightloop_zero(targetAngle);
HARUKIDELTA 19:0955311b0db6 1311 }
HARUKIDELTA 19:0955311b0db6 1312 else{
taknokolat 10:652071c20bf6 1313 targetAngle[ROLL] = g_rightloopROLL;
taknokolat 10:652071c20bf6 1314 targetAngle[PITCH] = g_rightloopPITCH ;
HARUKIDELTA 17:55249ea37dff 1315 autopwm[RUD]=g_rightloopRUD; //RUD固定
HARUKIDELTA 22:438bedf24707 1316 if(autopwm[AIL_R]<trimpwm[AIL_R]){ //エルロン上がりやすさ調節
taknokolat 10:652071c20bf6 1317 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
taknokolat 10:652071c20bf6 1318 }
taknokolat 20:9393b0cfa44d 1319 else {autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
taknokolat 10:652071c20bf6 1320 autopwm[AIL_R]=autopwm[AIL_R]+AIL_R_correctionrightloop;
taknokolat 20:9393b0cfa44d 1321 }
taknokolat 20:9393b0cfa44d 1322 }
taknokolat 10:652071c20bf6 1323 break;
taknokolat 20:9393b0cfa44d 1324
taknokolat 10:652071c20bf6 1325 case 'L': //左旋回セヨ
HARUKIDELTA 19:0955311b0db6 1326 if(zeroTHR==0){
HARUKIDELTA 19:0955311b0db6 1327 UpdateTargetAngle_Leftloop_zero(targetAngle);
HARUKIDELTA 19:0955311b0db6 1328 }
HARUKIDELTA 19:0955311b0db6 1329 else{
taknokolat 10:652071c20bf6 1330 targetAngle[ROLL] = g_leftloopROLL;
taknokolat 10:652071c20bf6 1331 targetAngle[PITCH] = g_leftloopPITCH;
HARUKIDELTA 17:55249ea37dff 1332 autopwm[RUD]=g_leftloopRUD;
HARUKIDELTA 22:438bedf24707 1333 if(autopwm[AIL_R]<trimpwm[AIL_R]){
taknokolat 10:652071c20bf6 1334 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
taknokolat 10:652071c20bf6 1335 }
taknokolat 10:652071c20bf6 1336 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
HARUKIDELTA 19:0955311b0db6 1337 }
taknokolat 10:652071c20bf6 1338 break;
taknokolat 10:652071c20bf6 1339
taknokolat 10:652071c20bf6 1340 case 'G': //直進セヨ
HARUKIDELTA 19:0955311b0db6 1341 if(zeroTHR==0){
HARUKIDELTA 19:0955311b0db6 1342 UpdateTargetAngle_GoStraight_zero(targetAngle);
HARUKIDELTA 19:0955311b0db6 1343 }
HARUKIDELTA 19:0955311b0db6 1344 else{
taknokolat 10:652071c20bf6 1345 targetAngle[ROLL] = g_gostraightROLL;
taknokolat 10:652071c20bf6 1346 targetAngle[PITCH] = g_gostraightPITCH;
HARUKIDELTA 19:0955311b0db6 1347 }
taknokolat 10:652071c20bf6 1348
taknokolat 10:652071c20bf6 1349 break;
taknokolat 10:652071c20bf6 1350
taknokolat 10:652071c20bf6 1351 case 'Y': //指定ノヨー方向ニ移動セヨ
taknokolat 10:652071c20bf6 1352 Rotate(targetAngle, g_SerialTargetYAW);
taknokolat 10:652071c20bf6 1353 break;
taknokolat 10:652071c20bf6 1354
taknokolat 10:652071c20bf6 1355 case 'B': //ブザーヲ鳴ラセ
taknokolat 10:652071c20bf6 1356 //buzzer = 1;
taknokolat 10:652071c20bf6 1357 break;
taknokolat 10:652071c20bf6 1358
taknokolat 10:652071c20bf6 1359 case 'D': //物資ヲ落トセ
taknokolat 10:652071c20bf6 1360 Chicken_Drop();
taknokolat 10:652071c20bf6 1361 break;
taknokolat 10:652071c20bf6 1362
taknokolat 10:652071c20bf6 1363 case 'C': //停止セヨ
taknokolat 10:652071c20bf6 1364 targetAngle[ROLL] = 0.0;
taknokolat 10:652071c20bf6 1365 targetAngle[PITCH] = -3.0;
taknokolat 10:652071c20bf6 1366 autopwm[THR] = minpwm[THR];
HARUKIDELTA 19:0955311b0db6 1367 zeroTHR=0;
taknokolat 10:652071c20bf6 1368 break;
HARUKIDELTA 0:17f575135219 1369
taknokolat 10:652071c20bf6 1370 }
taknokolat 10:652071c20bf6 1371 NVIC_EnableIRQ(USART2_IRQn);
taknokolat 10:652071c20bf6 1372 }
taknokolat 10:652071c20bf6 1373
taknokolat 10:652071c20bf6 1374 void checkHeight(float targetAngle[3]){
taknokolat 10:652071c20bf6 1375
taknokolat 10:652071c20bf6 1376 static int targetHeight = 200;
taknokolat 10:652071c20bf6 1377 NVIC_DisableIRQ(EXTI9_5_IRQn);
taknokolat 10:652071c20bf6 1378 if(g_distance < targetHeight + ALLOWHEIGHT){
taknokolat 10:652071c20bf6 1379 UpdateTargetAngle_NoseUP(targetAngle);
taknokolat 10:652071c20bf6 1380 if(CheckSW_Up(Ch7)) led2 = 1;
taknokolat 10:652071c20bf6 1381 }
taknokolat 10:652071c20bf6 1382 else if(g_distance > targetHeight - ALLOWHEIGHT){
taknokolat 10:652071c20bf6 1383 UpdateTargetAngle_NoseDOWN(targetAngle);
taknokolat 10:652071c20bf6 1384 if(CheckSW_Up(Ch7)) led2 = 1;
taknokolat 10:652071c20bf6 1385 }
taknokolat 10:652071c20bf6 1386 else led2=0;
taknokolat 10:652071c20bf6 1387 NVIC_EnableIRQ(EXTI9_5_IRQn);
HARUKIDELTA 0:17f575135219 1388 }
HARUKIDELTA 0:17f575135219 1389
taknokolat 10:652071c20bf6 1390 void UpdateTargetAngle_NoseUP(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 1391
HARUKIDELTA 0:17f575135219 1392 //targetAngle[PITCH] += 2.0f;
HARUKIDELTA 0:17f575135219 1393 //if(nowAngle[PITCH]<targetAngle[PITCH]) autopwm[THR] = SetTHRinRatio(0.6);
HARUKIDELTA 0:17f575135219 1394 autopwm[THR] = SetTHRinRatio(g_loopTHR+0.05);
HARUKIDELTA 0:17f575135219 1395 //pc.printf("nose UP");
HARUKIDELTA 0:17f575135219 1396 }
HARUKIDELTA 0:17f575135219 1397
HARUKIDELTA 0:17f575135219 1398 void UpdateTargetAngle_NoseDOWN(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 1399
HARUKIDELTA 0:17f575135219 1400 //targetAngle[PITCH] -= 2.0f;
HARUKIDELTA 0:17f575135219 1401 autopwm[THR] = SetTHRinRatio(g_loopTHR-0.05);
HARUKIDELTA 0:17f575135219 1402 //pc.printf("nose DOWN");
HARUKIDELTA 0:17f575135219 1403 }
HARUKIDELTA 0:17f575135219 1404
HARUKIDELTA 0:17f575135219 1405 //直進
HARUKIDELTA 0:17f575135219 1406 void UpdateTargetAngle_GoStraight(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 1407
HARUKIDELTA 19:0955311b0db6 1408 autopwm[RUD] = trimpwm[RUD];
HARUKIDELTA 0:17f575135219 1409 targetAngle[ROLL] = g_gostraightROLL;
HARUKIDELTA 0:17f575135219 1410 targetAngle[PITCH] = g_gostraightPITCH;
HARUKIDELTA 0:17f575135219 1411 autopwm[THR] = SetTHRinRatio(g_loopTHR);
HARUKIDELTA 0:17f575135219 1412
HARUKIDELTA 0:17f575135219 1413 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
HARUKIDELTA 0:17f575135219 1414 }
HARUKIDELTA 0:17f575135219 1415
HARUKIDELTA 19:0955311b0db6 1416 //直進(着陸時スロットル0のとき)
HARUKIDELTA 19:0955311b0db6 1417 void UpdateTargetAngle_GoStraight_zero(float targetAngle[3]){
HARUKIDELTA 19:0955311b0db6 1418
HARUKIDELTA 19:0955311b0db6 1419 autopwm[RUD] = trimpwm[RUD];
HARUKIDELTA 19:0955311b0db6 1420 targetAngle[ROLL] = g_gostraightROLL;
HARUKIDELTA 19:0955311b0db6 1421 targetAngle[PITCH] = g_gostraightPITCH;
HARUKIDELTA 19:0955311b0db6 1422 autopwm[THR] = minpwm[THR];
HARUKIDELTA 19:0955311b0db6 1423
HARUKIDELTA 19:0955311b0db6 1424 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
HARUKIDELTA 19:0955311b0db6 1425 }
HARUKIDELTA 3:206b17251f5b 1426
HARUKIDELTA 0:17f575135219 1427 //右旋回
HARUKIDELTA 0:17f575135219 1428 void UpdateTargetAngle_Rightloop(float targetAngle[3]){ //右旋回
HARUKIDELTA 0:17f575135219 1429
HARUKIDELTA 0:17f575135219 1430 targetAngle[ROLL] = g_rightloopROLL;
HARUKIDELTA 0:17f575135219 1431 targetAngle[PITCH] = g_rightloopPITCH ;
HARUKIDELTA 16:7fca5b938da6 1432 autopwm[RUD]=g_rightloopRUD; //RUD固定
TUATBM 9:f6367b7fd7be 1433 autopwm[THR] = SetTHRinRatio(0.5); //手動スロットル記憶
HARUKIDELTA 22:438bedf24707 1434 if(autopwm[AIL_R]<trimpwm[AIL_R]){ //エルロン上がりやすさ調節
HARUKIDELTA 3:206b17251f5b 1435 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
HARUKIDELTA 3:206b17251f5b 1436 }
HARUKIDELTA 3:206b17251f5b 1437 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
TUATBM 9:f6367b7fd7be 1438 autopwm[AIL_R]=autopwm[AIL_R]+AIL_R_correctionrightloop;
TUATBM 9:f6367b7fd7be 1439
HARUKIDELTA 0:17f575135219 1440 //checkHeight(targetAngle);
HARUKIDELTA 0:17f575135219 1441 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
HARUKIDELTA 0:17f575135219 1442 }
HARUKIDELTA 0:17f575135219 1443
HARUKIDELTA 4:fff1165ca50c 1444 //右旋回(着陸時スロットル0の時)
HARUKIDELTA 4:fff1165ca50c 1445 void UpdateTargetAngle_Rightloop_zero(float targetAngle[3]){ //右旋回
taknokolat 20:9393b0cfa44d 1446 autopwm[THR]=minpwm[THR];
HARUKIDELTA 4:fff1165ca50c 1447 targetAngle[ROLL] = g_rightloopROLL;
HARUKIDELTA 4:fff1165ca50c 1448 targetAngle[PITCH] = g_rightloopPITCH ;
taknokolat 20:9393b0cfa44d 1449 autopwm[RUD]=g_rightloopRUD; //RUD固定
HARUKIDELTA 22:438bedf24707 1450 if(autopwm[AIL_R]<trimpwm[AIL_R]){ //エルロン上がりやすさ調節
taknokolat 20:9393b0cfa44d 1451 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
taknokolat 20:9393b0cfa44d 1452 }
taknokolat 20:9393b0cfa44d 1453 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
taknokolat 20:9393b0cfa44d 1454 autopwm[AIL_R]=autopwm[AIL_R]+AIL_R_correctionrightloop;
HARUKIDELTA 4:fff1165ca50c 1455
HARUKIDELTA 4:fff1165ca50c 1456 //checkHeight(targetAngle);
HARUKIDELTA 4:fff1165ca50c 1457 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
HARUKIDELTA 4:fff1165ca50c 1458 }
HARUKIDELTA 4:fff1165ca50c 1459
HARUKIDELTA 0:17f575135219 1460 void UpdateTargetAngle_Rightloop_short(float targetAngle[3]){ //右旋回
HARUKIDELTA 0:17f575135219 1461
HARUKIDELTA 0:17f575135219 1462 targetAngle[ROLL] = g_rightloopROLLshort;
HARUKIDELTA 0:17f575135219 1463 targetAngle[PITCH] = g_rightloopPITCHshort;
HARUKIDELTA 16:7fca5b938da6 1464 autopwm[RUD]=g_rightloopshortRUD;
HARUKIDELTA 19:0955311b0db6 1465 autopwm[THR] = SetTHRinRatio(0.5);
HARUKIDELTA 22:438bedf24707 1466 if(autopwm[AIL_R]<trimpwm[AIL_R]){
HARUKIDELTA 3:206b17251f5b 1467 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
HARUKIDELTA 3:206b17251f5b 1468 }
HARUKIDELTA 3:206b17251f5b 1469 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionrightloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
HARUKIDELTA 0:17f575135219 1470
HARUKIDELTA 0:17f575135219 1471 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
HARUKIDELTA 0:17f575135219 1472 }
HARUKIDELTA 0:17f575135219 1473
HARUKIDELTA 0:17f575135219 1474 //左旋回
HARUKIDELTA 0:17f575135219 1475 void UpdateTargetAngle_Leftloop(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 1476
HARUKIDELTA 0:17f575135219 1477 targetAngle[ROLL] = g_leftloopROLL;
HARUKIDELTA 0:17f575135219 1478 targetAngle[PITCH] = g_leftloopPITCH;
HARUKIDELTA 16:7fca5b938da6 1479 autopwm[RUD]=g_leftloopRUD;
HARUKIDELTA 19:0955311b0db6 1480 autopwm[THR] = SetTHRinRatio(0.5);
HARUKIDELTA 22:438bedf24707 1481 if(autopwm[AIL_R]<trimpwm[AIL_R]){
HARUKIDELTA 17:55249ea37dff 1482 autopwm[AIL_L]=trimpwm[AIL_L]+g_AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
HARUKIDELTA 4:fff1165ca50c 1483 }
HARUKIDELTA 17:55249ea37dff 1484 else autopwm[AIL_L]=trimpwm[AIL_L]+g_AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
HARUKIDELTA 4:fff1165ca50c 1485 //checkHeight(targetAngle);
HARUKIDELTA 4:fff1165ca50c 1486
HARUKIDELTA 4:fff1165ca50c 1487 //printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[], targetAngle[PITCH], autopwm[THR]);
HARUKIDELTA 4:fff1165ca50c 1488 }
HARUKIDELTA 4:fff1165ca50c 1489
HARUKIDELTA 4:fff1165ca50c 1490 //左旋回(着陸時スロットル0のとき)
HARUKIDELTA 4:fff1165ca50c 1491 void UpdateTargetAngle_Leftloop_zero(float targetAngle[3]){
HARUKIDELTA 4:fff1165ca50c 1492
HARUKIDELTA 4:fff1165ca50c 1493 targetAngle[ROLL] = g_leftloopROLL;
HARUKIDELTA 4:fff1165ca50c 1494 targetAngle[PITCH] = g_leftloopPITCH;
HARUKIDELTA 16:7fca5b938da6 1495 autopwm[RUD]=g_leftloopRUD;
HARUKIDELTA 4:fff1165ca50c 1496 autopwm[THR] = minpwm[THR];
HARUKIDELTA 22:438bedf24707 1497 if(autopwm[AIL_R]<trimpwm[AIL_R]){
HARUKIDELTA 4:fff1165ca50c 1498 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
HARUKIDELTA 4:fff1165ca50c 1499 }
HARUKIDELTA 4:fff1165ca50c 1500 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloop+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
HARUKIDELTA 4:fff1165ca50c 1501 //autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_Ratio_leftloop;
HARUKIDELTA 0:17f575135219 1502 //checkHeight(targetAngle);
HARUKIDELTA 0:17f575135219 1503
HARUKIDELTA 2:23daa5fa28b4 1504 //printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[], targetAngle[PITCH], autopwm[THR]);
HARUKIDELTA 0:17f575135219 1505 }
HARUKIDELTA 0:17f575135219 1506
HARUKIDELTA 0:17f575135219 1507 void UpdateTargetAngle_Leftloop_short(float targetAngle[3]){
HARUKIDELTA 0:17f575135219 1508
HARUKIDELTA 0:17f575135219 1509 targetAngle[ROLL] = g_leftloopROLLshort;
HARUKIDELTA 0:17f575135219 1510 targetAngle[PITCH] = g_leftloopPITCHshort;
HARUKIDELTA 16:7fca5b938da6 1511 autopwm[RUD]=g_leftloopRUD;
HARUKIDELTA 19:0955311b0db6 1512 autopwm[THR] = SetTHRinRatio(0.5);
HARUKIDELTA 22:438bedf24707 1513 if(autopwm[AIL_R]<trimpwm[AIL_R]){
HARUKIDELTA 4:fff1165ca50c 1514 autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioRising;
HARUKIDELTA 4:fff1165ca50c 1515 }
HARUKIDELTA 4:fff1165ca50c 1516 else autopwm[AIL_L]=trimpwm[AIL_L]+AIL_L_correctionleftloopshort+(autopwm[AIL_R]-trimpwm[AIL_R])*AIL_L_RatioDescent;
HARUKIDELTA 4:fff1165ca50c 1517
HARUKIDELTA 0:17f575135219 1518 //pc.printf("Roll = %f, PITCH = %f, THR = %d \r\n", targetAngle[ROLL], targetAngle[PITCH], autopwm[THR]);
HARUKIDELTA 0:17f575135219 1519 }
taknokolat 10:652071c20bf6 1520
HARUKIDELTA 18:cce82f3374fc 1521 void Sbusprintf(){
HARUKIDELTA 18:cce82f3374fc 1522
HARUKIDELTA 18:cce82f3374fc 1523 for(uint8_t i=0; i<8; i++) pc.printf("ch.%d = %d ",i+1,sbus.manualpwm[i]);
HARUKIDELTA 18:cce82f3374fc 1524 pc.printf("\r\n");
HARUKIDELTA 18:cce82f3374fc 1525
HARUKIDELTA 18:cce82f3374fc 1526 }
HARUKIDELTA 18:cce82f3374fc 1527
HARUKIDELTA 0:17f575135219 1528
HARUKIDELTA 0:17f575135219 1529 //デバッグ用
HARUKIDELTA 0:17f575135219 1530 void DebugPrint(){
HARUKIDELTA 0:17f575135219 1531 /*
HARUKIDELTA 0:17f575135219 1532 static int16_t deltaT = 0, t_start = 0;
HARUKIDELTA 0:17f575135219 1533 deltaT = t.read_u2s() - t_start;
HARUKIDELTA 0:17f575135219 1534 pc.printf("t:%d us, ",deltaT);
HARUKIDELTA 0:17f575135219 1535 pc.printf("\r\n");
HARUKIDELTA 0:17f575135219 1536 t_start = t.read_us();
HARUKIDELTA 0:17f575135219 1537 */
HARUKIDELTA 0:17f575135219 1538 //for(uint8_t i=0; i<8; i++) pc.printf("%d ",sbus.manualpwm[i]);
HARUKIDELTA 0:17f575135219 1539 //for(uint8_t i=1; i<4; i++) pc.printf("%d ",sbus.manualpwm[i]);
HARUKIDELTA 0:17f575135219 1540 //pc.printf("\r\n");
HARUKIDELTA 2:23daa5fa28b4 1541 //for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]);
HARUKIDELTA 0:17f575135219 1542 //for(uint8_t i=0; i<2; i++) pc.printf("%3.2f\t",nowAngle[i]); //ロール、ピッチのみ 小数点以下2ケタ
taknokolat 10:652071c20bf6 1543 //pc.printf("%d\t",autopwm[AIL_L]); // pc.printf("%d\t",autopwm[RUD]);
HARUKIDELTA 0:17f575135219 1544 //pc.printf("%d",g_distance);
HARUKIDELTA 0:17f575135219 1545
HARUKIDELTA 0:17f575135219 1546 //pc.printf("Mode: %c: ",g_buf[0]);
HARUKIDELTA 0:17f575135219 1547 //if(g_buf[0] == 'Y') pc.printf(" %3.1f",g_SerialTargetYAW);
taknokolat 10:652071c20bf6 1548 //pc.printf("\r\n");
HARUKIDELTA 0:17f575135219 1549 }