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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-nc-4.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
tags:
|
| 6 |
+
- robotics
|
| 7 |
+
- random-forest
|
| 8 |
+
- pid-control
|
| 9 |
+
- line-follower
|
| 10 |
+
- raspberry-pi
|
| 11 |
+
- sklearn
|
| 12 |
+
- tabular-regression
|
| 13 |
+
pipeline_tag: tabular-regression
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# π€ pid-ml-follower-model
|
| 17 |
+
|
| 18 |
+
**Random Forest Regressor** trained on 100 real robot runs to predict next-step
|
| 19 |
+
line position error for a Hybrid PID + ML line following robot on Raspberry Pi.
|
| 20 |
+
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
## π Model Overview
|
| 24 |
+
|
| 25 |
+
This model predicts `next_error` β the PID line position error at time t+1 β
|
| 26 |
+
using 15 basic sensor inputs available at time t. The prediction is used as a
|
| 27 |
+
residual correction on top of a classical PID controller, forming a
|
| 28 |
+
**Hybrid PID + ML control system**.
|
| 29 |
+
|
| 30 |
+
| Property | Value |
|
| 31 |
+
|----------|-------|
|
| 32 |
+
| Algorithm | `RandomForestRegressor` (scikit-learn) |
|
| 33 |
+
| Target | `next_error` = pid_error at t+1 |
|
| 34 |
+
| Input features | 15 sensor + PID state columns |
|
| 35 |
+
| Training rows | 166,053 |
|
| 36 |
+
| Test rows | 42,930 |
|
| 37 |
+
| Training runs | 80 robot runs |
|
| 38 |
+
| Test runs | 20 robot runs (zero leakage) |
|
| 39 |
+
|
| 40 |
+
---
|
| 41 |
+
|
| 42 |
+
## π Performance
|
| 43 |
+
|
| 44 |
+
| Model | RMSE | MAE |
|
| 45 |
+
|-------|------|-----|
|
| 46 |
+
| Predict zero baseline | 0.4252 | 0.3678 |
|
| 47 |
+
| Persist current error baseline | 0.0219 | 0.0124 |
|
| 48 |
+
| Original RF (n=300, depth=15) | 0.018756 | β |
|
| 49 |
+
| **This model (Optuna tuned)** | **0.018731** | **0.008295** |
|
| 50 |
+
|
| 51 |
+
- **14.3% improvement** over persist-current-error baseline
|
| 52 |
+
- Better than original RF on **13 / 20** test runs
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
## βοΈ Hyperparameters
|
| 57 |
+
|
| 58 |
+
| Parameter | Value |
|
| 59 |
+
|-----------|-------|
|
| 60 |
+
| `n_estimators` | 328 |
|
| 61 |
+
| `max_depth` | 22 |
|
| 62 |
+
| `min_samples_leaf` | 9 |
|
| 63 |
+
| `max_features` | sqrt |
|
| 64 |
+
| Tuning | Optuna β 50 trials, TPE sampler |
|
| 65 |
+
|
| 66 |
+
---
|
| 67 |
+
|
| 68 |
+
## π’ Input Features (exact order)
|
| 69 |
+
|
| 70 |
+
```python
|
| 71 |
+
features = [
|
| 72 |
+
"pid_error", # current line position error
|
| 73 |
+
"pid_error_prev", # error from previous tick
|
| 74 |
+
"pid_error_delta", # error(t) - error(t-1)
|
| 75 |
+
"pid_derivative", # derivative term computed by PID
|
| 76 |
+
"ir_centroid", # weighted centre of IR readings
|
| 77 |
+
"ir_spread", # width of line across sensors
|
| 78 |
+
"ir1_inv", # IR sensor 1 (inverted)
|
| 79 |
+
"ir2_inv", # IR sensor 2 (inverted)
|
| 80 |
+
"ir3_inv", # IR sensor 3 (inverted)
|
| 81 |
+
"ir4_inv", # IR sensor 4 (inverted)
|
| 82 |
+
"ir5_inv", # IR sensor 5 (inverted)
|
| 83 |
+
"left_speed", # left motor speed
|
| 84 |
+
"right_speed", # right motor speed
|
| 85 |
+
"speed_diff", # left_speed - right_speed
|
| 86 |
+
"loop_dt", # time elapsed since last tick
|
| 87 |
+
]
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
**Order is critical β must match exactly during inference.**
|
| 91 |
+
|
| 92 |
+
---
|
| 93 |
+
|
| 94 |
+
## π How to Use
|
| 95 |
+
|
| 96 |
+
### Install
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
pip install scikit-learn numpy huggingface_hub
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
### Load Model
|
| 103 |
+
|
| 104 |
+
```python
|
| 105 |
+
import pickle
|
| 106 |
+
from huggingface_hub import hf_hub_download
|
| 107 |
+
|
| 108 |
+
# Download model
|
| 109 |
+
model_path = hf_hub_download(
|
| 110 |
+
repo_id = "satwikshreshth1/pid-ml-follower-model",
|
| 111 |
+
filename = "rf_model_tuned.pkl"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Load
|
| 115 |
+
with open(model_path, "rb") as f:
|
| 116 |
+
rf = pickle.load(f)
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
### Inference
|
| 120 |
+
|
| 121 |
+
```python
|
| 122 |
+
import numpy as np
|
| 123 |
+
|
| 124 |
+
features = [pid_error, pid_error_prev, pid_error_delta, pid_derivative,
|
| 125 |
+
ir_centroid, ir_spread, ir1_inv, ir2_inv, ir3_inv, ir4_inv,
|
| 126 |
+
ir5_inv, left_speed, right_speed, speed_diff, loop_dt]
|
| 127 |
+
|
| 128 |
+
predicted_next_error = rf.predict([features])[0]
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
### Hybrid Control Loop on Raspberry Pi
|
| 132 |
+
|
| 133 |
+
```python
|
| 134 |
+
# Alpha controls ML correction strength
|
| 135 |
+
alpha = 0.6 # recommended starting value
|
| 136 |
+
Kp = 13.2 # must match your robot tuning
|
| 137 |
+
|
| 138 |
+
ml_correction = -predicted_next_error * Kp * alpha
|
| 139 |
+
ml_correction = np.clip(ml_correction, -10, 10) # safety clip
|
| 140 |
+
final_output = pid_output + ml_correction
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
### Tuning Alpha
|
| 144 |
+
|
| 145 |
+
| Alpha | Behaviour |
|
| 146 |
+
|-------|-----------|
|
| 147 |
+
| 0.0 | Pure PID β ML disabled |
|
| 148 |
+
| 0.3 | Conservative hybrid |
|
| 149 |
+
| 0.6 | Standard hybrid (recommended) |
|
| 150 |
+
| 1.0 | Full ML correction |
|
| 151 |
+
|
| 152 |
+
---
|
| 153 |
+
|
| 154 |
+
## π¦ Files in This Repository
|
| 155 |
+
|
| 156 |
+
| File | Description |
|
| 157 |
+
|------|-------------|
|
| 158 |
+
| `rf_model_tuned.pkl` | Trained Random Forest model (pickle) |
|
| 159 |
+
| `model_meta.json` | Feature list, hyperparameters, metrics |
|
| 160 |
+
|
| 161 |
+
---
|
| 162 |
+
|
| 163 |
+
## ποΈ Training Data
|
| 164 |
+
|
| 165 |
+
- 100 real robot runs collected on 20 March 2026
|
| 166 |
+
- 221,967 total rows β 208,983 after cleaning
|
| 167 |
+
- Robot: IR-based line follower on Raspberry Pi
|
| 168 |
+
- PID tuning: Kp=13.2, Ki=0.0, Kd=0.475 | Base speed: 40
|
| 169 |
+
- Loop frequency: ~65β70 Hz
|
| 170 |
+
|
| 171 |
+
**Full dataset on Kaggle:**
|
| 172 |
+
[satwikshreshth01/path-error-data](https://www.kaggle.com/datasets/satwikshreshth01/path-error-data)
|
| 173 |
+
|
| 174 |
+
---
|
| 175 |
+
|
| 176 |
+
## π Related Links
|
| 177 |
+
|
| 178 |
+
- **GitHub Repository:** [satwik-shreshth/pid-ml-follower](https://github.com/satwik-shreshth/pid-ml-follower)
|
| 179 |
+
- **Training Notebook:** included in GitHub repo under `notebook/`
|
| 180 |
+
- **Related Project:** [Tripathagamini-S_Auto](https://github.com/satwik-shreshth/Tripathagamini-S_Auto)
|
| 181 |
+
|
| 182 |
+
---
|
| 183 |
+
|
| 184 |
+
## π License
|
| 185 |
+
|
| 186 |
+
CC BY-NC 4.0 β Attribution required, non-commercial use only.
|
| 187 |
+
https://creativecommons.org/licenses/by-nc/4.0/
|
| 188 |
+
|
| 189 |
+
---
|
| 190 |
+
|
| 191 |
+
## π¨βπ» Author
|
| 192 |
+
|
| 193 |
+
**Satwik Shreshth**
|
| 194 |
+
MCA (Final Year), Sikkim University
|
| 195 |
+
satwikshreshth2002@gmail.com
|
| 196 |
+
[@satwik-shreshth](https://github.com/satwik-shreshth)
|