The electronic compass is an important navigation tool in modern times, ranging from the navigation of aircraft and ships to the navigation of personal mobile phones. The electronic compass can be said to be closely related to our life and inseparable. Why can an electronic compass indicate the direction? This Demo will present to you, which contains human wisdom and the mystery of nature. This project is divided into data collection end (device end) and effect display end (application end):
1. Compass data acquisition terminal: The Geek_Lite_Board development board is used, which has a built-in three-axis magnetometer AK8963, and the compass data information is obtained by analyzing the magnetometer data. The operating system version is OpenAtom OpenHarmony 3.0 (hereinafter referred to as "OpenHarmony");
2. Compass effect display side: Runhe RK3568 development board is used, and the operating system version is OpenHarmony 3.1 release.
The effect display side reflects the capabilities of OpenHarmony JS UI, Canvas components and NAPI:
1. The Canvas component is a canvas component. After obtaining the canvas object, you can customize the drawing graphics, such as circles, lines, etc. The compass interface on the application side in this project is developed based on the Canvas component;
2. NAPl (NativeAPI) is a JS API implementation mechanism of the OpenHarmony standard system. Through NAPI, JS and C/C++ code can access each other. The application side of this project receives the detection information sent by the device side through NAPI.
When the device application is started, the running effect is shown in the following animation:

1. Basic Principles

The earth is a large magnet. The two poles of the earth are close to the geographic south pole and the geographic north pole. Generally, the magnetic field strength of the earth is about 0.5 Gauss (Gauss is the unit of magnetic field strength).
The Geek_Lite_Board board comes with an AK8963 three-axis magnetometer. The three-axis magnetometer can measure the magnitude of the magnetic force in three directions perpendicular to each other. Usually we lay the sensor flat, that is, let the direction of gravity be perpendicular to the sensor, assuming that the direction of gravity is the z-axis, and the other two axes are the x-axis and the y-axis. In an environment only affected by the earth's magnetic field (ignoring other weak interference), the vector sum of the magnetic data detected on the x-axis and the y-axis is equal to the received earth's magnetic field.
We can use the ratio of the x-axis to the y-axis to determine how far away we are currently facing due north. For example, the measured x-axis data is close to 0.5 Gauss, and the y-axis data is close to 0, and the current x-axis direction is considered to be true north. Which direction is the x-axis? Regarding the x-axis direction, the manufacturer of the sensor chip will predefine the x-axis, y-axis and z-axis direction of the sensor (usually the z-axis perpendicular to the surface of the chip).
data flow

The overall scheme of the smart compass is shown in the figure above, which is mainly composed of the Geek_Lite_Board development board and the Runhe RK3568 development board, which use the communication method of the local area network (router) TCP protocol.

  1. The Geek_Lite_Board development board obtains the magnetic field data through the onboard magnetometer, and the magnetic field data is processed to obtain the angle data;
  2. The angle information is sent to the compass application through the ESP8266 wireless Wi-Fi module;
  3. The compass application side obtains the underlying network data through the NAPI interface and displays it on the page.

    2. Function realization

    Acquisition of Compass Data
    The Geek_Lite_Board development board communicates with the AK8963 three-axis magnetometer through the IIC interface, reads the magnetic field data in the three-axis direction, and obtains the orientation data of the compass after calculating the magnetic field data.
    ● Introduction of AK8963
    The AK8963 adopts high-sensitivity Hall sensor technology and integrates a magnetic sensor for detecting x, y, and z axes, a sensor drive circuit, a signal amplifier, and an arithmetic circuit for processing each sensor signal. At the same time, it is also equipped with a self-test function. Its compact package can also be applied to map navigation of mobile phones equipped with gps to realize functions such as pedestrian navigation.
    ● Reading of AK8963 measurement data
    The AK8963 and the microcontroller are connected through the IIC interface. The microcontroller can operate the IIC bus to read the data of the AK8963 according to the operation sequence of the data manual. The function of the AK8963 to obtain the measurement data is implemented as follows:

 uint8_t Mpu_Read_Bytes(uint8_t const regAddr, uint8_t *pData, uint8_t len)
{
    int i = 0;
    MPU_ENABLE;
    while (SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_TXE) == RESET);
    SPI_I2S_SendData(SPI5, regAddr | 0x80);
    while (SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_RXNE) == RESET);
    SPI_I2S_ReceiveData(SPI5);
    for(i=0; i<len; i++) {
        while(SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_TXE) == RESET);
        SPI_I2S_SendData(SPI5, 0x00);
        while(SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_RXNE) == RESET);
        pData[i] = SPI_I2S_ReceiveData(SPI5);
    }
    MPU_DISABLE;
    return 0;
}

● AK8963 data processing to obtain magnetic data <br>Call Mpu_Read_Bytes function to obtain measurement data, among which the six bytes of data from MPU_BUFF[15] to MPU_BUFF[20] are the data of the magnetometer. At this time, the magnetometer data is not stable and cannot be directly used to calculate the angle of the compass. It also needs to be filtered. The filtering algorithm used here is sliding mean filtering. The data processing code is as follows:

 Mpu_Read_Bytes(MPUREG_ACCEL_XOUT_H, MPU_BUFF, 28);
