Skip to content

Commit 9f99ca8

Browse files
committed
Fixed drive straight
1 parent 6dac9fe commit 9f99ca8

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

XRPLib/differential_drive.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
137137

138138
if main_controller is None:
139139
main_controller = PID(
140-
kp = 0.5,
141-
minOutput = 0.12,
140+
kp = 0.075,
141+
kd = 0.005
142+
minOutput = 0.25,
142143
maxOutput = max_effort,
143144
tolerance = 0.1,
144145
toleranceCount = 3,
@@ -147,12 +148,9 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
147148
# Secondary controller to keep encoder values in sync
148149
if secondary_controller is None:
149150
secondary_controller = PID(
150-
kp = 0.0175, kd=0.005,
151+
kp = 0.25, kd=0.0025,
151152
)
152153

153-
rotationsToDo = distance / (self.wheel_diam * math.pi)
154-
#print("rot:", rotationsToDo)
155-
156154
if self.imu is not None:
157155
# record current heading to maintain it
158156
initial_heading = self.imu.get_yaw()
@@ -164,10 +162,10 @@ def straight(self, distance: float, max_effort: float = 0.5, timeout: float = No
164162
# calculate the distance traveled
165163
leftDelta = self.get_left_encoder_position() - startingLeft
166164
rightDelta = self.get_right_encoder_position() - startingRight
167-
rotationsDelta = (leftDelta + rightDelta) / 2
165+
distTraveled = (leftDelta + rightDelta) / 2
168166

169167
# PID for distance
170-
distanceError = rotationsToDo - rotationsDelta
168+
distanceError = distance - distTraveled
171169
effort = main_controller.tick(distanceError)
172170

173171
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
178176
# record current heading to maintain it
179177
current_heading = self.imu.get_yaw()
180178
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)
182180

183-
headingCorrection = secondary_controller.tick(current_heading - initial_heading)
181+
headingCorrection = secondary_controller.tick(initial_heading - current_heading)
184182

185183
self.set_effort(effort - headingCorrection, effort + headingCorrection)
186184

@@ -252,7 +250,7 @@ def turn(self, turn_degrees: float, max_effort: float = 0.5, timeout: float = No
252250
turnError = turn_degrees - self.imu.get_yaw()
253251
else:
254252
# 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)
256254

257255
# Pass the turn error to the main controller to get a turn speed
258256
turnSpeed = main_controller.tick(turnError)

0 commit comments

Comments
 (0)