|
5 | 5 | from .timeout import Timeout |
6 | 6 | import time |
7 | 7 | import math |
| 8 | +from sys import implementation |
8 | 9 |
|
9 | 10 | class DifferentialDrive: |
10 | 11 |
|
@@ -56,8 +57,6 @@ def __init__(self, left_motor: EncodedMotor, right_motor: EncodedMotor, imu: IMU |
56 | 57 | self.turning = False |
57 | 58 |
|
58 | 59 | if self.imu: |
59 | | - # if the IMU is initialized, then create a PID controller that can be used |
60 | | - # to maintain a constant heading when driving |
61 | 60 | self.heading_pid = PID( kp = 0.075, kd=0.001, ) |
62 | 61 |
|
63 | 62 | def set_effort(self, left_effort: float, right_effort: float) -> None: |
@@ -198,24 +197,40 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No |
198 | 197 | starting_left = self.get_left_encoder_position() |
199 | 198 | starting_right = self.get_right_encoder_position() |
200 | 199 |
|
201 | | - |
202 | | - if main_controller is None: |
203 | | - main_controller = PID( |
204 | | - kp = 0.1, |
205 | | - ki = 0.04, |
206 | | - kd = 0.04, |
207 | | - min_output = 0.3, |
208 | | - max_output = max_effort, |
209 | | - max_integral = 10, |
210 | | - tolerance = 0.25, |
211 | | - tolerance_count = 3, |
212 | | - ) |
213 | | - |
214 | | - # Secondary controller to keep encoder values in sync |
215 | | - if secondary_controller is None: |
216 | | - secondary_controller = PID( |
217 | | - kp = 0.075, kd=0.001, |
218 | | - ) |
| 200 | + if "NanoXRP" in implementation._machine: |
| 201 | + if main_controller is None: |
| 202 | + main_controller = PID( |
| 203 | + kp = 0.32, |
| 204 | + kd = 0.0184, |
| 205 | + min_output = 0.1, |
| 206 | + max_output = max_effort, |
| 207 | + tolerance = 0.25, |
| 208 | + tolerance_count = 3, |
| 209 | + ) |
| 210 | + |
| 211 | + # Secondary controller to keep encoder values in sync |
| 212 | + if secondary_controller is None: |
| 213 | + secondary_controller = PID( |
| 214 | + kp = 0.012, kd=0.00129, |
| 215 | + ) |
| 216 | + else: |
| 217 | + if main_controller is None: |
| 218 | + main_controller = PID( |
| 219 | + kp = 0.1, |
| 220 | + ki = 0.04, |
| 221 | + kd = 0.04, |
| 222 | + min_output = 0.3, |
| 223 | + max_output = max_effort, |
| 224 | + max_integral = 10, |
| 225 | + tolerance = 0.25, |
| 226 | + tolerance_count = 3, |
| 227 | + ) |
| 228 | + |
| 229 | + # Secondary controller to keep encoder values in sync |
| 230 | + if secondary_controller is None: |
| 231 | + secondary_controller = PID( |
| 232 | + kp = 0.075, kd=0.001, |
| 233 | + ) |
219 | 234 |
|
220 | 235 | if self.imu is not None: |
221 | 236 | # record current heading to maintain it |
@@ -284,28 +299,44 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No |
284 | 299 | time_out = Timeout(timeout) |
285 | 300 | starting_left = self.get_left_encoder_position() |
286 | 301 | starting_right = self.get_right_encoder_position() |
287 | | - |
288 | | - if main_controller is None: |
289 | | - main_controller = PID( |
290 | | - # kp = 0.2, |
291 | | - # ki = 0.004, |
292 | | - # kd = 0.0036, |
293 | | - kd = 0.0036 + 0.0034 * (max(max_effort, 0.5) - 0.5) * 2, |
294 | | - kp = 0.2, |
295 | | - ki = 0.004, |
296 | | - #kd = 0.007, |
297 | | - min_output = 0.1, |
298 | | - max_output = max_effort, |
299 | | - max_integral = 30, |
300 | | - tolerance = 1, |
301 | | - tolerance_count = 3 |
302 | | - ) |
303 | | - |
304 | | - # Secondary controller to keep encoder values in sync |
305 | | - if secondary_controller is None: |
306 | | - secondary_controller = PID( |
307 | | - kp = 0.25, |
308 | | - ) |
| 302 | + |
| 303 | + if "NanoXRP" in implementation._machine: |
| 304 | + if main_controller is None: |
| 305 | + main_controller = PID( |
| 306 | + kp = 0.016, |
| 307 | + kd = 0.0008, |
| 308 | + min_output = 0.05, |
| 309 | + max_output = max_effort, |
| 310 | + tolerance = 1, |
| 311 | + tolerance_count = 3 |
| 312 | + ) |
| 313 | + # Secondary controller to keep encoder values in sync |
| 314 | + if secondary_controller is None: |
| 315 | + secondary_controller = PID( |
| 316 | + kp = 0.32, |
| 317 | + kd = 0.0184, |
| 318 | + ) |
| 319 | + else: |
| 320 | + if main_controller is None: |
| 321 | + main_controller = PID( |
| 322 | + # kp = 0.2, |
| 323 | + # ki = 0.004, |
| 324 | + # kd = 0.0036, |
| 325 | + kd = 0.0036 + 0.0034 * (max(max_effort, 0.5) - 0.5) * 2, |
| 326 | + kp = 0.2, |
| 327 | + ki = 0.004, |
| 328 | + #kd = 0.007, |
| 329 | + min_output = 0.1, |
| 330 | + max_output = max_effort, |
| 331 | + max_integral = 30, |
| 332 | + tolerance = 1, |
| 333 | + tolerance_count = 3 |
| 334 | + ) |
| 335 | + # Secondary controller to keep encoder values in sync |
| 336 | + if secondary_controller is None: |
| 337 | + secondary_controller = PID( |
| 338 | + kp = 0.25, |
| 339 | + ) |
309 | 340 |
|
310 | 341 | if use_imu and (self.imu is not None): |
311 | 342 | turn_degrees += self.imu.get_yaw() |
|
0 commit comments