找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7093|回复: 6
打印 上一主题 下一主题
收起左侧

安卓APP+STM32蓝牙控制小车系统设计源码与资料下载

[复制链接]
跳转到指定楼层
楼主
ID:347672 发表于 2018-6-8 17:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
电机驱动模块、L298N
主控板、
蓝牙通信模块、FBT06_LPDB
android控制端等

4.1.1 main程序设计
主函数主要分为延迟时间初始化、串口接收模块程序、电机初始化三部分。主函数的流程图如图4.1所示:
图4.1 主函数流程图
主函数程序代码如下:
int main(void)
{  
delay_init(168);//延迟时间初始化
uart_init(9600);//串口初始化
Direction(1);//电机初始化
delay_ms(10);//延迟10ms
printf(" welcome to control the smart car!:\n\r");//输入语句
while(1);
}
4.1.2 串口接收模块程序
主控板接收到蓝牙从串口传来的数据后存入Res变量,然后通过分支程序来选择执行前进、后退、左转、右转和停止等功能。该模块的程序流程图如图2所示:
图2 串口接收程序流程图
4.1.3 修改PWM输出值程序
该程序是基于“4_PWM的实现”中的程序改编的。代码如下:
  1. void Change_PWM(int duty1,int duty2,int duty3,int duty4)
  2. {
  3.               SCB->AIRCR=0x05AF00;// 中断优先级分组 抢占:响应=3:1
  4.   RCC->AHB1ENR|=(1<<2);// 打开GPIOC时钟
  5.   GPIOC->MODER|=0x000AA000;// pc6789第二功能,推挽输出
  6.   GPIOC->OSPEEDR|=0x000FF000;//输出速度为100m
  7.   GPIOC->PUPDR|=0x00055000;//上拉

  8.   GPIOC->AFR[0]|=0x22000000;//pc6789的第二功能为AF2
  9.   GPIOC->AFR[1]|=0x00000022;

  10.   RCC->APB1ENR|=(1<<1);//打开TIM3时钟
  11.   TIM3->PSC=83;//对84M时钟进行84分频,使得计数频率为1M
  12.   TIM3->ARR=10000;//周期为10ms
  13.   TIM3->EGR|=1;//产生一次更新时间

  14.   TIM3->CCMR1|=0x6060;//PWM模式1
  15.   TIM3->CCMR2|=0x6060;//PWM模式2

  16.   TIM3->CCR1=duty1;//1路PWM
  17.   TIM3->CCR2=duty2;//2路PWM
  18.   TIM3->CCR3=duty3;//3路PWM
  19.   TIM3->CCR4=duty4;//4路PWM

  20.   TIM3->CCER|=0x1111;//使能比较输出

  21.   TIM3->CCMR1|=0x0808;//启动预装载
  22.   TIM3->CCMR2|=0x8080;

  23.   TIM3->CR1|=1;//开始计时
  24. }
复制代码

