๐Ÿง‘๐Ÿป  **: Beshr , Adham / 22209885**  
๐Ÿ“š  **: Technische Hochschule Deggendorf, Bavaria**  
๐Ÿ“„ : [Wiki](https://mygit.th-deg.de/ab11885/autonomous-line-following-car-with-camera-based-navigation/-/wikis/home).  
๐ŸฆŠ : [GitLab Repository](https://mygit.th-deg.de/ab11885/autonomous-line-following-car-with-camera-based-navigation).

# ๐Ÿš— **Autonomous Line-Following Car Project** 

![NXP Car](https://mygit.th-deg.de/ab11885/autonomous-line-following-car-with-camera-based-navigation/-/raw/main/Images/NXP_Car.png?ref_type=heads)

## ๐Ÿš€ **Project Overview**

This project involves building an autonomous vehicle that can follow a track using a line scan camera and a BLDC motor. The vehicle uses real-time image data to detect the trackโ€™s boundaries and calculates the deviation from the center of the track. Based on this information, the car adjusts its steering to stay on the track.

The system integrates image processing, motor control, and real-time sensor data to ensure smooth and accurate navigation. The BLDC motor runs in a sensorless mode, while a servo motor controls the steering, allowing the car to make precise turns and follow the path autonomously. This project demonstrates the application of embedded systems, motor control, and real-time data processing for building an autonomous vehicle.

## ๐Ÿ’ก **Key Features**

- ๐ŸŒŸ **Real-time Image Processing**: Using an ADC to read pixel intensities, the carโ€™s eyes (the camera) are always on the lookout for the trackโ€™s boundaries.

- ๐ŸŽฏ **Precise Lane Following**: Calculates deviation from the track's center, ensuring the car stays perfectly aligned.
- โšก **Sensorless BLDC Motor Control**: With back-EMF detection, the motor powers the vehicle efficiently, without additional sensors.
- ๐Ÿ”„ **Dynamic Steering with Servo Motors**: The servo motor adjusts the steering based on real-time deviation calculations for fluid motion.
- โฑ๏ธ **Efficient Interrupt-Driven Architecture**: Real-time data acquisition with minimized CPU load, ensuring the system responds instantly.

## ๐Ÿ› ๏ธ **Hardware Components**

| **Component**       | **Description**                                         |
|---------------------|---------------------------------------------------------|
| **Microcontroller**  | ๐Ÿง  **NXP K66F**, Cortex-M4, 180 MHz โ€“ The brain of the system |
| **Camera Sensor**    | ๐Ÿ“ธ **TSL1401R-LF Line Scan Camera** โ€“ The eyes that detect the track |
| **BLDC Motor**       | โš™๏ธ **6-step commutation** โ€“ Powering the carโ€™s movement |
| **H-Bridge**         | ๐Ÿ”Œ Controls motor direction & speed โ€“ The heart of the motion |
| **Servo Motor**      | ๐ŸŽฎ Adjusts steering based on real-time deviations โ€“ Precision in motion |
| **LED Matrix**       | ๐Ÿ’ก **WS2812B** โ€“ Illuminates the track for enhanced visibility |

## ๐Ÿ’ป **Software & Firmware**

All development is done using **MCUXpresso IDE**, which ensures efficient coding and debugging. The project utilizes an essential file, `pins.mex`, for configuring peripherals on the microcontroller.

### ๐Ÿ”ง **Microcontroller Peripherals Used**

- **FTM (FlexTimer Module)**: Generates PWM signals for the BLDC motor and servo motor for accurate control.
- **ADC (Analog-to-Digital Converter)**: Reads pixel intensity from the line scan camera for real-time image processing.
- **GPIO**: Manages motor phase switching and camera signal control.
- **SPI**: Powers the **LED Matrix**, enabling stunning track illumination.
- **DMA**: Transfers data with minimal CPU involvement, ensuring real-time responsiveness.

## ๐Ÿ› ๏ธ **Main Functionalities**

1. **๐Ÿ“ธ Image Capture**: The ADC reads pixel intensity from the camera in real-time.
2. **๐Ÿ Edge Detection**: Tracks boundaries are detected by analyzing intensity differences between the track and surroundings.
3. **๐Ÿ“Š Deviation Calculation**: Constantly calculates how far the car is from the center of the track, ensuring precision.
4. **๐Ÿš— Motor & Steering Control**: Adjusts speed and steering angle based on deviation, ensuring smooth navigation.
5. **โšก Interrupt Handling**: ADC and FTM interrupts allow for immediate reaction to environmental changes, enabling the car to respond to curves and obstacles instantly.

## ๐Ÿšง **Challenges & Innovative Solutions**

- **โŒ Unexpected Deviation Values**  
  **Problem**: The car was drifting due to incorrect deviation values on a straight track.  
  **Solution**: Refined control by removing unnecessary functions and focusing on specific left/right turns for accurate lane corrections.

- **โŒ BLDC Motor Failure to Start**  
  **Problem**: The motor failed to initiate under forced commutation.  
  **Solution**: A switch to **Free-Running mode** enabled successful commutation through back-EMF, enhancing motor efficiency.

- **โŒ Microcontroller Resetting**  
  **Problem**: The high LED intensity caused the MCU to reset during operation.  
  **Solution**: Lowered the LED intensity by adjusting bit pattern values, stabilizing the system.

## ๐ŸŽ‰ **Results**

- โœ… The car successfully follows the track autonomously, demonstrating effective edge detection and steering.
- โœ… **Deviation-based steering** ensures smooth and precise lane corrections.
- โœ… The **BLDC motor** operates efficiently in sensorless mode, saving power and reducing complexity.

# ๐Ÿ”ง Installation Instructions

Follow the steps below to set up the development environment and test the autonomous vehicle project:

## 1. **Install MCUXpresso IDE**

   - Download and install the **MCUXpresso IDE** from the official NXP website.
   - Follow the installation instructions provided on the website for your operating system (Windows, macOS, or Linux).

## 2. **Open the Project in MCUXpresso IDE**

   - Launch the **MCUXpresso IDE** after installation.
   - Go to the **File** menu and select **Open Projects from File System**.
   - In the dialog that appears, click **Browse** and select the **ZIP file** containing the project, which is attached above.
   - Unzip the project and open it in the MCUXpresso IDE.

## 3. **Connect the NXP K66F Microcontroller**

   - Use a **USB cable** to connect your **NXP K66F microcontroller** to your computer.
   - Ensure that the microcontroller is properly powered and recognized by the IDE.

## 4. **Debug the Project**

   - In MCUXpresso IDE, click on the **Debug** button (represented by a bug icon) on the top toolbar.
   - Select **All Sessions** to start debugging.
   - The IDE will automatically load the project onto the NXP K66F microcontroller.

## 5. **Test the Car**

   - After successfully loading the project onto the microcontroller, test the autonomous vehicle.
   - Ensure that the vehicle follows the track as expected, making turns and adjusting based on the calculated deviation.
   - If available, observe the vehicleโ€™s behavior and make any necessary adjustments to the system.

---

By following these steps, you should be able to successfully set up the development environment and test the autonomous vehicle with the NXP K66F microcontroller.



## ๐ŸŒŸ **DriveX Team**

๐Ÿ‘ค **Adham Beshr** โ€“ Embedded Systems Student  
๐Ÿ‘ค **Ismail Shah** โ€“ IOT Student 


๐Ÿ”Ž **For an in-depth breakdown of each component, visit the** [Wiki](https://mygit.th-deg.de/ab11885/autonomous-line-following-car-with-camera-based-navigation/-/wikis/home).