2020-11-09 20:34:43 +00:00
|
|
|
import numpy as np
|
|
|
|
import math
|
|
|
|
import time
|
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
from controller import ControllerBase
|
|
|
|
|
2020-11-17 20:23:22 +00:00
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
class PIDController(ControllerBase):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.t = None
|
2020-11-09 20:34:43 +00:00
|
|
|
|
2020-11-14 14:22:41 +00:00
|
|
|
self.P_angle = 0.4
|
2020-11-09 21:07:09 +00:00
|
|
|
self.I_angle = 0.35
|
2020-11-09 20:34:43 +00:00
|
|
|
self.D_angle = 0.1
|
|
|
|
|
|
|
|
self.P_pos = 0.50
|
|
|
|
self.I_pos = 0.3
|
|
|
|
self.D_pos = 0.1
|
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
self.mode = 'combined'
|
|
|
|
|
|
|
|
self.e_angle_old = 0.0
|
|
|
|
self.e_pos_old = 0.0
|
|
|
|
|
|
|
|
self.i = 0.0
|
|
|
|
self.i_angle = 0.0
|
|
|
|
self.i_pos = 0.0
|
|
|
|
|
|
|
|
def set_target_position(self, target_pos):
|
|
|
|
super(PIDController, self).set_target_position(target_pos)
|
|
|
|
self.mode = 'combined'
|
|
|
|
self.e_angle_old = 0.0
|
|
|
|
self.e_pos_old = 0.0
|
|
|
|
|
|
|
|
self.i = 0.0
|
|
|
|
self.i_angle = 0.0
|
|
|
|
self.i_pos = 0.0
|
|
|
|
|
|
|
|
def compute_control(self, state):
|
|
|
|
# measure state
|
|
|
|
x_pred = state[1:]
|
|
|
|
|
|
|
|
if self.t is None:
|
|
|
|
dt = 0.1
|
|
|
|
else:
|
2020-11-18 20:38:41 +00:00
|
|
|
dt = time.time() - self.t # time since last control was applied
|
2020-11-09 21:07:09 +00:00
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
if self.mode == 'angle':
|
|
|
|
# compute angle such that robot faces to target point
|
|
|
|
target_angle = self.target_pos[2]
|
2020-11-09 21:07:09 +00:00
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
angles_unwrapped = np.unwrap([x_pred[2], target_angle]) # unwrap angle to avoid jump in data
|
2020-11-09 21:07:09 +00:00
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
e_angle = angles_unwrapped[0] - angles_unwrapped[1] # angle difference
|
|
|
|
p = self.P_angle * e_angle
|
|
|
|
self.i += self.I_angle * dt * (e_angle + self.e_angle_old)/2.0 # trapezoidal rule
|
|
|
|
d = self.D_angle * (e_angle - self.e_angle_old)/dt
|
|
|
|
|
|
|
|
u1 = p + self.i + d
|
|
|
|
u2 = - u1
|
|
|
|
|
|
|
|
self.e_angle_old = e_angle
|
|
|
|
|
|
|
|
elif self.mode == 'combined':
|
|
|
|
# compute angle such that robot faces to target point
|
|
|
|
v = self.target_pos[0:2] - x_pred[0:2]
|
|
|
|
target_angle = math.atan2(v[1], v[0])
|
|
|
|
|
|
|
|
angles_unwrapped = np.unwrap([x_pred[2], target_angle]) # unwrap angle to avoid jump in data
|
|
|
|
|
|
|
|
e_angle = angles_unwrapped[0] - angles_unwrapped[1] # angle difference
|
2021-09-09 19:36:13 +00:00
|
|
|
e_pos = np.linalg.norm(v[0:2])
|
2020-11-14 15:41:16 +00:00
|
|
|
|
2021-09-09 19:36:13 +00:00
|
|
|
if e_pos < 0.03:
|
2020-11-14 15:41:16 +00:00
|
|
|
self.mode = 'angle'
|
|
|
|
self.e_angle_old = 0
|
|
|
|
self.e_pos_old = 0
|
|
|
|
self.i_angle = 0
|
|
|
|
self.i_pos = 0
|
|
|
|
u1 = 0
|
|
|
|
u2 = 0
|
|
|
|
else:
|
|
|
|
forward = abs(e_angle) < np.pi/2.0
|
|
|
|
|
|
|
|
if not forward:
|
|
|
|
if e_angle > np.pi/2.0:
|
|
|
|
e_angle -= np.pi
|
|
|
|
else:
|
|
|
|
e_angle += np.pi
|
2020-11-09 21:07:09 +00:00
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
p_angle = self.P_angle * e_angle
|
|
|
|
self.i_angle += self.I_angle * dt * (e_angle + self.e_angle_old) / 2.0 # trapezoidal rule
|
|
|
|
d_angle = self.D_angle * (e_angle - self.e_angle_old) / dt
|
2020-11-09 21:07:09 +00:00
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
p_pos = self.P_pos * e_pos
|
|
|
|
self.i_pos += self.I_pos * dt * (e_pos + self.e_pos_old) / 2.0 # trapezoidal rule
|
|
|
|
d_pos = self.D_pos * (e_pos - self.e_pos_old) / dt
|
2020-11-09 20:34:43 +00:00
|
|
|
|
2020-11-14 15:41:16 +00:00
|
|
|
if forward:
|
|
|
|
u1 = p_angle + p_pos + self.i_angle + self.i_pos + d_angle + d_pos
|
|
|
|
u2 = - p_angle + p_pos - self.i_angle + self.i_pos - d_angle + d_pos
|
2020-11-09 20:34:43 +00:00
|
|
|
else:
|
2020-11-14 15:41:16 +00:00
|
|
|
u1 = p_angle - p_pos + self.i_angle - self.i_pos + d_angle - d_pos
|
|
|
|
u2 = - p_angle - p_pos - self.i_angle - self.i_pos - d_angle - d_pos
|
|
|
|
|
|
|
|
self.e_pos_old = e_pos
|
|
|
|
self.e_angle_old = e_angle
|
|
|
|
else:
|
|
|
|
u1 = 0.0
|
|
|
|
u2 = 0.0
|
2020-11-17 20:23:22 +00:00
|
|
|
return u1, u2
|
2020-11-18 20:38:41 +00:00
|
|
|
|
2021-09-09 19:36:13 +00:00
|
|
|
def stop(self):
|
|
|
|
super(PIDController, self).stop()
|
|
|
|
self.t = None
|
|
|
|
|
2020-11-18 20:38:41 +00:00
|
|
|
def apply_control(self, control):
|
|
|
|
self.t = time.time() # save time when the most recent control was applied
|
2020-11-22 14:57:53 +00:00
|
|
|
super(PIDController, self).apply_control(control)
|