Challenge

Project 2 - Integrate following sensors to the Dev Board

Project 2 - Integrate following sensors to the Dev Board

Integrate following sensors to the Dev Board:

 GPS

 IMU Sensor

Achieve the following task with the help of these sensors:

1. The microcontroller should automatically detect sensor’s (GPS + IMU) connection and disconnection and print suitable string over the terminal (Serial to USB used)

2. You should use buffer for receiving GPS data over UART, and should enable UART Reception Interrupt.

3. IMU data to come over I2C, you should carry this data in Separate Queue according to Sensor (Accelerometer + Gyro) to a particular task.

4. Main Objective is to identify the GPS location and whenever there is a change in distance greater than 100m, the system should print out the Co-ordinates along with IMU sensor values.

5. Printing of the values can be done by transferring values over UART and then Serial to USB can be used to view at Terminal Window.

Aim:

To integrate an Inertial Measurement Unit (IMU) with a microcontroller and classify data coming from the accelerometer and gyroscope. The goal is to send the respective sensor data to separate queues and use a dedicated task to identify the data source using Queue Sets. Finally, the project transfers the data to the appropriate UART channel for monitoring and analysis.

Tools and components Used:

1. STM32CubeIDE for writing and compiling the code,

2. STM32CubeMX for configuring the microcontroller peripherals, and

3. Hercules for monitoring the UART output,

4. STM32F446RE Nucleo64 Board,

5. MPU6050 Sensor module.

Theory:

The MPU6050 is initialized via I2C for acquiring Accelerometer and Gyroscope data. GPS data is received over UART using the USART1 interface. Two separate queues are created for storing Accelerometer and Gyroscope data. A Queue Set is utilized to wait for data from either queue. UART interrupts are used for GPS data reception.

Code:

Note: click the below URL to download complete Project ZIP file

URL:https://drive.google.com/drive/folders/1AVRtF4cXikpa-38bRgrUWakd90VT0y8s?usp=sharing

CODE EXPLANATION:

 Initialization:

 MCU, GPIO, I2C, and UART peripherals are initialized.

 MPU6050 module is configured for accelerometer and gyroscope parameters.

 Task Creation:

 Three tasks are created:

 DataAcquisitionTask: Acquires and logs Accelerometer and Gyroscope data.

 DataProcessingTask: Processes and logs data from the accelerometer and gyroscope queues.

 GPS_ProcessDataTask: Processes received GPS data.

 Data Acquisition Task:

 Acquires accelerometer and gyroscope data.

 Logs data over UART.

 Data Processing Task:

 Waits for data in the Queue Set.

 Processes and logs accelerometer and gyroscope data.

 GPS Process Data Task:

 Monitors GPS data reception flag.

 Processes and prints GPS data.

 Calculates distance change using Haversine formula.

 Prints coordinates and IMU sensor values if the distance change is greater than 100m.

EXPECTED OUTPUT:

1. Initial data from GPS:

UART initialized

[0] Sent Accelerometer: X= 0.1, Y= -0.2, Z= 9.8

[1] Sent Gyroscope: X= 2.5, Y= -1.0, Z= 0.3

[2] Received Accelerometer: X= 0.1, Y= -0.2, Z= 9.8

[3] Received Gyroscope: X= 2.5, Y= -1.0, Z= 0.3

GPS Data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47

Distance Change: 0.0m, Latitude: 48.117300, Longitude: 11.516700, Altitude: 545.4,

Accelerometer: X= 0.1, Y= -0.2, Z= 9.8, Gyroscope: X= 2.5, Y= -1.0, Z= 0.3

2. Subsequent data after a simulated distance change:

[GPS] Received Data: $GPGGA,34.0535,-118.2420,100

[GPS] Distance Change: 150.05m

[IMU] Accelerometer: X= 0.1, Y= -0.2, Z= 9.8

[IMU] Gyroscope: X= 1.5, Y= 2.0, Z= -0.5

[INFO] Distance Change > 100m. Coordinates: Lat= 34.0535, Lon= -118.2420, Alt= 100.0

[INFO] Accelerometer: X= 0.1, Y= -0.2, Z= 9.8, Gyroscope: X= 1.5, Y= 2.0

CONCLUSION:

The code successfully integrates GPS and IMU sensors, demonstrating accurate data acquisition and processing. The implementation ensures proper sensor connections and effectively detects and reacts to significant changes in distance.

KEY leanings:

The project provides a practical example of integrating sensors, specifically an Inertial Measurement Unit (IMU) with an accelerometer, gyroscope and GPS, into a FreeRTOS-based embedded system. The key learnings from this project include:

1. Sensor Integration:

 Learned how to integrate and manage different sensors, such as GPS and IMU, in a microcontroller-based system.

2. Interrupt-driven UART Communication:

 Gained experience in setting up and handling UART communication with interrupt-driven reception, enhancing the efficiency of data transfer.

