-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildRobotz_controller1.py
More file actions
69 lines (54 loc) · 1.83 KB
/
Copy pathBuildRobotz_controller1.py
File metadata and controls
69 lines (54 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Your autonomous controller implementation.
Edit the get_control() method to implement your driving algorithm!
"""
import math
class Controller:
def __init__(self):
# PID parameters - tune these!
self.Kp = 0.015
self.Ki = 0.0002
self.Kd = 0.10
# PID state
self.previous_error = 0.0
self.integral = 0.0
def get_control(self, car, sensor_readings):
"""
Calculate steering, throttle, and brake.
Args:
car: Dictionary with x, y, theta, velocity
sensor_readings: List of 16 distance values
Returns:
[steering, throttle, brake]
"""
# Calculate error from center
error = self.calculate_center_error(sensor_readings)
# PID steering
steering = self.pid_control(error)
# Simple speed control (reduced for better control)
throttle = 0.25
brake = 0.0
return [steering, throttle, brake]
def calculate_center_error(self, sensor_readings):
"""Calculate lateral error from track center."""
mid = len(sensor_readings) // 2
left_avg = sum(sensor_readings[:mid]) / mid
right_avg = sum(sensor_readings[mid:]) / mid
return right_avg - left_avg
def pid_control(self, error):
"""PID controller for steering."""
dt = 1.0 / 60.0
# P term
p_term = self.Kp * error
# I term
self.integral += error * dt
self.integral = max(-100, min(100, self.integral))
i_term = self.Ki * self.integral
# D term
d_term = self.Kd * (error - self.previous_error) / dt
self.previous_error = error
return p_term + i_term + d_term
def reset(self):
"""Reset controller state."""
self.previous_error = 0.0
self.integral = 0.0