Overview
Before integrating everything, test each component individually. This unit testing approach helps you:- Identify problems early
- Debug systematically
- Build confidence in your system
- Create documentation
Testing Philosophy
Bottom-Up Testing
Start with simplest components (LED blink) and build up to complex systems (PID control).
One Thing at a Time
Test ONE component per session. Don’t add multiple unknowns simultaneously.
Document Results
Keep a testing log with date, test, result, and any issues found.
Known-Good Baseline
Once something works, save that code as a reference for future debugging.
Complete Testing Sequence
1
Test 1: ESP32 Basic I/O
Goal: Verify ESP32 programming worksTest Code:Success Criteria:
- Code uploads without errors
- LED blinks at 1Hz
- Can modify blink rate and see change
- Upload fails → Check drivers, USB cable, COM port
- LED doesn’t blink → Try different GPIO pin
2
Test 2: Serial Communication
Goal: Verify Serial Monitor worksTest Code:Success Criteria:
- Serial Monitor opens (baud: 115200)
- Messages print correctly
- Counter increments by ~1000 each second
- Garbage characters → Check baud rate matches
- No output → Press ESP32 RESET button
3
Test 3: PWM Output
Goal: Verify PWM generationTest Code:Success Criteria:
- LED fades smoothly from off to full bright
- Can measure with oscilloscope (optional)
- Frequency ~5kHz (if measured)
- Connect PWM_PIN to motor driver RPWM
- Motor should spin, speed varying
4
Test 4: Motor Driver (Open-Loop)
Goal: Verify motor spinsHardware:Success Criteria:
- ESP32 → IBT-2 driver (RPWM, LPWM, GND, VCC, EN)
- IBT-2 → Motor (M+, M-)
- 12V power → IBT-2 (B+, B-)
- Motor spins forward for 3 sec
- Motor stops for 2 sec
- Motor spins reverse for 3 sec
- Direction is correct
- Swap M+ and M- wires on motor
- Start at low duty cycle (50-100)
- Secure robot (won’t roll off table)
- Emergency stop ready (unplug battery)
5
Test 5: Encoder Reading
Goal: Verify encoder pulses are detectedHardware:Success Criteria:
- Encoder A → GPIO 22
- Encoder B → GPIO 23
- Encoder GND → ESP32 GND
- Encoder VCC → ESP32 5V
- Count changes when wheel rotated manually
- Count increases when rotated one direction
- Count decreases when rotated opposite direction
- ~330 counts per output shaft revolution
- Mark wheel position
- Rotate exactly 1 revolution
- Count should change by ~330 (±10)
- Count doesn’t change → Check encoder power and wiring
- Counts randomly → Noise on wires, add capacitor
- Wrong direction → Swap A and B pins in code
6
Test 6: Velocity Estimation
Goal: Calculate motor speed from encoderTest Code: (Add to encoder test)Test Method:
- Run motor at constant PWM (e.g., 128/255)
- Observe velocity readings
- Should stabilize after a few seconds
- Velocity reading is stable (±10%)
- Higher PWM → higher velocity
- Velocity ~5-15 rad/s range
7
Test 7: Closed-Loop Control (PID)
Goal: Control motor speed preciselyUse complete motor control code from D3.1Test Procedure:
- Upload PID control code
- Open Serial Monitor
- Send command:
q5(target 5 rad/s) - Observe response:
- Motor accelerates
- Settles to ~5 rad/s
- Minimal oscillation
- Motor reaches setpoint within 1 second
- Steady-state error < 5%
- Overshoot < 20%
- No continuous oscillation
- If oscillates: Reduce Kp
- If too slow: Increase Kp
- If steady-state error: Increase Ki
- If overshoots: Add/increase Kd
8
Test 8: I2C Communication
Goal: Verify I2C bus worksTest Code: I2C ScannerSuccess Criteria:
- Scan completes without crash
- IMU detected at 0x68 or 0x69
- Any other I2C devices show up
- No devices found → Check SDA/SCL wiring
- Scanner hangs → Short circuit or power issue
9
Test 9: IMU Data Reading
Goal: Read accelerometer and gyroscopeUse IMU integration code from D3.2Success Criteria:
- IMU initializes successfully
- Accelerometer reads ~9.8 m/s² on Z axis (when flat)
- Gyroscope reads near zero when stationary
- Values change when IMU moved/rotated
10
Test 10: Multi-Motor Control
Goal: Control all 4 motors simultaneouslyExpand single motor code to 4 motorsSuccess Criteria:
- All 4 motors can be commanded independently
- No interference between motors
- Encoder readings from all 4 motors
- PID control works on all motors
- All forward at same speed → robot goes straight
- Diagonal pairs opposite → robot strafes
- Left/right opposite → robot rotates
Testing Log Template
Keep a record of your tests:Debugging Techniques
Serial Print Debugging
Serial Print Debugging
Most basic and effective:Tips:
- Print before and after suspect code
- Print multiple variables to trace logic
- Use descriptive labels
LED Indicators
LED Indicators
When Serial not available:Blink patterns:
- 1 blink = Stage 1 reached
- 2 blinks = Stage 2 reached
- Fast blinking = Error condition
Multimeter Measurements
Multimeter Measurements
Essential for hardware issues:
- Verify voltages (12V, 5V, 3.3V)
- Check continuity (wires connected)
- Measure current draw
- Check PWM signal (DC voltage mode shows average)
Oscilloscope (If Available)
Oscilloscope (If Available)
For advanced debugging:
- View PWM waveforms
- Check encoder pulses
- Measure timing (interrupt latency)
- Detect noise/glitches
Incremental Changes
Incremental Changes
When something breaks:
- Go back to last working version
- Add ONE small change
- Test
- If works, add next change
- If fails, undo and try different approach
Common Problems & Solutions
| Symptom | Likely Cause | Solution |
|---|---|---|
| Code won’t upload | USB driver, wrong port | Install drivers, check COM port |
| ESP32 keeps resetting | Power issue, infinite loop | Check power supply, add delays |
| Motor doesn’t spin | Wiring, power, enable pins | Check connections, verify 12V |
| Encoder count zero | Power, wiring, ISR not attached | Verify 5V to encoder, check code |
| Velocity very noisy | Electrical noise, low resolution | Add filtering, increase counts |
| PID oscillates | Gains too high | Reduce Kp, Ki, Kd |
| IMU not detected | I2C wiring, address wrong | I2C scanner, check SDA/SCL |
| Everything works alone, fails together | Power/ground issues | Check common ground, power capacity |
Performance Benchmarks
Expected Results:| Component | Metric | Expected | Notes |
|---|---|---|---|
| Encoder | Resolution | 330 CPR | ±2% acceptable |
| Velocity | Accuracy | ±5% | At steady state |
| PID Rise Time | Time to 90% | <0.5 sec | Depends on tuning |
| PID Overshoot | % over target | <15% | Lower is better |
| IMU Update Rate | Frequency | >50 Hz | 100+ Hz ideal |
| Control Loop | Frequency | 100 Hz | Critical for smooth control |
Pre-Integration Checklist
Before combining all systems:- Each motor tested individually
- Each encoder verified (count and direction)
- PID tuned for at least one motor
- IMU reads sensible data
- Serial communication stable
- Power system adequate (no brownouts)
- All wiring labeled and secure
- Safety stop procedure defined
- Testing log documented
Next Steps
WP4: ROS2 Setup
Set up ROS2 environment for autonomous navigation
Integration Testing
Combine all subsystems
Troubleshooting Guide
Solutions to common problems
Code Examples
Reference implementations