a

Dependencies:   HCSR04_2 MPU6050_2 mbed SDFileSystem3

Fork of Autoflight2018_4 by 航空研究会

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