usb実装中

Dependencies:   mbed MPU6050_2 HMC5883L_2 SDFileSystem3

Committer:
taknokolat
Date:
Sun Feb 10 09:05:27 2019 +0000
Revision:
10:63fe920595a7
Parent:
9:21cd5d18ad9e
Child:
11:8427ecccf07d
s

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 10:63fe920595a7 106 MoveCameraBoard();
taknokolat 10:63fe920595a7 107
taknokolat 7:8989a4b84695 108 if(g_landingcommand!='N') MatchPosition();
taknokolat 8:d11a59d2a2f1 109
taknokolat 8:d11a59d2a2f1 110 if(jevoisFlag==true) NVIC_DisableIRQ(USART1_IRQn);
taknokolat 8:d11a59d2a2f1 111 else NVIC_DisableIRQ(USART2_IRQn);
taknokolat 8:d11a59d2a2f1 112
taknokolat 7:8989a4b84695 113 MoveCansat(g_landingcommand);
Skykon 3:c18342e4fddd 114 wait_ms(23);
taknokolat 8:d11a59d2a2f1 115
taknokolat 7:8989a4b84695 116 if(jevoisFlag==true) NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 117 else NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 118 }
HARUKIDELTA 0:84ddd6d354e1 119 }
HARUKIDELTA 0:84ddd6d354e1 120
Skykon 3:c18342e4fddd 121
taknokolat 1:290e621741fd 122 void MoveCansat(char g_landingcommand)
HARUKIDELTA 0:84ddd6d354e1 123 {
taknokolat 2:f30666d7838b 124 //NVIC_DisableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 125 //NVIC_DisableIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 126 switch(g_landingcommand){
taknokolat 2:f30666d7838b 127 case 'N': //MOVE_NEUTRAL
HARUKIDELTA 0:84ddd6d354e1 128 servoR.pulsewidth_us(servo_NEUTRAL_R);
HARUKIDELTA 0:84ddd6d354e1 129 servoL.pulsewidth_us(servo_NEUTRAL_L);
taknokolat 2:f30666d7838b 130 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 131 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 132 break;
taknokolat 1:290e621741fd 133
taknokolat 2:f30666d7838b 134 case 'Y': //MOVE_FORWARD
HARUKIDELTA 0:84ddd6d354e1 135 servoR.pulsewidth_us(servo_FORWARD_R);
HARUKIDELTA 0:84ddd6d354e1 136 servoL.pulsewidth_us(servo_FORWARD_L);
taknokolat 2:f30666d7838b 137 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 138 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 139 break;
taknokolat 1:290e621741fd 140
taknokolat 1:290e621741fd 141 case 'L': //MOVE_LEFT
HARUKIDELTA 0:84ddd6d354e1 142 servoR.pulsewidth_us(servo_slow_FORWARD_R);
HARUKIDELTA 0:84ddd6d354e1 143 servoL.pulsewidth_us(servo_slow_back_L);
taknokolat 2:f30666d7838b 144 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 145 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 146
taknokolat 2:f30666d7838b 147 case 'R': //MOVE_RIGHT
HARUKIDELTA 0:84ddd6d354e1 148 servoR.pulsewidth_us(servo_slow_back_R);
HARUKIDELTA 0:84ddd6d354e1 149 servoL.pulsewidth_us(servo_slow_FORWARD_L);
taknokolat 2:f30666d7838b 150 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 151 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 152 break;
taknokolat 1:290e621741fd 153
taknokolat 1:290e621741fd 154 case 'B': //MOVE_BACK
HARUKIDELTA 0:84ddd6d354e1 155 servoR.pulsewidth_us(servo_back_R);
HARUKIDELTA 0:84ddd6d354e1 156 servoL.pulsewidth_us(servo_back_L);
taknokolat 2:f30666d7838b 157 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 158 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 159 break;
taknokolat 1:290e621741fd 160
taknokolat 1:290e621741fd 161 case 'G': //GOAL_FORWARD
HARUKIDELTA 0:84ddd6d354e1 162 servoR.pulsewidth_us(servo_slow_FORWARD_R);
HARUKIDELTA 0:84ddd6d354e1 163 servoL.pulsewidth_us(servo_slow_FORWARD_L);
taknokolat 2:f30666d7838b 164 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 165 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 7:8989a4b84695 166 break;
taknokolat 7:8989a4b84695 167
taknokolat 7:8989a4b84695 168 case 'J': //MOVE_FORWARD Tim
taknokolat 7:8989a4b84695 169 servoR.pulsewidth_us(servo_FORWARD_R);
taknokolat 7:8989a4b84695 170 servoL.pulsewidth_us(servo_FORWARD_L);
taknokolat 7:8989a4b84695 171 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 172 wait(5);
taknokolat 10:63fe920595a7 173 do{
taknokolat 10:63fe920595a7 174 }while((nowAngle[PITCH]<20&&nowAngle[PITCH]>-20)&&(nowAngle[ROLL]<20&&nowAngle[ROLL]>-20));
taknokolat 7:8989a4b84695 175 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 7:8989a4b84695 176 break;
taknokolat 7:8989a4b84695 177
taknokolat 7:8989a4b84695 178 case 'M': //MatchPosition
taknokolat 7:8989a4b84695 179 servoR.pulsewidth_us(MatchSpeed);
taknokolat 7:8989a4b84695 180 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 181 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 7:8989a4b84695 182 break;
taknokolat 7:8989a4b84695 183
taknokolat 7:8989a4b84695 184 case 'T': //jevoisからraspberry piへの切り替え
taknokolat 7:8989a4b84695 185 jevoisFlag = false;
taknokolat 7:8989a4b84695 186 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 187 break;
taknokolat 1:290e621741fd 188
taknokolat 1:290e621741fd 189 default :
taknokolat 2:f30666d7838b 190 //NVIC_EnableIRQ(USART1_IRQn);
taknokolat 7:8989a4b84695 191 //NVIC_EnableIRQ(USART2_IRQn);
HARUKIDELTA 0:84ddd6d354e1 192 break;
taknokolat 1:290e621741fd 193
HARUKIDELTA 0:84ddd6d354e1 194 }
taknokolat 7:8989a4b84695 195
HARUKIDELTA 0:84ddd6d354e1 196 return;
taknokolat 1:290e621741fd 197 }
taknokolat 1:290e621741fd 198
taknokolat 7:8989a4b84695 199 void MoveCameraBoard(){
taknokolat 7:8989a4b84695 200 MoveCansat('N');
taknokolat 10:63fe920595a7 201 g_landingcommand='N';
taknokolat 7:8989a4b84695 202 servoTurnTable.pulsewidth_us(2000);
taknokolat 7:8989a4b84695 203 wait_ms(300);
taknokolat 7:8989a4b84695 204 servoTurnTable.pulsewidth_us(TurnTable_NEUTRAL);
taknokolat 7:8989a4b84695 205 if(jevoisFlag == true) FocusAdjust();
taknokolat 7:8989a4b84695 206 else wait(1);
taknokolat 7:8989a4b84695 207
taknokolat 7:8989a4b84695 208 if(g_landingcommand!='N') return;
taknokolat 7:8989a4b84695 209 if(!CameraDegFlag){
taknokolat 7:8989a4b84695 210 servoCameradeg.pulsewidth_us(1800);
taknokolat 7:8989a4b84695 211 CameraDegFlag=!CameraDegFlag;
taknokolat 7:8989a4b84695 212 }else{
taknokolat 7:8989a4b84695 213 servoCameradeg.pulsewidth_us(1500);
taknokolat 7:8989a4b84695 214 CameraDegFlag = !CameraDegFlag;
taknokolat 7:8989a4b84695 215 }
taknokolat 7:8989a4b84695 216
taknokolat 7:8989a4b84695 217 if(jevoisFlag == true) FocusAdjust();
taknokolat 7:8989a4b84695 218 else wait(1);
taknokolat 7:8989a4b84695 219
taknokolat 7:8989a4b84695 220 return;
taknokolat 7:8989a4b84695 221 }
taknokolat 7:8989a4b84695 222
taknokolat 7:8989a4b84695 223 void MatchPosition(){
taknokolat 7:8989a4b84695 224 SensingMPU();
taknokolat 7:8989a4b84695 225 SensingHMC();
taknokolat 7:8989a4b84695 226 DebugPrint();
taknokolat 7:8989a4b84695 227
taknokolat 9:21cd5d18ad9e 228 while(nowAngle[YAW] <= nowAngle_HMC+5 && nowAngle[YAW] >= nowAngle_HMC-5){
taknokolat 7:8989a4b84695 229 MoveCansat('M');
taknokolat 9:21cd5d18ad9e 230 SensingMPU();
taknokolat 7:8989a4b84695 231 }
taknokolat 7:8989a4b84695 232 return;
taknokolat 7:8989a4b84695 233 }
taknokolat 7:8989a4b84695 234
taknokolat 7:8989a4b84695 235 void FocusAdjust(){
taknokolat 7:8989a4b84695 236 servoCameraPinto.pulsewidth_us(minFocus);
taknokolat 7:8989a4b84695 237
taknokolat 7:8989a4b84695 238 for(int i=0; i<400; i++){
taknokolat 7:8989a4b84695 239 servoCameraPinto.pulsewidth_us(minFocus+i);
taknokolat 7:8989a4b84695 240 wait_ms(30);
taknokolat 7:8989a4b84695 241 if(g_landingcommand!='N') return;
taknokolat 7:8989a4b84695 242 }
taknokolat 7:8989a4b84695 243 return;
taknokolat 7:8989a4b84695 244 }
taknokolat 7:8989a4b84695 245
taknokolat 1:290e621741fd 246 void getSF_Serial_jevois(){
taknokolat 1:290e621741fd 247
taknokolat 1:290e621741fd 248
taknokolat 1:290e621741fd 249 static char SFbuf[16]={'Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q'};
taknokolat 1:290e621741fd 250
taknokolat 1:290e621741fd 251 static int bufcounter=0;
taknokolat 1:290e621741fd 252
taknokolat 1:290e621741fd 253
taknokolat 1:290e621741fd 254
taknokolat 1:290e621741fd 255 if(pc.readable()) { // 受信確認
taknokolat 1:290e621741fd 256
taknokolat 1:290e621741fd 257 SFbuf[bufcounter] = pc.getc(); // 1文字取り出し
taknokolat 1:290e621741fd 258 if(SFbuf[0]!='S'){
taknokolat 1:290e621741fd 259 //pc.printf("x");
taknokolat 1:290e621741fd 260 return;
taknokolat 1:290e621741fd 261 }
taknokolat 1:290e621741fd 262
taknokolat 1:290e621741fd 263
taknokolat 1:290e621741fd 264
taknokolat 1:290e621741fd 265 //pc.printf("%c",SFbuf[bufcounter]);
taknokolat 1:290e621741fd 266
Skykon 3:c18342e4fddd 267 if(SFbuf[0]=='S' && bufcounter<5)bufcounter++;
taknokolat 1:290e621741fd 268
taknokolat 1:290e621741fd 269 if(bufcounter==5 && SFbuf[4]=='F'){
taknokolat 1:290e621741fd 270
taknokolat 1:290e621741fd 271 g_landingcommand = SFbuf[1];
taknokolat 2:f30666d7838b 272 wait_ms(31);//信号が速すぎることによる割り込み防止
taknokolat 1:290e621741fd 273 //pc.printf("%c",g_landingcommand);
taknokolat 1:290e621741fd 274 //wait_ms(20);
taknokolat 1:290e621741fd 275 //if(g_landingcommand=='Y')g_SerialTargetYAW = ConvertByteintoFloat(SFbuf[2], SFbuf[3]);
taknokolat 1:290e621741fd 276 bufcounter = 0;
taknokolat 1:290e621741fd 277 memset(SFbuf, 0, sizeof(SFbuf));
taknokolat 1:290e621741fd 278 NVIC_ClearPendingIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 279 //pc.printf("command = %c, commandYAW = %f\r\n", g_landingcommand, g_SerialTargetYAW);
taknokolat 1:290e621741fd 280 }
taknokolat 1:290e621741fd 281
taknokolat 1:290e621741fd 282 else if(bufcounter>=5){
taknokolat 1:290e621741fd 283 //pc.printf("Communication Falsed.\r\n");
taknokolat 1:290e621741fd 284 memset(SFbuf, 0, sizeof(SFbuf));
taknokolat 1:290e621741fd 285 bufcounter = 0;
taknokolat 1:290e621741fd 286 NVIC_ClearPendingIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 287 }
taknokolat 1:290e621741fd 288 }
taknokolat 1:290e621741fd 289
taknokolat 1:290e621741fd 290
taknokolat 1:290e621741fd 291 }
taknokolat 1:290e621741fd 292
taknokolat 1:290e621741fd 293
taknokolat 1:290e621741fd 294 void getSF_Serial_pi(){
taknokolat 2:f30666d7838b 295
taknokolat 7:8989a4b84695 296 //NVIC_DisableIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 297
taknokolat 1:290e621741fd 298 static char SFbuf[16]={'Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q'};
taknokolat 1:290e621741fd 299
taknokolat 1:290e621741fd 300 static int bufcounter=0;
taknokolat 1:290e621741fd 301
taknokolat 1:290e621741fd 302
taknokolat 1:290e621741fd 303
taknokolat 1:290e621741fd 304 if(pc2.readable()) { // 受信確認
taknokolat 1:290e621741fd 305
taknokolat 1:290e621741fd 306 SFbuf[bufcounter] = pc2.getc(); // 1文字取り出し
taknokolat 1:290e621741fd 307 if(SFbuf[0]!='S'){
taknokolat 1:290e621741fd 308 //pc.printf("x");
taknokolat 1:290e621741fd 309 return;
taknokolat 1:290e621741fd 310 }
taknokolat 1:290e621741fd 311
taknokolat 1:290e621741fd 312
taknokolat 1:290e621741fd 313
taknokolat 1:290e621741fd 314 //pc.printf("%c",SFbuf[bufcounter]);
taknokolat 1:290e621741fd 315
taknokolat 1:290e621741fd 316 if(SFbuf[0]=='S'&&bufcounter<5)bufcounter++;
taknokolat 1:290e621741fd 317
taknokolat 1:290e621741fd 318 if(bufcounter==5 && SFbuf[4]=='F'){
taknokolat 1:290e621741fd 319
taknokolat 1:290e621741fd 320 g_landingcommand = SFbuf[1];
taknokolat 2:f30666d7838b 321 wait_ms(31);//信号が速すぎることによる割り込み防止
taknokolat 1:290e621741fd 322 //pc.printf("%c",g_landingcommand);
taknokolat 1:290e621741fd 323 //wait_ms(20);
taknokolat 1:290e621741fd 324 //if(g_landingcommand=='Y')g_SerialTargetYAW = ConvertByteintoFloat(SFbuf[2], SFbuf[3]);
taknokolat 1:290e621741fd 325 bufcounter = 0;
taknokolat 1:290e621741fd 326 memset(SFbuf, 0, sizeof(SFbuf));
taknokolat 1:290e621741fd 327 NVIC_ClearPendingIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 328 //pc.printf("command = %c, commandYAW = %f\r\n", g_landingcommand, g_SerialTargetYAW);
taknokolat 1:290e621741fd 329 }
taknokolat 1:290e621741fd 330
taknokolat 1:290e621741fd 331 else if(bufcounter>=5){
taknokolat 1:290e621741fd 332 //pc.printf("Communication Falsed.\r\n");
taknokolat 1:290e621741fd 333 memset(SFbuf, 0, sizeof(SFbuf));
taknokolat 1:290e621741fd 334 bufcounter = 0;
taknokolat 1:290e621741fd 335 NVIC_ClearPendingIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 336 }
taknokolat 1:290e621741fd 337 }
taknokolat 2:f30666d7838b 338
taknokolat 7:8989a4b84695 339 //NVIC_EnableIRQ(USART2_IRQn);
taknokolat 1:290e621741fd 340
Skykon 3:c18342e4fddd 341 }
taknokolat 1:290e621741fd 342
Skykon 3:c18342e4fddd 343
Skykon 3:c18342e4fddd 344 void setup(){
Skykon 3:c18342e4fddd 345
Skykon 3:c18342e4fddd 346 Init_sensors();
Skykon 3:c18342e4fddd 347 //switch2.rise(ResetTrim);
Skykon 3:c18342e4fddd 348
taknokolat 7:8989a4b84695 349 //NVIC_SetPriority(USART1_IRQn,0);
taknokolat 7:8989a4b84695 350 //NVIC_SetPriority(EXTI0_IRQn,1);
taknokolat 7:8989a4b84695 351 //NVIC_SetPriority(TIM5_IRQn,2);
taknokolat 7:8989a4b84695 352 //NVIC_SetPriority(EXTI9_5_IRQn,3);
taknokolat 7:8989a4b84695 353 //NVIC_SetPriority(USART2_IRQn,4);
taknokolat 7:8989a4b84695 354
taknokolat 7:8989a4b84695 355 NVIC_SetPriority(USART1_IRQn,0);//割り込み優先度
taknokolat 7:8989a4b84695 356 NVIC_SetPriority(USART2_IRQn,1);
taknokolat 7:8989a4b84695 357
Skykon 3:c18342e4fddd 358 DisplayClock();
Skykon 3:c18342e4fddd 359 t.start();
Skykon 3:c18342e4fddd 360
Skykon 3:c18342e4fddd 361 pc.printf("MPU calibration start\r\n");
taknokolat 6:166746820555 362 pc.printf("HMC calibration start\r\n");
Skykon 3:c18342e4fddd 363
Skykon 3:c18342e4fddd 364 float offsetstart = t.read();
Skykon 3:c18342e4fddd 365 while(t.read() - offsetstart < 26){
Skykon 3:c18342e4fddd 366 SensingMPU();
taknokolat 6:166746820555 367 SensingHMC();
Skykon 3:c18342e4fddd 368 for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]);
taknokolat 6:166746820555 369 pc.printf("%3.2f\t",nowAngle_HMC);
Skykon 3:c18342e4fddd 370 pc.printf("\r\n");
Skykon 3:c18342e4fddd 371 }
Skykon 3:c18342e4fddd 372
Skykon 3:c18342e4fddd 373 FirstROLL = nowAngle[ROLL];
Skykon 3:c18342e4fddd 374 FirstPITCH = nowAngle[PITCH];
Skykon 3:c18342e4fddd 375 nowAngle[ROLL] -=FirstROLL;
Skykon 3:c18342e4fddd 376 nowAngle[PITCH] -=FirstPITCH;
taknokolat 6:166746820555 377 FirstYAW = nowAngle[YAW];
taknokolat 6:166746820555 378 nowAngle[YAW] -= FirstYAW;
taknokolat 6:166746820555 379
taknokolat 6:166746820555 380 g_FirstYAW_HMC = nowAngle_HMC;
taknokolat 6:166746820555 381 nowAngle_HMC -=g_FirstYAW_HMC;
taknokolat 6:166746820555 382 if(nowAngle_HMC<0)nowAngle_HMC+=360;
Skykon 3:c18342e4fddd 383
Skykon 3:c18342e4fddd 384 wait(0.2);
Skykon 3:c18342e4fddd 385
Skykon 3:c18342e4fddd 386 pc.printf("All initialized\r\n");
taknokolat 4:67f705d42f1e 387 setupFlag=true;
taknokolat 1:290e621741fd 388 }
taknokolat 1:290e621741fd 389
Skykon 3:c18342e4fddd 390
Skykon 3:c18342e4fddd 391 void SensingMPU(){
Skykon 3:c18342e4fddd 392 //static int16_t deltaT = 0, t_start = 0;
Skykon 3:c18342e4fddd 393 //t_start = t.read_us();
Skykon 3:c18342e4fddd 394
taknokolat 7:8989a4b84695 395 float rpy[3] = {0};
Skykon 3:c18342e4fddd 396 static uint16_t count_changeRPY = 0;
Skykon 3:c18342e4fddd 397 static bool flg_checkoutlier = false;
Skykon 3:c18342e4fddd 398 NVIC_DisableIRQ(USART1_IRQn);
Skykon 3:c18342e4fddd 399 NVIC_DisableIRQ(USART2_IRQn);
Skykon 3:c18342e4fddd 400 NVIC_DisableIRQ(TIM5_IRQn);
Skykon 3:c18342e4fddd 401 NVIC_DisableIRQ(EXTI0_IRQn);
Skykon 3:c18342e4fddd 402 NVIC_DisableIRQ(EXTI9_5_IRQn);
Skykon 3:c18342e4fddd 403
Skykon 3:c18342e4fddd 404 mpu6050.getRollPitchYaw_Skipper(rpy);
Skykon 3:c18342e4fddd 405
Skykon 3:c18342e4fddd 406 NVIC_EnableIRQ(USART1_IRQn);
Skykon 3:c18342e4fddd 407 NVIC_EnableIRQ(USART2_IRQn);
Skykon 3:c18342e4fddd 408 NVIC_EnableIRQ(TIM5_IRQn);
Skykon 3:c18342e4fddd 409 NVIC_EnableIRQ(EXTI0_IRQn);
Skykon 3:c18342e4fddd 410 NVIC_EnableIRQ(EXTI9_5_IRQn);
Skykon 3:c18342e4fddd 411
Skykon 3:c18342e4fddd 412
Skykon 3:c18342e4fddd 413 //外れ値対策
Skykon 3:c18342e4fddd 414 for(uint8_t i=0; i<3; i++) rpy[i] *= 180.0f/PI;
Skykon 3:c18342e4fddd 415 rpy[ROLL] -= FirstROLL;
Skykon 3:c18342e4fddd 416 rpy[PITCH] -= FirstPITCH;
taknokolat 4:67f705d42f1e 417 if(!setupFlag){
taknokolat 4:67f705d42f1e 418 rpy[YAW] -= FirstYAW;
taknokolat 4:67f705d42f1e 419 }else{
taknokolat 4:67f705d42f1e 420 if(rpy[YAW] >= FirstYAW){
taknokolat 4:67f705d42f1e 421 rpy[YAW] -= FirstYAW;
taknokolat 4:67f705d42f1e 422 }else{
taknokolat 4:67f705d42f1e 423 rpy[YAW] += 360.0f;
taknokolat 4:67f705d42f1e 424 rpy[YAW] -= FirstYAW;
taknokolat 4:67f705d42f1e 425 }
taknokolat 4:67f705d42f1e 426 }
Skykon 3:c18342e4fddd 427
Skykon 3:c18342e4fddd 428 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 429 if(!flg_checkoutlier || count_changeRPY >= 2){
Skykon 3:c18342e4fddd 430 for(uint8_t i=0; i<3; i++){
Skykon 3:c18342e4fddd 431 nowAngle[i] = (rpy[i] + nowAngle[i])/2.0f; //2つの移動平均
Skykon 3:c18342e4fddd 432 }
Skykon 3:c18342e4fddd 433 count_changeRPY = 0;
Skykon 3:c18342e4fddd 434 }else count_changeRPY++;
Skykon 3:c18342e4fddd 435 flg_checkoutlier = false;
Skykon 3:c18342e4fddd 436
Skykon 3:c18342e4fddd 437 }
Skykon 3:c18342e4fddd 438
Skykon 3:c18342e4fddd 439
Skykon 3:c18342e4fddd 440 void Init_sensors(){
Skykon 3:c18342e4fddd 441 if(mpu6050.setup() == -1){
Skykon 3:c18342e4fddd 442 pc.printf("failed initialize\r\n");
Skykon 3:c18342e4fddd 443 }
Skykon 3:c18342e4fddd 444 }
Skykon 3:c18342e4fddd 445
Skykon 3:c18342e4fddd 446
Skykon 3:c18342e4fddd 447 void DisplayClock(){
Skykon 3:c18342e4fddd 448 pc.printf("System Clock = %d[MHz]\r\n", HAL_RCC_GetSysClockFreq()/1000000);
Skykon 3:c18342e4fddd 449 pc.printf("HCLK Clock = %d[MHz]\r\n", HAL_RCC_GetHCLKFreq()/1000000);
Skykon 3:c18342e4fddd 450 pc.printf("PCLK1 Clock = %d[MHz]\r\n", HAL_RCC_GetPCLK1Freq()/1000000);
Skykon 3:c18342e4fddd 451 pc.printf("PCLK2 Clock = %d[MHz]\r\n", HAL_RCC_GetPCLK2Freq()/1000000);
Skykon 3:c18342e4fddd 452 pc.printf("\r\n");
taknokolat 4:67f705d42f1e 453 }
taknokolat 6:166746820555 454
taknokolat 6:166746820555 455 void SensingHMC(){
taknokolat 6:166746820555 456 //static int16_t deltaT = 0, t_start = 0;
taknokolat 6:166746820555 457 //t_start = t.read_us();
taknokolat 6:166746820555 458
taknokolat 7:8989a4b84695 459 float rpy=0;
taknokolat 6:166746820555 460 static uint16_t count_changeRPY = 0;
taknokolat 6:166746820555 461 static bool flg_checkoutlier = false;
taknokolat 6:166746820555 462 NVIC_DisableIRQ(USART1_IRQn);
taknokolat 6:166746820555 463 NVIC_DisableIRQ(USART2_IRQn);
taknokolat 6:166746820555 464 NVIC_DisableIRQ(TIM5_IRQn);
taknokolat 6:166746820555 465 NVIC_DisableIRQ(EXTI0_IRQn);
taknokolat 6:166746820555 466 NVIC_DisableIRQ(EXTI9_5_IRQn);
taknokolat 4:67f705d42f1e 467
taknokolat 6:166746820555 468 rpy= compass.getHeadingXYDeg(20,50);
taknokolat 6:166746820555 469
taknokolat 6:166746820555 470 NVIC_EnableIRQ(USART1_IRQn);
taknokolat 6:166746820555 471 NVIC_EnableIRQ(USART2_IRQn);
taknokolat 6:166746820555 472 NVIC_EnableIRQ(TIM5_IRQn);
taknokolat 6:166746820555 473 NVIC_EnableIRQ(EXTI0_IRQn);
taknokolat 6:166746820555 474 NVIC_EnableIRQ(EXTI9_5_IRQn);
taknokolat 6:166746820555 475
taknokolat 6:166746820555 476
taknokolat 6:166746820555 477 //外れ値対策
taknokolat 6:166746820555 478 //rpy*= 180.0f/PI;
taknokolat 6:166746820555 479 if(!setupFlag){
taknokolat 6:166746820555 480 rpy -= g_FirstYAW_HMC;
taknokolat 6:166746820555 481 }else{
taknokolat 6:166746820555 482 if(rpy >= g_FirstYAW_HMC){
taknokolat 6:166746820555 483 rpy -= g_FirstYAW_HMC;
taknokolat 6:166746820555 484 }else{
taknokolat 6:166746820555 485 rpy += 360.0f;
taknokolat 6:166746820555 486 rpy -= g_FirstYAW_HMC;
taknokolat 6:166746820555 487 }
taknokolat 6:166746820555 488 }
taknokolat 6:166746820555 489
taknokolat 6:166746820555 490 if(rpy < nowAngle_HMC-10 || rpy > nowAngle_HMC+10) {flg_checkoutlier = true;}
taknokolat 6:166746820555 491 if(!flg_checkoutlier || count_changeRPY >= 2){
taknokolat 6:166746820555 492
taknokolat 6:166746820555 493 nowAngle_HMC = (rpy + nowAngle_HMC)/2.0f; //2つの移動平均
taknokolat 6:166746820555 494
taknokolat 6:166746820555 495 count_changeRPY = 0;
taknokolat 6:166746820555 496 }else count_changeRPY++;
taknokolat 6:166746820555 497 flg_checkoutlier = false;
taknokolat 6:166746820555 498
taknokolat 6:166746820555 499 }
taknokolat 6:166746820555 500
taknokolat 4:67f705d42f1e 501 void DebugPrint(){
taknokolat 4:67f705d42f1e 502 //for(uint8_t i=0; i<3; i++) pc.printf("%3.2f\t",nowAngle[i]); //skipper地磁気センサ_デバック用
taknokolat 7:8989a4b84695 503 //pc.printf("%3.2f\t",nowAngle[2]);
taknokolat 7:8989a4b84695 504 //pc.printf("%3.2f\t",nowAngle_HMC); //HMC
taknokolat 7:8989a4b84695 505 //pc.printf("\r\n");
taknokolat 6:166746820555 506 }