ACCELERATION SENSING¶
Sampling can be said to be one of the most important functions of this project. It allows us to collect and store data from sensors for subsequent analysis and processing. Since Arduino performance is very limited, this project uses a method of sampling and storing at the same time to achieve data collection. Since there is no real-time operating system, the storage process will have a certain impact on sampling, so a high sampling frequency cannot be achieved, but due to the demonstration and teaching nature of this project, the sampling frequency does not need to be very high. After testing, a sampling frequency of 200Hz can be fully achieved, and since it is sampling and storing at the same time, the data limit is basically equivalent to the capacity of the SD card.
As shown in the code above, the sampling process is divided into several stages:
-
Sampling start time and end time: This part is completed when the MQTT command callback is called.
-
calling
sensing_prepare()
: called at the preparation state, opens the SD card file and writes the sampling metadata. In this function, aload_log_number()
function is called to load the current log number and use it in the file name. The file name format isN001_001.txt
, whereN001
is the node ID and001
is the log number. There is a file in the SD card that records the current log number, which will automatically increase after the sampling is completed. -
calling
sensing_sample_once()
: called repeatedly in the sampling state, reads the sensor data and writes it to the SD card file. Each sampling will check whether the set sampling rate (sensing_rate_hz
) is reached. If it is reached, the sensor data is read and written to the file. In the main program loop, when the current time minus the last sampling time is greater than or equal to1000 / sensing_rate_hz
, a sampling is performed. The sampling data includes the timestamp and the three axial data of the accelerometer (ax, ay, az), and is written to the SD card file. -
calling
sensing_stop()
: Called at the end of the sampling state, closes the SD card file and prints the sampling results. This function prints the total number of samples and reopens the file content for printing to the serial port.
Info
In this project, since the serial port output speed is very slow, it will drag down the sampling and storage, so during the sampling process, we only do storage without serial port output. After the sampling is completed, the file can be reopened and the content can be printed to the serial port.