Android App:自动驾驶的智能小车:物联网实战项目(带源码和演示)

当今社会正在步入一个万物互联的时代,它的技术基石主要来自5G、物联网和人工智能。 三者融合产生了许多新产品,其中最璀璨的当数自动驾驶的电动车;它汇聚了最新科技与工程实践的成果,引得各大巨头如华为、小米、百度、阿里、腾讯纷纷入场造车。 为啥这些科技巨头如此热衷造车呢?一个重要原因是智能车与移动互联密切相关,智能电动车与传统汽油车之间,犹如智能手机与功能手机的区别,可想而知,这是一个多么具有颠覆性的革命技术了。

一、需求描述

为了实现自动行驶,智能小车必须具备下列功能方可正常工作:

(1)自动在符合要求的道路上开行;

(2)如果遇到障碍物,要能主动避让防止碰撞;

(3)服从命令听指挥,能够接收外部指令改变行驶状态;

二、功能分析

小车组装效果如下 读者可自行前往淘宝等平台购买 本次选择的是51单片机小车

小车包括以下三大智能模块

1)红外循迹模块 循迹模块采用红外反射传感器,检查路面对红外线的反射率是否在目标阈值之内。

该模块通过检测小车下方的路面颜色决定行进路线,检测方案采用红外反射传感器,检查路面对红外线的反射率是否在目标阈值内,反射距离范围为2毫米到30毫米

(2)红外避障模块 避障模块检测小车前方是否有障碍物,决定是否需要改变路线以躲避障碍。

该模块与红外迅疾地原理类似,通过检测小车前方是否有障碍物决定是否需要改变路线以及躲避障碍

(3)蓝牙遥控模块 操纵者使用手机向小车发送蓝牙信号,小车接到蓝牙信号后,依据指令调整行驶状态。

小车连接遥控模块之后,操纵者使用手机向小车发送蓝牙信号,小车接到蓝牙信号后,依据指令调整行驶状态。本次通过Wireshark抓取两部BLE设备之间的通信包

三、效果展示

成功连接手机

可以在手机上行操纵电机的马力实现加速或者减速

实物演示效果如下

 
四、代码

部分代码如下  需要全部源码请点赞关注收藏后评论区留言~~~~

package com.example.iot;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.iot.adapter.BlueListAdapter;
import com.example.iot.bean.BlueDevice;
import com.example.iot.util.BluetoothUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScanCarActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private static final String TAG = "ScanCarActivity";
    private CheckBox ck_bluetooth; // 声明一个复选框对象
    private TextView tv_discovery; // 声明一个文本视图对象
    private ListView lv_bluetooth; // 声明一个用于展示蓝牙设备的列表视图对象
    private BlueListAdapter mListAdapter; // 声明一个蓝牙设备的列表适配器对象

    private Map<String, BlueDevice> mDeviceMap = new HashMap<>(); // 蓝牙设备映射
    private List<BlueDevice> mDeviceList = new ArrayList<>(); // 蓝牙设备列表
    private Handler mHandler = new Handler(Looper.myLooper()); // 声明一个处理器对象
    private BluetoothAdapter mBluetoothAdapter; // 声明一个蓝牙适配器对象
    private boolean isScaning = false; // 是否正在扫描

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_car);
        initView(); // 初始化视图
        initBluetooth(); // 初始化蓝牙适配器
        if (BluetoothUtil.getBlueToothStatus()) { // 已经打开蓝牙
            ck_bluetooth.setChecked(true);
        }
    }

    // 初始化视图
    private void initView() {
        ck_bluetooth = findViewById(R.id.ck_bluetooth);
        tv_discovery = findViewById(R.id.tv_discovery);
        lv_bluetooth = findViewById(R.id.lv_bluetooth);
        ck_bluetooth.setOnCheckedChangeListener(this);
        mListAdapter = new BlueListAdapter(this, mDeviceList);
        lv_bluetooth.setAdapter(mListAdapter);
        lv_bluetooth.setOnItemClickListener((parent, view, position, id) -> {
            BlueDevice item = mDeviceList.get(position);
            Intent intent = new Intent(this, SmartCarActivity.class);
            intent.putExtra("address", item.address);
            startActivity(intent);
        });
    }

    // 初始化蓝牙适配器
    private void initBluetooth() {
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, "当前设备不支持低功耗蓝牙", Toast.LENGTH_SHORT).show();
            finish(); // 关闭当前页面
        }
        // 获取蓝牙管理器,并从中得到蓝牙适配器
        BluetoothManager bm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bm.getAdapter(); // 获取蓝牙适配器
    }

    // 创建一个开启BLE扫描的任务
    private Runnable mScanStart = new Runnable() {
        @Override
        public void run() {
            if (!isScaning && BluetoothUtil.getBlueToothStatus()) {
                isScaning = true;
                // 获取BLE设备扫描器
                BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
                scanner.startScan(mScanCallback); // 开始扫描BLE设备
                tv_discovery.setText("点击BLE设备进入管理界面");
            } else {
                mHandler.postDelayed(this, 2000);
            }
        }
    };

    // 创建一个扫描回调对象
    private ScanCallback mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            if (TextUtils.isEmpty(result.getDevice().getName())) {
                return;
            }
            Log.d(TAG, "callbackType="+callbackType+", result="+result.toString());
            // 下面把找到的蓝牙设备添加到设备映射和设备列表
            BlueDevice device = new BlueDevice(result.getDevice().getName(), result.getDevice().getAddress(), 0);
            mDeviceMap.put(device.address, device);
            mDeviceList.clear();
            mDeviceList.addAll(mDeviceMap.values());
            runOnUiThread(() -> mListAdapter.notifyDataSetChanged());
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
        }
    };

    // 创建一个停止BLE扫描的任务
    private Runnable mScanStop = () -> {
        isScaning = false;
        // 获取BLE设备扫描器
        BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
        scanner.stopScan(mScanCallback); // 停止扫描BLE设备
    };

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.ck_bluetooth) {
            if (isChecked) { // 开启蓝牙功能
                ck_bluetooth.setText("蓝牙开");
                if (!BluetoothUtil.getBlueToothStatus()) { // 还未打开蓝牙
                    BluetoothUtil.setBlueToothStatus(true); // 开启蓝牙功能
                }
                mHandler.post(mScanStart); // 启动开始BLE扫描的任务
            } else { // 关闭蓝牙功能
                ck_bluetooth.setText("蓝牙关");
                mHandler.removeCallbacks(mScanStart); // 移除开始BLE扫描的任务
                BluetoothUtil.setBlueToothStatus(false); // 关闭蓝牙功能
                mDeviceList.clear();
                mListAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mScanStart); // 移除开始BLE扫描的任务
        mHandler.removeCallbacks(mScanStop); // 移除停止BLE扫描的任务
    }

}
物联沃分享整理
物联沃-IOTWORD物联网 » Android App:自动驾驶的智能小车:物联网实战项目(带源码和演示)

发表评论