You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
ifSpeed>75:
214
+
Speed=75
215
+
print("Speed must be between -75 and 75 (inclusive).")
216
+
elifSpeed<-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
0 commit comments