1. Introduction
On a sunny and sunny afternoon, the coders were all focused on coding as usual. Suddenly the little brother in front stood up, held the development board in his hand, and punched back and forth. How is this going?
It turns out that this is an interactive boxing game, and this article will take you to unravel the mystery. Developers can not only experience the joy of learning knowledge, but also enjoy the fun of fitness. Relying on the OpenAtom OpenHarmony (hereinafter referred to as "OpenHarmony") 3.2 Beta1 operating system, the sample is divided into two parts: the application side and the device side. This article mainly introduces the implementation of the device side, and will share the development of the application side later.
Device side: Use BearPi-HM Nano (Hi3861) development board to process accelerometer sensor data.
Application side: Runhe DAYU200 (RK3568) development board is used, which mainly deals with display and sound effects.
As shown in the figure below, the left side is the device side, and the right side is the application side: the developer holds the bear pie development board on the device side, observes the screen, and completes the punching action at the specified time according to the APP display on the application side; the punching information is transmitted wirelessly On the application side, the application side APP has corresponding scoring rules for the timing of punching, and finally counts the total score.
2. Principle
Compared with the normal state, the punching action will cause a larger acceleration change of the arm. According to this feature, we use the expansion module E53_SC2 of the BearPi-HM_Nano development board, which integrates the MPU6050 sensor inside and can read the magnitude of the acceleration.
Do the punching motion experiment, statistical data, and get the threshold of acceleration when punching. When the program is executed, the real-time data is compared with the threshold value to determine whether the punching action is triggered. Then through wireless communication, the data is sent to the application in real time.
3. Instructions for using the accelerometer sensor
The key to the development of the device side is the use of the accelerometer sensor, which mainly involves two points: 1. The understanding of the gravitational acceleration g; 2. How to convert the data of the MPU6050 register into unit data?
1. The acceleration sensor used in the sample is MPU6050, which has four ranges of ±2g, ±4g, ±8g and ±16g to choose from. A g refers to a gravitational acceleration, representing a magnitude of 9.8 m/s². For example: if the device is dropped from a height, the acceleration measured by its accelerometer will be 0g, because the sensor is not squeezed by force and is in a weightless state; if the device is placed horizontally on a table, the accelerometer will measure The acceleration is 1g (9.8 m/s²), we can understand it as a pressure of 1g;
2. MPU6050 adopts 16-bit ADC sampling. What does 16-bit ADC sampling mean? For example: if the range selection (selected by the register) is ±2g, the 16-bit ADC sampling means that 65536 (ie 2 to the 16th power) are used to express the situation from -2 to +2g. The screenshot of the datasheet below shows that AFS_SEL=0 means ±2g range. When the data in the data register is 16384, it corresponds to a force of 1g. For example: the value read by the data register is X, and the magnitude of the corresponding force is Y, then Y=X/16384, and the unit is g.
Fourth, code analysis
The device-side code is mainly divided into two threads: 1. Sensor data processing thread; 2. TCP communication thread; they communicate synchronously through events.
1. Description of the main functions of the sensor data processing thread:
//E53_SC2模块MPU6050传感器数据处理主要流程
static void DataHandleTask(void)
{
uint8_t ret;
ret = E53SC2Init();//MPU6050传感器初始化及配置,配置为+—8g量程
if (ret != 0) {
printf("E53_SC2 Init failed!\r\n");
return;
}
while (1) {
ret = E53SC2ReadData(&data);//MPU6050传感器寄存器数据读取
if (ret != 0) {
printf("E53_SC2 Read Data!\r\n");
return;
}
AccDataHandle(&data);//MPU6050传感器数据处理,转化为单位为g的数据
if (myCaldata.Accel[ACCEL_X_AXIS] < 0) {
myCaldata.Accel[ACCEL_X_AXIS] = myCaldata.Accel[ACCEL_X_AXIS] * -1.0;
}
if (myCaldata.Accel[ACCEL_Y_AXIS] < 0) {
myCaldata.Accel[ACCEL_Y_AXIS] = myCaldata.Accel[ACCEL_Y_AXIS] * -1.0;
}
if (myCaldata.Accel[ACCEL_Z_AXIS] < 0) {
myCaldata.Accel[ACCEL_Z_AXIS] = myCaldata.Accel[ACCEL_Z_AXIS] * -1.0;
}
//判断实时数据是否大于拳击阈值Boxing_ACC,大于则设置事件
if (myCaldata.Accel[ACCEL_X_AXIS] > Boxing_ACC || myCaldata.Accel[ACCEL_Y_AXIS] > Boxing_ACC || myCaldata.Accel[ACCEL_Z_AXIS] > Boxing_ACC) {
printf("MPU set flg\r\n");
osEventFlagsSet(g_eventFlagsId, FLAGS_MSK1);//触发拳击事件
}
usleep(Delay_10ms);
}
}
#define MAX_POS_NUM 32767
#define LSB 4096.0
//MPU6050传感器数据处理,转化为单位为g的数据
int AccDataHandle(E53SC2Data *dat)
{ //量程为+-8g,所以分辨率为4096
if (dat->Accel[ACCEL_X_AXIS] < MAX_POS_NUM) {
myCaldata.Accel[ACCEL_X_AXIS] = dat->Accel[ACCEL_X_AXIS]/LSB;
} else {
myCaldata.Accel[ACCEL_X_AXIS] =(-1)* (dat->Accel[ACCEL_X_AXIS]-MAX_POS_NUM)/LSB;
}
if (dat->Accel[ACCEL_Y_AXIS] < MAX_POS_NUM) {
myCaldata.Accel[ACCEL_Y_AXIS] = dat->Accel[ACCEL_Y_AXIS]/LSB;
} else {
myCaldata.Accel[ACCEL_Y_AXIS] = (-1)*(dat->Accel[ACCEL_Y_AXIS]-MAX_POS_NUM)/LSB;
}
if (dat->Accel[ACCEL_Z_AXIS] < MAX_POS_NUM) {
myCaldata.Accel[ACCEL_Z_AXIS] = dat->Accel[ACCEL_Z_AXIS]/LSB;
} else {
myCaldata.Accel[ACCEL_Z_AXIS] =(-1)*(dat->Accel[ACCEL_Z_AXIS]- MAX_POS_NUM)/LSB;
}
return 0;
}
2. Description of the main functions of the TCP communication thread:
In the network communication of this example, the BearPi-HM Nano (Hi3861) development board is used as the client, and the Runhe DAYU200 (RK3568) development board is used as the server. The TCP mechanism is used to communicate between them.
The following code: After the TCP communication is established, the communication thread is blocked in the normal state. When the boxing event is triggered, it will send information to the server:
static void TCPClientTask(void)
{
// 在sock_fd 进行监听,在 new_fd 接收新的链接
int sock_fd;
uint32_t flags;
struct sockaddr_in send_addr; // 服务器的地址信息
socklen_t addr_length = sizeof(send_addr);
char recvBuf[recvLen];
memset(recvBuf, '\0', sizeof(recvBuf));
// 连接Wifi
WifiConnect(CONFIG_WIFI_SSID, CONFIG_WIFI_PWD);
// 创建socket
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("create socket failed!\r\n");
exit(1);
}
// 初始化预连接的服务端地址
send_addr.sin_family = AF_INET;
send_addr.sin_port = htons(CONFIG_SERVER_PORT);
send_addr.sin_addr.s_addr = inet_addr(CONFIG_SERVER_IP);
addr_length = sizeof(send_addr);
//连接
connect(sock_fd,(struct sockaddr *)&send_addr, addr_length);
printf("TCPClient connect success\r\n");
while (1) {
memset(recvBuf, '\0', sizeof(recvBuf));
//等待事件是否触发
flags = osEventFlagsWait(g_eventFlagsId, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);
printf("TCP get flag\r\n");
sprintf(sendbuf,"right\r\n");
send(sock_fd, sendbuf, strlen(sendbuf), 0);//tcp发出触发信息
// 线程休眠一段时间
usleep(Delay_100ms);//100ms
}
closesocket(sock_fd);
Five, code construction, compilation and burning
1. OpenHarmony 3.2 Beta1 source code download, refer to the link at the end of the article for the address;
2. In the vendor directory under the source code root directory, create a new team_x folder;
3. Copy the boxing folder to the team_x directory, as shown in the following figure:
4. In the source code directory, enter hb set, and then select the current file path, that is, enter . (dot), and then use the arrow keys to select boxing under team_x, as shown below:
5. Enter hb build -f to start compiling. After the compilation is successful, Hi3861_wifiiot_app_allinone.bin will be generated in the out/bearpi_hm_nano/boxing directory under the root directory, as shown below:
6. Use the HiBurn tool to burn the program, and the reference link for burning is at the end of the article;
After the burning is successful, you can locally verify whether the project is successful:
1. Use the network debugging assistant software on the computer to establish a TCP server. The following points need to be paid attention to when establishing a server on the computer:
(1) The computer and the BearPi-HM Nano development board are connected to the same Wi-Fi hotspot, as shown in the figure: both the computer and the development board are connected to the hotspot "YYYYYY";
(2) The IP set by the BearPi-HM Nano development board program, the IP of the computer, and the IP of the network debugging assistant server should be consistent, as shown in the following figure "192.168.1.100";
(3) Click the "Connect" button of the network debugging assistant, that is, start the server first.
2. Connect the serial port of the BearPi-HM Nano development board to the computer, and set the baud rate to 115200;
3. Reset the BearPi-HM Nano development board. After reset, the serial port will print information such as successful Wi-Fi connection and successful TCP connection, as shown in the figure below (right side);
4. Hold the development board and try to punch (that is, swing the development board). You can see the TCP server window of the network assistant, and successfully receive the synchronous fist information "right", as shown in the following figure (left side):
6. Summary
This article mainly describes the development of the device side in the boxing interactive game, using the BearPi-HM Nano (Hi3861) development board hardware, and doing secondary development on the basic routines of the BearPi. This device-side development uses the basic knowledge of OpenHarmony's threads, events, GPIO, IIC, TCP communication, etc., combined with the use of accelerometer sensors, to achieve the function of synchronous interaction with the application side.
This sample is a sample shared by the OpenHarmony Knowledge System Working Group (related links at the end of the article) for developers. Combined with daily life, the knowledge system working group has planned demo samples of various scenarios for developers, such as smart home scenarios, audio-visual entertainment scenarios, sports and health scenarios, etc. Developers are welcome to participate in the development of OpenHarmony and improve the samples. Learn from each other and progress from each other.
7. Reference link
This sample code download link:
https://gitee.com/openharmony-sig/knowledge_demo_entainment/tree/master/dev/team_x/boxing
OpenHarmony knowledge system to build a development warehouse:
https://gitee.com/openharmony-sig/knowledge/blob/master/docs/co-construct_demos/README_en.md
OpenHarmony Learning Path:
https://growing.openharmony.cn/mainPlay/learnPath
BearPi-HM Nano development board learning path:
https://growing.openharmony.cn/mainPlay/learnPathMaps?id=19
https://gitee.com/bearpi/bearpi-hm_nano/tree/master
Runhe DAYU200 (RK3568) development board introduction:
https://gitee.com/hihope_iot/docs/blob/master/HiHope_DAYU200/docs/README.md
https://growing.openharmony.cn/mainPlay/learnPathMaps?id=27
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。