基于Android的智能家政机器人设计与实现

发布时间:2025-01-02 08:29

智能家居设备可提升家政服务效率,如智能扫地机器人 #生活知识# #家政服务#

一、概述

        Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。系统中基于Android的部分使用JAVA完成开发工作。整个系统由安卓手机控制端和机器人执行端组成,其中安卓端完成用户语音命令识别、交互式操作界面和蓝牙无线通讯传送用户命令;机器人端负责响应控制命令,完成用户的要求。安卓端为运行在Android手机上的一套控制软件,基于Google的Google Voice Search完成语音识别,识别结果经由软件处理生成相应的控制指令,通过蓝牙发送到机器人端。机器人端通过HC-05蓝牙模块接收控制命令,在STM32RBT6的控制下驱动一组CDS5500舵机完成用户的命令。在对系统进行测试中,该系统成功的实现了对于非特定人的语音识别,机器人能够较为准确的运动到指定地点执行特定命令。为了减小机械传动部分的误差,我在上位机安卓端加入了前进、后退、左转、右转和机械手各自由度的控制功能。

二、设计指标

(1)机器人要求为移动车体,具有机械手;

(2)基于“安卓”实现简单语音识别;

(3)实现机械手臂简单操作;

(4)通过蓝牙实现“安卓”对机器人的无线控制;

(5)实现自动定位。

三、相关代码

机器人端代码:

#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 <stm32f10x_lib.h>

#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;

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;

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) {

}

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);

if(activities.size() != 0){

try{

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

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");

}

catch (Exception e) {

}

}

}

);

}

@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) {

}

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;

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");

}

catch (Exception e) {

}

} else if (x < -tMax) {

Log.e("origen", "turn right");

try {

bluetoothData.SendMessage("4");

}

catch (Exception e) {

}

}

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

if (y < - tMax) {

Log.e("origen", "turn up");

try {

bluetoothData.SendMessage("1");

}

catch (Exception e) {

}

} else if (y > tMax) {

Log.e("origen", "turn down");

try {

bluetoothData.SendMessage("3");

}

catch (Exception e) {

}

}

}

else if (absx < tMax && absy < tMax) {

Log.e("origen", "screen up");

try {

bluetoothData.SendMessage("0");

} catch (Exception e) {

}

}

}

};

@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() {

super.onStop();

if(D)

{

Log.e(TAG, "++ ON STOP ++");

}

}

@Override

protected void onDestroy() {

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");

}

}

网址:基于Android的智能家政机器人设计与实现 https://www.yuejiaxmz.com/news/view/626591

相关内容

android控制中心实现,基于Android平台的智能家居系统控制中心的设计与实现
基于Android平台Camera的设计与实现
基于Android的家庭理财系统的设计与实现
《基于Android的移动校园手机智能日程管理系统的设计与实现》.doc
基于Android的家庭理财APP的设计与实现(论文+源码)
基于Android平台的生活小助手app的设计与实现
基于android的个人健康助理设计与实现(论文)
(开题报告)基于Android的日程表管理系统设计与实现
基于Android系统的智能社区平台系统APP设计与实现(含论文)
基于Android平台生活小助手app设计与实现.doc

随便看看