Skip to content

fares-haoua/Simpl-robot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

ESP32 Odometry System with N20 Motors

A complete odometry system for ESP32 with N20 motors and encoders, featuring real-time position tracking, motor control, and a web-based dashboard for your phone.

๐Ÿš€ Features

  • Real-time Odometry: 100Hz position and velocity tracking
  • Motor Control: PID-controlled N20 motors with encoder feedback
  • Web Dashboard: Beautiful mobile-friendly interface accessible from your phone
  • WiFi Communication: ESP32 creates its own WiFi network
  • Encoder Calibration: Built-in calibration tools for accurate measurements
  • Multi-threaded: Separate tasks for odometry and communication

๐Ÿ› ๏ธ Hardware Requirements

  • ESP32 Development Board
  • 2x N20 Motors with Encoders (typically 6V, 12 CPR)
  • Motor Driver (L298N, TB6612FNG, or DRV8833)
  • Wheels (65mm diameter recommended)
  • Chassis with ~200mm wheelbase
  • Battery (6-12V depending on motors)

๐Ÿ“ Pin Connections

Motor Control

Left Motor:
- IN1: GPIO 25
- IN2: GPIO 26  
- PWM: GPIO 27

Right Motor:
- IN1: GPIO 32
- IN2: GPIO 33
- PWM: GPIO 14

Encoders

Left Encoder:
- A: GPIO 18
- B: GPIO 19

Right Encoder:
- A: GPIO 16
- B: GPIO 17

๐Ÿ”ง Setup Instructions

1. Install Dependencies

# Using PlatformIO (recommended)
pio run

# Or using Arduino IDE
# Install these libraries:
# - WiFiManager
# - ArduinoJson
# - ESPAsyncWebServer
# - AsyncTCP
# - ESP32TimerInterrupt
# - PID_v1

2. Configure System

Edit src/Config.h to match your hardware:

#define WHEELBASE_MM      200   // Distance between wheels
#define WHEEL_DIAMETER_MM 65    // Wheel diameter
#define ENCODER_CPR       12    // Encoder counts per revolution

3. Upload Code

pio run --target upload

4. Connect to WiFi

  1. Power on the ESP32
  2. Look for WiFi network: ESP32_Odometry
  3. Connect with password: 12345678
  4. Open browser and go to: 192.168.4.1

๐Ÿ“ฑ Web Dashboard

The system provides a beautiful web interface accessible from any device:

  • Real-time Position Display: Live X, Y coordinates and heading
  • Velocity Monitoring: Linear and angular velocity in real-time
  • Position Visualization: Canvas showing robot movement path
  • Control Panel: Manual movement commands and system controls
  • Mobile Optimized: Works perfectly on phones and tablets

๐Ÿ” Calibration Process

Encoder Calibration

  1. Open Serial Monitor (115200 baud)
  2. Send command: CALIBRATE
  3. Rotate wheel exactly 1 revolution
  4. Press any key when complete
  5. System calculates distance_per_count

Manual Calibration

// In Config.h, update these values after calibration:
#define LEFT_DISTANCE_PER_COUNT  0.017  // mm per encoder count
#define RIGHT_DISTANCE_PER_COUNT 0.017  // mm per encoder count

๐Ÿ“Š Odometry Algorithm

The system uses differential drive odometry:

Linear Velocity = (left_distance + right_distance) / (2 ร— dt)
Angular Velocity = (right_distance - left_distance) / (wheelbase ร— dt)

Position Update:
X += linear_velocity ร— cos(heading) ร— dt
Y += linear_velocity ร— sin(heading) ร— dt
Heading += angular_velocity ร— dt

๐ŸŽฎ Control Commands

Web Interface Commands

  • Reset: Reset odometry to origin
  • Calibrate: Start encoder calibration
  • Move Forward 100mm: Move robot forward 100mm
  • Turn 90ยฐ: Rotate robot 90 degrees

Serial Commands

RESET           - Reset odometry
CALIBRATE       - Start calibration
MOVE:FORWARD:100 - Move forward 100mm
MOVE:TURN:90    - Turn 90 degrees

โš™๏ธ PID Tuning

Default PID values are conservative. Tune for your specific robot:

// In Config.h
#define MOTOR_KP          2.0   // Proportional gain
#define MOTOR_KI          0.1   // Integral gain  
#define MOTOR_KD          0.05  // Derivative gain

Tuning Process:

  1. Start with P only, increase until oscillation
  2. Add D to reduce oscillation
  3. Add I to eliminate steady-state error
  4. Fine-tune for smooth operation

๐Ÿ”ง Troubleshooting

Common Issues

Motors not moving:

  • Check motor driver connections
  • Verify PWM pins are correct
  • Check battery voltage

Encoders not reading:

  • Verify encoder pin connections
  • Check for loose connections
  • Ensure proper pull-up resistors

WiFi not connecting:

  • Check SSID and password in code
  • Ensure ESP32 has enough power
  • Try resetting ESP32

Inaccurate odometry:

  • Recalibrate encoders
  • Check wheel diameter and wheelbase
  • Verify encoder CPR value

Debug Output

Enable debug output in Config.h:

#define DEBUG_ENCODER            true
#define DEBUG_MOTOR              true
#define DEBUG_ODOMETRY           true
#define DEBUG_COMMUNICATION      true

๐Ÿ“ˆ Performance

  • Odometry Update Rate: 100Hz (10ms intervals)
  • Communication Rate: 20Hz (50ms intervals)
  • Position Accuracy: ยฑ2-5mm (depends on calibration)
  • WiFi Range: ~10-20m (indoor)

๐Ÿš€ Advanced Features

Custom Movement Patterns

// Add to main.cpp
void customMovement() {
    // Move in a square pattern
    odometry.moveForward(100);
    odometry.turn(PI/2);
    odometry.moveForward(100);
    odometry.turn(PI/2);
    // ... continue pattern
}

Data Logging

// Log odometry data to SD card or cloud
void logData() {
    String data = String(odometry.getX()) + "," + 
                  String(odometry.getY()) + "," + 
                  String(odometry.getHeading());
    // Save to storage
}

๐Ÿค Contributing

Feel free to contribute improvements:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

๐Ÿ“„ License

This project is open source. Feel free to use and modify for your projects.

๐Ÿ†˜ Support

If you encounter issues:

  1. Check the troubleshooting section
  2. Review Serial Monitor output
  3. Verify hardware connections
  4. Check configuration values

Happy Building! ๐Ÿค–โœจ

Your ESP32 odometry system is now ready to track position and provide real-time feedback to your phone!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages