How to make a consistent auton

Hello,
Currently, I am trying to make autonomous code for a 6 motor drivetain. I have noticed that after perfecting the distance the robot turns/moves. After a while, the distance it travels or some of the turns’ rotations changes, even though me or my team mates have done nothing to the robot. It is quite frustrating to see the auton change every time. Please help me out with some tips. Everything helps. :slight_smile:

I am using an inertial sensor.
Here’s my code below. (It’s in Python.)


def turn(direction, heading): #turn using inertial sensor
    wait(50, MSEC)
    inertial.reset_rotation()
    #inertial.set_rotation(0)
    brain.screen.clear_screen()
    if direction == 'r':
        while inertial.rotation() < heading:
            brain.screen.print(inertial.heading())
            brain.screen.new_line()
            lt.spin(REVERSE)
            lm.spin(REVERSE)
            lb.spin(REVERSE)
            rt.spin(REVERSE)
            rm.spin(REVERSE)
            rb.spin(REVERSE)
            wait(5, MSEC)
        error = inertial.rotation() - heading
        while inertial.rotation() > heading:
            brain.screen.print("Correcting Error:" + str(error))
            brain.screen.new_line()
            lt.spin(FORWARD)
            lm.spin(FORWARD)
            lb.spin(FORWARD)
            rt.spin(FORWARD)
            rm.spin(FORWARD)
            rb.spin(FORWARD)
            wait(5, MSEC)
            

    else:
        while inertial.rotation() > heading * -1:
            brain.screen.print("Inertial Heading:" + str(inertial.heading()))
            brain.screen.new_line()
            lt.spin(FORWARD)
            lm.spin(FORWARD)
            lb.spin(FORWARD)
            rt.spin(FORWARD)
            rm.spin(FORWARD)
            rb.spin(FORWARD)
            wait(5, MSEC)
        error = inertial.rotation() - heading
        while inertial.rotation() > heading:
            brain.screen.print("Correcting Error:" + str(error))
            brain.screen.new_line()
            lt.spin(FORWARD)
            lm.spin(FORWARD)
            lb.spin(FORWARD)
            rt.spin(FORWARD)
            rm.spin(FORWARD)
            rb.spin(FORWARD)
            wait(5, MSEC)

    brain.screen.print("Final Heading:" + str(inertial.rotation()))
            
    lt.stop()
    lm.stop()
    lb.stop()
    rt.stop()
    rm.stop()
    rb.stop()
    wait(50, MSEC)



 def moveY(direction="f", distancecm=0.0): #move forward or backward a certain distance
      circum = 2 * 3.1415926 * 4.064
     rotations = distancecm / circum #need to multiply gear ratio in
     rm.reset_position()
    
    while abs(rm.position(DEGREES)) < abs(rotations * 360):
       # brain.screen.print(rm.position(DEGREES))
        if direction == 'f':
            lt.spin(REVERSE)
            lm.spin(REVERSE)
            lb.spin(REVERSE)
            rt.spin(FORWARD)
            rm.spin(FORWARD)
            rb.spin(FORWARD)
            wait(5, MSEC)
        else:
            #brain.screen.print(rm.position(DEGREES))
            lt.spin(FORWARD)
            lm.spin(FORWARD)
            lb.spin(FORWARD)
            rt.spin(REVERSE)
            rm.spin(REVERSE)
            rb.spin(REVERSE)
            wait(5, MSEC)

        
        brain.screen.print(rm.position(DEGREES))
        #brain.screen.new_line()

    lt.stop()
    lm.stop()
    lb.stop()
    rt.stop()
    rm.stop()
    rb.stop()
    wait(50, MSEC) #sys.sleep(0.05)

def autonomous(): #call autonomous functions here
    global isAuton
    isAuton = True
    brain.screen.clear_screen()
    brain.screen.print("autonomous code")
    #set_velo(32,32,32,32,32,32) #try increasing speed to maybe 32-33 to complete faster.
    intakeA.set_velocity(80, PERCENT)
    intakeB.set_velocity(70, PERCENT)
    intakeC.set_velocity(100, PERCENT)
   #

    inertial.calibrate()
    while inertial.is_calibrating():
        wait(100, MSEC)

    #Autonomous code here

    #score middle goal - 3 balls
    moveY("f", 27)
    
    turn("l", 70)
    autonIntake("in")
    autonexitIntake("u") #don't place prelaod on top of the flex wheels
    stopperAndDescore.set(False)
    timedStopIntake(3)

Not very experienced in coding here, but I might know something. Intertial Sensors can sometimes suffer from “Intertial Drift”, which can cause your turns or other movement to become somewhat inaccurate at times. A possible fix is to use 3 different inertial sensors, so that way if any of them are suffering from intertial drift, you can just code the robot to ignore the weird measurments. Another possible solution (although this can only really fix the turns) is using a distance sensor to align your robot. Listen to some other people though, I’m not very experienced in coding, so there might be something else at play here in the code that I can’t see.

How does the inertial sensor affect forward and backward movement? I know we can use it for PID but what else? Also do you have a method that doesn’t involve multiple sensors? We are kind of poor and can’t really buy multiple sensors.

The inertial sensor also tracks forward and backward movement, which is why intertial drift still affects those axises of movement. Sadly, there isn’t a good way (to my knowledge) to make super accurate autonomouses with one inertial sensor, as the only real ways that you can make accurate autonomouses are what I said earlier or using Odometry (which all of course may make the team spend money on those materials.) Having one inertial sensor is much better than nothing though, so I’d just make use of what you have right now, and try to obtain funding for some new sensors. Hopefully that helped, and no matter what happens, I wish you the best of luck out there!

Lots of teams have successfully done so.

Thank you, I have managed to make every turn except for the first accurate (for some reason it is only accurate on some fields). I have no idea why but it always over by around 9 degrees, regardless of what speed it is turning at