MicroPython Tutorial XII

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)
gyro = GyroSensor(Port.S2)
wheelDiam = 56
wheelDist = 114
speed = 50
angle = 0
fudge = 0.5
robot = DriveBase(leftMotor, rightMotor, wheelDiam, wheelDist)while True:
drift = leftMotor.angle() - rightMotor.angle()
angle = (drift * fudge) * -1

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 means of doing to called a PID algorithm or PID controller. PID is an acronym for the words proportional, integral, derivative; all calculus terms I fear. Although it all sounds far more scary than it is.

A formula for a PID controller contains four variables which all combine to keep the line following of your robot as smooth as possible. It looks like this.

Correction = (error * Kp) + (intergral * Ki) + (derivative * Kd)

The K values are all constants that you will need to tweek like the fudge value we talked about in the previous example. Let jump right into the implementation.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor, GyroSensor
from pybricks.parameters import Port, Stop
from pybricks.tools import print, wait
from pybricks.robotics import DriveBase
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)
gyro = GyroSensor(Port.S2)
robot = DriveBase(left_motor, right_motor, 56, 114)targetAG = 0Kp = 1
Ki = 0
Kd = 0
error = 0
lastError = 0
intergral = 0
deriative = 0
while True:
angle = gyro.angle()
error = (targetAG - angle)
direct = error * Kp
intergral = (intergral + error) * Ki
deriative = (error - lastError) * Kd
turn = direct + intergral + deriative
print("A",gyro.angle(),"P",direct,"I",intergral,"D",deriative, "sum",turn) lastError = error robot.drive(90,turn)

So the targetAG variable here is the angle we want to stick too, 0 degrees in our case. The error is the feedback value that comes from the gyro sensor. The error on the next line is the difference between the target value and the feedback. Then you have the first fudge value. Next you set an intergral value equal to itself and the error, and use our second fudge value. Next we set a deriative value to predict the coming error by subtracting the current error from the last one. Beyond that we have our third fudge value. And almost the final action we add them all together to get a value to turn on. Finally our last two actions are to set the last error [used to build the derivative] and execute a turn if needed.

You can apply the PID algorthm to any system that has some feedback mechanism within it. So you can use a PID controller to great improve a line following script for example.

What of the fudge values then. Well it works like this. When you start out neither the integral or the derivative are part of the equation, the fudge factor cancels them out. What you need to do is slow introduce/increase their values to make things work better.

There are no correct values fudge values for the K factors Ki and Kd, it is very much a trial and error. They will be fractions for sure, a Ki on 0.0005 and a Kd of 0.01 absolutely normal.

One final comment that I need credit the builderdude35 on youtube. He found the Ki aspect of this formula actually made things worse over time and recommended you don’t use for a highly predictable situation like a straight line.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store