@@ -137,8 +137,9 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
137
137
138
138
if main_controller is None :
139
139
main_controller = PID (
140
- kp = 0.5 ,
141
- minOutput = 0.12 ,
140
+ kp = 0.075 ,
141
+ kd = 0.005
142
+ minOutput = 0.25 ,
142
143
maxOutput = max_effort ,
143
144
tolerance = 0.1 ,
144
145
toleranceCount = 3 ,
@@ -147,12 +148,9 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
147
148
# Secondary controller to keep encoder values in sync
148
149
if secondary_controller is None :
149
150
secondary_controller = PID (
150
- kp = 0.0175 , kd = 0.005 ,
151
+ kp = 0.25 , kd = 0.0025 ,
151
152
)
152
153
153
- rotationsToDo = distance / (self .wheel_diam * math .pi )
154
- #print("rot:", rotationsToDo)
155
-
156
154
if self .imu is not None :
157
155
# record current heading to maintain it
158
156
initial_heading = self .imu .get_yaw ()
@@ -164,10 +162,10 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
164
162
# calculate the distance traveled
165
163
leftDelta = self .get_left_encoder_position () - startingLeft
166
164
rightDelta = self .get_right_encoder_position () - startingRight
167
- rotationsDelta = (leftDelta + rightDelta ) / 2
165
+ distTraveled = (leftDelta + rightDelta ) / 2
168
166
169
167
# PID for distance
170
- distanceError = rotationsToDo - rotationsDelta
168
+ distanceError = distance - distTraveled
171
169
effort = main_controller .tick (distanceError )
172
170
173
171
if main_controller .is_done () or time_out .is_done ():
@@ -178,9 +176,9 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
178
176
# record current heading to maintain it
179
177
current_heading = self .imu .get_yaw ()
180
178
else :
181
- current_heading = ((leftDelta - rightDelta )/ 2 )* 360 * self . wheel_diam / self .track_width
179
+ current_heading = ((leftDelta - rightDelta )/ 2 )* 360 / ( self .track_width * math . pi )
182
180
183
- headingCorrection = secondary_controller .tick (current_heading - initial_heading )
181
+ headingCorrection = secondary_controller .tick (initial_heading - current_heading )
184
182
185
183
self .set_effort (effort - headingCorrection , effort + headingCorrection )
186
184
@@ -252,7 +250,7 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
252
250
turnError = turn_degrees - self .imu .get_yaw ()
253
251
else :
254
252
# calculate turn error (in degrees) from the encoder counts
255
- turnError = turn_degrees - ((rightDelta - leftDelta )/ 2 )* 360 * self . wheel_diam / self .track_width
253
+ turnError = turn_degrees - ((leftDelta - rightDelta )/ 2 )* 360 / ( self .track_width * math . pi )
256
254
257
255
# Pass the turn error to the main controller to get a turn speed
258
256
turnSpeed = main_controller .tick (turnError )
0 commit comments