Numerical integration and differentation in the PID algorithm: Difference between revisions
| Line 108: | Line 108: | ||
df["steering"] = df.sum(axis=1) | df["steering"] = df.sum(axis=1) | ||
df.plot() | df.plot() | ||
<syntaxhighlight> | </syntaxhighlight> | ||
== Exercises == | == Exercises == | ||
Revision as of 15:43, 17 October 2020
Introduction
PID is elaborated algorithm to use in numerous ways. Though it might difficult to set up the parameters correctly, it makes the robot move more smoothly and thus more rapid. The P is for Proportional, I is the Integral and D is Differential. We use PID in line following.
Aim
Proportion, Integration and Derivation.
Plotting, Error function.
Robot
Almost any robot will do, but we will use Asimov 2/ Verne robot.
Sensors
A light sensor is used.
Example Video
Theory
If applying only proportional coding, the steering is affected by the amount of difference in reflectance from the average. The integral term is a sum (or integral) of the previous (historical) errors, and that is used to correct steering if it has been wrong for a while. The differential is used to forecast the next error based on the previous errors, and we use it to steer more or less depending on the previous error.
| Term | Math | Meaning |
|---|---|---|
| Error | Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \epsilon} | Sensor value - target value |
| Integral | Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle I} | Integral + error |
| Derivative | Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle D} | Error - previous error |
Steering is Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle S = K_p \epsilon + K_i I + K_d D } .
We use three different parameters Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle K_p} , Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle K_i} and Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle K_d} . Furthermore, the integral needs to forget the very old data, and thus we need an Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \alpha} term (which can be multiplied together to Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle K_i} already). Of course, the parameters depend on the line and robot.
Example Code
Python v2 Ev3dev
Set the folders properly and by pressing F5 the script will run.
#!/usr/bin/env python3
# https://sites.google.com/site/ev3devpython/
#84 is Max
#5 is Min
from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.motor import MoveSteering, OUTPUT_B, OUTPUT_C
import os
os.system('setfont Lat15-TerminusBold32x16')
import csv
f = open("steering.csv","w")
writer = csv.writer(f)
steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
cl = ColorSensor()
clMax = 84
clMin = 5
clAve = (clMax + clMin)/2
Kp= .5
Ki= 0.4
Kd= 1.4
clN = clAve
steering = 0
integral = 0
derivative = 0
error_prev = 0
while clN < 95:
clN = cl.reflected_light_intensity
error = ( clN - clAve )
integral = Ki*( integral + error )
derivative = Kd*( error - error_prev )
error_prev = error
error = Kp*error
writer.writerow(( error, integral, derivative) )
steering = error + integral + derivative
steering = min(steering, 100)
steering = max(steering, -100)
print( clN )
steer_pair.on(steering=steering, speed=-20)
steer_pair.off()
f.close()
Python analysis on a computer
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('C:/Users/markkuleino/Downloads/steering.csv', names=["error", "integral", "derivative"])
df["steering"] = df.sum(axis=1)
df.plot()
Exercises
References
See [PID Controller For Lego Mindstorms Robots]
Back to Mahtavaa Matematiikkaa 2020
