Skip to content

LiWeny16/ai_based_remote_control_car_arduino_mega2560

Repository files navigation

AI-Based Omnidirectional Remote Control Car

Arduino Mega 2560 Platform

License: GPL v3 Arduino Platform FreeRTOS C++

English | 中文


Project Overview

An advanced omnidirectional robotic car platform designed for autonomous navigation and remote control applications. Built on Arduino Mega 2560, this system integrates sophisticated motion control algorithms, real-time PID regulation, wireless communication, and environmental sensing capabilities. The architecture supports both traditional remote operation and AI-assisted autonomous navigation scenarios.

Technical Highlights

  • Omnidirectional Motion System: 360-degree movement capability with precise angle control
  • Advanced PID Control: Independent multi-motor closed-loop speed regulation
  • Real-Time Operating System: FreeRTOS implementation for deterministic task scheduling
  • Wireless Communication: ESP8266-based command and telemetry interface
  • Modular Software Architecture: Object-oriented design with hardware abstraction layers
  • Interrupt-Driven Sensing: Hardware-based encoder counting for accurate velocity feedback

Table of Contents


System Architecture

Motion Control Subsystem

Omnidirectional Movement Engine

  • 360-degree movement capability with 1-degree resolution
  • Mecanum wheel kinematics implementation
  • Vector composition for diagonal and compound movements
  • Independent wheel speed control with synchronized execution

PID Speed Regulation

  • Four independent PID controllers (one per motor)
  • Incremental PID algorithm with anti-windup protection
  • Configurable proportional, integral, and derivative coefficients
  • Real-time error tracking and correction (50Hz update rate)

Encoder Feedback System

  • Quadrature encoder signal processing
  • Hardware interrupt-based pulse counting
  • Direction detection and speed calculation
  • 20ms sampling interval for velocity estimation

Communication Subsystem

Wireless Interface (ESP8266)

  • 9600 baud SoftwareSerial communication
  • Protocol-based command structure with delimiters
  • Bidirectional data transmission
  • Real-time telemetry streaming (10Hz update rate)

Command Protocol

  • Omnidirectional movement: 1,<angle>,<speed>?
  • Rotation control: 2,<direction>?
  • Emergency stop: 3?
  • Data format: ASCII text with '?' terminator

FreeRTOS Integration (Enhanced Version)

  • Task-based serial reception (high priority)
  • Queue-based command buffering
  • Non-blocking parser task (medium priority)
  • Decoupled interrupt handling and command processing

Sensor Subsystem

Ultrasonic Distance Measurement

  • HC-SR04 sensor integration
  • Trigger-echo timing measurement
  • Distance calculation with temperature compensation
  • 50Hz measurement frequency

Servo-Based Scanning

  • 180-degree horizontal scanning range
  • Automated sweep pattern (40° to 140°)
  • Synchronized distance mapping
  • Real-time obstacle detection

Software Design

Modular Architecture

Project Structure:
├── main.ino                    # Main control loop with millis-based timing
├── main_freertos_serial.ino    # FreeRTOS implementation with task scheduling
├── all_init.cpp                # System initialization routines
├── movement.cpp                # Omnidirectional motion control algorithms
├── pid.cpp                     # PID controller implementation
├── motor.cpp                   # Motor driver interface and encoder processing
├── serial.cpp                  # Basic serial communication handler
├── serial_freertos.cpp         # Non-blocking FreeRTOS serial implementation
├── ultrasonic.cpp              # HC-SR04 sensor driver
├── other.cpp                   # Utility functions and debugging tools
├── head.h                      # Global declarations and class definitions
└── port.h                      # Hardware pin mapping configuration

Object-Oriented Design Patterns

Base Movement Class (Polymorphic Interface)

class Base_Movement {
  virtual void stop(Speed_Set *speed_set);
  virtual void any(Speed_Set *speed_set, float angle, int speed_rate);
  // Additional virtual methods for extensibility
};

PID Controller Encapsulation

class PID_Motor {
  float P, I, D;                          // Tunable coefficients
  void calculate_pid_motor(Err_Speed *err_speed);
  int constrain_motor_out(int motor_out);
  void pid_control_motor(Motor_Port motor_port);
};

Speed and Error Tracking Classes

  • Type-safe data structures for speed setpoints and measurements
  • Error calculation with differential tracking for PID derivative term
  • Automatic update of previous error states

Real-Time Execution Models

Standard Implementation (main.ino)

  • millis()-based multi-rate execution
  • Speed calculation: 20ms interval (50Hz)
  • PID control: 50ms interval (20Hz)
  • Serial communication: 100ms interval (10Hz)
  • Non-blocking execution with time-stamped intervals

