-
Notifications
You must be signed in to change notification settings - Fork 337
/
SimplePIDControl.py
240 lines (211 loc) · 10.3 KB
/
SimplePIDControl.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import numpy as np
import pybullet as p
from gym_pybullet_drones.control.BaseControl import BaseControl
from gym_pybullet_drones.utils.enums import DroneModel
from gym_pybullet_drones.utils.utils import nnlsRPM
class SimplePIDControl(BaseControl):
"""Generic PID control class without yaw control.
Based on https://github.com/prfraanje/quadcopter_sim.
"""
################################################################################
def __init__(self,
drone_model: DroneModel,
g: float=9.8
):
"""Common control classes __init__ method.
Parameters
----------
drone_model : DroneModel
The type of drone to control (detailed in an .urdf file in folder `assets`).
g : float, optional
The gravitational acceleration in m/s^2.
"""
super().__init__(drone_model=drone_model, g=g)
if self.DRONE_MODEL != DroneModel.HB:
print("[ERROR] in SimplePIDControl.__init__(), SimplePIDControl requires DroneModel.HB")
exit()
self.P_COEFF_FOR = np.array([.1, .1, .2])
self.I_COEFF_FOR = np.array([.0001, .0001, .0001])
self.D_COEFF_FOR = np.array([.3, .3, .4])
self.P_COEFF_TOR = np.array([.3, .3, .05])
self.I_COEFF_TOR = np.array([.0001, .0001, .0001])
self.D_COEFF_TOR = np.array([.3, .3, .5])
self.MAX_ROLL_PITCH = np.pi/6
self.L = self._getURDFParameter('arm')
self.THRUST2WEIGHT_RATIO = self._getURDFParameter('thrust2weight')
self.MAX_RPM = np.sqrt((self.THRUST2WEIGHT_RATIO*self.GRAVITY) / (4*self.KF))
self.MAX_THRUST = (4*self.KF*self.MAX_RPM**2)
self.MAX_XY_TORQUE = (self.L*self.KF*self.MAX_RPM**2)
self.MAX_Z_TORQUE = (2*self.KM*self.MAX_RPM**2)
self.A = np.array([ [1, 1, 1, 1], [0, 1, 0, -1], [-1, 0, 1, 0], [-1, 1, -1, 1] ])
self.INV_A = np.linalg.inv(self.A)
self.B_COEFF = np.array([1/self.KF, 1/(self.KF*self.L), 1/(self.KF*self.L), 1/self.KM])
self.reset()
################################################################################
def reset(self):
"""Resets the control classes.
The previous step's and integral errors for both position and attitude are set to zero.
"""
super().reset()
#### Initialized PID control variables #####################
self.last_pos_e = np.zeros(3)
self.integral_pos_e = np.zeros(3)
self.last_rpy_e = np.zeros(3)
self.integral_rpy_e = np.zeros(3)
################################################################################
def computeControl(self,
control_timestep,
cur_pos,
cur_quat,
cur_vel,
cur_ang_vel,
target_pos,
target_rpy=np.zeros(3),
target_vel=np.zeros(3),
target_rpy_rates=np.zeros(3)
):
"""Computes the PID control action (as RPMs) for a single drone.
This methods sequentially calls `_simplePIDPositionControl()` and `_simplePIDAttitudeControl()`.
Parameters `cur_ang_vel`, `target_rpy`, `target_vel`, and `target_rpy_rates` are unused.
Parameters
----------
control_timestep : float
The time step at which control is computed.
cur_pos : ndarray
(3,1)-shaped array of floats containing the current position.
cur_quat : ndarray
(4,1)-shaped array of floats containing the current orientation as a quaternion.
cur_vel : ndarray
(3,1)-shaped array of floats containing the current velocity.
cur_ang_vel : ndarray
(3,1)-shaped array of floats containing the current angular velocity.
target_pos : ndarray
(3,1)-shaped array of floats containing the desired position.
target_rpy : ndarray, optional
(3,1)-shaped array of floats containing the desired orientation as roll, pitch, yaw.
target_vel : ndarray, optional
(3,1)-shaped array of floats containing the desired velocity.
target_rpy_rates : ndarray, optional
(3,1)-shaped array of floats containing the the desired roll, pitch, and yaw rates.
Returns
-------
ndarray
(4,1)-shaped array of integers containing the RPMs to apply to each of the 4 motors.
ndarray
(3,1)-shaped array of floats containing the current XYZ position error.
float
The current yaw error.
"""
self.control_counter += 1
if target_rpy[2]!=0:
print("\n[WARNING] ctrl it", self.control_counter, "in SimplePIDControl.computeControl(), desired yaw={:.0f}deg but locked to 0. for DroneModel.HB".format(target_rpy[2]*(180/np.pi)))
thrust, computed_target_rpy, pos_e = self._simplePIDPositionControl(control_timestep,
cur_pos,
cur_quat,
target_pos
)
rpm = self._simplePIDAttitudeControl(control_timestep,
thrust,
cur_quat,
computed_target_rpy
)
cur_rpy = p.getEulerFromQuaternion(cur_quat)
return rpm, pos_e, computed_target_rpy[2] - cur_rpy[2]
################################################################################
def _simplePIDPositionControl(self,
control_timestep,
cur_pos,
cur_quat,
target_pos
):
"""Simple PID position control (with yaw fixed to 0).
Parameters
----------
control_timestep : float
The time step at which control is computed.
cur_pos : ndarray
(3,1)-shaped array of floats containing the current position.
cur_quat : ndarray
(4,1)-shaped array of floats containing the current orientation as a quaternion.
target_pos : ndarray
(3,1)-shaped array of floats containing the desired position.
Returns
-------
float
The target thrust along the drone z-axis.
ndarray
(3,1)-shaped array of floats containing the target roll, pitch, and yaw.
float
The current position error.
"""
pos_e = target_pos - np.array(cur_pos).reshape(3)
d_pos_e = (pos_e - self.last_pos_e) / control_timestep
self.last_pos_e = pos_e
self.integral_pos_e = self.integral_pos_e + pos_e*control_timestep
#### PID target thrust #####################################
target_force = np.array([0, 0, self.GRAVITY]) \
+ np.multiply(self.P_COEFF_FOR, pos_e) \
+ np.multiply(self.I_COEFF_FOR, self.integral_pos_e) \
+ np.multiply(self.D_COEFF_FOR, d_pos_e)
target_rpy = np.zeros(3)
sign_z = np.sign(target_force[2])
if sign_z == 0:
sign_z = 1
#### Target rotation #######################################
target_rpy[0] = np.arcsin(-sign_z*target_force[1] / np.linalg.norm(target_force))
target_rpy[1] = np.arctan2(sign_z*target_force[0], sign_z*target_force[2])
target_rpy[2] = 0.
target_rpy[0] = np.clip(target_rpy[0], -self.MAX_ROLL_PITCH, self.MAX_ROLL_PITCH)
target_rpy[1] = np.clip(target_rpy[1], -self.MAX_ROLL_PITCH, self.MAX_ROLL_PITCH)
cur_rotation = np.array(p.getMatrixFromQuaternion(cur_quat)).reshape(3, 3)
thrust = np.dot(cur_rotation, target_force)
return thrust[2], target_rpy, pos_e
################################################################################
def _simplePIDAttitudeControl(self,
control_timestep,
thrust,
cur_quat,
target_rpy
):
"""Simple PID attitude control (with yaw fixed to 0).
Parameters
----------
control_timestep : float
The time step at which control is computed.
thrust : float
The target thrust along the drone z-axis.
cur_quat : ndarray
(4,1)-shaped array of floats containing the current orientation as a quaternion.
target_rpy : ndarray
(3,1)-shaped array of floats containing the computed the target roll, pitch, and yaw.
Returns
-------
ndarray
(4,1)-shaped array of integers containing the RPMs to apply to each of the 4 motors.
"""
cur_rpy = p.getEulerFromQuaternion(cur_quat)
rpy_e = target_rpy - np.array(cur_rpy).reshape(3,)
if rpy_e[2] > np.pi:
rpy_e[2] = rpy_e[2] - 2*np.pi
if rpy_e[2] < -np.pi:
rpy_e[2] = rpy_e[2] + 2*np.pi
d_rpy_e = (rpy_e - self.last_rpy_e) / control_timestep
self.last_rpy_e = rpy_e
self.integral_rpy_e = self.integral_rpy_e + rpy_e*control_timestep
#### PID target torques ####################################
target_torques = np.multiply(self.P_COEFF_TOR, rpy_e) \
+ np.multiply(self.I_COEFF_TOR, self.integral_rpy_e) \
+ np.multiply(self.D_COEFF_TOR, d_rpy_e)
return nnlsRPM(thrust=thrust,
x_torque=target_torques[0],
y_torque=target_torques[1],
z_torque=target_torques[2],
counter=self.control_counter,
max_thrust=self.MAX_THRUST,
max_xy_torque=self.MAX_XY_TORQUE,
max_z_torque=self.MAX_Z_TORQUE,
a=self.A,
inv_a=self.INV_A,
b_coeff=self.B_COEFF,
gui=True
)