[LSM6DSOX finite state machine routine learning four] --4D detection (FourD position recognition)
[Copy link]
The previous article has introduced the ST official LSM6DSOX example library file structure and the introduction of each file in detail. At the end, there is a simple question: if the direction of the device sensor installation is not the positive direction of the Z axis towards the panel, but the positive direction of the X axis, then how should we modify the configuration to realize the action of identifying the flip device?
When we design the device, the posture of the PCB and the sensor will be different from the posture of the device in use, so it will cause the program to design the sensor Z axis to be triggered when it is buckled on the table, but when the device is buckled on the table, the sensor X axis is buckled on the table. So how to deal with it? In fact, it is very simple. In the previous article, it has been mentioned that the Mask register selects the input signal, so you only need to select the +X axis as the input, that is, change the value of MaskA to 0x80.
This article will analyze and explain the 4D detection ( FourD position recognition ) routine.
Let's first use a picture from the official tutorial to explain 4D detection. In fact, 4D here does not mean 4 dimensions ... It is just the recognition of four directions after the two-dimensional coordinate system composed of the X- axis and the Y- axis is rotated. Therefore, based on the following figure, the recognition process is analyzed. If the Y -axis acceleration is close to the gravity acceleration g in the opposite direction , it is the first posture; otherwise, it is the second posture; if the X -axis acceleration is close to g in the opposite direction , it is the third posture, otherwise, it is the fourth posture. The overall recognition idea is still easy to understand.
Next, let's take a look at the official routine. Import the parameters as in the previous chapter, and you can see the state machine code:
This routine also has 7 statements, but some new instructions appear. Next, let's analyze the functions of each statement one by one.
S0 : CMD MSKITEQ
The first one is an instruction. According to the instruction set description, the function of this instruction is to block the interrupt when the OUTC register is updated but the value is unchanged. The function of this instruction is connected to S6 . S6 will be explained later.
S1 : CMD SRP
This was mentioned in the previous chapter. Set the reset point pointer to the next one, which is S2 , so that the subsequent judgment conditions will return to S2 after being reset.
S2 : RNC NOP GNTH1
Judgment condition, GNTH1 was also mentioned in the previous chapter. The input data will be compared with the threshold Thresh1. If it is greater than, S3 will be executed, otherwise it will return to S2.
S3: RNC LNTH1 TH3
Judgment condition: after the previous line jumps to here, LNTH1 comparison will be performed, that is, if the input data is less than the threshold Thresh1, it will return to S2 , otherwise it will jump to S4 after persisting for the time period of Timer3 .
S4 CMD OUTC
This is a command instruction. The function of the OUTC command is to update the temporary mask value to the OUTS output register and generate an interrupt.
Here is an additional knowledge point. You can see that the value of MakA is 0xF0 . From the previous chapter, we can know that at this time, the positive direction of the X-axis, the negative direction of the X-axis, the positive direction of the Y-axis, and the negative direction of the Y-axis are selected as the input values . Then the previous comparison instruction will compare the four directions with the threshold. As long as the value passes the comparison, the corresponding bit of the temporary mask will be set to 1. When it runs to OUTC , the value will be assigned to the OUTS register. The phenomenon shown is what the action diagram above shows. Different postures correspond to the four values 0x01, 0x02, 0x04, and 0x08 .
After executing S4, continue to execute S5.
S5 RNC NOP LNTH2
The continuation judgment condition of this instruction is LNTH2 , that is, if the axis data identified in the previous instruction is less than the threshold 2 , then continue to execute S6 , otherwise, return to S2.
S6 CMD CONTROL
This command CONTROL : has several functions: 1. Restore all temporary values to the default state, such as the reset point value set by SRP , the temporary value compared by TH3 , and the temporary mask value. 2. End the loop and jump back to the default reset point. At this time, an interrupt will be generated, but the interrupt here is blocked by the function connected to the S0 instruction. The program is restored to the initial state, and the interrupt and OUTS are unchanged.
The overall state machine flow chart is as follows:
Combining the above action diagram, flow chart and instruction decomposition, I believe you will have a clearer understanding of the workflow of this routine.
Based on the above process, it is not difficult to analyze that as long as the value of MakA is changed to 0xFC , 6 directions can be identified .
|