FreeRTOS Implementation (main_freertos_serial.ino)

  • Deterministic task scheduling with priority-based preemption
  • Dedicated tasks for speed calculation, PID control, communication, and sensing
  • Queue-based inter-task communication for command buffering
  • Resource-efficient stack allocation per task

Hardware Configuration

Component List

Component Specification Quantity Purpose
Arduino Mega 2560 ATmega2560, 16MHz 1 Main controller
ESP8266 Wi-Fi module 1 Wireless communication
DC Motor with Encoder 12V, gear ratio 1:30 4 Omnidirectional drive
Motor Driver L298N or equivalent 2 Motor power control
HC-SR04 Ultrasonic sensor 1 Distance measurement
Servo Motor SG90 or equivalent 1 Sensor scanning
Power Supply 12V battery pack 1 System power
Voltage Regulator 5V output 1 Logic level power

Pin Configuration

Motor Control (PWM Outputs)

Motor 1 (Front-Left):  Pin 4, 5
Motor 2 (Front-Right): Pin 6, 7
Motor 3 (Rear-Left):   Pin 8, 9
Motor 4 (Rear-Right):  Pin 10, 11

Encoder Inputs (Interrupt Pins)

Encoder 1: Pin 2 (INT4), Pin 24 (direction)
Encoder 2: Pin 3 (INT5), Pin 25 (direction)
Encoder 3: Pin 18 (INT3), Pin 26 (direction)
Encoder 4: Pin 19 (INT2), Pin 27 (direction)

Sensor Interfaces

Ultrasonic Trigger: Pin 22
Ultrasonic Echo:    Pin 23
Servo PWM:          Pin 12

Communication

ESP8266 RX: Pin 52 (SoftwareSerial)
ESP8266 TX: Pin 53 (SoftwareSerial)
Debug Serial: Hardware UART (USB)

Circuit Diagram Notes

  • Motor drivers require separate power supply (12V for motors, 5V for logic)
  • Encoder signals use internal pull-up resistors
  • ESP8266 operates at 3.3V; level shifters recommended for TX/RX lines
  • Decoupling capacitors (100nF) near each IC power pin
  • External power supply should handle peak current of 5A

Getting Started

Prerequisites

Software Requirements

  • Arduino IDE 1.8.19 or later
  • Arduino_FreeRTOS library (for RTOS version)
  • SoftwareSerial library (included in Arduino core)
  • Servo library (included in Arduino core)

Hardware Assembly

  1. Mount four motors on chassis in omnidirectional configuration
  2. Connect motor drivers to Arduino according to pin configuration
  3. Wire encoders to designated interrupt pins
  4. Install ultrasonic sensor on servo mount
  5. Connect ESP8266 module with appropriate level shifting
  6. Verify power supply connections (12V for motors, 5V for logic)

Installation Steps

1. Clone Repository

git clone https://github.com/LiWeny16/ai_based_remote_control_car_arduino_mega2560.git
cd ai_based_remote_control_car_arduino_mega2560

2. Install Dependencies

# In Arduino IDE Library Manager:
# - Search and install "FreeRTOS" by Phillip Stevens
# - Verify Servo and SoftwareSerial are available

3. Configure Hardware Pins Edit port.h if your wiring differs from default configuration:

#define Motor_PWM_1_1 4
#define Motor_PWM_1_2 5
// Adjust as needed for your setup

4. Upload Firmware

File → Open → select main.ino (or main_freertos_serial.ino)
Tools → Board → Arduino Mega or Mega 2560
Tools → Port → [Select your Arduino port]
Sketch → Upload

5. Verify Operation

  • Open Serial Monitor (115200 baud)
  • Observe initialization messages
  • Send test command: 1,0,10? (move forward at speed 10)

Command Protocol

Movement Commands

Command Format Example Description
Omnidirectional 1,<angle>,<speed>? 1,45,20? Move at 45° angle, speed 20
Rotate CW 2,1? 2,1? Rotate clockwise at default speed
Rotate CCW 2,2? 2,2? Rotate counter-clockwise
Stop 3? 3? Emergency stop all motors

Parameter Ranges

  • Angle: 0-360 degrees (0° = forward, 90° = right, 180° = backward, 270° = left)
  • Speed: 0-30 (arbitrary units, maps to PWM duty cycle)
  • Commands must end with '?' terminator

Performance Metrics

Control System Performance

