vuoi
o PayPal
tutte le volte che vuoi
The system then enters a continuous loop, checking if the Timer Interrupt has occurred. When
this happens, the system reads the data from the three sensors and sends it via UART.
We then moved on to the configuration via the graphical interface of STM32CubeMX:
• Install the STMicroelectronics.X-CUBE-MEMS1 package and enable the IKS01A3 Board
Extension, which includes the specific drivers for the board and allows you to interface
with its sensors through the microcontroller and configure the I2C communication
protocol to use the I2C1 bus, which is required to communicate with the board sensors
• Enable I2C1 communication and manually configure PINB8 to I2C1_SCL and PINB9 to
I2C1_SDA, as they are not pins by default
• Timer frequency set to 84MHz by the "Clock Configuration";
• Enabling channel 1 of Timer 3, enabling the Interrupt and calculating and setting PSC
and ARR: 1 84
= = =
( (839
500 + 1) ∙ + 1) ∙ 50000
• Enabling and Configuring USART2
• Enabling the USART2 Interrupt
We have identified the following functions to use:
• IKS01A3_MOTION_SENSOR_Axes_t : structure for storing data on the X, Y and Z axes
of the sensors;
• IKS01A3_MOTION_SENSOR_Init(): initializes a specific sensor (accelerometer,
gyroscope, or magnetometer) so that it can be used;
• IKS01A3_MOTION_SENSOR_Enable(): Physically enables the specified sensor to begin
data collection;
• HAL_TIM_Base_Start_IT(): Starts a timer in Interrupt mode;
• HAL_UART_Transmit_IT(): Sends data via UART in interrupt mode.
• HAL_TIM_PeriodElapsedCallback(): A callback function that is automatically
executed whenever the timer generates an interrupt.
• IKS01A3_MOTION_SENSOR_GetAxes(): Reads the X, Y, and Z values of a specific
sensor (accelerometer, gyroscope, or magnetometer) and stores them in the
corresponding data structure.
• HAL_UART_TxCpltCallback(): Callback is automatically executed when the UART
transmission is completed.
And then we moved to writing the code:
#include "main.h"
#include "string.h"
#include <stdio.h>
#include "iks01a3_motion_sensors.h"
#define LSM6DSO_INSTANCE 0 // Gyroscope
#define LIS2DW12_INSTANCE 1 // Accelerometer
#define LIS2MDL_INSTANCE 2 // Magnetometer
// Structures to store sensor data
IKS01A3_MOTION_SENSOR_Axes_t accel_data, gyro_data, mag_data;
IKS01A3_MOTION_SENSOR_Axes_t axes;
// Buffer for sending data
char uart_buffer[128];
int get = 0;
volatile int correctlySentData = 0;
// Initialize the sensors
IKS01A3_MOTION_SENSOR_Init(LIS2DW12_INSTANCE, MOTION_ACCELERO);
IKS01A3_MOTION_SENSOR_Init(LSM6DSO_INSTANCE, MOTION_GYRO);
IKS01A3_MOTION_SENSOR_Init(LIS2MDL_INSTANCE, MOTION_MAGNETO);
// Enable the sensors
IKS01A3_MOTION_SENSOR_Enable(LIS2DW12_INSTANCE, MOTION_ACCELERO);
IKS01A3_MOTION_SENSOR_Enable(LSM6DSO_INSTANCE, MOTION_GYRO);
IKS01A3_MOTION_SENSOR_Enable(LIS2MDL_INSTANCE, MOTION_MAGNETO);
// Start TIM3 timer in interrupt mode
HAL_TIM_Base_Start_IT(&htim3);
int main(void)
{ // Start TIM3 timer in interrupt mode
HAL_TIM_Base_Start_IT(&htim3);
/* Infinite loop */
while (1)
{ if (get == 1) {
get = 0;
// Format the data into a string
int len = snprintf(uart_buffer, sizeof(uart_buffer),
"ACCEL: %ld %ld %ld\nGYRO: %ld %ld %ld\nMAG: %ld %ld %ld\n",
accel_data.x, accel_data.y, accel_data.z,
gyro_data.x, gyro_data.y, gyro_data.z,
mag_data.x, mag_data.y, mag_data.z);
// Transmit the data via UART
HAL_UART_Transmit_IT(&huart2, (uint8_t *)uart_buffer, len);
if (correctlySentData == 1) {
correctlySentData = 0;
}
}
}
}
// Callback for Timer 3 interrupt
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM3) { // Timer 3
// Read data from the sensors
IKS01A3_MOTION_SENSOR_GetAxes(LIS2DW12_INSTANCE, MOTION_ACCELERO, &accel_data);
IKS01A3_MOTION_SENSOR_GetAxes(LSM6DSO_INSTANCE, MOTION_GYRO, &gyro_data);
IKS01A3_MOTION_SENSOR_GetAxes(LIS2MDL_INSTANCE, MOTION_MAGNETO, &mag_data);
get = 1;
}
}
// Callback for UART transmission complete
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{ correctlySentData = 1; // Set the transmission complete flag
}
Note: The iks01a3_motion_sensors.h library is essential for code to work, as it provides the
necessary functions to manage motion sensors. An important aspect is the association
between the sensors and their indices defined in the library, which specify how each sensor
should be used (for example, as a gyroscope, accelerometer, or magnetometer). This
configuration is defined in the implementation file iks01a3_motion_sensors.c, where the way
in which the sensors should operate is determined.
We consulted the sensor manuals to understand the meaning of the numbers obtained:
• Magnetometer in µT
• Accelerometer in mg (with g gravitational acceleration)
• Gyroscope in milli °/s
Finally, we checked the correct functioning of each sensor:
• Accelerometer: we moved the board in the 3 directions x, y and z, X being the axis
parallel to the longest section of the board, Y the axis parallel to the shortest section of
the board and Z the axis perpendicular to the plane of the board. Below, the results of
the acquired data are shown in a Matlab graph:
• Magnetometer: we approached and moved away a smartphone to check the variation
of the magnetic field. The results obtained are presented below:
• Gyroscope: We rotated the board to see the angular velocity changes. Below, the
results:
From the values we can understand the correct functioning of the program, in particular
between t = 3 and t = 4 we have kept the Nucleo still in a horizontal position and all the values
remain constant, to underline the acceleration along the z axis which corresponds to g, the
gravitational acceleration.
5.2 Read environmental sensors data
In this exercise we have to repeat what we did in the previous exercise with environmental
sensors, in particular:
• HTS221 as humidity sensor
• LPS22HH as pressure sensor
• STTS751 as temperature sensor
Let's move on to the flowchart:
It’s the same flowchart as in the previous exercise but with environmental sensors: pressure,
temperature and humidity.
The configuration of the STM32CubeMX is identical to the previous one, so we can directly move
on to the functions used:
• IKS01A3_ENV_SENSOR_Init(): initializes the specified sensor for the measurement of
the required data type (e.g., humidity, pressure, temperature);
• IKS01A3_ENV_SENSOR_Enable():enables the specified sensor;
• IKS01A3_ENV_SENSOR_GetValue(): Reads the value measured by the specified
sensor;
• HAL_TIM_Base_Start_IT(): Starts the specified timer in Interrupt mode;
• HAL_TIM_PeriodElapsedCallback(): callback automatically executed at the end of the
period set in the Timer;
• HAL_UART_Transmit_IT(): transmits data via UART;
• HAL_UART_TxCpltCallback(): Callback called automatically when the UART
transmission is completed.
From here, we wrote the code:
#include "main.h"
#include "iks01a3_env_sensors.h"
#include "string.h"
#include <stdio.h>
#define HTS221_INSTANCE 0 // Humidity sensor
#define LPS22HH_INSTANCE 1 // Pressure sensor
#define STTS751_INSTANCE 2 // Temperature sensor
float temperature;
float pressure;
float humidity;
volatile int correctlyDataSent = 0;
char uart_buffer[128];
int get = 0;
int main(void)
{ // Initialize the sensors
IKS01A3_ENV_SENSOR_Init(HTS221_INSTANCE, ENV_HUMIDITY);
IKS01A3_ENV_SENSOR_Init(LPS22HH_INSTANCE, ENV_PRESSURE);
IKS01A3_ENV_SENSOR_Init(STTS751_INSTANCE, ENV_TEMPERATURE);
// Enable the sensors
IKS01A3_ENV_SENSOR_Enable(HTS221_INSTANCE, ENV_HUMIDITY);
IKS01A3_ENV_SENSOR_Enable(LPS22HH_INSTANCE, ENV_PRESSURE);
IKS01A3_ENV_SENSOR_Enable(STTS751_INSTANCE, ENV_TEMPERATURE);
// Start timer with interrupt
HAL_TIM_Base_Start_IT(&htim3);
while (1)
{ if (get == 1){
get = 0;
snprintf(uart_buffer, sizeof(uart_buffer), "Temperature: %.2f C, Humidity: %.2f %%,
Pressure: %.2f hPa\r\n", temperature, humidity, pressure);
HAL_UART_Transmit_IT(&huart2, (uint8_t*)uart_buffer, strlen(uart_buffer));
}
if (correctlyDataSent == 1) {
correctlyDataSent = 0;
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{ if (htim->Instance == TIM3)
{ // Read humidity from HTS221
IKS01A3_ENV_SENSOR_GetValue(HTS221_INSTANCE, ENV_HUMIDITY, &humidity);
// Read pressure from LPS22HH
IKS01A3_ENV_SENSOR_GetValue(LPS22HH_INSTANCE, ENV_PRESSURE, &pressure);
// Read temperature from STTS751
IKS01A3_ENV_SENSOR_GetValue(STTS751_INSTANCE, ENV_TEMPERATURE, &temperature);
get = 1;
}
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{ correctlyDataSent = 1;
}
NOTE: Variables must be float in order to correctly read the value of the sensors in the function
IKS01A3_ENV_SENSOR_GetValue().
We plotted on Matlab the values that the PC received via serial communication.
To try to alter the values, we blew on the sensor and then brought it back to normal conditions.
• °):
Temperature sensor (in
• Humidity sensor (%):
• Pressure sensor (in hPa):
There was a notable change in both temperature and humidity, which followed a similar trend,
while the pressure remained constant, aligning with typical atmospheric pressure values for
areas above sea level.
5.3 and 5.4 Filtering data with the Moving Average FIR and Filters
comparison
In these exercises, our focus is on the accelerometer. We have to collect accelerometer data at intervals
of 10ms, apply three different filters to the data, and analyse their impact. The three filters are:
• Moving Average with 5 coefficients;
• Moving Average with 5 coefficients;
• IIR Low Pass filter.
The Moving Average Filter is a simple and effective linear digital filter that is widely used for
filtering noise in signals. His main idea is to average a defined number of consecutive samples
to generate a new filtered value.
The general formula for a moving average filter is:
−1
1
[] = ∑ [ + ]
=0
IIR Filters is a type of digital filter that uses both the past values of the input signal and the
output signal to generate the response.
The equation for the IIR low pass filter is:
[] = . [] + [ − 1]
0 1
with a =1-α , b = α and α= −2
0 1
First, we designed the flowchart:
The flowchart is similar to the previous ones, we just add data filtering during the main loop.
- Risolvere un problema di matematica
- Riassumere un testo
- Tradurre una frase
- E molto altro ancora...
Per termini, condizioni e privacy, visita la relativa pagina.