Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MPU6050_Driver_Balance by
Revision 1:5e51ec875d34, committed 2018-05-09
- Comitter:
- brainliang
- Date:
- Wed May 09 09:59:32 2018 +0000
- Parent:
- 0:badebd32bd8b
- Commit message:
- adfsfdsa
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Apr 09 14:34:45 2018 +0000 +++ b/main.cpp Wed May 09 09:59:32 2018 +0000 @@ -2,12 +2,33 @@ #include "mpu6050.h" -DigitalOut myled(LED1); -Serial pc(SERIAL_TX, SERIAL_RX); +DigitalOut myled(PC_13); +Serial pc(PA_2, PA_3); +Ticker pid_ticker; //声明一个 Ticker 对象 + +void pid_calculator(); + +float pitch,roll,yaw; //欧拉角 +unsigned int filure_counter = 0; +unsigned int SystemTick = 0; + +void pid_calculator() //更新当前姿态 运行PID算法 +{ + SystemTick++; + if(SystemTick >= 5000) + SystemTick = 0; + if(mpu_dmp_get_data(&pitch,&roll,&yaw) != 0) + filure_counter++; +} + int main() { - float pitch,roll,yaw; //欧拉角 - + + int time_now = 0; + + pid_ticker.attach(&pid_calculator, 0.01); //中断跑在100Hz,因为6050也是以100Hz刷新,PID一定要写在中断pid_calculator()中 + + MPU_Init(); //初始化MPU6050 myled = 0; while(mpu_dmp_init()) @@ -17,9 +38,16 @@ } while(1) { - if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)//获取欧拉角 - { + //检测读取数据失败的次数 如感觉6050读数存在问题运行此段代码检查 串口打印的是每1s内 数据获取失败的次数 + /*if((SystemTick - time_now) >= 100) + { + time_now = SystemTick; + pc.printf("%d\r\n", filure_counter); + filure_counter = 0; + }*/ + + pc.printf("pitch: %.2f roll: %.2f yaw: %.2f\r\n", pitch,roll,yaw); - } } } +