Metric Value Notes
Movement Precision ±2° Angle accuracy in omnidirectional mode
Speed Control Error ±5% Steady-state error with tuned PID
Command Response <100ms Time from command receipt to execution
Encoder Resolution 16 pulses/rev Per motor shaft rotation
PID Update Rate 50Hz Control loop frequency
Speed Sampling 50Hz Velocity measurement frequency

Communication Specifications

Parameter Value Protocol
Wireless Range 50m+ ESP8266 dependent on environment
Baud Rate 9600 SoftwareSerial to ESP8266
Debug Serial 115200 USB connection
Telemetry Rate 10Hz Speed and sensor data transmission
Command Latency <50ms From ESP8266 to execution

Power Consumption

Component Typical Peak Notes
Arduino Mega 50mA 100mA At 5V
ESP8266 80mA 170mA During transmission
Motors (4x) 500mA 2A Per motor at stall
Servo 100mA 500mA During movement
Total System ~1A ~5A With all motors active

Development Guide

PID Tuning

The PID controllers can be tuned by modifying coefficients in main.ino or main_freertos_serial.ino:

// Example: Tuning Motor 1 PID parameters
pid_motor_1.P = 0.37;  // Proportional gain
pid_motor_1.I = 0.44;  // Integral gain  
pid_motor_1.D = 0.15;  // Derivative gain

Tuning Guidelines:

  1. Start with P only (I=0, D=0), increase until oscillation occurs
  2. Reduce P by 50%, add I term to eliminate steady-state error
  3. Add D term to reduce overshoot and improve stability
  4. Test under various load conditions

Adding Custom Movement Patterns

Extend the All_Direction_Movement class:

void All_Direction_Movement::custom_pattern(Speed_Set *speed_set) {
  // Implement your movement logic
  speed_set->speed_set_1 = custom_value_1;
  speed_set->speed_set_2 = custom_value_2;
  speed_set->speed_set_3 = custom_value_3;
  speed_set->speed_set_4 = custom_value_4;
}

FreeRTOS Task Priority

Task priorities are configured as follows (higher number = higher priority):

Task_Serial_Receive:    Priority 3 (Highest - time-critical)
Task_PID_Control:       Priority 3 (Control loop)
Task_Speed_Calculation: Priority 2 (Sensor processing)
Task_Command_Parser:    Priority 2 (Command execution)
Task_Serial_Transmit:   Priority 1 (Telemetry)
Task_Ultrasonic:        Priority 1 (Lowest - periodic sensing)

Debugging Tips

Serial Output

// Enable debug output in your code
Serial.print("Speed 1: ");
Serial.println(speed_now.speed_1);

Common Issues

  • Motors not responding: Check power supply and driver connections
  • Erratic encoder readings: Verify pull-up resistors and interrupt configuration
  • Lost ESP8266 commands: Reduce baud rate or check wiring
  • PID instability: Reduce gains and verify encoder direction

Testing Procedures

  1. Motor Test: Verify each motor responds correctly to PWM signals
  2. Encoder Test: Confirm pulse counting and direction detection
  3. Communication Test: Send commands via Serial Monitor
  4. PID Test: Observe speed convergence with oscilloscope or serial plot
  5. Integration Test: Execute complex movement sequences

Future Enhancements

This platform provides a solid foundation for advanced robotics applications:

  • Autonomous Navigation: Implement SLAM algorithms using ultrasonic and encoder data
  • Computer Vision: Integrate camera module for object detection and tracking
  • Sensor Fusion: Add IMU for improved odometry and stability control
  • Path Planning: Implement A* or RRT algorithms for obstacle avoidance
  • Machine Learning: Deploy lightweight neural networks for behavior learning
  • Multi-Robot Coordination: Implement swarm intelligence algorithms

Contributing

Contributions are welcome! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Areas for Contribution:

  • Additional sensor drivers
  • Alternative control algorithms
  • Performance optimizations
  • Documentation improvements
  • Test coverage

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

Key Points:

  • Free to use, modify, and distribute
  • Must disclose source code for derivative works
  • Must use the same license for derivative works

Citation

If you use this project in academic research, please cite:

@misc{ai_remote_control_car,
  author = {Bigonion},
  title = {AI-Based Omnidirectional Remote Control Car},
  year = {2024},
  publisher = {GitHub},
  url = {https://github.com/LiWeny16/ai_based_remote_control_car_arduino_mega2560}
}

Author

Bigonion

Acknowledgments

  • Arduino community for extensive library support
  • FreeRTOS project for RTOS implementation
  • Contributors and users providing feedback and improvements

About

Advanced omnidirectional robotic car with PID control, real-time sensing, and wireless communication on Arduino Mega 2560

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors