学业辅导精编版2科创_第1页
学业辅导精编版2科创_第2页
学业辅导精编版2科创_第3页
学业辅导精编版2科创_第4页
学业辅导精编版2科创_第5页
已阅读5页,还剩47页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

曾FFF安F摘要:本项目设计实现基于安卓平台蓝牙通信的小车控制。控制系统由车载设备和设备组成,小车以STC系列的STC11L02系列51单片机与蓝牙模块为,使用以Eclipse搭建的安卓应用程序,: 开发背 项目任 2.1控制 2.2小车改 小车的选择与改 小车本身的缺 检测与改 的改 安卓平台部 编译与调试环 安卓平台搭 单片机平台部 编译环境与功能实 程序烧 基本功能说 附加功能的实 多方位控 自由路 数次性的改变,从有线到无线,从一对一到一对多,从简单繁多。而到如今,平台也已成为一项十分热门的开发目标,在众多的平台中,安卓凭借其不断成系统构架和方便强大多元编译环境功能独树一帜。安卓是以Linux为的手再是以前只能使用Java开发安卓应用程序的单一局面,因而受到众多开发者的欢迎,成为真正意义上的开放式操作系统。在安,开发者可以使用Java

1.1.1以通过NDK使用C/C++作为编程语言来开发应用程序,也可使用SL4A来使用其他语言行编程(如:python、lua、tcl、php等等),还有其他诸如:Qt、Mono等一些著名编程框架也开始支持安卓编程,甚至通过MonoDroidC#作为编程语言来开发应用程序。另外,2009年特别发布了针对初学者的安卓SimpleBasic语言。而在网页编程语言方面,JavaScript、ajax、HTML5、jquery、sencha、dojo、mobl、PhoneGap等等都已经支持安卓开发。因此,以安卓平台作为控制媒介的开发具有相当大的潜在价值与意义。本次项目的任务为,造(或改造)一辆小车,用户能够在远处对它进行控制,可通过、嵌入式开发板(9B96)、开发板、开发软件的模拟器等作为控制媒介,优先考虑无线模式。为了达到控制小车的目的,需要对所用小车的工作方式进行确定,可以考虑在简单小车上加装控制系统,以确定控制小车的具体方式(如通过端口电平的高低进行控制,通过改变电机两端电压极性进行控制等),然后再通过平台将上述的指令方式通过合适的传输方法使小车正确接收,即可达到控制效果。此方案缺点在于控制设备的难度过大,且不能够很好地保证自行在市面上可以很轻松地买到简单的无线玩具小车,其小车结构较为简单,控制方法也相当简易,属于现有的比较好的可用资源,如果考虑使用控制,再通过控制小车,则图2.2.1小车与小车的控制一般是通过一定频率的无线发射控制信号到车体上,并对相应的控制对象的电信号进行控制,比如置高电平或置低电,使得相应的接口形成通路,电机达到规定的动作标准,如前进、后退、左转或右转等,下图为控制端的举例示意。根据以上的已有信息,我们可以确定,只要能够做到用控制相应控制端口的信号输出,即可实现对小车的控制。一般情况下,对于利用高低电平控制端口信号的控制器来说,可集成属性,因此在与控制器之间可通过单片机作为媒介,发送控制信号到单片机,单片机做出相应的响应,即对对应端口置高或低电平,完成对小车的控制,于是我们选定此思路为2.2.3无线,充电器与充电电池3.1.1玩具小车与我们对小车本身的性能进行了检测,发现了一系列的问题,可能会影响到后续的控制小车问题一:小车电池使用短,单次充电时间超过8h,单次充电有效使用时间约5min。解决方法:将直接与小车固定。451.2所用单片机是ATMEL公司的STC系列的STC11L02系列51单片机,主要性能参数如下8CPU8KFlashROM;1000次可擦写周期;3级加密程序器3.2.2RS232PCB版上,使得单片机能够通过蓝牙获取外界指令(来自手3.2.3软件部分主要为两部分程序的编写,其一为安卓平台上用户操作界面和指令发送系统的搭安卓平台部由于安卓平台软件开发多以JAVA语言编程为主,因此我们采用Eclipse软件进行用户界面的编OracleVMVirtualBoxX86Android2.2IP地址EclipseEclipse1控制小车动作一共需要五个按钮,即:前进、后退、左转、右转和停止。于是我们在2安卓平台的信息传输依靠的是蓝牙通信方式,因此,我们调用了蓝牙配置器类,并且设定将用于通信的变量存放于缓存区message中。五个按钮按下后发送到缓存区的字符分别为:‘1’-与此同时要注意对控制目标蓝牙MAC地址的录入,保证控制对象唯一且有效,需要事先通过MAC地址,然后直接输入到编写的程序中去。对于按钮动作,简单的按动动作分为两个部分,其一为按下,其二为松开,具体配置为:当按下某一特定按钮时,向缓存区写入字符‘X’(X为上文事先规定的字符),当松开时,自动mButtonF.setOnTouchListener(newButton.OnTouchListener(){publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubStringmessage;byte[]intaction={casetryoutStream=//Toast.makeText(SchoolFlower2Activity.this,String.format("%d",a),}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{Toast.makeText(SchoolFlower2Activity.this,message,}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}casetryoutStream=}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{Toast.makeText(SchoolFlower2Activity.this,message,}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}}return}限于计算机本身兼容性原因,我们选择的单片机编译环境为KeiluVision2,通过单片机对引脚P3^2、P3^3、P3^4、P3^5,分别对应前进、后退、左转、右转(由焊接时的实际连接决定),由于在安卓平台部分已对指令信号作出了规定,因此我们运用了多重语句,对不同的获得信息作出不同的反应,即对要求的宽口赋“0”(等效为置低电平),对不需”()单片机的程序烧录是基于RS232通信线以及烧录进行的,USB端接电脑,TxD接无线发送模块的数据端VccVcc相连GNDGND连接。电脑端需使用USBRS232驱动。烧录软件我们选用了宏晶科技的STC-ISP进行单片机握手与程序烧录。STC-ISP电脑型号:联想lenovo昭阳E-43操作系统为WIN7旗舰版 单片机程序编写环境:KeilμVision2i.MCU3点击”USBSTCEclipseAPK,在安卓上安装成功,然后开启蓝牙,与小车蓝牙模块成功配对。这样,即可通过按下界面5.1.15个控制按钮的基础上,考虑到小车实际操作时用户对灵活性的要求,我们按照原按JAVA对图形化界面的强大编辑能力,可以通过窗体架构下已有的坐标体系编程实现用户基础部分能够完整地实现控制小车动作的任务,但由于受到小车本身质量的影响,在运行1、自由路径控制通过控制转弯作用时长对小车动作进行调整,但时长的确定不仅受到小车本身转弯能力的限制,还受到小车行驶场地和车轮材料的限制,不适宜的场合会造成打滑,直接影响2、小车方向会随路径点选取而发生改变,并未将方向随时还原为正北,操控时需要另外注意。对于像本文中的玩具小车那样的类型,在日后更加充沛的时间与经费支持下,可以做出的3c,一个新的科创,没有学长的报告,没有固定的思维模式,没有具体的方法步骤,对我们来说是一个,但的是对我们对于只是理解和融会贯通的要求。助下,我们用万用表对板子进量检查,最终找出了问题的根源将其解决。虽然在发现我们的硬——通过无线通信控制小车,更重要的是,拓展功能设定一条多边形封闭路径,让其自动循首先,因为有了科创2B的基础,开始的时候觉得这些任务不是很难,后来慢慢到利用安卓系统进行无线是一项新的技术,需要完成基于Eclipse的Android开发环境搭建、在VirtualBox虚拟环境下的Android调试与开发等诸多的软件任务。此次科创中,我主要负责了硬件我们要更加注重对基础知识的了解,很多问题并不是很大,都是出自于我们的粗心以及对基起初被科创3的题目吸引,最后决定选修此门课程。那时我已经做好了迎接的心理准备。第三周听完老师的讲座我对科创3有了基本的了解。随后我们小组便投入到“蓝牙控制小车行333结束了,但是它带给我们的意义远不止一次实践活——是真正想要或多或少地学到一些东西。作为新开设的科创项目,在没有任何前届学长经验支持了许许多多的:编写好的用户界面无法在上正常运行,连打开都无法打开;小车本身的遥控性能不如人意,更不用说再用控制;单片机的程序烧录与握手连接一直不成功;小车的蓝牙模块和无法正确配对传输指令……虽然有过一定的思想准备,可遇到这么多让我们措手不及的问题还是不免让人绝望,好在我们并没有放弃,在助教、老师的指导和身边的鼓励下,一次6debug下,4个小时几十次地调整尝试后,恐中,只要我们迈出脚步,一路走过,回首的时候,我们会收获意想不到的美丽。——曾谢开设这门课程并负责最终检验的的老师,是他给我们宽松的氛围、齐备的材料,才使我们有信心和实力去做这个科创实验。其次要感谢的是轮流在给予我们悉心指导的各位助教,是受益匪浅,也是我们能顺利做完课程实验不可缺少的因一。还要感谢教授在小车固件改造在和我们一起做实验,给我们帮助指导的其他组同学,与他们在一起做科创实验我们互相学III-C《JAVABruce程序: <?xmlversion="1.0"encoding="utf- >>>>>>>>>SchoolFlower2Activity.java//主程序1packagecom.Android.SchoolFlower2;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importimportjava.io.OutputStream;importjava.util.UUID;importjava.util.concurrent.Semaphore;importandroid.app.Activity;importimportimportandroid.bluetooth.BluetoothSocket;importandroid.content.DialogInterface;importandroid.content.DialogInterface.OnClickListener;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importimportimport importimportandroid.view.MotionEvent;importandroid.view.SurfaceHolder;importandroid.view.SurfaceView;importandroid.view.View;importandroid.widget.Button;importandroid.widget.Toast;publicclassSchoolFlower2ActivityextendsActivity{privateinta=0;privatestaticfinalStringTAG="THINBT privatestaticfinalbooleanD=true;privateBluetoothAdaptermBluetoothAdapter=null;privateBluetoothSocketbtSocket=null;privateOutputStreamoutStream=null;ButtonmButtonF;ButtonButtonButtonButtonButtonButtonSurfaceViewSurfaceHolderSemaphoresurface_ready=newSemaphore(0);PathManagerpm=newPathManager();privatestaticfinalUUIDMY_UUID=UUID.fromString(" privatestaticStringaddress"00:10:04:08:00:30"要连接的蓝牙/**Calledwhentheactivityisfirstcreated.*/publicvoidonCreate(BundlesavedInstanceState)publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubStringmessage;byte[]intaction=event.getAction();{casetryoutStream=//Toast.makeText(SchoolFlower2Activity.this,String.format("%d",a),}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{Toast.makeText(SchoolFlower2Activity.this,message,}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}casetryoutStream=}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{Toast.makeText(SchoolFlower2Activity.this,message,}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}}return}publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubStringmessage;byte[]intaction=event.getAction();{casetryoutStream=//Toast.makeText(SchoolFlower2Activity.this,String.format("%d",a),}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{Toast.makeText(SchoolFlower2Activity.this,message,}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}casetryoutStream=}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{Toast.makeText(SchoolFlower2Activity.this,message,}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}}return}publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubStringmessage;byte[]intaction=event.getAction();{casetryoutStream=//Toast.makeText(SchoolFlower2Activity.this,String.format("%d",a),}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{Toast.makeText(SchoolFlower2Activity.this,message,}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}casetryoutStream=}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{Toast.makeText(SchoolFlower2Activity.this,message,}catch(IOExceptione){Toast.makeText(SchoolFlower2Activity.this,e.getMessage(),Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}}return}publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubStringmessage;byte[]intaction=event.getAction();{casetryoutStream=}catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=message.getBytes();try{}catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}casetryoutStream=}catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{}catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}}return}publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubStringmessage;byte[]intaction=event.getAction();{casetryoutStream=}catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{}catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}casetryoutStream=}catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{}catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}}return}publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubStringmessage;byte[]intaction=event.getAction();{casetryoutStream=}catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{}catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}casetryoutStream=}catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}message=msgBuffer=try{}catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}}return}publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubtry{outStream=}catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}Stringmessage=byte[]msgBuffer=message.getBytes();try{}catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}return}mSV=(SurfaceView)findViewById(R.id.viewDraw);mSH=(SurfaceHolder)mSV.getHolder();mSH.addCallback(newSurfaceHolder.Callback(){publicvoidsurfaceDestroyed(SurfaceHolderholder)//TODOAuto-generatedmethod}publicvoidsurfaceCreated(SurfaceHolderholder)//TODOAuto-generatedmethodint

