# 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 brickfrom pybricks.parameters import Button, Port, Stopfrom pybricks.ev3devices import Motor, GyroSensorfrom pybricks.tools import waitfrom pybricks.robotics import DriveBaseleftMotor = Motor(Port.B)rightMotor = Motor(Port.C)leftMotor.reset_angle(0)rightMotor.reset_angle(0)gyro = GyroSensor(Port.S2)gyro.reset_angle(0)wheelDiam = 56wheelDist = 114speed = 50angle = 0fudge = 0.5robot = 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 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-micropythonfrom pybricks import ev3brick as brickfrom pybricks.ev3devices import Motor, GyroSensorfrom pybricks.parameters import Port, Stopfrom pybricks.tools import print, waitfrom pybricks.robotics import DriveBaseleft_motor = Motor(Port.B)right_motor = Motor(Port.C)gyro = GyroSensor(Port.S2)gyro.reset_angle(0)robot = DriveBase(left_motor, right_motor, 56, 114)targetAG = 0Kp = 1Ki = 0Kd = 0error = 0lastError = 0intergral = 0deriative = 0while 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.