skipper_raspi_uart_test

Dependencies:   mbed MPU6050_2 HMC5883L_4 SDFileSystem3

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