MicroPython Tutorial XII

Mark Lucking
3 min readAug 1, 2019

In the last tutorial we covered two things, a new command DriveBase and an industry developed means of getting things more predicable called a PID Controller. Both a means to help you stay on the straight and narrow.

You recall our final script in the last tutorial shows the driveBase command with the gyro. You can use the giro in fact to try and correct any drift when getting him to drive in a straight line.

#!/usr/bin/env pybricks-micropythonfrom pybricks import ev3brick as brick
from pybricks.parameters import Button, Port, Stop
from pybricks.ev3devices import Motor, GyroSensor
from pybricks.tools import wait
from pybricks.robotics import DriveBase
leftMotor = Motor(Port.B)
rightMotor = Motor(Port.C)
leftMotor.reset_angle(0)
rightMotor.reset_angle(0)
gyro = GyroSensor(Port.S2)
gyro.reset_angle(0)
wheelDiam = 56
wheelDist = 114
speed = 50
angle = 0
fudge = 0.5
robot = DriveBase(leftMotor, rightMotor, wheelDiam, wheelDist)while True:
robot.drive(speed,angle)
drift = leftMotor.angle() - rightMotor.angle()
angle = (drift * fudge) * -1
print("drift",gyro.angle())

The most important line here is the fudge value. You will need to play around with the fudge value for your robot until it works.

The feed back loop control is very common in systems like this, and there is an even more accurate…

--

--

Mark Lucking
Mark Lucking

Written by Mark Lucking

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

No responses yet