forked from augcog/ROAR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bce6c67
commit c6230fa
Showing
8 changed files
with
216 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,26 @@ | ||
{ | ||
"longitudinal_controller": { | ||
"40": { | ||
"Kp": 0.8, | ||
"Kd": 0.4, | ||
"Ki": 0 | ||
}, | ||
"60": { | ||
"Kp": 0.5, | ||
"Kd": 0.3, | ||
"Ki": 0 | ||
}, | ||
"90": { | ||
"Kp": 0.3, | ||
"Kd": 0.3, | ||
"Ki": 0 | ||
}, | ||
"150": { | ||
"Kp": 0.2, | ||
"Kd": 0.2, | ||
"Ki": 0.1 | ||
} | ||
"10": { | ||
"Kp": 0.1, | ||
"Kd": 0.0, | ||
"Ki": 0.005 | ||
} | ||
}, | ||
"latitudinal_controller": { | ||
"60": { | ||
"Kp": 0.8, | ||
"Kd": 0.1, | ||
"Ki": 0.1 | ||
"3": { | ||
"Kp": 0.006, | ||
"Kd": 0.075, | ||
"Ki": 0.00025 | ||
}, | ||
"100": { | ||
"Kp": 0.6, | ||
"Kd": 0.2, | ||
"Ki": 0.1 | ||
"4": { | ||
"Kp": 0.004, | ||
"Kd": 0, | ||
"Ki": 0 | ||
}, | ||
"150": { | ||
"Kp": 0.5, | ||
"Kd": 0.2, | ||
"Ki": 0.1 | ||
} | ||
"6": { | ||
"Kp": 0.002, | ||
"Kd": 0, | ||
"Ki": 0 | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
ROAR/control_module/real_world_image_based_pid_controller.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import json | ||
|
||
from ROAR.control_module.controller import Controller | ||
from ROAR.utilities_module.data_structures_models import Transform | ||
from ROAR.utilities_module.vehicle_models import Vehicle | ||
from ROAR.utilities_module.vehicle_models import VehicleControl | ||
from collections import deque | ||
import numpy as np | ||
from ROAR_iOS.config_model import iOSConfig | ||
from pathlib import Path | ||
|
||
|
||
class RealWorldImageBasedPIDController(Controller): | ||
def __init__(self, agent, **kwargs): | ||
super().__init__(agent, **kwargs) | ||
long_error_deque_length = 10 | ||
lat_error_deque_length = 10 | ||
self.lat_error_queue = deque(maxlen=lat_error_deque_length) # this is how much error you want to accumulate | ||
self.long_error_queue = deque(maxlen=long_error_deque_length) # this is how much error you want to accumulate | ||
self.target_speed = 10 # m / s | ||
self.config = json.load(Path(self.agent.agent_settings.pid_config_file_path).open('r')) | ||
self.long_config = self.config["longitudinal_controller"] | ||
self.lat_config = self.config["latitudinal_controller"] | ||
|
||
def run_in_series(self, next_waypoint=None, **kwargs) -> VehicleControl: | ||
steering = self.lateral_pid_control() | ||
throttle = self.long_pid_control() | ||
return VehicleControl(throttle=throttle, steering=steering) | ||
|
||
def lateral_pid_control(self) -> float: | ||
error = self.agent.kwargs.get("lat_error", 0) | ||
error_dt = 0 if len(self.lat_error_queue) == 0 else error - self.lat_error_queue[-1] | ||
self.lat_error_queue.append(error) | ||
error_it = sum(self.lat_error_queue) | ||
k_p, k_d, k_i = self.find_k_values(self.agent.vehicle, self.lat_config) | ||
# print(f"Speed = {self.agent.vehicle.get_speed(self.agent.vehicle)}" | ||
# f"kp, kd, ki = {k_p, k_d, k_i} ") | ||
|
||
e_p = k_p * error | ||
e_d = k_d * error_dt | ||
e_i = k_i * error_it | ||
lat_control = np.clip((e_p + e_d + e_i), -1, 1) | ||
# print(f"speed = {self.agent.vehicle.get_speed(self.agent.vehicle)} " | ||
# f"e = {round((e_p + e_d + e_i), 3)}, " | ||
# f"e_p={round(e_p, 3)}," | ||
# f"e_d={round(e_d, 3)}," | ||
# f"e_i={round(e_i, 3)}," | ||
# f"lat_control={lat_control}") | ||
# print() | ||
return lat_control | ||
|
||
def long_pid_control(self) -> float: | ||
k_p, k_d, k_i = self.find_k_values(self.agent.vehicle, self.long_config) | ||
e = self.target_speed - self.agent.vehicle.get_speed(self.agent.vehicle) | ||
neutral = -90 | ||
incline = self.agent.vehicle.transform.rotation.pitch - neutral | ||
e = e * - 1 if incline < -10 else e | ||
self.long_error_queue.append(e) | ||
de = 0 if len(self.long_error_queue) < 2 else self.long_error_queue[-2] - self.long_error_queue[-1] | ||
ie = 0 if len(self.long_error_queue) < 2 else np.sum(self.long_error_queue) | ||
incline = np.clip(incline, -20, 20) | ||
|
||
e_p = k_p * e | ||
e_d = k_d * de | ||
e_i = k_i * ie | ||
e_incline = 0.015 * incline | ||
total_error = e_p + e_d + e_i + e_incline | ||
long_control = np.clip(total_error, 0, 1) | ||
# print(f"speed = {self.agent.vehicle.get_speed(self.agent.vehicle)} " | ||
# f"e = {round(total_error,3)}, " | ||
# f"e_p={round(e_p,3)}," | ||
# f"e_d={round(e_d,3)}," | ||
# f"e_i={round(e_i,3)}," | ||
# f"e_incline={round(e_incline, 3)}, " | ||
# f"long_control={long_control}") | ||
return long_control | ||
|
||
@staticmethod | ||
def find_k_values(vehicle: Vehicle, config: dict) -> np.array: | ||
current_speed = Vehicle.get_speed(vehicle=vehicle) | ||
k_p, k_d, k_i = 1, 0, 0 | ||
for speed_upper_bound, kvalues in config.items(): | ||
speed_upper_bound = float(speed_upper_bound) | ||
if current_speed < speed_upper_bound: | ||
k_p, k_d, k_i = kvalues["Kp"], kvalues["Kd"], kvalues["Ki"] | ||
break | ||
return np.array([k_p, k_d, k_i]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import logging | ||
from ROAR.utilities_module.utilities import get_ip | ||
import qrcode | ||
import cv2 | ||
import numpy as np | ||
import socket | ||
|
||
|
||
def showIPUntilAckUDP(): | ||
img = np.array(qrcode.make(f"{get_ip()}").convert('RGB')) | ||
success = False | ||
addr = None | ||
|
||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
s.settimeout(0.1) | ||
|
||
try: | ||
s.bind((get_ip(), 8008)) | ||
while success is False: | ||
try: | ||
cv2.imshow("Scan this code to connect to phone", img) | ||
k = cv2.waitKey(1) & 0xff | ||
seg, addr = s.recvfrom(1024) # this command might timeout | ||
|
||
if k == ord('q') or k == 27: | ||
s.close() | ||
break | ||
addr = addr | ||
success = True | ||
for i in range(10): | ||
print("data sent") | ||
s.sendto(b"hi", addr) | ||
except socket.timeout as e: | ||
logging.info(f"Please tap on the ip address to scan QR code. ({get_ip()}:{8008}). {e}") | ||
except Exception as e: | ||
logging.error(f"Unable to bind socket: {e}") | ||
finally: | ||
s.close() | ||
cv2.destroyWindow("Scan this code to connect to phone") | ||
return success, addr[0] | ||
|
||
|
||
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(name)s ' | ||
'- %(message)s', | ||
datefmt="%H:%M:%S", | ||
level=logging.DEBUG) | ||
print(showIPUntilAckUDP()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.