Skip to content

Commit e554958

Browse files
committed
Added PID following to DriveBump and fixed it to work driving in reverse as well.
1 parent d264983 commit e554958

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

FLLMaster/DriveLibraries.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,77 @@ def ArcTurn(self, Degrees, Radius, Speed):
202202
self.tank.stop()
203203

204204
def DriveBump(self, Heading, Speed):
205-
lsrs = self.steer.get_speed_steering(0, Speed)
205+
"""
206+
Moves the robot in a specified direction at a specified speed until it hits something, while using the gyro sensor to keep the robot moving in a straight line.
207+
208+
``Heading``: The angle at which to drive, with the direction the gyro was last calibrated in being zero.
209+
``Speed``: The speed at which to drive, in motor percentage (same speed units as EV3-G). A negative value will make the robot drive backwards.
210+
"""
211+
212+
# Ensure values are within reasonable limits, and change them if necessary (Idiotproofing).
213+
if Speed > 75:
214+
Speed = 75
215+
print("Speed must be between -75 and 75 (inclusive).")
216+
elif Speed < -75:
217+
Speed = -75
218+
print("Speed must be between -75 and 75 (inclusive).")
219+
220+
# Check and store the sign of the input speed (for PID correction), and convert the target speed to encoder ticks per second
221+
sign = Speed * -1 / abs(Speed)
222+
target = abs((1050 * Speed) / 100)
223+
224+
# Initialize variables for PID control
225+
integral = 0.0
226+
last_error = 0.0
227+
derivative = 0.0
228+
229+
# Read the gyro
230+
current_angle = self.correctedAngle()
231+
232+
# Calculate the PID components
233+
error = current_angle - Heading
234+
integral = integral + error
235+
derivative = error - last_error
236+
last_error = error
237+
238+
# Calculate Steering value based on PID components and kp, ki, and kd
239+
turn_native_units = sign * max([min([(self.kp * error) + (self.ki * integral) + (self.kd * derivative), 100]), -100])
240+
241+
# Start the motors without speed regulation, using the Steering value and Speed
242+
lsrs = self.steer.get_speed_steering(turn_native_units, Speed)
206243
lsNative = lsrs[0]
207244
rsNative = lsrs[1]
208-
target = (1050 * Speed) / 100
209245
self.lm.on(SpeedNativeUnits(lsNative))
210246
self.rm.on(SpeedNativeUnits(rsNative))
247+
248+
# Wait for motors to get up to speed, then check and store the average speed (between the two motors)
211249
time.sleep(1)
212-
avgSpd = (self.lm.speed + self.rm.speed) / 2
250+
avgSpd = abs((self.lm.speed + self.rm.speed) / 2)
251+
252+
# Check if the motors have slowed down (because the robot hit something)
213253
while avgSpd > 0.90 * target:
214-
avgSpd = (self.lm.speed + self.rm.speed) / 2
254+
# Read the gyro
255+
current_angle = self.correctedAngle()
256+
257+
# Calculate the PID components
258+
error = current_angle - Heading
259+
integral = integral + error
260+
derivative = error - last_error
261+
last_error = error
262+
263+
# Calculate Steering value based on PID components and kp, ki, and kd
264+
turn_native_units = sign * max([min([(self.kp * error) + (self.ki * integral) + (self.kd * derivative), 100]), -100])
265+
266+
# Start the motors without speed regulation, using the Steering value and Speed
267+
lsrs = self.steer.get_speed_steering(turn_native_units, Speed)
268+
lsNative = lsrs[0]
269+
rsNative = lsrs[1]
270+
self.lm.on(SpeedNativeUnits(lsNative))
271+
self.rm.on(SpeedNativeUnits(rsNative))
272+
273+
# Check and store the average speed again
274+
avgSpd = abs((self.lm.speed + self.rm.speed) / 2)
275+
276+
# Stop the motors
215277
self.lm.stop()
216278
self.rm.stop()

FLLMaster/Main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
R1.zeroGyro()
88
#R1.spkr.speak("Starting")
99
R1.spkr.beep()
10-
R1.DriveBump(0, 30)
10+
R1.DriveBump(0, -30)

0 commit comments

Comments
 (0)