We met the same question as yours that we setup the PstraightForward code and it works great. But when we setup the PstraightReverse code, it goes back forever. The reverse code is exactly the same as the forward code except for the motors spin reverse. Do you have any suggestions? Thanks.
I’m having the same issue with my turning loop which seems to not be changing the error at all. Here is the code:
#Function to turn
def turn_to(TARGET):
#Creates all the local variables
ERROR = 0
LASTERROR = 0
TOTALERROR = 0
OUTPUT = 0
START = time.time()
#Define starting variables
TOL = 1
TIME = 3
INT_RANGE = 10
while True:
ERROR = TARGET - brain_inertial.rotation()
if ERROR > 180:
ERROR -= 360
elif ERROR < -180:
ERROR += 360
brain.screen.print(brain_inertial.rotation())
brain.screen.next_row()
if abs(ERROR) < TOL:
brain.play_sound(SoundType.SIREN)
break
if time.time() - START > TIME:
break
if abs(ERROR) < abs(TOTALERROR):
break
TOTALERROR += ERROR
TOTALERROR = max(min(TOTALERROR, INT_RANGE), -INT_RANGE)
P = ERROR * turnkp
I = TOTALERROR * turnti
D = (ERROR - LASTERROR) * turntd
OUTPUT = P+I+D
left_drive.set_velocity(-OUTPUT, RPM)
right_drive.set_velocity(OUTPUT, RPM)
left_drive.spin(FORWARD)
right_drive.spin(FORWARD)
LASTERROR = ERROR
wait(20,MSEC)
left_drive.stop()
right_drive.stop()