基于android和arduino 的小车控制,小车部分采用的是 arduino 开源硬件实现,arduino 学起来比较简单,看看文档在看几个例子基本就会了。官网是http://arduino.cc/
我是在某宝上购买的一组套件,最终发现很多都没有用上,有点眼高手低。android 和 arduino 通信 是通过蓝牙。小车控制比较简单,前、后、左、右、向左转圈、向右转圈。
以下是源码;
1:arduino 源码:
#include <BaseCar.h> // log const boolean VERBOSE = true; const int SERIAL_SPEED = 9600; // for Car //1-ok const int MOTO1_PIN = 2; const int I11_PIN = 3; const int I12_PIN = 4; //2 const int MOTO2_PIN = 5; const int I21_PIN = 6; const int I22_PIN = 7; //3-ok const int MOTO3_PIN = 8; const int I31_PIN = 9; const int I32_PIN =10; //4 const int MOTO4_PIN = 11; const int I41_PIN = 12; const int I42_PIN = 13; // speed const int SPEED_VAL =500; // delay const int DELAY_TIME = 300; //bouttooth char RECEIVE_VALUE='q_z_1'; // car control BaseCar car(MOTO1_PIN, MOTO2_PIN, MOTO3_PIN, MOTO4_PIN,I11_PIN, I12_PIN, I21_PIN, I22_PIN,I31_PIN, I32_PIN, I41_PIN, I42_PIN); const int adj = 19; void setup() { if (VERBOSE) { Serial.begin(SERIAL_SPEED); } } void loop() { // RECEIVE_VALUE=Serial.read(); //Serial.println(RECEIVE_VALUE); if(RECEIVE_VALUE=='q_z_1'){ // 左前一度 car.forward(SPEED_VAL, adj); }else if(RECEIVE_VALUE=='q_z_2'){ // 左前二度 car.turnLeft(SPEED_VAL, adj); }else if(RECEIVE_VALUE=='q_z_3'){ // 左前三度-向左旋转 car.rotateLeft(SPEED_VAL, adj); }else if(RECEIVE_VALUE=='q_y_1'){ // 右前一度 car.forward(SPEED_VAL, adj); }else if(RECEIVE_VALUE=='q_y_2'){ // 右前二度 car.turnRight(SPEED_VAL, adj); }else if(RECEIVE_VALUE=='q_y_3'){ // 右前三度-向右旋转 car.rotateRight(SPEED_VAL, adj); }else{ // 刹车 car.standBy(); } }
1.1 将 以下两个类放在libiriary 下 文件夹名命名为 car
#include "Arduino.h" #include "BaseCar.h" BaseCar::BaseCar(int left_back_speed, int left_forward_speed,int right_forward_speed, int right_back_speed, int left_back_1, int left_back_2, int left_forward_1, int left_forward_2,int right_forward_1, int right_forward_2,int right_back_1, int right_back_2){ _left_back_speed = left_back_speed; _left_forward_speed = left_forward_speed; _right_forward_speed = right_forward_speed; _right_back_speed = right_back_speed; pinMode(left_back_speed, OUTPUT); pinMode(left_forward_speed, OUTPUT); pinMode(right_forward_speed, OUTPUT); pinMode(right_back_speed, OUTPUT); _left_back_1 = left_back_1; _left_back_2 = left_back_2; _left_forward_1 = left_forward_1; _left_forward_2 = left_forward_2; _right_forward_1 = right_forward_1; _right_forward_2 = right_forward_2; _right_back_1 = right_back_1; _right_back_2 = right_back_2; pinMode(left_back_1, OUTPUT); pinMode(left_back_2, OUTPUT); pinMode(left_forward_1, OUTPUT); pinMode(left_forward_2, OUTPUT); pinMode(right_forward_1, OUTPUT); pinMode(right_forward_2, OUTPUT); pinMode(right_back_1, OUTPUT); pinMode(right_back_2, OUTPUT); } //停止 void BaseCar::standBy() { _status.left_back_speed_h = 0; _status.left_forward_speed_h = 0; _status.right_forward_speed_h = 0; _status.right_back_speed_h = 0; _status.left_back_1_h = HIGH; _status.left_back_2_h = HIGH; _status.left_forward_1_h = HIGH; _status.left_forward_2_h = HIGH; // _status.right_forward_1_h = HIGH; _status.right_forward_2_h = HIGH; _status.right_back_1_h = HIGH; _status.right_back_2_h = HIGH; _go(); } //向前 void BaseCar::forward(int moto_speed, int adj) { _status.left_back_speed_h = moto_speed + adj; _status.left_forward_speed_h = moto_speed + adj; _status.right_forward_speed_h = moto_speed + adj; _status.right_back_speed_h = moto_speed + adj; _status.left_back_1_h = LOW; _status.left_back_2_h = HIGH; _status.left_forward_1_h = LOW; _status.left_forward_2_h = HIGH; _status.right_forward_1_h = LOW; _status.right_forward_2_h = HIGH; _status.right_back_1_h = LOW; _status.right_back_2_h = HIGH; _status.adj = adj; _go(); } //向后 void BaseCar::backward(int moto_speed, int adj) { _status.left_back_speed_h = moto_speed + adj; _status.left_forward_speed_h = moto_speed + adj; _status.right_forward_speed_h = moto_speed + adj; _status.right_back_speed_h = moto_speed + adj; _status.left_back_1_h = HIGH; _status.left_back_2_h = LOW; _status.left_forward_1_h = HIGH; _status.left_forward_2_h = LOW; _status.right_forward_1_h = HIGH; _status.right_forward_2_h = LOW; _status.right_back_1_h = HIGH; _status.right_back_2_h = LOW; _status.adj = adj; _go(); } //左转 void BaseCar::turnLeft(int moto_speed, int adj) { _status.left_back_speed_h = moto_speed - adj; _status.left_forward_speed_h = moto_speed - adj; _status.right_forward_speed_h = moto_speed + adj; _status.right_back_speed_h = moto_speed + adj; _status.left_back_1_h = LOW; _status.left_back_2_h = HIGH; _status.left_forward_1_h = LOW; _status.left_forward_2_h = HIGH; _status.right_forward_1_h = LOW; _status.right_forward_2_h = HIGH; _status.right_back_1_h = LOW; _status.right_back_2_h = HIGH; _status.adj = adj; _go(); } //右转 void BaseCar::turnRight(int moto_speed, int adj) { _status.left_back_speed_h = moto_speed + adj; _status.left_forward_speed_h = moto_speed + adj; _status.right_forward_speed_h = moto_speed - adj; _status.right_back_speed_h = moto_speed - adj; _status.left_back_1_h = LOW; _status.left_back_2_h = HIGH; _status.left_forward_1_h = LOW; _status.left_forward_2_h = HIGH; _status.right_forward_1_h = LOW; _status.right_forward_2_h = HIGH; _status.right_back_1_h = LOW; _status.right_back_2_h = HIGH; _status.adj = adj; _go(); } //向左旋转(右边向前转,左边向后转) void BaseCar::rotateLeft(int moto_speed, int adj) { _status.left_back_speed_h = moto_speed + adj; _status.left_forward_speed_h = moto_speed + adj; _status.right_forward_speed_h = moto_speed + adj; _status.right_back_speed_h = moto_speed + adj; _status.left_back_1_h = HIGH; _status.left_back_2_h = LOW; _status.left_forward_1_h = HIGH; _status.left_forward_2_h = LOW; _status.right_forward_1_h = LOW; _status.right_forward_2_h = HIGH; _status.right_back_1_h = LOW; _status.right_back_2_h = HIGH; _status.adj = adj; _go(); } //向右旋转 void BaseCar::rotateRight(int moto_speed, int adj) { _status.left_back_speed_h = moto_speed + adj; _status.left_forward_speed_h = moto_speed + adj; _status.right_forward_speed_h = moto_speed + adj; _status.right_back_speed_h = moto_speed + adj; _status.left_back_1_h = LOW; _status.left_back_2_h = HIGH; _status.left_forward_1_h = LOW; _status.left_forward_2_h = HIGH; _status.right_forward_1_h = HIGH; _status.right_forward_2_h = LOW; _status.right_back_1_h = HIGH; _status.right_back_2_h = LOW; _status.adj = adj; _go(); } void BaseCar::_go() { digitalWrite(_left_back_1, _status.left_back_1_h); digitalWrite(_left_back_2, _status.left_back_2_h); digitalWrite(_left_forward_1, _status.left_forward_1_h); digitalWrite(_left_forward_2, _status.left_forward_2_h); digitalWrite(_right_forward_1, _status.right_forward_1_h); digitalWrite(_right_forward_2, _status.right_forward_2_h); digitalWrite(_right_back_1, _status.right_back_1_h); digitalWrite(_right_back_2, _status.right_back_2_h); analogWrite(_left_back_speed, _status.left_back_speed_h); analogWrite(_left_forward_speed, _status.left_forward_speed_h); analogWrite(_right_forward_speed, _status.right_forward_speed_h); analogWrite(_right_back_speed, _status.right_back_speed_h); } CarStatus BaseCar::getStatus() { return _status; }
#ifndef BaseCar_h #define BaseCar_h #include "Arduino.h" struct CarStatus { int left_back_1_h; int left_back_2_h; int left_forward_1_h; int left_forward_2_h; int right_forward_1_h; int right_forward_2_h; int right_back_1_h; int right_back_2_h; int left_back_speed_h; int left_forward_speed_h; int right_forward_speed_h; int right_back_speed_h; int adj; }; class BaseCar { public: BaseCar(int left_back_speed, int left_forward_speed,int right_forward_speed, int right_back_speed, int left_back_1, int left_back_2, int left_forward_1, int left_forward_2,int right_forward_1, int right_forward_2,int right_back_1, int right_back_2); void standBy(); void forward(int moto_speed, int adj); void backward(int moto_speed, int adj); void turnLeft(int moto_speed, int adj); void turnRight(int moto_speed, int adj); void rotateLeft(int moto_speed, int adj); void rotateRight(int moto_speed, int adj); CarStatus getStatus(); private: int _left_back_speed; int _left_forward_speed; int _right_forward_speed; int _right_back_speed; int _left_back_1; int _left_back_2; int _left_forward_1; int _left_forward_2; int _right_forward_1; int _right_forward_2; int _right_back_1; int _right_back_2; CarStatus _status; void _go(); }; #endif
2:android 源码如下:
package com.robot; import java.io.IOException; import java.io.OutputStream; import java.util.UUID; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class AAActivity extends Activity { private TextView state; private ImageView forward; private ImageView back; private ImageView left_2; private ImageView right_2; boolean isStart = false; private static final UUID MY_UUID = UUID .fromString("00001101-0000-1000-8000-00805F9B34FB"); private static String address = "00:12:02:06:01:32"; private static final String TAG = "THINBTCLIENT"; private static final boolean D = true; private BluetoothAdapter mBluetoothAdapter = null; private BluetoothSocket btSocket = null; private OutputStream outStream = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.index); initUI(); } @Override protected void onStart() { super.onStart(); } public void setValue(String message, final String text) { if (isStart) { state.setText(text); byte[] msgBuffer; try { outStream = btSocket.getOutputStream(); } catch (IOException e) { Log.e(TAG, "ON RESUME: Output stream creation failed.", e); } msgBuffer = message.getBytes(); try { outStream.write(msgBuffer); } catch (IOException e) { Log.e(TAG, "ON RESUME: Exception during write.", e); } } else { Toast.makeText(this, "请先选择开启!", Toast.LENGTH_SHORT).show(); } } private void initUI() { state = (TextView) findViewById(R.id.state); CheckBox blueT = (CheckBox) findViewById(R.id.blueT); blueT.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { isStart = isChecked; } }); forward = (ImageView) findViewById(R.id.forward); forward.setImageResource(R.drawable.forward); forward.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { forward.setImageResource(R.drawable.forward_r); setValue("1", "直走"); } if (event.getAction() == MotionEvent.ACTION_UP) { forward.setImageResource(R.drawable.forward); setValue("7", "刹车"); } return true; } }); back = (ImageView) findViewById(R.id.back); back.setImageResource(R.drawable.back); back.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { back.setImageResource(R.drawable.back_r); setValue("6", "后退"); } if (event.getAction() == MotionEvent.ACTION_UP) { back.setImageResource(R.drawable.back); setValue("7", "刹车"); } return true; } }); left_2 = (ImageView) findViewById(R.id.left_2); left_2.setImageResource(R.drawable.left_2); left_2.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { left_2.setImageResource(R.drawable.left_2_r); setValue("3", "左前二度"); } if (event.getAction() == MotionEvent.ACTION_UP) { left_2.setImageResource(R.drawable.left_2); setValue("7", "刹车"); } return true; } }); right_2 = (ImageView) findViewById(R.id.right_2); right_2.setImageResource(R.drawable.right_2); right_2.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { right_2.setImageResource(R.drawable.right_2_r); setValue("5", "右前二度"); } if (event.getAction() == MotionEvent.ACTION_UP) { right_2.setImageResource(R.drawable.right_2); setValue("7", "刹车"); } return true; } }); 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 +++"); } public void onPause() { if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } super.onPause(); if (D) Log.e(TAG, "- ON PAUSE -"); if (outStream != null) { try { outStream.flush(); } catch (IOException e) { Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e); } } try { btSocket.close(); } catch (IOException e2) { DisplayToast("套接字关闭失败!"); } } public void onStop() { super.onStop(); if (D) Log.e(TAG, "-- ON STOP --"); } public void onDestroy() { super.onDestroy(); if (D) Log.e(TAG, "--- ON DESTROY ---"); } public void DisplayToast(String str) { Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP, 0, 220); toast.show(); } public void onResume() { super.onResume(); if (D) { Log.e(TAG, "+ ON RESUME +"); Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +"); } DisplayToast("正在尝试连接智能小车,请稍后····"); BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { e.printStackTrace(); DisplayToast("套接字创建失败!"); } DisplayToast("成功连接智能小车!可以开始操控了~~~"); mBluetoothAdapter.cancelDiscovery(); try { btSocket.connect(); DisplayToast("连接成功建立,数据连接打开!"); } catch (IOException e) { try { btSocket.close(); } catch (IOException e2) { DisplayToast("连接没有建立,无法关闭套接字!"); } } if (D) Log.e(TAG, "+ ABOUT TO SAY SOMETHING TO SERVER +"); } }
相关推荐
这种技术的实现基于Android系统的蓝牙通信功能,通过蓝牙模块连接Android设备和Arduino主板,实现两者之间的数据传输。 1. **蓝牙模块与Arduino的连接** - Arduino蓝牙模块通常有四条线:SCL(Serial Clock),SDA...
7. **手机APP开发**:为了实现“无死角切换”的控制功能,需要开发一款手机应用程序,可能基于Android或iOS平台,通过蓝牙连接到arduino,设计友好的用户界面,使用户能够直观地操作小车的各项功能。 这个项目不仅...
本文档详细介绍了基于 Android 平台和 Arduino 平台的智能小车设计,借助蓝牙通信技术,实现智能小车的无线控制。该设计将蓝牙技术、Arduino 开发技术和 Android 移动智能终端平台结合在一起,通过移动智能终端使用...
系统设计包含了执行模块、测距模块、报警模块和显示模块,同时支持自动控制和通过Android智能手机手动控制两种模式。 在硬件设计方面,系统利用超声波和红外线两种测距技术,确保在智能小车行进和倒车时能有效检测...
基于Arduino_Android的语音控制小车设计.pdf
【作品名称】:基于Arduino+Android的无线智能小车系统的设计与实现【毕业...本文将基于Arduino平台和Android设计并实现能够无线远程控制运动并采集温湿度的智能小车系统。 关键词:Arduino,Android,无线,智能小车
Arduino智能小车是一种基于Arduino开源硬件平台的微型车辆,它结合了电子技术、机械工程和计算机编程,使得用户可以通过编写程序来控制小车的各种行为。本项目中的小车实现了蓝牙控制,PWM调速以及红外避障和自主...
在讨论基于Android手机操作和控制的Wifi小车程序设计时,我们首先需要了解几个关键的知识点和设计思想。 一、小车平台选择 首先,小车平台的选择对于整个系统的构建至关重要。文章中提到,作者选择了轮式驱动单元和...
【基于Android系统的Arduino智能车设计】是一篇关于利用Android平台设计和控制Arduino智能车的学术文章。Arduino是一种开源电子原型平台,广泛应用于电子设计实训,它包括硬件(各种型号的Arduino板)和软件...
在这个项目中,手机作为主设备,Arduino小车作为从设备。手机APP发送控制命令(例如前进、后退、左转、右转等),蓝牙模块接收到这些命令后,通过串行通信接口将命令传输给Arduino。 【arduino】:Arduino是一种...
基于 Arduino 的蓝牙控制避障智能小车设计是现代科技与智能控制技术的结合,它将传统的遥控小车提升到了一个新的层次。在这个项目中,我们利用 Arduino 开发板作为核心控制器,通过蓝牙通信模块与安卓手机建立连接,...
【蓝牙遥控控制智能小车Android Studio参考代码】项目是一个典型的移动设备与硬件设备交互的应用,主要涉及了Android Studio开发环境、蓝牙通信技术和智能小车控制技术。以下将详细阐述这些关键知识点。 首先,**...
【标题】"WiFi小车Android手机控制端源码"是一个基于Android平台的软件开发项目,其主要目的是实现通过智能手机对配备WiFi模块的Arduino小车进行远程控制。此源码是整个控制系统的重要组成部分,使得用户可以通过...
Arduino蓝牙小车程序是一个基于Arduino开发平台的项目,旨在通过蓝牙通信控制小型机器人车辆。这个项目的重点在于结合硬件和软件,实现远程控制小车的移动、转向和其他功能。以下是对这个项目的详细解释: 1. **...
8. **代码编写**:在Arduino IDE中编写程序,包括初始化硬件接口、读取传感器数据、解析手机APP发送的颜色信息、执行追踪算法和控制电机动作等部分。 9. **调试与优化**:项目完成后,需要进行多次测试以确保小车能...
在本文中,我们将深入探讨如何使用Android手机或平板电脑通过WiFi来控制智能小车机器人,主要基于提供的"Android手机、PAD控制WIFI智能小车机器人安装包 for 2.3"。这个安装包是一个专门为Android 2.3系统设计的应用...
在电子制作和物联网领域,Android与Arduino的交互是常见的应用场景,比如智能小车、智能家居、环境监测等。本文将深入探讨这个主题,基于提供的“安卓与arduino交互程序”这一资源,解析其背后的原理和实现方法。 ...