Project 2 - Integrate following sensors to the Dev Board
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.