完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
` 无人机预热,一个无线速度控制小车。加了触屏开关和蓝牙模块进行无线控制.测试通讯能力后期加装超声波传感器进行防撞保护,以及加装编码盘可以实现闭环速度控制。同时附上mpu6050加速度传感器卡尔曼滤波程序,得到的程序测试结果很稳定。`
|
|
相关推荐
6 个讨论
|
|
附上小车相关程序 这个是别的板子控制,程序都差不多
int incomingByte = 0; // for incoming serial data int ltwf=2; int ltwb=3; int rtwf=4; int rtwb=5; int lhwf=6; int lhwb=7; int rhwf=8; int rhwb=9; int i=0; int t=0; int m=1023; int touch=22; int spd=90; int lspd=50; int lswitch=0; void setup() { pinMode(ltwf,OUTPUT); pinMode(ltwb,OUTPUT); pinMode(rtwf,OUTPUT); pinMode(rtwb,OUTPUT); pinMode(lhwf,OUTPUT); pinMode(lhwb,OUTPUT); pinMode(rhwf,OUTPUT); pinMode(rhwb,OUTPUT); pinMode(touch,INPUT); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps digitalWrite(rtwf,LOW); digitalWrite(ltwf,LOW); digitalWrite(rhwf,LOW); digitalWrite(lhwf,LOW); digitalWrite(rtwb,LOW); digitalWrite(ltwb,LOW); digitalWrite(rhwb,LOW); digitalWrite(lhwb,LOW); } void loop() { lswitch=digitalRead(touch); t=lswitch; /* if(lswitch==1){ if(t==0){t++;i=0;} else{t=0;} delay(100); } // Serial.print(lswitch); //Serial.println(t);*/ if(t==1){ if (Serial.available() > 0) { // send data only when you receive data: incomingByte = Serial.read(); // read the incoming byte: i=incomingByte; Serial.print(incomingByte); switch (i){ case '2':analogWrite(ltwf,spd); analogWrite(rtwf,spd); analogWrite(lhwf,spd); analogWrite(rhwf,spd); digitalWrite(ltwb,LOW); digitalWrite(rtwb,LOW); digitalWrite(lhwb,LOW); digitalWrite(lhwb,LOW);i=0;delay(100); break; case '8':analogWrite(ltwb,spd); analogWrite(rtwb,spd); analogWrite(lhwb,spd); analogWrite(rhwb,spd); digitalWrite(ltwf,LOW); digitalWrite(rtwf,LOW); digitalWrite(lhwf,LOW); digitalWrite(lhwf,LOW);i=0;delay(100); break; case '6':analogWrite(ltwf,LOW); analogWrite(ltwb,lspd); analogWrite(lhwf,LOW); analogWrite(lhwb,lspd); analogWrite(rtwf,lspd); analogWrite(rtwb,LOW); analogWrite(rhwf,lspd); analogWrite(rhwb,LOW);i=0;delay(100); break; case '4':analogWrite(ltwf,lspd); analogWrite(ltwb,LOW); analogWrite(lhwf,lspd); analogWrite(lhwb,LOW); analogWrite(rtwf,LOW); analogWrite(rtwb,lspd); analogWrite(rhwf,LOW); analogWrite(rhwb,lspd); i=0;delay(100); break; case '5':digitalWrite(rtwf,LOW); digitalWrite(ltwf,LOW); digitalWrite(rhwf,LOW); digitalWrite(lhwf,LOW); digitalWrite(rtwb,LOW); digitalWrite(ltwb,LOW); digitalWrite(rhwb,LOW); digitalWrite(lhwb,LOW); i=0;delay(100); break; } } } } |
|
|
|
|
|
以及卡尔曼滤波程序及测试曲线
|
|
|
|
|
|
这是卡尔曼滤波算法
MPU6050 accelgyro; unsigned long now, lastTime = 0; float dt; //微分时间 int16_t ax, ay, az, gx, gy, gz; //加速度计陀螺仪原始数据 float aax=0, aay=0,aaz=0, agx=0, agy=0, agz=0; //角度变量 long axo = 0, ayo = 0, azo = 0; //加速度计偏移量 long gxo = 0, gyo = 0, gzo = 0; //陀螺仪偏移量 float pi = 3.1415926; float AcceRatio = 16384.0; //加速度计比例系数 float GyroRatio = 131.0; //陀螺仪比例系数 uint8_t n_sample = 8; //加速度计滤波算法采样个数 float aaxs[8] = {0}, aays[8] = {0}, aazs[8] = {0}; //x,y轴采样队列 long aax_sum, aay_sum,aaz_sum; //x,y轴采样和 float a_x[10]={0}, a_y[10]={0},a_z[10]={0} ,g_x[10]={0} ,g_y[10]={0},g_z[10]={0}; //加速度计协方差计算队列 float Px=1, Rx, Kx, Sx, Vx, Qx; //x轴卡尔曼变量 float Py=1, Ry, Ky, Sy, Vy, Qy; //y轴卡尔曼变量 float Pz=1, Rz, Kz, Sz, Vz, Qz; //z轴卡尔曼变量 void setup() { Wire.begin(); Serial.begin(115200); accelgyro.initialize(); //初始化 unsigned short times = 200; //采样次数 for(int i=0;i accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //读取六轴原始数值 axo += ax; ayo += ay; azo += az; //采样和 gxo += gx; gyo += gy; gzo += gz; } axo /= times; ayo /= times; azo /= times; //计算加速度计偏移 gxo /= times; gyo /= times; gzo /= times; //计算陀螺仪偏移 } void loop() { unsigned long now = millis(); //当前时间(ms) dt = (now - lastTime) / 1000.0; //微分时间(s) lastTime = now; //上一次采样时间(ms) accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //读取六轴原始数值 float accx = ax / AcceRatio; //x轴加速度 float accy = ay / AcceRatio; //y轴加速度 float accz = az / AcceRatio; //z轴加速度 aax = atan(accy / accz) * (-180) / pi; //y轴对于z轴的夹角 aay = atan(accx / accz) * 180 / pi; //x轴对于z轴的夹角 aaz = atan(accz / accy) * 180 / pi; //z轴对于y轴的夹角 aax_sum = 0; // 对于加速度计原始数据的滑动加权滤波算法 aay_sum = 0; aaz_sum = 0; for(int i=1;i aaxs[i-1] = aaxs[i]; aax_sum += aaxs[i] * i; aays[i-1] = aays[i]; aay_sum += aays[i] * i; aazs[i-1] = aazs[i]; aaz_sum += aazs[i] * i; } aaxs[n_sample-1] = aax; aax_sum += aax * n_sample; aax = (aax_sum / (11*n_sample/2.0)) * 9 / 7.0; //角度调幅至0-90° aays[n_sample-1] = aay; //此处应用实验法取得合适的系数 aay_sum += aay * n_sample; //本例系数为9/7 aay = (aay_sum / (11*n_sample/2.0)) * 9 / 7.0; aazs[n_sample-1] = aaz; aaz_sum += aaz * n_sample; aaz = (aaz_sum / (11*n_sample/2.0)) * 9 / 7.0; float gyrox = - (gx-gxo) / GyroRatio * dt; //x轴角速度 float gyroy = - (gy-gyo) / GyroRatio * dt; //y轴角速度 float gyroz = - (gz-gzo) / GyroRatio * dt; //z轴角速度 agx += gyrox; //x轴角速度积分 agy += gyroy; //x轴角速度积分 agz += gyroz; /* kalman start */ Sx = 0; Rx = 0; Sy = 0; Ry = 0; Sz = 0; Rz = 0; for(int i=1;i<10;i++) { //测量值平均值运算 a_x[i-1] = a_x[i]; //即加速度平均值 Sx += a_x[i]; a_y[i-1] = a_y[i]; Sy += a_y[i]; a_z[i-1] = a_z[i]; Sz += a_z[i]; } a_x[9] = aax; Sx += aax; Sx /= 10; //x轴加速度平均值 a_y[9] = aay; Sy += aay; Sy /= 10; //y轴加速度平均值 a_z[9] = aaz; Sz += aaz; Sz /= 10; for(int i=0;i<10;i++) { Rx += sq(a_x[i] - Sx); Ry += sq(a_y[i] - Sy); Rz += sq(a_z[i] - Sz); } Rx = Rx / 9; //得到方差 Ry = Ry / 9; Rz = Rz / 9; Px = Px + 0.0025; // 0.0025在下面有说明... Kx = Px / (Px + Rx); //计算卡尔曼增益 agx = agx + Kx * (aax - agx); //陀螺仪角度与加速度计速度叠加 Px = (1 - Kx) * Px; //更新p值 Py = Py + 0.0025; Ky = Py / (Py + Ry); agy = agy + Ky * (aay - agy); Py = (1 - Ky) * Py; Pz = Pz + 0.0025; Kz = Pz / (Pz + Rz); agz = agz + Kz * (aaz - agz); Pz = (1 - Kz) * Pz; /* kalman end */ } |
|
|
|
|
|
待我加上pid速度算法写完 撸一个无人机
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
2248个成员聚集在这个小组
加入小组灵动微电子MM32全系列MCU产品应用手册,库函数和例程和选型表
11665 浏览 3 评论
【MM32 eMiniBoard试用连载】+基于OLED12864的GUI---U8G2
5920 浏览 1 评论
【MM32 eMiniBoard试用连载】移植RT-Thread至MM32L373PS
10950 浏览 0 评论
【MM32 eMiniBoard测评报告】+ 开箱 + 初探
4571 浏览 1 评论
灵动微课堂(第106讲) | MM32 USB功能学习笔记 —— WinUSB设备
4296 浏览 1 评论
[MM32软件] MM32F002使用内部flash存储数据怎么操作?
963浏览 1评论
792浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-23 02:58 , Processed in 0.741798 second(s), Total 81, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号