3. Multitasking with FreeRTOS:

 Explored the implementation of multitasking using FreeRTOS, organizing tasks for data acquisition, processing, and GPS handling concurrently.

PROJECT 2

4

4. I2C Communication with MPU 6050:

 Understanding of I2C communication for interfacing with the MPU 6050 sensor, retrieving accurate accelerometer and gyroscope data.

5. Queue-based Communication:

 Utilized queues for efficient communication between tasks, ensuring a synchronized flow of data and preventing race conditions.

6. Mutex for Data Integrity:

 Implemented a mutex to protect shared data, ensuring thread-safe operations and preventing conflicts in data manipulation.

7. Coordinate Distance Calculation:

 Learned the Haversine formula for calculating distances between GPS coordinates, a crucial skill for location-based applications.

8. Debugging and Monitoring:

 Gained proficiency in debugging embedded systems using tools like Hercules for monitoring UART output, aiding in identifying and resolving issues.

9. Real-world Application Understanding:

 Applied theoretical knowledge to solve real-world problems, emphasizing the practical application of sensor data in a location-based context.

10. System Responsiveness:

 Achieved a system that responds dynamically to changes, such as significant shifts in GPS coordinates, demonstrating the importance of real-time responsiveness in embedded systems.

These key learnings collectively contribute to a comprehensive understanding of sensor integration, communication protocols, multitasking in embedded systems, and practical application scenarios.

OBJECTIVE

The goal is to integrate a GPS and an IMU sensor with a microcontroller development board to accomplish

specific tasks detecting sensor connection/disconnection, managing data communication, and conditionally

reporting sensor data based on location changes. The program starts with the main function, where FreeRTOS

is initialized.The GPS data is sent to the UART for communication.FreeRTOS is a real-time operating system that

allows you to create tasks and manage concurrency in embedded systems.Proper error handling and sensor

disconnection detection are essential in real-world applicationsUse a circular buffer or queue to store received

data in the ISR.Modify the vTaskGPS task to read data from the circular buffer or queue instead of directly

reading from UART.

Approach and Setup

1. Microcontroller Selection: Use a microcontroller that supports UART, I2C, and external interrupts.

Examples include STM32, ESP32, or Arduino Mega.

2. Sensor Integration:

GPS Module: Connect the GPS module via UART to receive NMEA sentences. Configure UART with an interrupt

for data reception.

IMU Sensor: Connect via I2C. This sensor typically includes an accelerometer and a gyroscope.

3. Connection/Disconnection Detection:

Use GPIO pins configured as digital inputs with interrupt capability to detect the physical connection of

sensors. Monitor the data streams; a sudden halt in data can indicate disconnection.

Hardware setup

GPS: Connect the GPS TX pin to the microcontroller's RX pin designated for UART.

IMU: Connect the SDA and SCL pins of the IMU to the corresponding I2C pins on the microcontroller.

Connection Detection Pins: Optionally use additional GPIOs to detect physical connection.

Software Setup

Initialize UART for GPS

UART_Init(9600 baudrate);

// Set baud rate for GPS

UART_RX_InterruptEnable();

// Enable receive interrupt

Initialize I2C for IMU:

I2C_Init(400 kHz);

// Standard fast mode I2C

Connection Monitoring:

SetupConnectionInterrupts();

// Setup GPIO interrupts for connection detection

Implement Interrupt Handlers

UART Interrupt Handler:

void UART_RX_Handler()

{

char data = UART_ReadData();

Buffer_Store(data);

if (Buffer_Full()) {

Parse_GPS_Data();

}

}

Connection Detection Handler:

void Connection_Handler()

{

if (GPIO_ReadPin(connection_pin) == HIGH) {

Serial_Print("Sensor Connected");

} else {

Serial_Print("Sensor Disconnected");

}

}

GPS Data Handling

Parsing GPS Data:

Use a buffer to collect incoming data until a complete NMEA sentence is received, then parse it to extract the

latitude and longitude.

Distance Calculation:

Calculate the distance between the current and the previous coordinates. If the distance exceeds 100 meters,

trigger an action.

if (Calculate_Distance(current_position, last_position) > 100)

{

Serial_Print("Location: ", current_position.latitude, current_position.longitude);

Serial_Print("IMU Data: ", imu_data.acceleration, imu_data.gyro);

}

IMU Data Handling

Read IMU Data:

void Read_IMU() {

imu_data.acceleration = I2C_ReadRegister(ACCEL_ADDRESS);

imu_data.gyro = I2C_ReadRegister(GYRO_ADDRESS);

Queue_Push(imu_queue, imu_data);

}

Task Scheduling:

Implement a scheduler or use an RTOS to periodically read IMU data and check GPS data. By integrating GPS

and IMU sensors with UART and I2C interfaces on a microcontroller, the system can monitor the connection

status, receive and process data effectively using interrupts and queues, and report significant location changes

along with sensor data. This setup ensures efficient data handling and timely reporting based on predefined

conditions, facilitating real-time monitoring and response.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.