Overall thinking
According to the relevant data, if the acceleration is divided into three directions: X, y and Z, the Z axis is perpendicular to the ground, and the X and Y axes are parallel to the ground, the acceleration in the Z axis direction is about equal to the gravitational acceleration g under normal conditions; In case of falling behavior, the center of gravity offset will lead to a rapid decrease in the acceleration in the z-axis direction, and it can be approximately equal to 0.5 when lying flat
Therefore, the first threshold is the judgment of Z-axis acceleration, If it is less than a set value (set to 5, modifiable), then go to the next step of judgment. According to the data consulted, most people fall within 6 seconds, so the second threshold is a delay judgment. If it is still lower than the threshold after a specific time, it will enter the third judgment; human daily behavior can also lead to the decrease of Z-axis acceleration, but the fall behavior accelerates The speed of degree reduction is much faster than daily behavior, so the third threshold is the differential operation of acceleration. Set marks for the three thresholds respectively. If the above three conditions are met at the same time, the product of the marks is 1, and then the alarm is triggered; Otherwise, it will not be triggered.
Concrete implementation
Threshold I (acceleration judgment)
The first threshold judgment is relatively simple. You only need to use circular statements:
Threshold 2 (delay judgment)
When the conventional delay function is used, the whole program will usually enter the pause state. Therefore, after the specific delay time, the collected acceleration data is still the data before the delay, which can not meet the goal of this algorithm; Therefore, goto begin function is used to replace the traditional delay function. Firstly, a counter can be set. As soon as the threshold meets the conditions, goto begin function is used to return to the starting point of the program, and the collection of acceleration data is not suspended; At the same time, the counter automatically decreases to a specific value, and then enters the judgment of threshold 2. Because the goto begin environment includes the interval for obtaining acceleration data, it is equivalent to using the operation of space instead of timing change. The delay time can be controlled by modifying the counter (goto begin execution times). In the experiment, the data is collected every 0.1 seconds, and the fall delay is judged as 5 seconds, which is realized by executing the goto begin function 50 times.
Threshold three (back check difference)
This part of the algorithm is the most complex. Because to perform differential operation on the acceleration for a period of time before and after reaching the threshold value 1, in fact, it is necessary to perform differential operation on all data, that is, make a difference between the collected data and the previous data, take the absolute value, and then mark the data that separately meets the threshold value 3. During the back inspection, it can judge whether it meets the threshold value 3 according to the mark. According to the experiment, it is appropriate to set the absolute value of the front and rear acceleration difference to about 0.2g. As for the selection of the time period before and after, according to the situation of many experiments, it is more appropriate to delay 1 / 2 times (2.5 seconds) before and after the falling behavior. In addition, considering the memory problem of the single chip microcomputer, the array storing relevant data is refreshed circularly, and the original array is overwritten from 0 every 1000 data to prevent data overflow.
Core code
void FallenDetecting(void) { while(1) { begin: delay_ms(100);//Delay 100ms //Get acceleration data (saved in accx/y/z) JY901_Get_Accelerometer(); ct++; if (ct >= 999) { for (int kk = 1; kk < 1000; kk++) { io[kk] = 0;//initialization } ct = 1; } z[ct] = aacz; z[0] = 10; cf[ct] = ABS(z[ct-1] - z[ct]);//The absolute value of the last data minus the next data if (cf[ct] > 2)//The data needs to be tested. 2 represents the absolute value of the difference between the two accelerations. { io[ct] = 1;//Full registration of acceleration difference exceeding the standard } if (1) { //Analysis of abnormal acceleration judgment (condition 1) if (z[ct] < 5) { mpu_1_flag = 1; } else { mpu_1_flag = 0; } //Delay function (condition 2) if( (mpu_1_flag == 1)&&((yy--)>0)) { printf("yy=%d",yy); goto begin; }//Execute 100 times if (mpu_1_flag == 1)//Still stubborn (I haven't stood up after 100 times) { mpu_2_flag = 1; } else mpu_2_flag = 0;//Standing up means you didn't fall. //The third stage (used to judge slow or fast) if (mpu_2_flag == 1) { //Differential operation (condition 3) if (ct < 999) { mpu_3_flag = 0; for (int ii = ct-TimDelay/2*3; ii <= ct-TimDelay/2; ii++) { if (io[ii] == 1) { mpu_3_flag = 1; } } } //Comprehensive judgment abnormality if (mpu_2_flag * mpu_3_flag * mpu_1_flag) { mpu__flag = 1; //Field alarm LED = 0;//Light up BEEP = 1;//Buzzer alarm // //Send alarm SMS // SendMessage(); } else mpu__flag = 0; //Automatic alarm release procedure if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)==0 && aacz>7) { //Disarm the alarm (the old man was helped up) LED = 1;//Turn off the lights BEEP = 0;//Release buzzer alarm printf("baojingjiechu"); } mpu_2_flag = 0; mpu_1_flag = 0; mpu_3_flag = 0; yy = TimDelay; } } }