[Android App] The self-driving smart car of the Internet of Things actual combat project (with source code and demonstration super detailed)

If you need the source code, please like and follow the collection and leave a private message in the comment area~~~~

Today's society is entering an era of the Internet of Everything, and its technological cornerstones mainly come from 5G, the Internet of Things and artificial intelligence. The integration of the three has produced many new products, the most brilliant of which is the self-driving electric car; it brings together the latest technology and engineering practices, and has attracted major giants such as Huawei, Xiaomi, Baidu, Ali, and Tencent to enter the market one after another. car. Why are these technology giants so keen on making cars? An important reason is that smart cars are closely related to mobile Internet. The difference between smart electric cars and traditional gasoline cars is like the difference between smart phones and feature phones. It is conceivable how disruptive and revolutionary technology this is.

1. Requirements description

In order to realize automatic driving, the smart car must have the following functions to work properly:

(1) Automatically drive on roads that meet the requirements;

(2) If you encounter an obstacle, you must be able to actively avoid collisions;

(3) Obey orders and command, and be able to receive external instructions to change the driving state;

2. Functional analysis

The car assembly effect is as follows. Readers can go to Taobao and other platforms to buy this time. The 51 single-chip microcomputer car

 

The trolley includes the following three intelligent modules

1) Infrared tracking module The tracking module uses an infrared reflection sensor to check whether the reflectivity of the road to infrared rays is within the target threshold. 

The module determines the route by detecting the color of the road under the car. The detection scheme uses an infrared reflection sensor to check whether the reflectivity of the road to infrared rays is within the target threshold. The reflection distance ranges from 2mm to 30mm

(2) Infrared obstacle avoidance module The obstacle avoidance module detects whether there is an obstacle in front of the car, and decides whether to change the route to avoid the obstacle.

This module is similar to the principle of infrared rapid ground, by detecting whether there are obstacles in front of the car to determine whether to change the route and avoid obstacles

(3) The operator of the Bluetooth remote control module uses a mobile phone to send a Bluetooth signal to the car. After receiving the Bluetooth signal, the car adjusts the driving state according to the instruction.

After the car is connected to the remote control module, the operator uses the mobile phone to send a Bluetooth signal to the car. After receiving the Bluetooth signal, the car adjusts its driving state according to the instructions. This time, capture the communication packets between two BLE devices through Wireshark

3. Effect display

Successfully connected to the phone 

 The horsepower of the motor can be controlled on the mobile phone to achieve acceleration or deceleration

 

 The actual demonstration effect is as follows

 
 4. Code

Part of the code is as follows. If you need all the source code, please like and follow the collection and leave a message in the comment area~~~~

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扫描的任务
    }

}

It's not easy to create and find it helpful, please like, follow and collect~~~

Guess you like

Origin blog.csdn.net/jiebaoshayebuhui/article/details/128147493