4.1.4 设置电机转向程序
改程序将电机驱动模块的8个输入端口接到了主控板的8个GPIO口,通过推挽输出,从而控制电机的转向,代码如下:
  1. void Direction(int direction)
  2. {
  3. SysTick_Config(SystemCoreClock / 1000); //时钟中断设为1ms
  4.               RCC->AHB1ENR |= 0x00000005; //使能GPIOA和GPIOD时钟
  5.    RCC->APB2ENR |= (1<<14);  //使能syscfg时钟
  6. if(direction==0)
  7. {
  8.    GPIOA->MODER &= 0xffff0000; //设置PA0,1,2,3为输出
  9.    GPIOA->MODER |= 0x00005555;
  10.    GPIOA->OTYPER &= 0xFFFFff00; //设置PA0,1,2,3为推挽输出
  11.    GPIOA->OSPEEDR &= 0xffff0000; //设置PA0,1,2,3的输出速度为100M
  12.    GPIOA->OSPEEDR |= 0x0000ffff;
  13.    SYSCFG->CMPCR = 0x00000001; //使用IO补偿单元
  14.    GPIOA->PUPDR &= 0xffffff00;  //设置PA0,1,2,3无上拉,无下拉
  15.    GPIOA->BSRRH = 0x00ff;  //复位GPIOA_BSRRH寄存器
  16.    GPIOA->BSRRL = 0x0055;                           
  17.               }
  18.               else
  19.               {
  20. GPIOA->MODER &= 0xffff0000; //设置PA0,1,2,3为输出
  21.    GPIOA->MODER |= 0x0000005555;
  22.    GPIOA->OTYPER &= 0xFFFFff00; //设置PA0,1,2,3为推挽输出
  23.    GPIOA->OSPEEDR &= 0xffff0000; //设置PA0,1,2,3的输出速度为100M
  24.    GPIOA->OSPEEDR |= 0x0000ffff;
  25.    SYSCFG->CMPCR = 0x00000001; //使用IO补偿单元
  26.    GPIOA->PUPDR &= 0xffffff00;  //设置PA0,1,2,3无上拉,无下拉
  27.    GPIOA->BSRRH = 0x00ff;  //复位GPIOA_BSRRH寄存器
  28.    GPIOA->BSRRL = 0x00AA;
  29. }
  30. }
复制代码

4.2 android客户端程序设计4.2.1 控制界面的布局
控制界面主要运用了线性布局、相对布局和表格布局。整体采用线性布局,局部采用相对布局,而控制按钮采用了表格布局。控制界面的布局如图4.1所示:
图4.1 控制界面的布局
4.2.2 布局的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<RelativeLayout
   android:id = "@+id/container"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id= "@+id/edit_bottombar"
                android:layout_alignParentBottom = "true">
     <Button android:id="@+id/btn_disconnect"
                                          android:layout_width="65dp"
                                          android:layout_height="wrap_content"
                                          android:layout_alignParentLeft ="true"
                                          android:text="断开"/>
                            <Button android:id="@+id/btn_msg_send"
                                          android:layout_width="65dp"
                                          android:layout_height="wrap_content"
                                          android:layout_alignParentRight ="true"
                                          android:text="发送"/>
                           
                            <EditText
                                android:id="@+id/MessageText"
                                android:layout_width="98dp"
                                android:layout_height="wrap_content"
                                android:layout_toRightOf="@+id/btn_disconnect"
                                android:hint="说点什么呢?"
                                android:textSize="15dip"
                                  />
              </RelativeLayout>
              <ListView
                   android:id="@+id/list"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_above="@id/edit_bottombar"
                   android:layout_below="@id/container"
                   android:layout_weight="1.0"
                   android:divider="#ffc6c6c6"
                   android:scrollingCache="false"
                   android:visibility="visible" />
              <TableLayout
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content" >
                   <TableRow
                       android:id="@+id/tableRow1"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content" >
                       <Button
                           android:id="@+id/button1"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:text="Button"
                           android:visibility="invisible" />
                       <Button
                           android:id="@+id/start"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="0dp"
                           android:text="start"
                           android:width="120px" />
                   </TableRow>
                   <TableRow
                       android:id="@+id/tableRow2"
                       android:layout_width="wrap_content"
                       android:layout_height="0dp"
                       android:layout_weight="1" >
                       <Button
                           android:id="@+id/left"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="0dp"
                           android:text="left"
                           android:width="120px" />
                       <Button
                           android:id="@+id/stop"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:text="stop"
                           android:width="120px" />
                       <Button
                           android:id="@+id/right"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:text="right"
                           android:width="120px" />
                   </TableRow>
                   <TableRow
                       android:id="@+id/tableRow3"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content" >
                       <Button
                           android:id="@+id/button2"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:text="Button"
                           android:visibility="invisible" />
                       <Button
                           android:id="@+id/back"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:text="Back" />
                   </TableRow>
              </TableLayout>