publicvoidsurfaceChanged(SurfaceHolderholder,intintheight)//TODOAuto-generatedmethodstub}mSV.setOnTouchListener(newView.OnTouchListener()publicbooleanonTouch(Viewv,MotionEventevent)//TODOAuto-generatedmethodstubpm.addPoint(newPoint((int)event.getX(),(int)}

return//RedrawingfinalHandlerhandler=newHandler();newThread(newRunnable(){publicvoidrun()//TODOAuto-generatedmethodstubsynchronized(mSH){while(true)try}catch(InterruptedExceptione1)//TODOAuto-generatedcatchblock}handler.post(newRunnable()publicvoidrun()

//TODOAuto-generatedmethodtry{}catch(Exceptione)Toast.makeText(SchoolFlower2Activity.this,e.toString()+e.getMessage(),Toast.LENGTH_SHORT).show();}try

}catch(InterruptedExceptione)//TODOAuto-generatedcatchblock}}}}ifLog.e(TAG,"+++ONCREATEmBluetoothAdapter=if(mBluetoothAdapter==null){Toast.makeText(this,"Bluetoothisnotavailable.",}if(!mBluetoothAdapter.isEnabled())Toast.makeText(this,"PleaseenableyourBluetoothandre-runthisprogram.",Toast.LENGTH_LONG).show();}ifLog.e(TAG,"+++DONEINONCREATE,GOTLOCALBTADAPTER}publicvoidonStart(){if(D)Log.e(TAG,"++ONSTART}publicvoidonResume(){if(D)Log.e(TAG,"+ONRESUMELog.e(TAG,"+ABOUTTO CONNECT}Toast.makeText(SchoolFlower2Activity.this,"Resume:Connecting",BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);try{btSocket= pm.btSocket=this.btSocket;}catch(IOExceptione)Log.e(TAG,"ONRESUME:Socketcreationfailed.",}try{Log.e(TAG,"ONRESUME:BTconnectionestablished,datatransferlink}catch(IOExceptione){try{}catch(IOExceptione2)Log.e(TAG,"ONRESUME:Unabletoclosesocketduringconnectionfailure",e2);}}//Createadatastreamsowecantalktoserver.if(D)Log.e(TAG,"+ABOUTTOSAYSOMETHINGTOSERVERtryoutStream=}catch(IOExceptione)Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",}Stringmessage=byte[]msgBuffer=message.getBytes();try{}catch(IOExceptione)Log.e(TAG,"ONRESUME:Exceptionduringwrite.",}}publicvoidonPause(){ifLog.e(TAG,"-ONPAUSE-if(outStream!=null){try{}catch(IOExceptione)Log.e(TAG,"ONPAUSE:Couldn'tflushoutputstream.",}}try{}catch(IOExceptione2)Log.e(TAG,"ONPAUSE:Unabletoclosesocket.",}}publicvoidonStop(){if(D)Log.e(TAG,"--ONSTOP--}publicvoidonDestroy(){if(D)Log.e(TAG,"---ONDESTROY---}publicvoidonDraw()throwsException{Canvascanvas=if(canvas==null)thrownewException("Canvasisnull");Paintpaint=newPaint();paint.setARGB(255,230,230,Point[]path=newPoint[0];synchronized(pm.path){path=}PathManager.unit_vectordir=pm.curDir;for(inti=1;i<path.length;i++){canvas.drawLine(path[i-1].x,path[i-1].y,path[i].y,}canvas.drawCircle(path[0].x,path[0].y,10,canvas.drawLine(path[0].x,path[0].y,path[0].x+dir.x*15.0f,path[0].y+dir.y*15.0f,paint);}}PathManager.java//主程序packagecom.Android.SchoolFlower2;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importimportandroid.bluetooth.BluetoothSocket;importandroid.graphics.Point;publicclassPathManager{publicclassunit_vector{floatpublicunit_vector(Pointst,Pointed){x=ed.x-st.x;y=ed.y-floatmod=(float)Math.sqrt(x*x+y*y);x/=mod;y/=}publicfloatangleTo(unit_vectorvec) inradian,positivevaluemeansclockwisefloatangle=(float)Math.acos(x*vec.x+y*vec.y);if(x*vec.y>y*vec.x){return}elsereturn-}}}floatdistance(Pointa,Pointb){floatx=a.x-b.x;floaty=a.y-return(float)}publicList<Point>path=newpublicunit_vectorcurDir=newunit_vector(newPoint(0,0),newpublicBluetoothSocketbtSocket=privateSemaphorepoints=newSemaphore(0);publicPathManager(){path.add(newnewThread(newRunnable()publicfinalfloatrot_speed=45.54f;publicfinalfloatmov_speed=10.0f;publicvoidrun()//TODOAuto-generatedmethodstubtry{while(true)Pointst,ed;synchronized(path){st=path.get(0);ed=

unit_vectordir=floatrot=curDir.angleTo(dir);if(rot>0.1f||rot<-0.1f){try

if(rot>0.1f Turn Turn}catch(Exceptione)}Thread.sleep(Math.round(Math.abs(rot)*rot_speed)}curDir=floatdist=distance(st,ed);try{}catch(IOExceptione)//TODOAuto-generatedmov_speed)

Thread.sleep(Math.round(dist

try}catch(IOExceptione)//TODOAuto-generated}synchronized(path)}}}catch(InterruptedExceptione)//TODOAuto-generatedcatch}}}publicvoidaddPoint(Pointp){synchronized(path){}}}#include<reg51.h>#include<absacc.h>unsignedchardatacmd=0x00;unsignedcharcnt=0;//Enablebit,setto0torunthemotorsbitup=P3^2;sbitdown=P3^3;sbitright=P3^4;sbitleft=P3^5;sfrA

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论