if(MPU_BUFF[14] == 1) {
  // 从 MPU_BUFF[]中提取磁力数据
    Mpu_Data.mag_x = (MPU_BUFF[16] << 8) | MPU_BUFF[15];
    Mpu_Data.mag_y = (MPU_BUFF[18] << 8) | MPU_BUFF[17];
    Mpu_Data.mag_z = (MPU_BUFF[20] << 8) | MPU_BUFF[19];
  // 对x轴方向磁力计数据进行滤波,取滑动平均
  for(i=0;i<14;i++) {
    mag_x_buff[i] = mag_x_buff[i+1]   //滑动
  }  
    if(Mpu_Data.mag_x > -500 && Mpu_Data.mag_x < 500) {
        mag_x_buff[14] = Mpu_Data.mag_x;
    }
    //取平均值
    Mpu_Calc.mag_x = ( mag_x_buff[0] + mag_x_buff[1] + mag_x_buff[2] \ 
    + mag_x_buff[3] + mag_x_buff[4] + mag_x_buff[5] + mag_x_buff[6] \
    + mag_x_buff[7] + mag_x_buff[8] + mag_x_buff[9] + mag_x_buff[10] \
    + mag_x_buff[11] + mag_x_buff[12] + mag_x_buff[13] 
    + mag_x_buff[14] )/15.0f;
     // 对y轴方向磁力计数据进行滤波,取滑动平均
    for(i=0;i<14;i++){
      mag_y_buff[i] = mag_y_buff[i+1];  //滑动         
    }         
    if(Mpu_Data.mag_y > -500 && Mpu_Data.mag_y < 500){
     mag_y_buff[14] = Mpu_Data.mag_y;
    }
    //取平均值
    Mpu_Calc.mag_y = ( mag_y_buff[0] + mag_y_buff[1] + mag_y_buff[2] \ 
    + mag_y_buff[3] + mag_y_buff[4] + mag_y_buff[5] + mag_y_buff[6] \
    + mag_y_buff[7] + mag_y_buff[8] + mag_y_buff[9] + mag_y_buff[10] \
    + mag_y_buff[11] + mag_y_buff[12] + mag_y_buff[13] 
    + mag_y_buff[14] )/15.0f;
    // 对磁力计z轴方向进行滤波
    mag_z_buff[0] = mag_z_buff[1];
    mag_z_buff[1] = Mpu_Data.mag_z;
    Mpu_Calc.mag_z = (int16_t)((mag_z_buff[0] + mag_z_buff[1])/ 2.0f);
}

● Calculation of angle data <br>The magnetometer data is filtered to obtain the magnetic force components of the three axes of xyz, and the tan values of the x and y axes are calculated, and then the angle is calculated by the arc tangent. The angle is obtained by sliding average and finally needs to be displayed. The compass angle value of , the calculation process is shown in the following code.

 angle_buff[0] = angle_buff[1];
  angle_buff[1] = angle_buff[2];
  angle_buff[2] = ((uint16_t)(atan2((Mpu_Calc.mag_y - Mag_y_OffSet),\
      (Mpu_Calc.mag_x - Mag_x_OffSet)) *180 / PI + 180 ));
  angle = ((uint16_t)((angle_buff[0] + angle_buff[1] + angle_buff[2]) \
      / 3.0 + 0.5));

Transfer of Compass Data
The ESP8266 Wi-Fi module attached to the Geek_Lite_Board development board transmits the angle data to the Runhe RK3568 development board through the local area network TCP communication. The Runhe RK3568 development board obtains the underlying network data through the NAPI interface, parses the angle data from the network data, and then shown on the display.
Display of angle data The display of angle data is realized by Runhe RK3568 development board, which is mainly divided into the drawing of the compass display page and the angle data obtained by NAPI from the local area network and displayed on the interface.
Compass display page <br>The compass display page is mainly completed by drawing the Canvas component, including azimuth angle, compass dial and indicator line. The overall effect of the display is shown in the following figure.

The compass dial is composed of a Canvas component, which includes three parts, namely the dial, the angle numbers, and the orientation text. Their renderings are as follows:
● Dial

● Angle numbers


● Orientation text

For Canvas component related knowledge, please refer to: https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/arkui-js/js-components-canvas-canvas.md
NAPI
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 receives the detection information sent by the device side through NAPI.
Bottom-level NAPI module package ● The module name packaged by this application is tcpserverapi. Download the source code first. The source code path is:
https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/dev/team_x/napi_tcpservermodule/tcpservermodule
● After the download is complete, put it in the root directory of the source code of OpenHarmony 3.1 Release version, and configure the compilation script; after the first compilation is completed, the entire image needs to be programmed, please refer to [New on Development Board | 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
● To modify the module source code later, just send the library to the board. The command is as follows:

 先挂载,再send
hdc_std shell mount -o remount,rw /
hdc_std file send libtcpserverapi.z.so system/lib/module/libtcpserverapi.z.so
应用端导入NAPI模块
import tcpserverapi from '@ohos.tcpserverapi'
应用端NAPI接口调用
//调用initServer接口 初始化 TCP 服务器
tcpserverapi.initServer() 
//调用recvMsg 获取并解析SMT32板子发送过来的角度
tcpserverapi.recvMsg().then((result) => {  
                var resultAngle = result.angle;
                })

For more NAPI related knowledge, please refer to the video course "Native Api for Standard Device Application Development".
https://www.bilibili.com/video/BV1L44y1p7KE/
Application code address
https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/FA/electronic_compass
Device side code address
https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/dev/team_x/electronic_compass
https://gitee.com/geekros/OpenHarmony_For_STM32F427/tree/master/dev-code
Reference document [electronic compass]
https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/electronic_compass_FA
https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/electronic_compass_Dev
【RK3568 guide】
https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld
【Geek_Lite_Board related website】
www.geekros.com
The rich and varied 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.
How to co-build development samples
https://gitee.com/openharmony-sig/knowledge/blob/master/docs/co-construct_demos/README_en.md


OpenHarmony开发者
160 声望1.1k 粉丝

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


引用和评论

0 条评论