</RelativeLayout>
</LinearLayout>
4.2.3 android客户端的界面如图4.2所示:
图4.2 android控制界面
4.2.4 发送按钮的代码
sendButton= (Button)findViewById(R.id.btn_msg_send);
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String msgText =editMsgView.getText().toString();//获取编辑框内的内容
if (msgText.length()>0) {
              sendMessageHandle(msgText);//发送编辑框的内容给串口            
              editMsgView.setText("");//清空编辑框
              editMsgView.clearFocus();
//close InputMethodManager
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                                          imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);
}
else
Toast.makeText(mContext, "发送内容不能为空!", Toast.LENGTH_SHORT).show();
}
});
4.2.5 控制按钮的代码
以左转按钮为例:
sendButton= (Button)findViewById(R.id.left);
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String msgText ="1"; // 发送左转命令“l”
if (msgText.length()>0) {
sendMessageHandle(msgText);//发送“l”给串口            
editMsgView.setText("");//清空编辑框
editMsgView.clearFocus();
//close InputMethodManager
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);
}
else
Toast.makeText(mContext, "发送内容不能为空!", Toast.LENGTH_SHORT).show();
}
});
1.        系统创新

自从有了智能手机,机器人的应用也就多了一个新的方向:手机控制机器人。2005年日本第二大电信运营商KDDI和机械制造商IBee KK联合推出了第一款手机控制机器人。当然了,想要操纵这种机器人,你首先需要使用KDDI网络,并且用户的手机上,本身还需要带有蓝牙功能,然后再通过KDDI提供BREW方式下载机器人的驱动程式和控制系统。不过这种机器人的价格却相对高昂,预售市价约合人民币15000元。随着Android系统技术的普及,可以做个基于Android的客户端,在小车上装个接收蓝牙信号的FBT蓝牙接收模块,然后就可以通过客户端发送蓝牙信号,来对蓝牙小车进行控制控制,其接收可达15米,完全能适应对小车的要求。其中这个FBT蓝牙接收模块是低耗能,这样就把更多的能量用在小车的驱动上。

小车需要很大的马力和很好的灵活性以应对不同的地形。这辆车的车轮使用四驱的直流电机来驱动的,用PWM波来控制小车的速度,可以很方便的更改其速度,有主控板通过推挽输出来控制电机的翻转以让车子进行后退的速度。这样就可以胜任对小车的要求。


2.        评测与结论
首先,给电源模块上12v的电源,然后打开电机驱动模块开关,同时将主控板的供电端连接到电源管理模块。然后,在android手机上安装“蓝牙通信”应用程序后,打开该APP,然后选择“允许打开蓝牙”。点击设备列表中的“开始搜索按钮”,在设备列表中选择蓝牙模块的名字进行连接。
              完成上述工作以后,就可以在手机上通过按下“start”、“left”、“stop”、“right”、“back”通过蓝牙给小车发送“前进”、“左转”、“停止”、“右转”、“后退”5个命令。小车可以解析命令轻松进行前进、后退、左转、右转和停止。



附录
图6-1 作品成果1
图6-2 作品成果2


全部资料51hei下载地址:
硬件设计及文档.zip (1.87 MB, 下载次数: 119)
软件源代码及文档.rar (639.46 KB, 下载次数: 126)
基于stm32f4的蓝牙控制小车_论文.doc (399 KB, 下载次数: 83)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:243748 发表于 2018-6-25 06:56 来自手机 | 只看该作者
谢谢分享
回复

使用道具 举报

板凳
ID:428445 发表于 2018-11-17 21:45 | 只看该作者
感谢分享
回复

使用道具 举报

地板
ID:366466 发表于 2019-1-10 09:50 | 只看该作者
很好的东西很有用
回复

使用道具 举报

5#
ID:673954 发表于 2019-12-25 20:47 | 只看该作者
刚需,但是黑币不够 太痛苦了
回复

使用道具 举报

6#
ID:673954 发表于 2019-12-25 20:50 | 只看该作者
牛逼!
回复

使用道具 举报

7#
ID:549580 发表于 2020-5-22 22:52 | 只看该作者
这个APP怎么安装啊

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表