着陸wait版9/16

Dependencies:   HCSR04_2 MPU6050_2 mbed SDFileSystem3

Fork of Autoflight2018_17 by 航空研究会

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