Last active
June 27, 2022 22:21
-
-
Save evanthebouncy/2e3037d90af31fb50fe6f767b6982ba7 to your computer and use it in GitHub Desktop.
armando car with backoff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
track = track4() | |
car = newCar(track) | |
startDisplay(track, car) | |
def control(sd, pastsd): | |
# average these values to be more smooth | |
leftt = sd[0] + pastsd[0]; | |
mid = sd[1] + pastsd[1]; | |
# this bit of assymetry is actually very clever, because | |
# "intuitively" assymetrical things has more information on it | |
# and we want to have as much information as we can with only 3 sensors | |
rightt = sd[2] + pastsd[2] + 5; | |
# car is "safe" if | |
# 1. clear straight ahead (sum 50) | |
# 2. left sensor == right sensor | |
safe_straight = mid / 50; | |
safe_turn = Math.min(leftt / rightt, rightt / leftt); | |
# both above number range from 0 to 1, 1 being most safe | |
safe = safe_straight * safe_turn; | |
# cruising ===== | |
left_diff = mid-leftt; | |
right_diff = mid-rightt; | |
speed = 40 * safe; | |
ang_diff = left_diff - right_diff; | |
ang = 1.8*ang_diff; | |
# hysteresis | |
# cap min speed | |
speed = Math.max(speed, 16) | |
ang = Math.min(Math.max(ang, -35),35) | |
# emergency jiggle | |
sensor_total = leftt + mid + rightt; | |
if (sensor_total < 50): | |
print("performing backoff") | |
speed = -20; | |
ang = 99999 * (leftt - rightt); | |
return [ang, speed] | |
print("Ready to simulate.") | |
simout = simulate(car, track, 750, control, true) | |
print("Done!") | |
displayResult(track, simout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment