pc,attachの変更

Dependencies:   HCSR04_2 MPU6050_2 mbed SDFileSystem3

Fork of Autoflight2018_36 by 航空研究会

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