毕业设计 STM32智能家政机器人

文章目录

  • 0 前言
  • 1 主要功能
  • 2 硬件设计(原理图)
  • 3 核心软件设计
  • 4 实现效果
  • 5 最后


0 前言


这两年开始毕业设计和毕业答辩的要求和难度不断提升,传统的毕设题目缺少创新和亮点,往往达不到毕业答辩的要求,这两年不断有学弟学妹告诉学长自己做的项目系统达不到老师的要求。

为了大家能够顺利以及最少的精力通过毕设,学长分享优质毕业设计项目,今天要分享的是

毕业设计 STM32智能家政机器人(源码+硬件+论文)

学长这里给一个题目综合评分(每项满分5分)

  • 难度系数:3分
  • 工作量:3分
  • 创新点:4分

项目分享:

https://gitee.com/sinonfin/sharing

毕业设计 STM32智能家政机器人_第1张图片

1 主要功能

本设计一种基于Android平台实现语音识别和命令生成,通过蓝牙无线传输至下位机,由下位机中的STM32F103控制器控制机器人完成用户指令。

研究内容:

随着微处理器技术的发展与成熟,嵌入式语音识别系统已逐渐成为各领域研究的热点方向,各种迎合人们需求的语音识别设备出现在我们的现实生活中。本课题重要研究的是通过语音识别技术控制机器人,该系统通过设备命令者的语言对机器人发出控制指令,让机器人为我们服务。考虑的语音识别需要很大的运算量,这对于微处理器的速度就有了一定的要求,所以在本系统中,我们采用了谷歌公司的Google Voice Search语音处理引擎,通过使用该引擎完成语音识别。同时,我们采用上位机识别并发送控制指令,下位机控制机器人的方式,中间传输环节通过HC-05蓝牙模块实现。在下位机中,我们采用STM32RBT6控制器通过串行方式控制机器人上的舵机,从而实现对机器人整体的控制。

实现功能:

  • (1)家政服务机器人要求为移动车体,具有机械手;
  • (2)基于“安卓”实现简单语音识别;
  • (3)实现机械手臂简单操作;
  • (4)通过蓝牙实现“安卓”对机器人的无线控制;
  • (5)实现自动定位。

2 硬件设计(原理图)

毕业设计 STM32智能家政机器人_第2张图片

3 核心软件设计

安卓端软件的工作流程

做为自动化专业的学生我在学习如何使用安卓方面遇到了很多原理上的困惑。因为自动化平时遇到的代码主要是运行在MCU、DSP和FPGA等处理器上,而本系统中安卓端软件为运行在操作系统中的软件,这就需要我从操作系统的层面来考虑软件的内部架构,以使得软件能够在启动操作系统后所剩无多的资源下高效的运行。

毕业设计 STM32智能家政机器人_第3张图片

机器人端程序

机器人端程序主要实现通过HC-05蓝牙模块接收用户的指令,然后控制机器人完成相应的动作。在程序运行过程中还要涉及到各个模块和传感器的初始化,以及各个模块与控制器的实时通信。

毕业设计 STM32智能家政机器人_第4张图片

篇幅有限,不过多复述详细设计细节,详细的设计分享在论文中。。。

关键代码

机器人端代码:

