1features = [
2 "pid_error", # current line position error
3 "pid_error_prev", # error from previous tick
4 "pid_error_delta", # error(t) - error(t-1)
5 "pid_derivative", # derivative term computed by PID
6 "ir_centroid", # weighted centre of IR readings
7 "ir_spread", # width of line across sensors
8 "ir1_inv", # IR sensor 1 (inverted)
9 "ir2_inv", # IR sensor 2 (inverted)
10 "ir3_inv", # IR sensor 3 (inverted)
11 "ir4_inv", # IR sensor 4 (inverted)
12 "ir5_inv", # IR sensor 5 (inverted)
13 "left_speed", # left motor speed
14 "right_speed", # right motor speed
15 "speed_diff", # left_speed - right_speed
16 "loop_dt", # time elapsed since last tick
17]
1import pickle
2from huggingface_hub import hf_hub_download
3
4# Download model
5model_path = hf_hub_download(
6 repo_id = "satwikshreshth1/pid-ml-follower-model",
7 filename = "rf_model_tuned.pkl"
8)
9
10# Load
11with open(model_path, "rb") as f:
12 rf = pickle.load(f)
1import numpy as np
2
3features = [pid_error, pid_error_prev, pid_error_delta, pid_derivative,
4 ir_centroid, ir_spread, ir1_inv, ir2_inv, ir3_inv, ir4_inv,
5 ir5_inv, left_speed, right_speed, speed_diff, loop_dt]
6
7predicted_next_error = rf.predict([features])[0]
1# Alpha controls ML correction strength
2alpha = 0.6 # recommended starting value
3Kp = 13.2 # must match your robot tuning
4
5ml_correction = -predicted_next_error * Kp * alpha
6ml_correction = np.clip(ml_correction, -10, 10) # safety clip
7final_output = pid_output + ml_correction
CC BY-NC 4.0 — Attribution required, non-commercial use only.
https://creativecommons.org/licenses/by-nc/4.0/
Satwik Shreshth
MCA (Final Year), Sikkim University
satwikshreshth2002@gmail.com
@satwik-shreshth