MODSERIAL

Dependencies:   HCSR04_2 MODSERIAL MPU6050_2 mbed SDFileSystem3

Fork of Autoflight2018_22 by 航空研究会

Committer:
taknokolat
Date:
Tue Sep 18 06:01:03 2018 +0000
Revision:
19:870b3004bc23
Parent:
18:f3dcb174b563
Child:
20:5befe788d303
?

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