#include "sys.h"
#include "usart.h"		
#include "usart2.h"		
#include "delay.h"	
#include "led.h"    	 	 
#include "key.h"	 	 
#include "exti.h"	 	 
#include "wdg.h" 	 
#include "timer.h"	 	 	 
#include "lcd.h"
#include "hc05.h"		 	 
#include "string.h"
#include "cds5500.h"
char tt;	    
void delay_s(u8 t)
{
 u8 tt;
 for(tt=0;tt<t;tt++)
 {
  delay_ms(1000);
 }
}
int main(void)
{	 
	u8 reclen=0;
	unsigned short int distance=520; 
	unsigned short int height=750; 
   Stm32_Clock_Init(9);	
	delay_init(72);			
	uart_init(72,1000000); 	
	LED_Init();				
	SetMode(1,0,0);delay_ms(30);
	SetMode(2,0,0);delay_ms(30);
	SetMode(3,0,0);delay_ms(30);
	SetMode(4,0,0);delay_ms(30);
	SetMode(5,0,1023);delay_ms(30);
	SetMode(6,0,1023);delay_ms(30);
	SetMode(7,0,1023);delay_ms(30);
	SetMode(8,0,1023);delay_ms(30);
	SetMode(9,0,1023);delay_ms(30);  
	HC05_Init();	 
	delay_ms(1000);				   	   
	PostureInit();	  
 	while(1) 
	{			  
		if(USART2_RX_STA&0X8000)	
		{		reclen=USART2_RX_STA&0X7FFF;	
		  	USART2_RX_BUF[reclen]=0;
			USART2_RX_STA=0;
			if(tt=='1')
			{
			 Forward();
			}
			if(tt=='3')
			{
			 Back();
			}
			if(tt=='0')
			{
			 LED0=1;LED1=1;
			 Stop();
			}
			if(tt=='4')
			{
			 TurnRight();
			}
			if(tt=='2')
			{
			 TurnLeft();
			}
			if(tt=='5')
			{
			 SetMode(9,0,1023);
			 delay_ms(30);
			 OpenPaw();
			 LED0=0;
			}
			if(tt=='6')
			{
			 SetMode(9,0,1023);
			 delay_ms(30);
			 ClosePaw();
			}						
			while(tt=='8')
			{
			 USART2_RX_STA=0;
			 distance-=10;			 if(distance<=200)distance=200;
			 SetMode(7,0,1023);
			 delay_ms(30);
			 PawGoFar(distance);
			 delay_ms(75);
			 LED1=0;
			} 
			while(tt=='7')
			{
			 USART2_RX_STA=0;
			 distance+=10;
	if(distance>=520)distance=520;
			 SetMode(7,0,1023);
			 delay_ms(30);
			 PawGoNear(distance);
			 delay_ms(75);
			 LED0=0;
			}			  
			while(tt=='9')
			{
			 USART2_RX_STA=0;
			 height+=10;
			 if(height>=750)height=750;
			 SetMode(6,0,1023);
			 delay_ms(30);
			 ArmRaiseUp(height);
			 delay_ms(75);
			 LED0=0;
			}
			while(tt=='a')
			{
			 USART2_RX_STA=0;
			 height-=10;
			 if(height<=500)height=500;
			 SetMode(6,0,1023);
			 delay_ms(30);
			 ArmSetDown(height);
			 delay_ms(75);
			 LED1=0;
			}					 
		}	 
	}								    
}
#include 
#include "delay.h"
#include "cds5500.h"
void ControlBit_Init(void)
{
 RCC->APB2ENR|=1<<2; 
 GPIOA->CRL&=0XF0FFFFFF; 
 GPIOA->CRL|=0X03000000; 	 
 GPIOA->ODR|=1<<6; 

 GPIOA->CRL&=0X0FFFFFFF; 
 GPIOA->CRL|=0X30000000; 
 GPIOA->ODR|=1<<7;
}

