3

1. Introduction

The intelligent metal detector is based on the OpenAtom OpenHarmony (hereinafter referred to as "OpenHarmony") operating system, and uses the principle of electromagnetic induction to detect surrounding metal objects. This sample adopts the method of multi-device collaboration, which is compatible with OpenHarmony device development and application development. The whole sample reflects OpenHarmony's technical features such as NAPI, eTS UI, UI management state @state, and audio playback.

This project consists of the Geek_Lite_Board development board and the Runhe RK3568 development board. The main control chip of the Geek_Lite_Board development board is STM32F427IIH6, which is used as the device to detect the change of the magnetic field strength, using the OpenHarmony 3.0 LTS version. Runhe RK3568 development board is developed by Runhe Software, using Rockchip RK3568 chip, as the application side to display metal detection information, using OpenHarmony 3.1 Release version.

2. Operation effect

When there is metal around, the buzzer on the device side will sound an alarm and send the detection information to the application side through the Wi-Fi module. After the application side receives the detection information, it will display the detection status on the screen and announce the detection result by voice.

3. Function realization

The Geek_Lite_Board development board obtains the Earth's magnetic field strength through the AK8963 electronic compass chip. Usually the magnetic field strength of the earth is 0.4-0.6 Gauss. When the metal is close to the electronic compass chip, according to the principle of electromagnetic induction, the induction of the metal in the magnetic field will cause the change of the magnetic field signal. The Geek_Lite_Board development board communicates with Runhe RK3568 development board in real time through Wi-Fi, and uploads the detected information in real time. Runhe RK3568 development board as the application side, uses the Ark development framework (ArkUI) to write pages based on the declarative development paradigm extended by eTS, receives the detection information of the Geek_Lite_Board development board on the device side through the NAPI interface, and uses the audio playback function in the OpenHarmony media subsystem. Implement voice broadcast metal detection status.

Magnetic field data acquisition
AK8963 is a three-axis electronic compass integrated chip with high-sensitivity Hall sensor technology. It contains a magnetic sensor inside, which can detect the strength of the earth's magnetic field in the x-axis, y-axis, and z-axis.

● Read the value of the chip data register through the Mpu_Read_Bytes() function.

 uint8_t Mpu_Read_Bytes(uint8_t const regAddr, uint8_t *pData, uint8_t len)

● Obtain the original magnetic field data of AK8963.

 Mpu_Data.mag_x = (MPU_BUFF[16] << 8) | MPU_BUFF[15]; // x轴磁场数据
Mpu_Data.mag_y = (MPU_BUFF[18] << 8) | MPU_BUFF[17]; // y轴磁场数据
Mpu_Data.mag_z = (MPU_BUFF[20] << 8) | MPU_BUFF[19]; // z轴磁场数据

● Convert the original magnetic field data of AK8963 to get the magnetic field strength in Gauss.

 Gauss_Mag_z = Mpu_Calc.mag_z * 0.15f * 0.01f;

Detecting Metal Information <br>Firstly collect 100 sets of z-axis magnetic field strength reference data, and then average these data to obtain the zero point data of z-axis magnetic field strength.

 const uint16_t calibrateCount = 1000; // 测量最大次数为1000
const uint16_t calibrateFrequency = 5; // 每测量5次取一次有效值
const uint16_t calibrateAverageCount = 100; // 取100次有效值
if(i < calibrateCount){
    i++;
    if(i%calibrateFrequency == calibrateFrequency){
       Mag_z_buff[j++] = Gauss_Mag_z;
        if(j >= calibrateAverageCount){
            i = calibrateCount;
            Mag_z_Flag = true;
            for(k=0;k<calibrateAverageCount;k++){
                origin_mag_z += Mag_z_buff[k];
            }
            origin_mag_z = origin_mag_z / calibrateAverageCount;
            // 校准完成,蓝灯亮,发送给应用端 "CalibrateOK"
            BLUE_LED_ON();
            memset(buff,0x00,sizeof(buff));
            sprintf(buff,"angle:%s","CalibrateOK");
            ESP8266_send_data(buff,strlen(buff));
        }
    }
}

Using origin_mag_z as the zero point, compare the acquired magnetic field data with it to determine whether metal is detected.
NAPI get data
NAPI (Native API) is a JS API implementation mechanism of the OpenHarmony standard system. It is suitable for encapsulating IO, CPU-intensive, OS bottom layer and other capabilities and exposing JS interfaces to the outside world. Through NAPI, JS and C/C++ code can access each other. Runhe RK3568 application side accepts the detection information sent by the device side through NAPI.
● Bottom layer NAPI module package <br>The package module name is tcpserverapi, which is downloaded to the tcpservermodule folder.
After the download is completed, put it in the root directory of the 3.1Release source code, and configure the compilation script; after the first compilation is completed, you need to burn the entire image, and then modify the module source code, just send the library to the board. The command is as follows:

 // 先挂载,再send
