本篇是关于如何自己制作手势控制汽车的文章。基本上是MPU-60503轴陀螺仪、加速度计的简单应用。在此基础上,还可以做一些其他模块的添加。在这篇文章中,我将重点介绍两个HC-05蓝牙模块之间的蓝牙到蓝牙通信,通过了解如何使用它、如何将其与Arduino连接以及如何通过蓝牙模块传输数据。下面给出了机器人和发射器单元的连接图,可以用于参考。现在来说说蓝牙模块配置。基本上,HC-05蓝牙模块带有从属模块出厂设置。这意味着我们可以通过插入它来向模块发送数据。不需要做任何其他设置来从移动设备发送数据到HC-05模块。只需输入其默认密码(1234/0000)即可连接。但是如果我们想使用这个模块将数据发送到其他相同的模块或移动设备呢?在这个项目中,我们做同样的事情,通过蓝牙模块发送数据。由mpu-6050陀螺仪传感器收集到另一个蓝牙模块。所以要做到这一点首先我们需要配置这两个蓝牙模块。使它们在通电后可以自动相互绑定。在这里,第一个模块充当从设备,它将接收来自远程单元的信号并安装在汽车上。并将第二个配置为主设备,它将充当发送器单元并向从设备发送数据,所以首先将第一个蓝牙模块配置为从设备。为此,请按照此接线图将其与Arduino连接。并按名称上传代码配置。断开模块。按住模块上的ky并将其连接回去。您将看到模块上的LED闪烁较慢。每2秒一次。这意味着HC-05处于AT命令模式。现在打开串行监视器,将波特率更改为9600,输出类型为NL和CR。现在在发送框中键入AT并发送。如果它回复ok,则表示一切都很好。但如果不是,并且回复有错误,请再次发送AT。直到它回复ok或chek连接并再次发送AT得到模块的OK响应后,一一输入以下命令,AT+ORGL并发送。此命令会将模块设置为出厂设置。AT+RMAAD此命令将从任何先前的配对中释放模块AT+串口?检查模块的当前波特率AT+UART=38400,0,0设置波特率为38400AT+角色?检查角色是slave还是master。它回复0或1。如果模块是从属设备,则回复0,如果是主设备,则回复1将角色设置为从设备。输入AT+ROLE=0AT+地址?检查模块地址。记下这个地址,得到这个地址后,就完成了对从模块的配置。现在是时候将第二个蓝牙模块配置为主设备了。将此模块与Arduino板连接并进入AT模式。正如我们对上一个所做的那样。按给定的顺序输入这些AT命令。AT+ORGLAT+RMAADAT+串口?AT+UART=38400,0,0AT+角色?将此模块的角色设置为主设备。AT+角色=1AT+CMODE=0这样模块将只连接单个设备。默认设置为0现在将此模块与从设备绑定以执行此输入,AT+BIND="从机地址"就搞定了现在为MPU-6050传感器和I2C通信安装库。由于MPU-6050陀螺仪传感器具有I2C接口。从这里下载库和源代码:http://www.mediafire.com/file/l8mru5emulb8x93/gesture_control_robot.rar/file如果您已预先安装这些库,请跳过此步骤。现在使用USB电缆将汽车单元与电脑连接。选择正确的com端口和板卡类型。并按名称“Gesture_controled_Robot__car_unit_”上传程序。上传程序时,请确保电池和蓝牙模块未与汽车连接。//programbyShubhamShinganapureon3-10-2019////forGesturecontroledRoboticCarintlm1=8;//leftmotoroutput1intlm2=9;//leftmotoroutput2intrm1=10;//rightmotoroutput1intrm2=11;//rightmotoroutput2chard=0;voidsetup(){pinMode(lm1,OUTPUT);pinMode(lm2,OUTPUT);pinMode(rm1,OUTPUT);pinMode(rm2,OUTPUT);Serial.begin(38400);sTOP();}voidloop(){if(Serial.available()>0){d=Serial.read();if(d=='F'){ForWard();}if(d=='B'){BackWard();}if(d=='L'){Left();}if(d=='R'){Right();}if(d=='S'){sTOP();}}}voidForWard(){digitalWrite(lm1,HIGH);digitalWrite(lm2,LOW);digitalWrite(rm1,HIGH);digitalWrite(rm2,LOW);}voidBackWard(){digitalWrite(lm1,LOW);digitalWrite(lm2,HIGH);digitalWrite(rm1,LOW);digitalWrite(rm2,HIGH);}voidLeft(){digitalWrite(lm1,LOW);digitalWrite(lm2,HIGH);digitalWrite(rm1,HIGH);digitalWrite(rm2,LOW);}voidRight(){digitalWrite(lm1,HIGH);digitalWrite(lm2,LOW);digitalWrite(rm1,LOW);digitalWrite(rm2,HIGH);}voidsTOP(){digitalWrite(lm1,LOW);digitalWrite(lm2,LOW);digitalWrite(rm1,LOW);digitalWrite(rm2,LOW);}对远程单元执行相同操作。按名称远程打开程序。并将其上传到远程单元。//programmodifiedon3/10/19by//byShubhamShinganapure.////forGesturecontroledRoboticCar(remote)#include"I2Cdev.h"#include"MPU6050_6Axis_MotionApps20.h"//#include"MPU6050.h"//notnecessaryifusingMotionAppsincludefile//ArduinoWirelibraryisrequiredifI2CdevI2CDEV_ARDUINO_WIREimplementation//isusedinI2Cdev.h#ifI2CDEV_IMPLEMENTATION==I2CDEV_ARDUINO_WIRE#include"Wire.h"#endif//classdefaultI2Caddressis0x68//specificI2Caddressesmaybepassedasaparameterhere//AD0low=0x68(defaultforSparkFunbreakoutandInvenSenseevaluationboard)//AD0high=0x69MPU6050mpu;#defineOUTPUT_READABLE_YAWPITCHROLL//MPUcontrol/statusvarsbooldmpReady=false;//settrueifDMPinitwassuccessfuluint8_tmpuIntStatus;//holdsactualinterruptstatusbytefromMPUuint8_tdevStatus;//returnstatusaftereachdeviceoperation(0=success,!0=error)uint16_tpacketSize;//expectedDMPpacketsize(defaultis42bytes)uint16_tfifoCount;//countofallbytescurrentlyinFIFOuint8_tfifoBuffer[64];//FIFOstoragebufferVectorFloatgravity;Quaternionq;floatypr[3];//[yaw,pitch,roll]yaw/pitch/rollcontainerandgravityvectoruint8_tteapotPacket[14]={'$',0x02,0,0,0,0,0,0,0,0,0x00,0x00,'\r','\n'};volatileboolmpuInterrupt=false;//indicateswhetherMPUinterruptpinhasgonehighvoiddmpDataReady(){mpuInterrupt=true;}#includeSoftwareSerialBTSerial(10,11);//RX|TXintbt=8;intx=1;voidsetup(){#ifI2CDEV_IMPLEMENTATION==I2CDEV_ARDUINO_WIREWire.begin();TWBR=24;//400kHzI2Cclock(200kHzifCPUis8MHz)#elifI2CDEV_IMPLEMENTATION==I2CDEV_BUILTIN_FASTWIREFastwire::setup(400,true);#endif//initializeserialcommunication//(115200chosenbecauseitisrequiredforTeapotDemooutput,butit's//reallyuptoyoudependingonyourproject)Serial.begin(115200);BTSerial.begin(38400);//while(!Serial);//waitforLeonardoenumeration,otherscontinueimmediatelySerial.println(F("InitializingI2Cdevices..."));mpu.initialize();//verifyconnectionSerial.println(F("Testingdeviceconnections..."));Serial.println(mpu.testConnection()?F("MPU6050connectionsuccessful"):F("MPU6050connectionfailed"));//waitforready//loadandconfiguretheDMPSerial.println(F("InitializingDMP..."));devStatus=mpu.dmpInitialize();//supplyyourowngyrooffsetshere,scaledforminsensitivitympu.setXGyroOffset(220);mpu.setYGyroOffset(76);mpu.setZGyroOffset(-85);mpu.setZAccelOffset(1788);//makesureitworked(returns0ifso)if(devStatus==0){//turnontheDMP,nowthatit'sreadySerial.println(F("EnablingDMP..."));mpu.setDMPEnabled(true);//enableArduinointerruptdetectionSerial.println(F("Enablinginterruptdetection(Arduinoexternalinterrupt0)..."));attachInterrupt(0,dmpDataReady,RISING);mpuIntStatus=mpu.getIntStatus();//setourDMPReadyflagsothemainloop()functionknowsit'sokaytouseitSerial.println(F("DMPready!Waitingforfirstinterrupt..."));dmpReady=true;//getexpectedDMPpacketsizeforlatercomparisonpacketSize=mpu.dmpGetFIFOPacketSize();}else{//ERROR!//1=initialmemoryloadfailed//2=DMPconfigurationupdatesfailed//(ifit'sgoingtobreak,usuallythecodewillbe1)Serial.print(F("DMPInitializationfailed(code"));Serial.print(devStatus);Serial.println(F(")"));}//configureLEDforoutputpinMode(bt,INPUT);}//================================================================//===MAINPROGRAMLOOP===//================================================================voidloop(){if(digitalRead(bt)==HIGH){x++;delay(150);}if((x%2)==0){//ifprogrammingfailed,don'ttrytodoanythingif(!dmpReady)return;//waitforMPUinterruptorextrapacket(s)availablewhile(!mpuInterrupt&&fifoCount//otherprogrambehaviorstuffhere//.//.//.//ifyouarereallyparanoidyoucanfrequentlytestinbetweenother//stufftoseeifmpuInterruptistrue,andifso,"break;"fromthe//while()looptoimmediatelyprocesstheMPUdata//.//.//.}//resetinterruptflagandgetINT_STATUSbytempuInterrupt=false;mpuIntStatus=mpu.getIntStatus();//getcurrentFIFOcountfifoCount=mpu.getFIFOCount();//checkforoverflow(thisshouldneverhappenunlessourcodeistooinefficient)if((mpuIntStatus&0x10)||fifoCount==1024){//resetsowecancontinuecleanlympu.resetFIFO();Serial.println(F("FIFOoverflow!"));//otherwise,checkforDMPdatareadyinterrupt(thisshouldhappenfrequently)}elseif(mpuIntStatus&0x02){//waitforcorrectavailabledatalength,shouldbeaVERYshortwaitwhile(fifoCount//readapacketfromFIFOmpu.getFIFOBytes(fifoBuffer,packetSize);//trackFIFOcounthereincasethereis>1packetavailable//(thisletsusimmediatelyreadmorewithoutwaitingforaninterrupt)fifoCount-=packetSize;#ifdefOUTPUT_READABLE_YAWPITCHROLL//displayEuleranglesindegreesmpu.dmpGetQuaternion(&q,fifoBuffer);mpu.dmpGetGravity(&gravity,&q);mpu.dmpGetYawPitchRoll(ypr,&q,&gravity);Serial.print("ypr\t");Serial.print(ypr[0]*180/M_PI);Serial.print("\t");Serial.print(ypr[1]*180/M_PI);Serial.print("\t");Serial.println(ypr[2]*180/M_PI);if((ypr[1]*180/M_PI){BTSerial.write('F');}elseif((ypr[1]*180/M_PI)>=25){BTSerial.write('B');}elseif((ypr[2]*180/M_PI){BTSerial.write('L');}elseif((ypr[2]*180/M_PI)>=20){BTSerial.write('R');}else{BTSerial.write('S');}#endif}}else{BTSerial.write('S');}}在车载单元上插入从属蓝牙模块,在远程单元上主控蓝牙模块。一切就完成了。直接打开它,就可以开始播放了。如果您对此项目有任何想法、意见或问题,请在下方留言。原文链接丨以上内容来源网络,如涉及侵权可联系删除。