SDの設定が反映されるように修正

Dependencies:   mbed MPU6050_2 HMC5883L_2 SDFileSystem3

Committer:
taknokolat
Date:
Sat Feb 09 06:34:54 2019 +0000
Revision:
8:d11a59d2a2f1
Parent:
7:8989a4b84695
Child:
9:21cd5d18ad9e
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HARUKIDELTA 0:84ddd6d354e1 1 #include "mbed.h"
Skykon 3:c18342e4fddd 2 #include "MPU6050_DMP6.h"
taknokolat 6:166746820555 3 #include "HMC5883L.h"
Skykon 3:c18342e4fddd 4
Skykon 3:c18342e4fddd 5 //MPU_check用
Skykon 3:c18342e4fddd 6 #define PI 3.14159265358979
HARUKIDELTA 0:84ddd6d354e1 7
taknokolat 6:166746820555 8 #define servo_NEUTRAL_R 1900
taknokolat 6:166746820555 9 #define servo_NEUTRAL_L 1900
HARUKIDELTA 0:84ddd6d354e1 10 #define servo_FORWARD_R 1860
HARUKIDELTA 0:84ddd6d354e1 11 #define servo_FORWARD_L 1860
HARUKIDELTA 0:84ddd6d354e1 12 #define servo_back_R 1060
HARUKIDELTA 0:84ddd6d354e1 13 #define servo_back_L 1060
HARUKIDELTA 0:84ddd6d354e1 14 #define servo_slow_FORWARD_R 1560
HARUKIDELTA 0:84ddd6d354e1 15 #define servo_slow_FORWARD_L 1560
HARUKIDELTA 0:84ddd6d354e1 16 #define servo_slow_back_R 1360
HARUKIDELTA 0:84ddd6d354e1 17 #define servo_slow_back_L 1360
taknokolat 7:8989a4b84695 18 #define TurnTable_NEUTRAL 1500 //カメラ台座のサーボ
taknokolat 7:8989a4b84695 19 #define MatchSpeed 1500 + 100 //カメラと方向を合わせるときの車輪の速度
taknokolat 7:8989a4b84695 20 #define minFocus 1200 //焦点合わせ用サーボの最小値
HARUKIDELTA 0:84ddd6d354e1 21
HARUKIDELTA 0:84ddd6d354e1 22 #define MOVE_NEUTRAL 0
HARUKIDELTA 0:84ddd6d354e1 23 #define MOVE_FORWARD 1
HARUKIDELTA 0:84ddd6d354e1 24 #define MOVE_LEFT 2
HARUKIDELTA 0:84ddd6d354e1 25 #define MOVE_RIGHT 3
HARUKIDELTA 0:84ddd6d354e1 26 #define MOVE_BACK 4
Skykon 3:c18342e4fddd 27 #define GOAL_FORWARD 5 //ゴール付近_ゆっくり
HARUKIDELTA 0:84ddd6d354e1 28 #define GOAL_LEFT 6
HARUKIDELTA 0:84ddd6d354e1 29 #define GOAL_RIGHT 7
Skykon 3:c18342e4fddd 30 #define MAX_FORWARD 8 //はやい_姿勢修正用
HARUKIDELTA 0:84ddd6d354e1 31 #define MAX_BACK 9
HARUKIDELTA 0:84ddd6d354e1 32
Skykon 3:c18342e4fddd 33
taknokolat 1:290e621741fd 34 void getSF_Serial_jevois();
taknokolat 1:290e621741fd 35 void getSF_Serial_pi();
taknokolat 1:290e621741fd 36
Skykon 3:c18342e4fddd 37 //MPU_check用
Skykon 3:c18342e4fddd 38 void SensingMPU();
taknokolat 7:8989a4b84695 39
taknokolat 7:8989a4b84695 40 void MoveCansat(char g_landingcommand);
Skykon 3:c18342e4fddd 41 void setup();
Skykon 3:c18342e4fddd 42 void Init_sensors();
Skykon 3:c18342e4fddd 43 void DisplayClock();
taknokolat 4:67f705d42f1e 44 void DebugPrint();
taknokolat 6:166746820555 45 void SensingHMC();
taknokolat 7:8989a4b84695 46 void MoveCameraBoard();
taknokolat 7:8989a4b84695 47 void MatchPosition();
taknokolat 7:8989a4b84695 48 void FocusAdjust();
Skykon 3:c18342e4fddd 49
taknokolat 6:166746820555 50 static float nowAngle[3] = {0,0,0},nowAngle_HMC=0;
taknokolat 6:166746820555 51 float FirstROLL = 0.0, FirstPITCH = 0.0 ,FirstYAW = 0.0,g_FirstYAW_HMC;
Skykon 3:c18342e4fddd 52
taknokolat 7:8989a4b84695 53 bool setupFlag = false;
taknokolat 7:8989a4b84695 54 bool CameraDegFlag = false;
taknokolat 7:8989a4b84695 55 bool jevoisFlag = true;
taknokolat 4:67f705d42f1e 56
Skykon 3:c18342e4fddd 57 enum Angle{ROLL, PITCH, YAW}; //yaw:北を0とした絶対角度
Skykon 3:c18342e4fddd 58
Skykon 3:c18342e4fddd 59 Timer t;
Skykon 3:c18342e4fddd 60
HARUKIDELTA 5:8bfe95431ec0 61 //PWM pin宣言
taknokolat 6:166746820555 62 PwmOut servoR(PC_6); //TIM3_CH1 車輪右
taknokolat 6:166746820555 63 PwmOut servoL(PC_7); //TIM3_CH2 車輪左
HARUKIDELTA 5:8bfe95431ec0 64 PwmOut servoTurnTable(PB_0); //TIM3_CH3 カメラ台回転Servo
HARUKIDELTA 5:8bfe95431ec0 65 PwmOut servoCameradeg(PB_1); //TIM3_CH4 カメラ角度調節Servo
HARUKIDELTA 5:8bfe95431ec0 66 PwmOut servoCameraPinto(PB_6); //TIM4_CH1 カメラピント合わせ
HARUKIDELTA 5:8bfe95431ec0 67 PwmOut servoCameramount(PA_6); //skipperウラ カメラマウント起動
HARUKIDELTA 5:8bfe95431ec0 68 PwmOut servoGetUP(PC_8); //skipperウラ 起き上がり動作
HARUKIDELTA 5:8bfe95431ec0 69 PwmOut servoParachute(PA_11); //skipper USB端子より パラシュート切り離し
Skykon 3:c18342e4fddd 70
HARUKIDELTA 5:8bfe95431ec0 71 /*通信用のpinは
HARUKIDELTA 5:8bfe95431ec0 72 PA_3(UART2_Rx) skipperウラ
HARUKIDELTA 5:8bfe95431ec0 73 PA_12(UART6_Rx) skipperオモテ USB端子より
HARUKIDELTA 5:8bfe95431ec0 74 PB_7 (UART1_Rx) skipperオモテ 6番目 TIM4_CH2
HARUKIDELTA 5:8bfe95431ec0 75 */
HARUKIDELTA 5:8bfe95431ec0 76
HARUKIDELTA 5:8bfe95431ec0 77 /*超音波はRaspberryPiに積む*/
HARUKIDELTA 5:8bfe95431ec0 78
HARUKIDELTA 5:8bfe95431ec0 79 //外付けコンパス
taknokolat 6:166746820555 80 HMC5883L compass(PB_9, PB_8); //コンパスセンサー TIM4_CH3とCH4
HARUKIDELTA 0:84ddd6d354e1 81
taknokolat 2:f30666d7838b 82 RawSerial pc(PA_2,PA_3,115200); //uart2
Skykon 3:c18342e4fddd 83 //pa2:UART2_TX,pa3:UART2_RX
taknokolat 2:f30666d7838b 84 RawSerial pc2(PB_6,PB_7,115200); //uart1
Skykon 3:c18342e4fddd 85 //pb6:UART1_TX,pb7:UART1_RX
taknokolat 1:290e621741fd 86
taknokolat 2:f30666d7838b 87 char g_landingcommand='N';
taknokolat 1:290e621741fd 88
Skykon 3:c18342e4fddd 89 //MPU_check用
Skykon 3:c18342e4fddd 90 MPU6050DMP6 mpu6050(PC_0,&pc);
Skykon 3:c18342e4fddd 91
Skykon 3:c18342e4fddd 92
HARUKIDELTA 0:84ddd6d354e1 93 int main() {
taknokolat 1:290e621741fd 94
Skykon 3:c18342e4fddd 95 //MPU_check
Skykon 3:c18342e4fddd 96 setup();
Skykon 3:c18342e4fddd 97
taknokolat 1:290e621741fd 98 // シリアル通信受信の割り込みイベント登録
taknokolat 1:290e621741fd 99 pc.attach(getSF_Serial_jevois, Serial::RxIrq);
taknokolat 1:290e621741fd 100 pc2.attach(getSF_Serial_pi, Serial::RxIrq);
taknokolat 1:290e621741fd 101
HARUKIDELTA 0:84ddd6d354e1 102 while(1) {
taknokolat 7:8989a4b84695 103 if(jevoisFlag==true) NVIC_DisableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 104 else NVIC_DisableIRQ(USART2_IRQn);
taknokolat 8:d11a59d2a2f1 105
taknokolat 7:8989a4b84695 106 if(g_landingcommand!='N') MatchPosition();
taknokolat 8:d11a59d2a2f1 107
taknokolat 8:d11a59d2a2f1 108 if(jevoisFlag==true) NVIC_DisableIRQ(USART1_IRQn);
taknokolat 8:d11a59d2a2f1 109 else NVIC_DisableIRQ(USART2_IRQn);
taknokolat 8:d11a59d2a2f1 110
taknokolat 7:8989a4b84695 111 MoveCansat(g_landingcommand);
Skykon 3:c18342e4fddd 112 wait_ms(23);
taknokolat 8:d11a59d2a2f1 113
taknokolat 7:8989a4b84695 114 if(jevoisFlag==true) NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 115 else NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 116 }
HARUKIDELTA 0:84ddd6d354e1 117 }
HARUKIDELTA 0:84ddd6d354e1 118
Skykon 3:c18342e4fddd 119
taknokolat 1:290e621741fd 120 void MoveCansat(char g_landingcommand)
HARUKIDELTA 0:84ddd6d354e1 121 {
taknokolat 2:f30666d7838b 122 //NVIC_DisableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 123 //NVIC_DisableIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 124 switch(g_landingcommand){
taknokolat 2:f30666d7838b 125 case 'N': //MOVE_NEUTRAL
HARUKIDELTA 0:84ddd6d354e1 126 servoR.pulsewidth_us(servo_NEUTRAL_R);
HARUKIDELTA 0:84ddd6d354e1 127 servoL.pulsewidth_us(servo_NEUTRAL_L);
taknokolat 2:f30666d7838b 128 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 129 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 130 break;
taknokolat 1:290e621741fd 131
taknokolat 2:f30666d7838b 132 case 'Y': //MOVE_FORWARD
HARUKIDELTA 0:84ddd6d354e1 133 servoR.pulsewidth_us(servo_FORWARD_R);
HARUKIDELTA 0:84ddd6d354e1 134 servoL.pulsewidth_us(servo_FORWARD_L);
taknokolat 2:f30666d7838b 135 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 136 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 137 break;
taknokolat 1:290e621741fd 138
taknokolat 1:290e621741fd 139 case 'L': //MOVE_LEFT
HARUKIDELTA 0:84ddd6d354e1 140 servoR.pulsewidth_us(servo_slow_FORWARD_R);
HARUKIDELTA 0:84ddd6d354e1 141 servoL.pulsewidth_us(servo_slow_back_L);
taknokolat 2:f30666d7838b 142 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 143 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 144
taknokolat 2:f30666d7838b 145 case 'R': //MOVE_RIGHT
HARUKIDELTA 0:84ddd6d354e1 146 servoR.pulsewidth_us(servo_slow_back_R);
HARUKIDELTA 0:84ddd6d354e1 147 servoL.pulsewidth_us(servo_slow_FORWARD_L);
taknokolat 2:f30666d7838b 148 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 149 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 150 break;
taknokolat 1:290e621741fd 151
taknokolat 1:290e621741fd 152 case 'B': //MOVE_BACK
HARUKIDELTA 0:84ddd6d354e1 153 servoR.pulsewidth_us(servo_back_R);
HARUKIDELTA 0:84ddd6d354e1 154 servoL.pulsewidth_us(servo_back_L);
taknokolat 2:f30666d7838b 155 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 156 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 157 break;
taknokolat 1:290e621741fd 158
taknokolat 1:290e621741fd 159 case 'G': //GOAL_FORWARD
HARUKIDELTA 0:84ddd6d354e1 160 servoR.pulsewidth_us(servo_slow_FORWARD_R);
HARUKIDELTA 0:84ddd6d354e1 161 servoL.pulsewidth_us(servo_slow_FORWARD_L);
taknokolat 2:f30666d7838b 162 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 163 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 7:8989a4b84695 164 break;
taknokolat 7:8989a4b84695 165
taknokolat 7:8989a4b84695 166 case 'J': //MOVE_FORWARD Tim
taknokolat 7:8989a4b84695 167 servoR.pulsewidth_us(servo_FORWARD_R);
taknokolat 7:8989a4b84695 168 servoL.pulsewidth_us(servo_FORWARD_L);
taknokolat 7:8989a4b84695 169 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 170 wait(5);
taknokolat 7:8989a4b84695 171 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 7:8989a4b84695 172 break;
taknokolat 7:8989a4b84695 173
taknokolat 7:8989a4b84695 174 case 'M': //MatchPosition
taknokolat 7:8989a4b84695 175 servoR.pulsewidth_us(MatchSpeed);
taknokolat 7:8989a4b84695 176 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 177 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 7:8989a4b84695 178 break;
taknokolat 7:8989a4b84695 179
taknokolat 7:8989a4b84695 180 case 'T': //jevoisからraspberry piへの切り替え
taknokolat 7:8989a4b84695 181 jevoisFlag = false;
taknokolat 7:8989a4b84695 182 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 183 break;
taknokolat 1:290e621741fd 184
taknokolat 1:290e621741fd 185 default :
taknokolat 2:f30666d7838b 186 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 187 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 188 break;
taknokolat 1:290e621741fd 189
HARUKIDELTA 0:84ddd6d354e1 190 }
taknokolat 7:8989a4b84695 191
HARUKIDELTA 0:84ddd6d354e1 192 return;
taknokolat 1:290e621741fd 193 }
taknokolat 1:290e621741fd 194
taknokolat 7:8989a4b84695 195 void MoveCameraBoard(){
taknokolat 7:8989a4b84695 196 MoveCansat('N');
taknokolat 7:8989a4b84695 197 servoTurnTable.pulsewidth_us(2000);
taknokolat 7:8989a4b84695 198 wait_ms(300);
taknokolat 7:8989a4b84695 199 servoTurnTable.pulsewidth_us(TurnTable_NEUTRAL);
taknokolat 7:8989a4b84695 200 if(jevoisFlag == true) FocusAdjust();
taknokolat 7:8989a4b84695 201 else wait(1);
taknokolat 7:8989a4b84695 202
taknokolat 7:8989a4b84695 203 if(g_landingcommand!='N') return;
taknokolat 7:8989a4b84695 204 if(!CameraDegFlag){
taknokolat 7:8989a4b84695 205 servoCameradeg.pulsewidth_us(1800);
taknokolat 7:8989a4b84695 206 CameraDegFlag=!CameraDegFlag;
taknokolat 7:8989a4b84695 207 }else{
taknokolat 7:8989a4b84695 208 servoCameradeg.pulsewidth_us(1500);
taknokolat 7:8989a4b84695 209 CameraDegFlag = !CameraDegFlag;
taknokolat 7:8989a4b84695 210 }
taknokolat 7:8989a4b84695 211
taknokolat 7:8989a4b84695 212 if(jevoisFlag == true) FocusAdjust();
taknokolat 7:8989a4b84695 213 else wait(1);
taknokolat 7:8989a4b84695 214
taknokolat 7:8989a4b84695 215 return;
taknokolat 7:8989a4b84695 216 }
taknokolat 7:8989a4b84695 217
taknokolat 7:8989a4b84695 218 void MatchPosition(){
taknokolat 7:8989a4b84695 219 SensingMPU();
taknokolat 7:8989a4b84695 220 SensingHMC();
taknokolat 7:8989a4b84695 221 DebugPrint();
taknokolat 7:8989a4b84695 222
taknokolat 7:8989a4b84695 223 while(nowAngle_HMC <= nowAngle[YAW]+5 && nowAngle_HMC >= nowAngle[YAW]-5){
taknokolat 7:8989a4b84695 224 MoveCansat('M');
taknokolat 7:8989a4b84695 225 }
taknokolat 7:8989a4b84695 226 return;
taknokolat 7:8989a4b84695 227 }
taknokolat 7:8989a4b84695 228
taknokolat 7:8989a4b84695 229 void FocusAdjust(){
taknokolat 7:8989a4b84695 230 servoCameraPinto.pulsewidth_us(minFocus);
taknokolat 7:8989a4b84695 231
taknokolat 7:8989a4b84695 232 for(int i=0; i<400; i++){
taknokolat 7:8989a4b84695 233 servoCameraPinto.pulsewidth_us(minFocus+i);
taknokolat 7:8989a4b84695 234 wait_ms(30);
taknokolat 7:8989a4b84695 235 if(g_landingcommand!='N') return;
taknokolat 7:8989a4b84695 236 }
taknokolat 7:8989a4b84695 237 return;
taknokolat 7:8989a4b84695 238 }
taknokolat 7:8989a4b84695 239
taknokolat 1:290e621741fd 240 void getSF_Serial_jevois(){
taknokolat 1:290e621741fd 241
taknokolat 1:290e621741fd 242
taknokolat 1:290e621741fd 243 static char SFbuf[16]={'Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q'};
taknokolat 1:290e621741fd 244
taknokolat 1:290e621741fd 245 static int bufcounter=0;
taknokolat 1:290e621741fd 246
taknokolat 1:290e621741fd 247
taknokolat 1:290e621741fd 248
taknokolat 1:290e621741fd 249 if(pc.readable()) { // 受信確認
taknokolat 1:290e621741fd 250
taknokolat 1:290e621741fd 251 SFbuf[bufcounter] = pc.getc(); // 1文字取り出し
taknokolat 1:290e621741fd 252 if(SFbuf[0]!='S'){
taknokolat 1:290e621741fd 253 //pc.printf("x");
taknokolat 1:290e621741fd 254 return;
taknokolat 1:290e621741fd 255 }
taknokolat 1:290e621741fd 256
taknokolat 1:290e621741fd 257
taknokolat 1:290e621741fd 258
taknokolat 1:290e621741fd 259 //pc.printf("%c",SFbuf[bufcounter]);
taknokolat 1:290e621741fd 260
Skykon 3:c18342e4fddd 261 if(SFbuf[0]=='S' && bufcounter<5)bufcounter++;
taknokolat 1:290e621741fd 262
taknokolat 1:290e621741fd 263 if(bufcounter==5 && SFbuf[4]=='F'){
taknokolat 1:290e621741fd 264
taknokolat 1:290e621741fd 265 g_landingcommand = SFbuf[1];
taknokolat 2:f30666d7838b 266 wait_ms(31);//信号が速すぎることによる割り込み防止
taknokolat 1:290e621741fd 267 //pc.printf("%c",g_landingcommand);
taknokolat 1:290e621741fd 268 //wait_ms(20);
taknokolat 1:290e621741fd 269 //if(g_landingcommand=='Y')g_SerialTargetYAW = ConvertByteintoFloat(SFbuf[2], SFbuf[3]);
taknokolat 1:290e621741fd 270 bufcounter = 0;
taknokolat 1:290e621741fd 271 memset(SFbuf, 0, sizeof(SFbuf));
taknokolat 1:290e621741fd 272 NVIC_ClearPendingIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 273 //pc.printf("command = %c, commandYAW = %f\r\n", g_landingcommand, g_SerialTargetYAW);
taknokolat 1:290e621741fd 274 }
taknokolat 1:290e621741fd 275
taknokolat 1:290e621741fd 276 else if(bufcounter>=5){
taknokolat 1:290e621741fd 277 //pc.printf("Communication Falsed.\r\n");
taknokolat 1:290e621741fd 278 memset(SFbuf, 0, sizeof(SFbuf));
taknokolat 1:290e621741fd 279 bufcounter = 0;
taknokolat 1:290e621741fd 280 NVIC_ClearPendingIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 281 }
taknokolat 1:290e621741fd 282 }
taknokolat 1:290e621741fd 283
taknokolat 1:290e621741fd 284
taknokolat 1:290e621741fd 285 }
taknokolat 1:290e621741fd 286
taknokolat 1:290e621741fd 287
taknokolat 1:290e621741fd 288 void getSF_Serial_pi(){
taknokolat 2:f30666d7838b 289
taknokolat 7:8989a4b84695 290 //NVIC_DisableIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 291
taknokolat 1:290e621741fd 292 static char SFbuf[16]={'Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q'};
taknokolat 1:290e621741fd 293
taknokolat 1:290e621741fd 294 static int bufcounter=0;
taknokolat 1:290e621741fd 295
taknokolat 1:290e621741fd 296
taknokolat 1:290e621741fd 297
taknokolat 1:290e621741fd 298 if(pc2.readable()) { // 受信確認
taknokolat 1:290e621741fd 299
taknokolat 1:290e621741fd 300 SFbuf[bufcounter] = pc2.getc(); // 1文字取り出し
taknokolat 1:290e621741fd 301 if(SFbuf[0]!='S'){
taknokolat 1:290e621741fd 302 //pc.printf("x");
taknokolat 1:290e621741fd 303 return;
taknokolat 1:290e621741fd 304 }
taknokolat 1:290e621741fd 305
taknokolat 1:290e621741fd 306
taknokolat 1:290e621741fd 307
taknokolat 1:290e621741fd 308 //pc.printf("%c",SFbuf[bufcounter]);
taknokolat 1:290e621741fd 309
taknokolat 1:290e621741fd 310 if(SFbuf[0]=='S'&&bufcounter<5)bufcounter++;
taknokolat 1:290e621741fd 311
taknokolat 1:290e621741fd 312 if(bufcounter==5 && SFbuf[4]=='F'){
taknokolat 1:290e621741fd 313
taknokolat 1:290e621741fd 314 g_landingcommand = SFbuf[1];
taknokolat 2:f30666d7838b 315 wait_ms(31);//信号が速すぎることによる割り込み防止
taknokolat 1:290e621741fd 316 //pc.printf("%c",g_landingcommand);
taknokolat 1:290e621741fd 317 //wait_ms(20);
taknokolat 1:290e621741fd 318 //if(g_landingcommand=='Y')g_SerialTargetYAW = ConvertByteintoFloat(SFbuf[2], SFbuf[3]);
taknokolat 1:290e621741fd 319 bufcounter = 0;
taknokolat 1:290e621741fd 320 memset(SFbuf, 0, sizeof(SFbuf));
taknokolat 1:290e621741fd 321 NVIC_ClearPendingIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 322 //pc.printf("command = %c, commandYAW = %f\r\n", g_landingcommand, g_SerialTargetYAW);
taknokolat 1:290e621741fd 323 }
taknokolat 1:290e621741fd 324
taknokolat 1:290e621741fd 325 else if(bufcounter>=5){
taknokolat 1:290e621741fd 326 //pc.printf("Communication Falsed.\r\n");
taknokolat 1:290e621741fd 327 memset(SFbuf, 0, sizeof(SFbuf));
taknokolat 1:290e621741fd 328 bufcounter = 0;
taknokolat 1:290e621741fd 329 NVIC_ClearPendingIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 330 }
taknokolat 1:290e621741fd 331 }
taknokolat 2:f30666d7838b 332
taknokolat 7:8989a4b84695 333 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 334
Skykon 3:c18342e4fddd 335 }
taknokolat 1:290e621741fd 336
Skykon 3:c18342e4fddd 337
Skykon 3:c18342e4fddd 338 void setup(){
Skykon 3:c18342e4fddd 339
Skykon 3:c18342e4fddd 340 Init_sensors();
Skykon 3:c18342e4fddd 341 //switch2.rise(ResetTrim);
Skykon 3:c18342e4fddd 342
taknokolat 7:8989a4b84695 343 //NVIC_SetPriority(USART1_IRQn,0);
taknokolat 7:8989a4b84695 344 //NVIC_SetPriority(EXTI0_IRQn,1);
taknokolat 7:8989a4b84695 345 //NVIC_SetPriority(TIM5_IRQn,2);
taknokolat 7:8989a4b84695 346 //NVIC_SetPriority(EXTI9_5_IRQn,3);
taknokolat 7:8989a4b84695 347 //NVIC_SetPriority(USART2_IRQn,4);
taknokolat 7:8989a4b84695 348
taknokolat 7:8989a4b84695 349 NVIC_SetPriority(USART1_IRQn,0);//割り込み優先度
taknokolat 7:8989a4b84695 350 NVIC_SetPriority(USART2_IRQn,1);
taknokolat 7:8989a4b84695 351
Skykon 3:c18342e4fddd 352 DisplayClock();
Skykon 3:c18342e4fddd 353 t.start();
Skykon 3:c18342e4fddd 354
Skykon 3:c18342e4fddd 355 pc.printf("MPU calibration start\r\n");
taknokolat 6:166746820555 356 pc.printf("HMC calibration start\r\n");
Skykon 3:c18342e4fddd 357
Skykon 3:c18342e4fddd 358 float offsetstart = t.read();
Skykon 3:c18342e4fddd 359 while(t.read() - offsetstart < 26){
Skykon 3:c18342e4fddd 360 SensingMPU();
taknokolat 6:166746820555 361 SensingHMC();
Skykon 3:c18342e4fddd 362 for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]);
taknokolat 6:166746820555 363 pc.printf("%3.2f\t",nowAngle_HMC);
Skykon 3:c18342e4fddd 364 pc.printf("\r\n");
Skykon 3:c18342e4fddd 365 }
Skykon 3:c18342e4fddd 366
Skykon 3:c18342e4fddd 367 FirstROLL = nowAngle[ROLL];
Skykon 3:c18342e4fddd 368 FirstPITCH = nowAngle[PITCH];
Skykon 3:c18342e4fddd 369 nowAngle[ROLL] -=FirstROLL;
Skykon 3:c18342e4fddd 370 nowAngle[PITCH] -=FirstPITCH;
taknokolat 6:166746820555 371 FirstYAW = nowAngle[YAW];
taknokolat 6:166746820555 372 nowAngle[YAW] -= FirstYAW;
taknokolat 6:166746820555 373
taknokolat 6:166746820555 374 g_FirstYAW_HMC = nowAngle_HMC;
taknokolat 6:166746820555 375 nowAngle_HMC -=g_FirstYAW_HMC;
taknokolat 6:166746820555 376 if(nowAngle_HMC<0)nowAngle_HMC+=360;
Skykon 3:c18342e4fddd 377
Skykon 3:c18342e4fddd 378 wait(0.2);
Skykon 3:c18342e4fddd 379
Skykon 3:c18342e4fddd 380 pc.printf("All initialized\r\n");
taknokolat 4:67f705d42f1e 381 setupFlag=true;
taknokolat 1:290e621741fd 382 }
taknokolat 1:290e621741fd 383
Skykon 3:c18342e4fddd 384
Skykon 3:c18342e4fddd 385 void SensingMPU(){
Skykon 3:c18342e4fddd 386 //static int16_t deltaT = 0, t_start = 0;
Skykon 3:c18342e4fddd 387 //t_start = t.read_us();
Skykon 3:c18342e4fddd 388
taknokolat 7:8989a4b84695 389 float rpy[3] = {0};
Skykon 3:c18342e4fddd 390 static uint16_t count_changeRPY = 0;
Skykon 3:c18342e4fddd 391 static bool flg_checkoutlier = false;
Skykon 3:c18342e4fddd 392 NVIC_DisableIRQ(USART1_IRQn);
Skykon 3:c18342e4fddd 393 NVIC_DisableIRQ(USART2_IRQn);
Skykon 3:c18342e4fddd 394 NVIC_DisableIRQ(TIM5_IRQn);
Skykon 3:c18342e4fddd 395 NVIC_DisableIRQ(EXTI0_IRQn);
Skykon 3:c18342e4fddd 396 NVIC_DisableIRQ(EXTI9_5_IRQn);
Skykon 3:c18342e4fddd 397
Skykon 3:c18342e4fddd 398 mpu6050.getRollPitchYaw_Skipper(rpy);
Skykon 3:c18342e4fddd 399
Skykon 3:c18342e4fddd 400 NVIC_EnableIRQ(USART1_IRQn);
Skykon 3:c18342e4fddd 401 NVIC_EnableIRQ(USART2_IRQn);
Skykon 3:c18342e4fddd 402 NVIC_EnableIRQ(TIM5_IRQn);
Skykon 3:c18342e4fddd 403 NVIC_EnableIRQ(EXTI0_IRQn);
Skykon 3:c18342e4fddd 404 NVIC_EnableIRQ(EXTI9_5_IRQn);
Skykon 3:c18342e4fddd 405
Skykon 3:c18342e4fddd 406
Skykon 3:c18342e4fddd 407 //外れ値対策
Skykon 3:c18342e4fddd 408 for(uint8_t i=0; i<3; i++) rpy[i] *= 180.0f/PI;
Skykon 3:c18342e4fddd 409 rpy[ROLL] -= FirstROLL;
Skykon 3:c18342e4fddd 410 rpy[PITCH] -= FirstPITCH;
taknokolat 4:67f705d42f1e 411 if(!setupFlag){
taknokolat 4:67f705d42f1e 412 rpy[YAW] -= FirstYAW;
taknokolat 4:67f705d42f1e 413 }else{
taknokolat 4:67f705d42f1e 414 if(rpy[YAW] >= FirstYAW){
taknokolat 4:67f705d42f1e 415 rpy[YAW] -= FirstYAW;
taknokolat 4:67f705d42f1e 416 }else{
taknokolat 4:67f705d42f1e 417 rpy[YAW] += 360.0f;
taknokolat 4:67f705d42f1e 418 rpy[YAW] -= FirstYAW;
taknokolat 4:67f705d42f1e 419 }
taknokolat 4:67f705d42f1e 420 }
Skykon 3:c18342e4fddd 421
Skykon 3:c18342e4fddd 422 for(uint8_t i=0; i<3; i++) {if(rpy[i] < nowAngle[i]-10 || rpy[i] > nowAngle[i]+10) {flg_checkoutlier = true;}}
Skykon 3:c18342e4fddd 423 if(!flg_checkoutlier || count_changeRPY >= 2){
Skykon 3:c18342e4fddd 424 for(uint8_t i=0; i<3; i++){
Skykon 3:c18342e4fddd 425 nowAngle[i] = (rpy[i] + nowAngle[i])/2.0f; //2つの移動平均
Skykon 3:c18342e4fddd 426 }
Skykon 3:c18342e4fddd 427 count_changeRPY = 0;
Skykon 3:c18342e4fddd 428 }else count_changeRPY++;
Skykon 3:c18342e4fddd 429 flg_checkoutlier = false;
Skykon 3:c18342e4fddd 430
Skykon 3:c18342e4fddd 431 }
Skykon 3:c18342e4fddd 432
Skykon 3:c18342e4fddd 433
Skykon 3:c18342e4fddd 434 void Init_sensors(){
Skykon 3:c18342e4fddd 435 if(mpu6050.setup() == -1){
Skykon 3:c18342e4fddd 436 pc.printf("failed initialize\r\n");
Skykon 3:c18342e4fddd 437 }
Skykon 3:c18342e4fddd 438 }
Skykon 3:c18342e4fddd 439
Skykon 3:c18342e4fddd 440
Skykon 3:c18342e4fddd 441 void DisplayClock(){
Skykon 3:c18342e4fddd 442 pc.printf("System Clock = %d[MHz]\r\n", HAL_RCC_GetSysClockFreq()/1000000);
Skykon 3:c18342e4fddd 443 pc.printf("HCLK Clock = %d[MHz]\r\n", HAL_RCC_GetHCLKFreq()/1000000);
Skykon 3:c18342e4fddd 444 pc.printf("PCLK1 Clock = %d[MHz]\r\n", HAL_RCC_GetPCLK1Freq()/1000000);
Skykon 3:c18342e4fddd 445 pc.printf("PCLK2 Clock = %d[MHz]\r\n", HAL_RCC_GetPCLK2Freq()/1000000);
Skykon 3:c18342e4fddd 446 pc.printf("\r\n");
taknokolat 4:67f705d42f1e 447 }
taknokolat 6:166746820555 448
taknokolat 6:166746820555 449 void SensingHMC(){
taknokolat 6:166746820555 450 //static int16_t deltaT = 0, t_start = 0;
taknokolat 6:166746820555 451 //t_start = t.read_us();
taknokolat 6:166746820555 452
taknokolat 7:8989a4b84695 453 float rpy=0;
taknokolat 6:166746820555 454 static uint16_t count_changeRPY = 0;
taknokolat 6:166746820555 455 static bool flg_checkoutlier = false;
taknokolat 6:166746820555 456 NVIC_DisableIRQ(USART1_IRQn);
taknokolat 6:166746820555 457 NVIC_DisableIRQ(USART2_IRQn);
taknokolat 6:166746820555 458 NVIC_DisableIRQ(TIM5_IRQn);
taknokolat 6:166746820555 459 NVIC_DisableIRQ(EXTI0_IRQn);
taknokolat 6:166746820555 460 NVIC_DisableIRQ(EXTI9_5_IRQn);
taknokolat 4:67f705d42f1e 461
taknokolat 6:166746820555 462 rpy= compass.getHeadingXYDeg(20,50);
taknokolat 6:166746820555 463
taknokolat 6:166746820555 464 NVIC_EnableIRQ(USART1_IRQn);
taknokolat 6:166746820555 465 NVIC_EnableIRQ(USART2_IRQn);
taknokolat 6:166746820555 466 NVIC_EnableIRQ(TIM5_IRQn);
taknokolat 6:166746820555 467 NVIC_EnableIRQ(EXTI0_IRQn);
taknokolat 6:166746820555 468 NVIC_EnableIRQ(EXTI9_5_IRQn);
taknokolat 6:166746820555 469
taknokolat 6:166746820555 470
taknokolat 6:166746820555 471 //外れ値対策
taknokolat 6:166746820555 472 //rpy*= 180.0f/PI;
taknokolat 6:166746820555 473 if(!setupFlag){
taknokolat 6:166746820555 474 rpy -= g_FirstYAW_HMC;
taknokolat 6:166746820555 475 }else{
taknokolat 6:166746820555 476 if(rpy >= g_FirstYAW_HMC){
taknokolat 6:166746820555 477 rpy -= g_FirstYAW_HMC;
taknokolat 6:166746820555 478 }else{
taknokolat 6:166746820555 479 rpy += 360.0f;
taknokolat 6:166746820555 480 rpy -= g_FirstYAW_HMC;
taknokolat 6:166746820555 481 }
taknokolat 6:166746820555 482 }
taknokolat 6:166746820555 483
taknokolat 6:166746820555 484 if(rpy < nowAngle_HMC-10 || rpy > nowAngle_HMC+10) {flg_checkoutlier = true;}
taknokolat 6:166746820555 485 if(!flg_checkoutlier || count_changeRPY >= 2){
taknokolat 6:166746820555 486
taknokolat 6:166746820555 487 nowAngle_HMC = (rpy + nowAngle_HMC)/2.0f; //2つの移動平均
taknokolat 6:166746820555 488
taknokolat 6:166746820555 489 count_changeRPY = 0;
taknokolat 6:166746820555 490 }else count_changeRPY++;
taknokolat 6:166746820555 491 flg_checkoutlier = false;
taknokolat 6:166746820555 492
taknokolat 6:166746820555 493 }
taknokolat 6:166746820555 494
taknokolat 4:67f705d42f1e 495 void DebugPrint(){
taknokolat 4:67f705d42f1e 496 //for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]); //skipper地磁気センサ_デバック用
taknokolat 7:8989a4b84695 497 //pc.printf("%3.2f\t",nowAngle[2]);
taknokolat 7:8989a4b84695 498 //pc.printf("%3.2f\t",nowAngle_HMC); //HMC
taknokolat 7:8989a4b84695 499 //pc.printf("\r\n");
taknokolat 6:166746820555 500 }