Calibrating for turning - Paladins-of-St-Pauls/StudentRobotics GitHub Wiki

Calibrating the turning of the robot.

R.compass.get_heading() has some jitter applied, so inorder to try and work out how long to turn to turn the robot a set amount, we need to somehow remove/reduce the jitter. It turns out, that repeatedly getting the heading is pretty fast so we can just call it a number of times and average to reduce the jitter.

from sr.robot import *
import statistics
import math

R = Robot()

m_left = R.motors[0].m0
m_right = R.motors[0].m1

def set_power(left, right):
    m_left.power = left
    m_right.power = right

def get_heading(n=5):
    heading = 0
    for i in range(0,n):
        heading += R.compass.get_heading()
    return heading/n


print("I found {} transmitter(s):".format(len(R.radio.sweep())))

print("Turning at 50%")
curr_heading = get_heading(1000)
last_heading = curr_heading
print(f"robot heading - {curr_heading}")
m_left.power = -50
m_right.power = 50

for speed in [25,50,75,100]:
    set_power(speed,-speed)

    for heading_iter in [5,100,1000,10000]:

        for sleeps in [10,100,1000]:
            curr_heading = get_heading(heading_iter)
            last_heading = curr_heading
            diffs = []
            for i in range(0,sleeps):
                curr_heading = get_heading(heading_iter)
                diff_heading = last_heading - curr_heading
                if diff_heading < -math.pi:
                    diff_heading += math.tau
                elif diff_heading > math.pi:
                    diff_heading -= math.tau
                diffs.append(diff_heading)
                last_heading = curr_heading
                R.sleep(1.0/sleeps)
            mean = statistics.mean(diffs)
            stddev = statistics.stdev(diffs,mean)
            print(f"Speed[{speed}] HeadIter[{heading_iter}] Sleep[{1.0/sleeps}] Mean - {mean}, Std dev - {stddev}")

set_power(0,0)

From which you get the following output:

0| Speed[25] HeadIter[5] Sleep[0.1] Mean - -0.12703742460384487, Std dev - 0.0750784940684335
0| Speed[25] HeadIter[5] Sleep[0.01] Mean - -0.022952317378343894, Std dev - 0.04721583743100244
0| Speed[25] HeadIter[5] Sleep[0.001] Mean - -0.011750270244051808, Std dev - 0.029806764509102336
0| Speed[25] HeadIter[100] Sleep[0.1] Mean - -0.13676466788747665, Std dev - 0.04680339045676442
0| Speed[25] HeadIter[100] Sleep[0.01] Mean - -0.023089382492135643, Std dev - 0.004272410252236615
0| Speed[25] HeadIter[100] Sleep[0.001] Mean - -0.011649102437596613, Std dev - 0.007970684280846607
0| Speed[25] HeadIter[1000] Sleep[0.1] Mean - -0.13640520108069634, Std dev - 0.04879598193491979
0| Speed[25] HeadIter[1000] Sleep[0.01] Mean - -0.02304464984977836, Std dev - 0.0037271437016533782
0| Speed[25] HeadIter[1000] Sleep[0.001] Mean - -0.011661322915905586, Std dev - 0.002650771923813323
0| Speed[25] HeadIter[10000] Sleep[0.1] Mean - -0.13665524971384838, Std dev - 0.047598248905590515
0| Speed[25] HeadIter[10000] Sleep[0.01] Mean - -0.023095559660027742, Std dev - 0.002355775118273589
0| Speed[25] HeadIter[10000] Sleep[0.001] Mean - -0.01168963923527761, Std dev - 0.0019351165258617135
0| Speed[50] HeadIter[5] Sleep[0.1] Mean - -0.27746855005632776, Std dev - 0.10790808465812182
0| Speed[50] HeadIter[5] Sleep[0.01] Mean - -0.04961727504810221, Std dev - 0.03778799211271665
0| Speed[50] HeadIter[5] Sleep[0.001] Mean - -0.02477389856834261, Std dev - 0.03172830918303942
0| Speed[50] HeadIter[100] Sleep[0.1] Mean - -0.2907313037078011, Std dev - 0.10341306131101585
0| Speed[50] HeadIter[100] Sleep[0.01] Mean - -0.04913224069971587, Std dev - 0.008461097878547435
0| Speed[50] HeadIter[100] Sleep[0.001] Mean - -0.024798105165989005, Std dev - 0.007757315878484507
0| Speed[50] HeadIter[1000] Sleep[0.1] Mean - -0.29062023337092446, Std dev - 0.10252166148665699
0| Speed[50] HeadIter[1000] Sleep[0.01] Mean - -0.049175210323977994, Std dev - 0.006114217070623507
0| Speed[50] HeadIter[1000] Sleep[0.001] Mean - -0.024809973932513885, Std dev - 0.003181217550884598
0| Speed[50] HeadIter[10000] Sleep[0.1] Mean - -0.2905866128842306, Std dev - 0.10209148868885255
0| Speed[50] HeadIter[10000] Sleep[0.01] Mean - -0.04916103796845093, Std dev - 0.006163275476701323
0| Speed[50] HeadIter[10000] Sleep[0.001] Mean - -0.024817904662441652, Std dev - 0.0025110425415509293
0| Speed[75] HeadIter[5] Sleep[0.1] Mean - -0.4343725649654817, Std dev - 0.16178225965402226
0| Speed[75] HeadIter[5] Sleep[0.01] Mean - -0.07399925772142997, Std dev - 0.0342841907233843
0| Speed[75] HeadIter[5] Sleep[0.001] Mean - -0.03734148276916213, Std dev - 0.03262689001959893
0| Speed[75] HeadIter[100] Sleep[0.1] Mean - -0.4370075285023674, Std dev - 0.15260142207915656
0| Speed[75] HeadIter[100] Sleep[0.01] Mean - -0.07391052672643916, Std dev - 0.010981724618990819
0| Speed[75] HeadIter[100] Sleep[0.001] Mean - -0.03734353185593669, Std dev - 0.00781518890176835
0| Speed[75] HeadIter[1000] Sleep[0.1] Mean - -0.43736999883881783, Std dev - 0.153769735602145
0| Speed[75] HeadIter[1000] Sleep[0.01] Mean - -0.07403976139936001, Std dev - 0.008288825087781164
0| Speed[75] HeadIter[1000] Sleep[0.001] Mean - -0.037345567336373404, Std dev - 0.003961168958190956
0| Speed[75] HeadIter[10000] Sleep[0.1] Mean - -0.43737389828257306, Std dev - 0.15367929907187777
0| Speed[75] HeadIter[10000] Sleep[0.01] Mean - -0.0740151069706204, Std dev - 0.008561758790341094
0| Speed[75] HeadIter[10000] Sleep[0.001] Mean - -0.03734488348767802, Std dev - 0.0029469103173401083
0| Speed[100] HeadIter[5] Sleep[0.1] Mean - -0.5520794679396928, Std dev - 0.19144551195410855
0| Speed[100] HeadIter[5] Sleep[0.01] Mean - -0.096589057660949, Std dev - 0.03191973184606239
0| Speed[100] HeadIter[5] Sleep[0.001] Mean - -0.048871538420211955, Std dev - 0.032277114563627966
0| Speed[100] HeadIter[100] Sleep[0.1] Mean - -0.5724468462551768, Std dev - 0.200012522942866
0| Speed[100] HeadIter[100] Sleep[0.01] Mean - -0.09685288296571411, Std dev - 0.013175402101554138
0| Speed[100] HeadIter[100] Sleep[0.001] Mean - -0.04888265490319742, Std dev - 0.00794290272793061
0| Speed[100] HeadIter[1000] Sleep[0.1] Mean - -0.5720455106973406, Std dev - 0.20169888287523483
0| Speed[100] HeadIter[1000] Sleep[0.01] Mean - -0.09682872138706675, Std dev - 0.011045920357484732
0| Speed[100] HeadIter[1000] Sleep[0.001] Mean - -0.04883321829038616, Std dev - 0.003949776117078154
0| Speed[100] HeadIter[10000] Sleep[0.1] Mean - -0.5719582041547526, Std dev - 0.20140884403212586
0| Speed[100] HeadIter[10000] Sleep[0.01] Mean - -0.09680196259879564, Std dev - 0.01017230713227097
0| Speed[100] HeadIter[10000] Sleep[0.001] Mean - -0.048836447475921835, Std dev - 0.0036807885450606814