void SetMode(unsigned char id, unsigned short int s_limit, unsigned short int n_limit)
{
 unsigned short int temp_n = 0;
 unsigned short int temp_s = 0;
 unsigned char temp_n_h = 0;	
 unsigned char temp_n_l = 0;	
 unsigned char temp_s_h = 0;
 unsigned char temp_s_l = 0;
 unsigned char temp_sum = 0;
 char len,t;
 char MODE[11];
 if (n_limit > 1023)
 	temp_n = 1023;	
 else 
 	temp_n = n_limit;
 if (s_limit > 1023)
	temp_s = 1023;
 else 
 	temp_s = s_limit;
 temp_n_h = (unsigned char)(temp_n >> 8);
 temp_n_l = (unsigned char)temp_n;	
 temp_s_h = (unsigned char)(temp_s >> 8);
 temp_s_l = (unsigned char)temp_s;	
 TXEN=1;
 RXEN=0;			
 MODE[0]=0XFF;
 MODE[1]=0XFF;		
 MODE[2]=id;		 //ID
 MODE[3]=0X07;	
 MODE[4]=0x03;	
 MODE[5]=0x06;	
 MODE[6]=temp_s_l;	
 MODE[7]=temp_s_h;	
 MODE[8]=temp_n_l; 
 MODE[9]=temp_n_h;
 temp_sum = id + 7 + 0x03 + 0x06 + temp_s_l + temp_s_h + temp_n_l + temp_n_h;
 temp_sum = ~temp_sum;
 MODE[10]=temp_sum;	 
 len=11;
 for(t=0;t<len;t++)
 {
  USART1->DR=MODE[t];
  while((USART1->SR&0X40)==0);
 }
 TXEN=0;
 RXEN=1;	
}
void SetServoPosition(unsigned char id, unsigned short int position, unsigned short int velocity)
{
 unsigned short int temp_velocity = 0;	
 unsigned short int temp_position = 0;
 unsigned char temp_velocity_h = 0;
 unsigned char temp_velocity_l = 0;	
 unsigned char temp_position_h = 0;
 unsigned char temp_position_l = 0;
 unsigned char temp_sum = 0;	
 char t,len;
 char SERVO[11];
 if (velocity > 1023)
 	temp_velocity = 1023;
 else
 	temp_velocity = velocity;
 if (position > 1023)
 	temp_position = 1023;
 else
 	temp_position = position;
 temp_velocity_h = (unsigned char)(temp_velocity >> 8);	
 temp_velocity_l = (unsigned char)temp_velocity;
 temp_position_h = (unsigned char)(temp_position >> 8); ¸ö 8bit Êý¾Ý
 temp_position_l = (unsigned char)temp_position;
 TXEN=1;
 RXEN=0;			
 SERVO[0]=0XFF;
 SERVO[1]=0XFF; 	 
 SERVO[2]=id;		 //ID
 SERVO[3]=0X07;		
 SERVO[4]=0x03;		
 SERVO[5]=0x1E;		
 SERVO[6]=temp_position_l;	
 SERVO[7]=temp_position_h;	
 SERVO[8]=temp_velocity_l;	 
 SERVO[9]=temp_velocity_h;	 
 temp_sum = id + 7 + 0x03 + 0x1E + temp_position_l + temp_position_h + temp_velocity_l +temp_velocity_h;
 temp_sum = ~temp_sum;
 SERVO[10]=temp_sum;	 
 len=11;
 for(t=0;t<len;t++)
 {
  USART1->DR=SERVO[t];
  while((USART1->SR&0X40)==0);
 }
 TXEN=0;
 RXEN=1;		
}
void SetVelocity(unsigned char id, signed short int velocity)
{
 unsigned char temp_sign = 0;	
 unsigned short int temp_velocity = 0;	
 unsigned char temp_value_h = 0;
 unsigned char temp_value_l = 0;
 unsigned char temp_sum = 0;
 char t,len;
 char VEL[9];
 if (velocity < 0)
 {
  temp_velocity = -velocity;	 
  temp_sign = 1;	}
 else
 {
  temp_velocity = velocity;
  temp_sign = 0;	
 }
 if (temp_velocity > 1023)
   temp_velocity = 1023;	
 temp_velocity |= (temp_sign << 10);
 temp_value_h = (unsigned char)(temp_velocity >> 8); 
 temp_value_l = (unsigned char)temp_velocity;
 TXEN=1;
 RXEN=0;				
 VEL[0]=0XFF;
 VEL[1]=0XFF;		
 VEL[2]=id;
 VEL[3]=5;				
 VEL[4]=0x03;			
 VEL[5]=0x20;		  
 VEL[6]=temp_value_l;   
 VEL[7]=temp_value_h;    
 temp_sum = id + 5 + 0x03 + 0x20 + temp_value_l + temp_value_h;
 temp_sum = ~temp_sum;  
 VEL[8]=temp_sum;
 len=9;					 
 for(t=0;t<len;t++)
 {
  USART1->DR=VEL[t];
  while((USART1->SR&0X40)==0);
 }
 TXEN=0;
 RXEN=1;		 		
}
void Forward(void)
{
 SetVelocity(1,1000);	
 SetVelocity(2,1000);	
 SetVelocity(3,-1000);	
 SetVelocity(4,-1000);	
}

