Member-only story
MicroPython Tutorial XI
Continuing our discussion from the previous tutorial things get a whole lot more interesting when you use odometry to figure how much your robot strayed off the straight and narrow.
Lets start by building a simple script to on which we can test the theory using the same model we were using earlier.
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import Button, Port, Stop
from pybricks.ev3devices import Motor, GyroSensor
from pybricks.tools import waitleftMotor = Motor(Port.B)
rightMotor = Motor(Port.C)gyro = GyroSensor(Port.S4)
gyro.reset_angle(0)leftMotor.run_angle(25,360,Stop.HOLD,False)
rightMotor.run_angle(25,270,Stop.HOLD,True)
wait(8000)angleOut = gyro.angle()
print("gyro phase I",angleOut)
Nothing too it. We import the required classes, define our motors and the gyro sensor and then use one of the commands I mentioned earlier to move the two wheels different amounts. Finishing by printing out the angle returned by our gyro.
Here are the figures. We’re interested at this point between the difference in distance that the left and right motors travelled. We have 360 — 270, so the difference is 90 clicks.
We than divided this number by distance between the two wheels, so in our case 90 / 125 or 0.44. Using trigonometry this time we can work out the angle that we’re off the line by and correct it.