hdc_std shell mount -oremount,rw /   
hdc_std file send libtcpserverapi.z.so system/lib/module/libtcpserverapi.z.so

● Import the NAPI module on the application side

 import tcpserverapi from '@ohos.tcpserverapi'

● Application-side NAPI interface call

 // 调用initServer接口 初始化 TCP 服务器
tcpserverapi.initServer() 
// 调用recvMsg 获取并解析Geek_Lite_Board开发板发送过来的角度
tcpserverapi.recvMsg().then((result) => {  
    var resultAngle = result.angle;
})

UI status display and management

As shown in the figure above, the page under detection is divided into text titles and a schematic diagram of the detection status.
The text title is implemented by the text component of the Flex layout container, which is used to present a piece of information. The following are the related properties of the text interface.

● Schematic diagram of detection status includes page background diagram, detection status diagram, and description text of detection results. The specific functions are implemented by Flex flexible layout components. The interface prototype looks like this:

 Flex(options?: { direction?: FlexDirection, wrap?: FlexWrap, justifyContent?: FlexAlign, alignItems?: ItemAlign, alignContent?: FlexAlign })

● Transfer the message of the metal detector by calling the recvMsg interface, and modify the display status of the home page. The key implementation code is described as follows:

 aboutToAppear() {
    var intervalID = setInterval(() => {
        tcpserverapi.recvMsg().then((result) => {
       }
   }
}

Among them, " var intervalID = setInterval(() => " uses a timer, regularly calls the "recvMsg" method, and obtains the metal detection result through NAPI.

 if (resultMetal.match("metal:")) {
    this.metal = resultMetal.slice(6);
    console.info('=======' + this.metal)
    if (this.metal === 'Detected') {
          this.detectionState = '发现金属';
          this.detection = $r("app.media.img_detected");
          this.isDisplay = false;
    }
    else if (this.metal === 'UnDetected' || this.metal ==='CalibrateOK') {
          this.detectionState = '检测中';
          this.detection = $r("app.media.img_detecting");
          this.isDisplay = true;
     }
}

When the detection information obtained through NAPI is "metal:Detected", set the detectionState to find the metal state, and the screen displays the metal detected page; when the detection information obtained through NAPI is "metal:UnDetected", at this time and If no metal information is detected, set detectionState to the state under detection, and the screen displays the page under detection.
Voice broadcast detection status <br>After metal detection, RK3568 will broadcast the voice information of the detected metal to notify the user, which is achieved through the audio playback function of the OpenHarmony media subsystem.
The OpenHarmony media subsystem provides developers with a set of simple and easy-to-understand interfaces, enabling developers to easily access the system and use the system's media resources. The media subsystem includes common functions such as audio playback, video playback, audio recording, and video recording.
The main work of audio playback is to transcode audio data into audible audio analog signals and play them through output devices, and manage playback tasks at the same time. This sample is mainly implemented by importing the media module, creating an audio playback instance, implementing the audio playback interface, and implementing the pause playback interface.
● Import the media module

 import media from '@ohos.multimedia.media';

● Create an audio playback instance

 // OH media对象
private player = media.createAudioPlayer();

● To implement the audio playback interface, use the play() call to start playing the audio resource, which can only be called after the audio data is loaded, that is, after the src attribute is set.
play():void

4. Summary

This article briefly describes how to use OpenHarmony for multi-device development, and demonstrates the application of technical features such as NAPI, eTS UI, UI management state @state, and audio playback. Through these diverse functions, we finally implement a metal detector sample . The rich and diverse OpenHarmony development samples are inseparable from the contributions of partners and developers. If you also want to share your own development samples, you are welcome to submit the samples to the OpenHarmony Knowledge System SIG warehouse to realize the development samples together. Build together.

5. Reference links

Silky experience OpenHarmony standard system on RK3568 development board
https://gitee.com/openharmony-sig/knowledge_demo_smart_home/tree/master/dev/docs/rk3568_quick_start
Compile OpenHarmony project on Windows
https://gitee.com/Cruise2019/team_x/blob/master/homework/ohos_build_win/readme.md
OpenHarmony NAPI Learning Documentation
https://gitee.com/javen678/hello-ohos-napi/tree/master/doc#/javen678/hello-ohos-napi/blob/master/doc/1.HelloNAPI.md
OpenHarmony's declarative development paradigm based on eTS extensions
https://gitee.com/openharmony/docs/blob/master/en-us/application-dev/reference/arkui-ts/Readme-CN.md
Audio playback development guide
https://gitee.com/openharmony/docs/blob/master/en-us/application-dev/media/audio-playback.md
Develop metal detector applications from scratch
https://gitee.com/openharmony-sig/knowledge_demo_temp/blob/master/docs/metal_detection/quick_develop.md
GEEKROS official website
https://www.geekros.com/


OpenHarmony开发者
160 声望1.1k 粉丝

OpenHarmony是由开放原子开源基金会(OpenAtom Foundation)孵化及运营的开源项目,


引用和评论

0 条评论