void Back(void)
{
 SetVelocity(1,-1000);	
 SetVelocity(2,-1000);	
 SetVelocity(3,1000);	
 SetVelocity(4,1000);
}
void Stop(void)
{
 SetVelocity(1,0);	
 delay_ms(30);
 SetVelocity(2,0);	
 delay_ms(30);
 SetVelocity(3,0);	
 delay_ms(30);
 SetVelocity(4,0);	
 delay_ms(30);
}
void TurnRight(void)
{
 SetVelocity(1,800);
 SetVelocity(2,800);	
 SetVelocity(3,800);
 SetVelocity(4,800);	
}
void TurnLeft(void)
{
 SetVelocity(1,-800);	
 SetVelocity(2,-800);	
 SetVelocity(3,-800);	
 SetVelocity(4,-800);	
}
void  OpenPaw(void)
{
 SetServoPosition(9,300,700);
}
void  ClosePaw(void)
{
 SetServoPosition(9,600,600);
}
void  PawGoFar(unsigned short int dis)
{
 SetServoPosition(7,dis,500);
}

void  PawGoNear(unsigned short int dis)
{
 SetServoPosition(7,dis,600);
}
void  ArmRaiseUp(unsigned short int hei)
{
 SetServoPosition(6,hei,500);
}
void  ArmSetDown(unsigned short int hei)
{
 SetServoPosition(6,hei,600);
}
void  PostureInit(void)
{
 SetMode(6,0,1023);
 delay_ms(30);
 SetServoPosition(6,750,600);
 delay_ms(200);
 SetMode(6,0,1023);
 delay_ms(30);
 SetServoPosition(6,750,500);
 delay_ms(200);
 SetMode(5,0,1023);
 delay_ms(30);
 SetServoPosition(5,512,500);
 delay_ms(200);
 SetMode(7,0,1023);
 delay_ms(30);
 SetServoPosition(7,480,500);
 delay_ms(200);
 SetMode(5,0,1023);
 delay_ms(30);
 SetServoPosition(8,530,500);
 delay_ms(200);
 SetMode(9,0,1023);
 delay_ms(30);
 SetServoPosition(9,600,600);
}

安卓端代码:

package com.example.voicerecognizer;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.view.View;
importandroid.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
importandroid.content.pm.PackageManage;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import java.io.IOException;
import java.io.OutputStream; 
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
importandroid.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class Main extends Activity {
	private static final int UPTATE_INTERVAL_TIME = 120;
	private static final String TAG = "MyBluetooth"; 
    private static final boolean D = true;
    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;
	private TextView textView1;
	private Button button1;
	private Button button2;
	public static final int VOICE_RECOGNITION_REQUEST_CODE = 0x1008;
	public String strRet;
	public String strRet1;
	private BluetoothData bluetoothData; 
	private BluetoothService mBluetoothService = null;
	private long lastUpdateTime;
 	private SensorManager sensorMag;
 	private Sensor gravitySensor;
 	// 保存上一次记录
 	float lastX = 0;
 	float lastY = 0;
 	float lastZ = 0;

	private ServiceConnection connection=new ServiceConnection(){  
		@Override  
		public void onServiceConnected(ComponentName arg0, IBinder service) {  
			bluetoothData = (BluetoothData)service;   
		}  
		@Override  
		public void onServiceDisconnected(ComponentName arg0) {  
			bluetoothData = null;  
		}  
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		sensorMag = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		gravitySensor = sensorMag.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		if (D)Log.e(TAG, "+++ ON CREATE +++");
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) { 
		  Toast.makeText(this, "蓝牙设备不可用,请打开蓝牙!", Toast.LENGTH_LONG).show();
		  finish();
		  return;
		 }
		 if (!mBluetoothAdapter.isEnabled()) {
		   Toast.makeText(this,  "请打开蓝牙并重新运行程序!", Toast.LENGTH_LONG).show();
		   finish();
		   return;
		 }      
		 if (D)Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");
		 try {  
			 	bindService(new Intent(Main.this,BluetoothService.class),   
	                connection, Context.BIND_AUTO_CREATE);  
	    } catch (Exception e) {  
	        // TODO: handle exception  
	    }
		
		textView1=(TextView)findViewById(R.id.textView1);
		button1=(Button)findViewById(R.id.button1);
		
		button1.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				PackageManager pm = getPackageManager();
				List<ResolveInfo> activities = pm.queryIntentActivities
						(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
				//若已经安装Google voice search engine
				if(activities.size() != 0){
					try{
						Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);//语音识别Intent
						intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
								RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
						intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "桂林电子科技大学 电子工程与自动化学院 贾寒光");  //识别画面出现的说明
						startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);  //启动语音识别
					}
					catch (Exception e){
						textView1.setText(""+e.getMessage());
						Toast.makeText(Main.this, e.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
				else{
					textView1.setText("RecognizerIntent Not Found!!!");
					Toast.makeText(Main.this, "RecognizerIntent Not Found!!!",
							Toast.LENGTH_SHORT).show();
				}
			}
		});	
		button2=(Button)findViewById(R.id.button2);
		button2.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				try {  
	            	   bluetoothData.SendMessage("0");  
	                   //  bluetoothData.getACK();  
	               } 
	               catch (Exception e) {  
	            	   // TODO: handle exception  
	               }
			}
		}
		);
		
	}
	@Override
	protected void onActivityResult(int requestcode, int resultcode, Intent data)
	{
		switch(requestcode)
		{
		 case VOICE_RECOGNITION_REQUEST_CODE:
			 if(requestcode == VOICE_RECOGNITION_REQUEST_CODE 
			 && resultcode == RESULT_OK)
			 {
				 boolean flag=false;
				 strRet = "";
				 strRet1 ="正在发送至机器人!";
ArrayList<String> results = 				 data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
				 for(int i=0;i<1;i++)
				 {
				  strRet +=results.get(i);
				 }
				 if(strRet.length()>0)
				 {	
				  if(strRet.equals("向前"))
				  {
					  strRet="1";
				  }
				  if(strRet.equals("右转"))
				  {
					  strRet="4";
				  }
				  if(strRet.equals("后退"))
				  {
					  strRet="3";
				  }
				  if(strRet.equals("左转"))
				  {
					  strRet="2";
				  }
				  if(strRet.equals("打开"))
				  {
					  strRet="5";
				  }
				  if(strRet.equals("抓取"))
				  {
					  strRet="6";
				  }
				  if(strRet.equals("伸出"))
				  {
					  strRet="7";
				  }
				  if(strRet.equals("收回"))
				  {
					  strRet="8";
				  }
				  if(strRet.equals("抬起"))
				  {
					  strRet="9";
				  }
				  if(strRet.equals("放下"))
				  {
					  strRet="a";
				  }
				  if(strRet.equals("停止工作"))
				  {
					  finish();
				  }
	               try {  
	            	   flag=bluetoothData.SendMessage(strRet);  
	               } 
	               catch (Exception e) {  
	            	   // TODO: handle exception  
	               } 
	               if(flag){
                    textView1.setText("您的命令为:"+strRet);
					Toast.makeText(Main.this, strRet+"\n"+strRet1, Toast.LENGTH_SHORT).show();
	               }
				 }
			 }
		}
	}
	
	float tMax = 0.5f;
	private SensorEventListener sensorLis = new SensorEventListener() {

		public void onAccuracyChanged(Sensor sensor, int accuracy) {

		}

		public void onSensorChanged(SensorEvent event) {

			if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
				return;
			}
			// 现在检测时间
			long currentUpdateTime = System.currentTimeMillis();
			// 两次检测的时间间隔
			long timeInterval = currentUpdateTime - lastUpdateTime;
			// 判断是否达到了检测时间间隔
			if (timeInterval < UPTATE_INTERVAL_TIME)
				return;
			// 现在的时间变成last时间
			lastUpdateTime = currentUpdateTime;
			// 获取加速度数值,以下三个值为重力分量在设备坐标的分量大小
			float x = event.values[SensorManager.DATA_X];

			float y = event.values[SensorManager.DATA_Y];

			float z = event.values[SensorManager.DATA_Z];

			float absx = Math.abs(x);
			float absy = Math.abs(y);
			float absz = Math.abs(z);

			if (absx > absy && absx > absz) {

				if (x > tMax) {
					Log.e("origen", "turn left");
					try {  
		            	 bluetoothData.SendMessage("2");  
		                   //  bluetoothData.getACK();  
		               } 
		               catch (Exception e) {  
		            	   // TODO: handle exception  
		               }

				} else if (x < -tMax) {
					Log.e("origen", "turn right");					
					try {  
		            	 bluetoothData.SendMessage("4");  
		                   //  bluetoothData.getACK();  
		               } 
		               catch (Exception e) {  
		            	   // TODO: handle exception  
		               }
				}

			} else if (absy > absx && absy > absz) {

				if (y < - tMax) {
					Log.e("origen", "turn up");
					try {  
		            	 bluetoothData.SendMessage("1");  
		                   //  bluetoothData.getACK();  
		               } 
		               catch (Exception e) {  
		            	   // TODO: handle exception  
		               }
					
				} else if (y > tMax) {
					Log.e("origen", "turn down");
					try {  
		            	 bluetoothData.SendMessage("3");  
		                   //  bluetoothData.getACK();  
		               } 
		               catch (Exception e) {  
		            	   // TODO: handle exception  
		               }
				}
				
			}
			else if (absx < tMax && absy < tMax) {
				Log.e("origen", "screen up");
				try {
					bluetoothData.SendMessage("0"); 
				} catch (Exception e) {
					// TODO: handle exception  
				}
			}
		}
	};
	
	@Override
    public void onStart() {
    	super.onStart();
    	if (D) Log.e(TAG, "++ ON START ++");
    	sensorMag.registerListener(sensorLis, gravitySensor,
				SensorManager.SENSOR_DELAY_UI);
    	
    }
	  
    @Override  
    protected void onStop() {  
        // TODO Auto-generated method stub  
        super.onStop();  
        if(D)  
        {  
            Log.e(TAG, "++ ON STOP ++");  
        }  
    }  
    @Override  
    protected void onDestroy() {  
        // TODO Auto-generated method stub  
        unbindService(connection);  
        super.onDestroy();  
        if (D)  
        {  
            Log.e(TAG, "++ ON DESTROY ++");  
        }  
        try {  
            if(btSocket != null)  
              btSocket.close();  
        } catch (IOException e) {  
            Log.e(TAG, "Close failed");  
        }  
    } 	

4 实现效果

毕业设计 STM32智能家政机器人_第5张图片
毕业设计 STM32智能家政机器人_第6张图片

5 最后

包含内容

毕业设计 STM32智能家政机器人_第7张图片

详细设计论文

项目分享:

https://gitee.com/sinonfin/sharing

你可能感兴趣的:(stm32,机器人,毕业设计,单片机,小车)