-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnityPID.py
175 lines (133 loc) · 4.77 KB
/
UnityPID.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
import time
import numpy as np
from math import *
'''
PID controller based on
https://vazgriz.com/621/pid-controllers/
'''
class PIDController:
# pg: p-gain
# ig: i-gain
# dg: d-gain
def __init__(self, pg, ig, dg):
self.proportionalGain = pg
self.integralGain = ig
self.derivativeGain = dg
self.valueLast = 0
self.integrationStored = 0
self.errorOutput = ""
# use for linear values e.g. x, y, z
def update(self, dt: float, currentValue: float, targetValue: float):
error = targetValue - currentValue
P = self.proportionalGain * error
self.integrationStored = self.integrationStored + (error * dt)
I = self.integralGain * self.integrationStored
# I = max(min(I, 5), -5)
valueRateOfChange = (currentValue - self.valueLast) / dt
self.valueLast = currentValue
D = self.derivativeGain * -valueRateOfChange
self.errorOutput = f"Pe: {error}, De: {-valueRateOfChange}"
return P + I + D
# all angles in degree
def angleDifference(self, a: float, b: float):
return (a - b + 540) % 360 - 180
# use for angle values e.g. yaw
def updateAngle(self, dt: float, currentAngle: float, targetAngle: float):
error = self.angleDifference(targetAngle, currentAngle)
print(f"target: {targetAngle}, current: {currentAngle}, error: {error}")
P = self.proportionalGain * error
self.integrationStored = self.integrationStored + (error * dt)
I = self.integralGain * self.integrationStored
I = max(min(I, 2), -2)
valueRateOfChange = self.angleDifference(currentAngle, self.valueLast) / dt
self.valueLast = currentAngle
D = self.derivativeGain * -valueRateOfChange
self.errorOutput = f"Pe: {error}, De: {-valueRateOfChange}"
return max(min(P + I + D, self.yaw_limit), -self.yaw_limit)
'''
velocity pid controller
outputs a velocity command in world frame,
based on input x, y, z, and yaw in world frame
'''
class VelocityPID:
'''
pid controller
kp: np.array 3, p gain for x, y, z
ki: np.array 3, i gain for x, y, z
kd: np.array 3, d gain for x, y, z
yg: np.array 3, yaw gains, p, i, d
dthresh: float, distance threshold
athresh: float, angle threshold
'''
def __init__(self, kp=np.array([0., 0., 0.]), ki=np.array([0., 0., 0.]), kd=np.array([0., 0., 0.]), yg=0.,
dthresh=.1, athresh=.1):
# in degrees, max requested change
self.yaw_limit = 10
# state
self.x = 0
self.y = 0
self.z = 0
self.yaw = 0
# previous
self.previous_distance_error = np.array([0, 0, 0])
self.previous_angle_error = np.array([0, 0, 0])
self.previous_integral_error = np.array([0, 0, 0])
# Default state
self.x_goal = 0
self.y_goal = 0
self.z_goal = 0
self.yaw_goal = 0
# gains
self.Kp = kp
self.Ki = ki
self.Kd = kd
self.yaw_gain = yg
# output values
self.velocity_out = np.array([0, 0, 0])
self.yaw_out = 0
# error publisher
self.errorOutput = ""
self.errorOutput1 = ""
self.errorOutput2 = ""
self.errorOutput3 = ""
# PID controller objects
self.cx = PIDController(kp[0], ki[0], kd[0])
self.cy = PIDController(kp[1], ki[1], kd[1])
self.cz = PIDController(kp[2], ki[2], kd[2])
self.cyaw = PIDController(yg[0], yg[1], yg[2])
# [x, y, z, yaw]
def setState(self, state):
self.x = state[0]
self.y = state[1]
self.z = state[2]
self.yaw = state[3]
# [x, y, z, yaw]
def setGoal(self, state):
self.x_goal = state[0]
self.y_goal = state[1]
self.z_goal = state[2]
self.yaw_goal = state[3]
# returns output of pid controller
def getVelocityYaw(self):
return (self.velocity_out, self.yaw_out)
# df: time delta since last update
def update(self, dt):
# Retrieve the UAV's state
x = self.x
y = self.y
z = self.z
yaw = self.yaw
goal_x = self.x_goal
goal_y = self.y_goal
goal_z = self.z_goal
# set current output values
self.velocity_out = np.array(
[self.cx.update(dt, x, goal_x), self.cy.update(dt, y, goal_y), self.cz.update(dt, z, goal_z)])
self.yaw_out = self.cyaw.update(dt, yaw, self.yaw_goal)
self.yaw_out = max(min(self.yaw_out, self.yaw_limit), -self.yaw_limit)
# error output
self.errorOutput = ""
self.errorOutput1 = f"x: {self.cx.errorOutput}"
self.errorOutput2 = f"y: {self.cy.errorOutput}"
self.errorOutput3 = f"z: {self.cz.errorOutput}"