Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c9c4b35e89 | |||
| 2ff69d64be | |||
| 29adc4b3df | |||
| 43067000db | |||
| 239fbf4087 | |||
| 387336b35a | |||
| 5a7952a91c | |||
| c5ecbae1cf | |||
| 87e5c6d931 | |||
| 28e7eb0692 | |||
| 6ffc9452f9 | |||
| 05db95385d | |||
| 8ab57840ba | |||
| 77120ecb7b | |||
| baaa0aa6ed | |||
| a52cdff013 | |||
| ec8b648a3b | |||
| f99fae68f6 | |||
| 294fe0bd79 | |||
| cf80becd17 | |||
| 6ab356a947 | |||
| 3a42120857 | |||
| 648cf32e9c | |||
|
|
092fb521e1 | ||
|
|
0e402c2b4c |
15
.gitignore
vendored
15
.gitignore
vendored
@@ -10,9 +10,14 @@ poetry.toml
|
|||||||
**/log/
|
**/log/
|
||||||
*.spec
|
*.spec
|
||||||
dist/
|
dist/
|
||||||
*.zip
|
*steps.zip
|
||||||
*.csv
|
|
||||||
*.json
|
|
||||||
*.xml
|
|
||||||
*.npz
|
|
||||||
*.pkl
|
*.pkl
|
||||||
|
best_model.zip
|
||||||
|
*.csv
|
||||||
|
*.npz
|
||||||
|
*.xml
|
||||||
|
*.json
|
||||||
|
*.yaml
|
||||||
|
*.iml
|
||||||
|
*.TXT
|
||||||
|
events.out.tfevents.*
|
||||||
|
|||||||
@@ -98,8 +98,12 @@ class Walk(Behavior):
|
|||||||
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
radian_joint_positions = np.deg2rad(list(robot.motor_positions.values()))
|
radian_joint_positions = np.deg2rad(
|
||||||
radian_joint_speeds = np.deg2rad(list(robot.motor_speeds.values()))
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
qpos_qvel_previous_action = np.vstack(
|
qpos_qvel_previous_action = np.vstack(
|
||||||
(
|
(
|
||||||
|
|||||||
0
command.md
Normal file
0
command.md
Normal file
15
readme.md
15
readme.md
@@ -74,21 +74,6 @@ poetry run ./build_binary.sh <team-name>
|
|||||||
|
|
||||||
Once binary generation is finished, the result will be inside the build folder, as ```<team-name>.tar.gz```
|
Once binary generation is finished, the result will be inside the build folder, as ```<team-name>.tar.gz```
|
||||||
|
|
||||||
### GYM
|
|
||||||
|
|
||||||
To use the gym, you need to install the following dependencies:
|
|
||||||
```bash
|
|
||||||
pip install gymnasium
|
|
||||||
pip install psutil
|
|
||||||
pip install stable-baselines3
|
|
||||||
```
|
|
||||||
|
|
||||||
Then, you can run gym examples under the ```GYM_CPU``` folder:
|
|
||||||
```bash
|
|
||||||
python3 -m scripts.gyms.Walk # Run the Walk gym example
|
|
||||||
# of course, you can run other gym examples
|
|
||||||
```
|
|
||||||
|
|
||||||
### Authors and acknowledgment
|
### Authors and acknowledgment
|
||||||
This project was developed and contributed by:
|
This project was developed and contributed by:
|
||||||
- **Chenxi Liu**
|
- **Chenxi Liu**
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import threading
|
|||||||
class Server():
|
class Server():
|
||||||
WATCHDOG_ENABLED = True
|
WATCHDOG_ENABLED = True
|
||||||
WATCHDOG_INTERVAL_SEC = 30.0
|
WATCHDOG_INTERVAL_SEC = 30.0
|
||||||
WATCHDOG_RSS_MB_LIMIT = 2000.0
|
WATCHDOG_RSS_MB_LIMIT = 800
|
||||||
|
|
||||||
def __init__(self, first_server_p, first_monitor_p, n_servers, no_render=True, no_realtime=True) -> None:
|
def __init__(self, first_server_p, first_monitor_p, n_servers, no_render=True, no_realtime=True) -> None:
|
||||||
try:
|
try:
|
||||||
@@ -109,14 +109,29 @@ class Server():
|
|||||||
def check_running_servers(self, psutil, first_server_p, first_monitor_p, n_servers):
|
def check_running_servers(self, psutil, first_server_p, first_monitor_p, n_servers):
|
||||||
''' Check if any server is running on chosen ports '''
|
''' Check if any server is running on chosen ports '''
|
||||||
found = False
|
found = False
|
||||||
p_list = [p for p in psutil.process_iter() if p.cmdline() and "rcssservermj" in " ".join(p.cmdline())]
|
|
||||||
range1 = (first_server_p, first_server_p + n_servers)
|
range1 = (first_server_p, first_server_p + n_servers)
|
||||||
range2 = (first_monitor_p, first_monitor_p + n_servers)
|
range2 = (first_monitor_p, first_monitor_p + n_servers)
|
||||||
bad_processes = []
|
bad_processes = []
|
||||||
|
|
||||||
|
def safe_cmdline(proc):
|
||||||
|
try:
|
||||||
|
return proc.cmdline()
|
||||||
|
except (psutil.ZombieProcess, psutil.NoSuchProcess, psutil.AccessDenied, OSError):
|
||||||
|
return []
|
||||||
|
|
||||||
|
p_list = []
|
||||||
|
for p in psutil.process_iter():
|
||||||
|
cmdline = safe_cmdline(p)
|
||||||
|
if cmdline and "rcssservermj" in " ".join(cmdline):
|
||||||
|
p_list.append(p)
|
||||||
|
|
||||||
for p in p_list:
|
for p in p_list:
|
||||||
# currently ignoring remaining default port when only one of the ports is specified (uncommon scenario)
|
# currently ignoring remaining default port when only one of the ports is specified (uncommon scenario)
|
||||||
ports = [int(arg) for arg in p.cmdline()[1:] if arg.isdigit()]
|
cmdline = safe_cmdline(p)
|
||||||
|
if not cmdline:
|
||||||
|
continue
|
||||||
|
|
||||||
|
ports = [int(arg) for arg in cmdline[1:] if arg.isdigit()]
|
||||||
if len(ports) == 0:
|
if len(ports) == 0:
|
||||||
ports = [60000, 60100] # default server ports (changing this is unlikely)
|
ports = [60000, 60100] # default server ports (changing this is unlikely)
|
||||||
|
|
||||||
@@ -128,7 +143,7 @@ class Server():
|
|||||||
print("\nThere are already servers running on the same port(s)!")
|
print("\nThere are already servers running on the same port(s)!")
|
||||||
found = True
|
found = True
|
||||||
bad_processes.append(p)
|
bad_processes.append(p)
|
||||||
print(f"Port(s) {','.join(conflicts)} already in use by \"{' '.join(p.cmdline())}\" (PID:{p.pid})")
|
print(f"Port(s) {','.join(conflicts)} already in use by \"{' '.join(cmdline)}\" (PID:{p.pid})")
|
||||||
|
|
||||||
if found:
|
if found:
|
||||||
print()
|
print()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from scripts.commons.UI import UI
|
|||||||
from shutil import copy
|
from shutil import copy
|
||||||
from stable_baselines3 import PPO
|
from stable_baselines3 import PPO
|
||||||
from stable_baselines3.common.base_class import BaseAlgorithm
|
from stable_baselines3.common.base_class import BaseAlgorithm
|
||||||
from stable_baselines3.common.callbacks import EvalCallback, CheckpointCallback, CallbackList, BaseCallback
|
from stable_baselines3.common.callbacks import EvalCallback, CheckpointCallback, CallbackList, BaseCallback, StopTrainingOnNoModelImprovement
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
# from world.world import World
|
# from world.world import World
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
@@ -266,11 +266,28 @@ class Train_Base():
|
|||||||
|
|
||||||
evaluate = bool(eval_env is not None and eval_freq is not None)
|
evaluate = bool(eval_env is not None and eval_freq is not None)
|
||||||
|
|
||||||
|
# Optional early stop: stop training when eval reward does not improve for N eval rounds.
|
||||||
|
no_improve_evals = int(os.environ.get("GYM_CPU_EARLY_STOP_NO_IMPROVE_EVALS", "0"))
|
||||||
|
min_evals_before_stop = int(os.environ.get("GYM_CPU_EARLY_STOP_MIN_EVALS", "6"))
|
||||||
|
stop_on_no_improve = None
|
||||||
|
if evaluate and no_improve_evals > 0:
|
||||||
|
stop_on_no_improve = StopTrainingOnNoModelImprovement(
|
||||||
|
max_no_improvement_evals=no_improve_evals,
|
||||||
|
min_evals=min_evals_before_stop,
|
||||||
|
verbose=1,
|
||||||
|
)
|
||||||
|
|
||||||
# Create evaluation callback
|
# Create evaluation callback
|
||||||
eval_callback = None if not evaluate else EvalCallback(eval_env, n_eval_episodes=eval_eps, eval_freq=eval_freq,
|
eval_callback = None if not evaluate else EvalCallback(
|
||||||
log_path=path,
|
eval_env,
|
||||||
best_model_save_path=path, deterministic=True,
|
n_eval_episodes=eval_eps,
|
||||||
render=False)
|
eval_freq=eval_freq,
|
||||||
|
log_path=path,
|
||||||
|
best_model_save_path=path,
|
||||||
|
deterministic=True,
|
||||||
|
render=False,
|
||||||
|
callback_after_eval=stop_on_no_improve,
|
||||||
|
)
|
||||||
|
|
||||||
# Create custom callback to display evaluations
|
# Create custom callback to display evaluations
|
||||||
custom_callback = None if not evaluate else Cyclic_Callback(eval_freq,
|
custom_callback = None if not evaluate else Cyclic_Callback(eval_freq,
|
||||||
|
|||||||
302
scripts/commons/UI.py
Normal file
302
scripts/commons/UI.py
Normal file
@@ -0,0 +1,302 @@
|
|||||||
|
from itertools import zip_longest
|
||||||
|
from math import inf
|
||||||
|
import math
|
||||||
|
import numpy as np
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
class UI():
|
||||||
|
console_width = 80
|
||||||
|
console_height = 24
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def read_particle(prompt, str_options, dtype=str, interval=[-inf,inf]):
|
||||||
|
'''
|
||||||
|
Read particle from user from a given dtype or from a str_options list
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
prompt : `str`
|
||||||
|
prompt to show user before reading input
|
||||||
|
str_options : `list`
|
||||||
|
list of str options (in addition to dtype if dtype is not str)
|
||||||
|
dtype : `class`
|
||||||
|
if dtype is str, then user must choose a value from str_options, otherwise it can also send a dtype value
|
||||||
|
interval : `list`
|
||||||
|
[>=min,<max] interval for numeric dtypes
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
choice : `int` or dtype
|
||||||
|
index of str_options (int) or value (dtype)
|
||||||
|
is_str_option : `bool`
|
||||||
|
True if `choice` is an index from str_options
|
||||||
|
'''
|
||||||
|
# Check if user has no choice
|
||||||
|
if dtype is str and len(str_options) == 1:
|
||||||
|
print(prompt, str_options[0], sep="")
|
||||||
|
return 0, True
|
||||||
|
elif dtype is int and interval[0] == interval[1]-1:
|
||||||
|
print(prompt, interval[0], sep="")
|
||||||
|
return interval[0], False
|
||||||
|
|
||||||
|
while True:
|
||||||
|
inp = input(prompt)
|
||||||
|
if inp in str_options:
|
||||||
|
return str_options.index(inp), True
|
||||||
|
|
||||||
|
if dtype is not str:
|
||||||
|
try:
|
||||||
|
inp = dtype(inp)
|
||||||
|
if inp >= interval[0] and inp < interval[1]:
|
||||||
|
return inp, False
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
print("Error: illegal input! Options:", str_options, f" or {dtype}" if dtype != str else "")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def read_int(prompt, min, max):
|
||||||
|
'''
|
||||||
|
Read int from user in a given interval
|
||||||
|
:param prompt: prompt to show user before reading input
|
||||||
|
:param min: minimum input (inclusive)
|
||||||
|
:param max: maximum input (exclusive)
|
||||||
|
:return: choice
|
||||||
|
'''
|
||||||
|
while True:
|
||||||
|
inp = input(prompt)
|
||||||
|
try:
|
||||||
|
inp = int(inp)
|
||||||
|
assert inp >= min and inp < max
|
||||||
|
return inp
|
||||||
|
except:
|
||||||
|
print(f"Error: illegal input! Choose number between {min} and {max-1}")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def print_table(data, titles=None, alignment=None, cols_width=None, cols_per_title=None, margins=None, numbering=None, prompt=None):
|
||||||
|
'''
|
||||||
|
Print table
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
data : `list`
|
||||||
|
list of columns, where each column is a list of items
|
||||||
|
titles : `list`
|
||||||
|
list of titles for each column, default is `None` (no titles)
|
||||||
|
alignment : `list`
|
||||||
|
list of alignments per column (excluding titles), default is `None` (left alignment for all cols)
|
||||||
|
cols_width : `list`
|
||||||
|
list of widths per column, default is `None` (fit to content)
|
||||||
|
Positive values indicate a fixed column width
|
||||||
|
Zero indicates that the column will fit its content
|
||||||
|
cols_per_title : `list`
|
||||||
|
maximum number of subcolumns per title, default is `None` (1 subcolumn per title)
|
||||||
|
margins : `list`
|
||||||
|
number of added leading and trailing spaces per column, default is `None` (margin=2 for all columns)
|
||||||
|
numbering : `list`
|
||||||
|
list of booleans per columns, indicating whether to assign numbers to each option
|
||||||
|
prompt : `str`
|
||||||
|
the prompt string, if given, is printed after the table before reading input
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
index : `int`
|
||||||
|
returns global index of selected item (relative to table)
|
||||||
|
col_index : `int`
|
||||||
|
returns local index of selected item (relative to column)
|
||||||
|
column : `int`
|
||||||
|
returns number of column of selected item (starts at 0)
|
||||||
|
* if `numbering` or `prompt` are `None`, `None` is returned
|
||||||
|
|
||||||
|
|
||||||
|
Example
|
||||||
|
-------
|
||||||
|
titles = ["Name","Age"]
|
||||||
|
data = [[John,Graciete], [30,50]]
|
||||||
|
alignment = ["<","^"] # 1st column is left-aligned, 2nd is centered
|
||||||
|
cols_width = [10,5] # 1st column's width=10, 2nd column's width=5
|
||||||
|
margins = [3,3]
|
||||||
|
numbering = [True,False] # prints: [0-John,1-Graciete][30,50]
|
||||||
|
prompt = "Choose a person:"
|
||||||
|
'''
|
||||||
|
|
||||||
|
#--------------------------------------------- parameters
|
||||||
|
cols_no = len(data)
|
||||||
|
|
||||||
|
if alignment is None:
|
||||||
|
alignment = ["<"]*cols_no
|
||||||
|
|
||||||
|
if cols_width is None:
|
||||||
|
cols_width = [0]*cols_no
|
||||||
|
|
||||||
|
if numbering is None:
|
||||||
|
numbering = [False]*cols_no
|
||||||
|
any_numbering = False
|
||||||
|
else:
|
||||||
|
any_numbering = True
|
||||||
|
|
||||||
|
if margins is None:
|
||||||
|
margins = [2]*cols_no
|
||||||
|
|
||||||
|
# Fit column to content + margin, if required
|
||||||
|
subcol = [] # subcolumn length and widths
|
||||||
|
for i in range(cols_no):
|
||||||
|
subcol.append([[],[]])
|
||||||
|
if cols_width[i] == 0:
|
||||||
|
numbering_width = 4 if numbering[i] else 0
|
||||||
|
if cols_per_title is None or cols_per_title[i] < 2:
|
||||||
|
cols_width[i] = max([len(str(item))+numbering_width for item in data[i]]) + margins[i]*2
|
||||||
|
else:
|
||||||
|
subcol[i][0] = math.ceil(len(data[i])/cols_per_title[i]) # subcolumn maximum length
|
||||||
|
cols_per_title[i] = math.ceil(len(data[i])/subcol[i][0]) # reduce number of columns as needed
|
||||||
|
cols_width[i] = margins[i]*(1+cols_per_title[i]) - (1 if numbering[i] else 0) # remove one if numbering, same as when printing
|
||||||
|
for j in range(cols_per_title[i]):
|
||||||
|
subcol_data_width = max([len(str(item))+numbering_width for item in data[i][j*subcol[i][0]:j*subcol[i][0]+subcol[i][0]]])
|
||||||
|
cols_width[i] += subcol_data_width # add subcolumn data width to column width
|
||||||
|
subcol[i][1].append(subcol_data_width) # save subcolumn data width
|
||||||
|
|
||||||
|
if titles is not None: # expand to acomodate titles if needed
|
||||||
|
cols_width[i] = max(cols_width[i], len(titles[i]) + margins[i]*2 )
|
||||||
|
|
||||||
|
if any_numbering:
|
||||||
|
no_of_items=0
|
||||||
|
cumulative_item_per_col=[0] # useful for getting the local index
|
||||||
|
for i in range(cols_no):
|
||||||
|
assert type(data[i]) == list, "In function 'print_table', 'data' must be a list of lists!"
|
||||||
|
|
||||||
|
if numbering[i]:
|
||||||
|
data[i] = [f"{n+no_of_items:3}-{d}" for n,d in enumerate(data[i])]
|
||||||
|
no_of_items+=len(data[i])
|
||||||
|
cumulative_item_per_col.append(no_of_items)
|
||||||
|
|
||||||
|
table_width = sum(cols_width)+cols_no-1
|
||||||
|
|
||||||
|
#--------------------------------------------- col titles
|
||||||
|
print(f'{"="*table_width}')
|
||||||
|
if titles is not None:
|
||||||
|
for i in range(cols_no):
|
||||||
|
print(f'{titles[i]:^{cols_width[i]}}', end='|' if i < cols_no - 1 else '')
|
||||||
|
print()
|
||||||
|
for i in range(cols_no):
|
||||||
|
print(f'{"-"*cols_width[i]}', end='+' if i < cols_no - 1 else '')
|
||||||
|
print()
|
||||||
|
|
||||||
|
#--------------------------------------------- merge subcolumns
|
||||||
|
if cols_per_title is not None:
|
||||||
|
for i,col in enumerate(data):
|
||||||
|
if cols_per_title[i] < 2:
|
||||||
|
continue
|
||||||
|
for k in range(subcol[i][0]): # create merged items
|
||||||
|
col[k] = (" "*margins[i]).join( f'{col[item]:{alignment[i]}{subcol[i][1][subcol_idx]}}'
|
||||||
|
for subcol_idx, item in enumerate(range(k,len(col),subcol[i][0])) )
|
||||||
|
del col[subcol[i][0]:] # delete repeated items
|
||||||
|
|
||||||
|
#--------------------------------------------- col items
|
||||||
|
for line in zip_longest(*data):
|
||||||
|
for i,item in enumerate(line):
|
||||||
|
l_margin = margins[i]-1 if numbering[i] else margins[i] # adjust margins when there are numbered options
|
||||||
|
item = "" if item is None else f'{" "*l_margin}{item}{" "*margins[i]}' # add margins
|
||||||
|
print(f'{item:{alignment[i]}{cols_width[i]}}', end='')
|
||||||
|
if i < cols_no - 1:
|
||||||
|
print(end='|')
|
||||||
|
print(end="\n")
|
||||||
|
print(f'{"="*table_width}')
|
||||||
|
|
||||||
|
#--------------------------------------------- prompt
|
||||||
|
if prompt is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not any_numbering:
|
||||||
|
print(prompt)
|
||||||
|
return None
|
||||||
|
|
||||||
|
index = UI.read_int(prompt, 0, no_of_items)
|
||||||
|
|
||||||
|
for i,n in enumerate(cumulative_item_per_col):
|
||||||
|
if index < n:
|
||||||
|
return index, index-cumulative_item_per_col[i-1], i-1
|
||||||
|
|
||||||
|
raise ValueError('Failed to catch illegal input')
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def print_list(data, numbering=True, prompt=None, divider=" | ", alignment="<", min_per_col=6):
|
||||||
|
'''
|
||||||
|
Print list - prints list, using as many columns as possible
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
data : `list`
|
||||||
|
list of items
|
||||||
|
numbering : `bool`
|
||||||
|
assigns number to each option
|
||||||
|
prompt : `str`
|
||||||
|
the prompt string, if given, is printed after the table before reading input
|
||||||
|
divider : `str`
|
||||||
|
string that divides columns
|
||||||
|
alignment : `str`
|
||||||
|
f-string style alignment ( '<', '>', '^' )
|
||||||
|
min_per_col : int
|
||||||
|
avoid splitting columns with fewer items
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
item : `int`, item
|
||||||
|
returns tuple with global index of selected item and the item object,
|
||||||
|
or `None` (if `numbering` or `prompt` are `None`)
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
WIDTH = shutil.get_terminal_size()[0]
|
||||||
|
|
||||||
|
data_size = len(data)
|
||||||
|
items = []
|
||||||
|
items_len = []
|
||||||
|
|
||||||
|
#--------------------------------------------- Add numbers, margins and divider
|
||||||
|
for i in range(data_size):
|
||||||
|
number = f"{i}-" if numbering else ""
|
||||||
|
items.append( f"{divider}{number}{data[i]}" )
|
||||||
|
items_len.append( len(items[-1]) )
|
||||||
|
|
||||||
|
max_cols = np.clip((WIDTH+len(divider)) // min(items_len),1,math.ceil(data_size/max(min_per_col,1))) # width + len(divider) because it is not needed in last col
|
||||||
|
|
||||||
|
#--------------------------------------------- Check maximum number of columns, considering content width (min:1)
|
||||||
|
for i in range(max_cols,0,-1):
|
||||||
|
cols_width = []
|
||||||
|
cols_items = []
|
||||||
|
table_width = 0
|
||||||
|
a,b = divmod(data_size,i)
|
||||||
|
for col in range(i):
|
||||||
|
start = a*col + min(b,col)
|
||||||
|
end = start+a+(1 if col<b else 0)
|
||||||
|
cols_items.append( items[start:end] )
|
||||||
|
col_width = max(items_len[start:end])
|
||||||
|
cols_width.append( col_width )
|
||||||
|
table_width += col_width
|
||||||
|
if table_width <= WIDTH+len(divider):
|
||||||
|
break
|
||||||
|
table_width -= len(divider)
|
||||||
|
|
||||||
|
#--------------------------------------------- Print columns
|
||||||
|
print("="*table_width)
|
||||||
|
for row in range(math.ceil(data_size / i)):
|
||||||
|
for col in range(i):
|
||||||
|
content = cols_items[col][row] if len(cols_items[col]) > row else divider # print divider when there are no items
|
||||||
|
if col == 0:
|
||||||
|
l = len(divider)
|
||||||
|
print(end=f"{content[l:]:{alignment}{cols_width[col]-l}}") # remove divider from 1st col
|
||||||
|
else:
|
||||||
|
print(end=f"{content :{alignment}{cols_width[col] }}")
|
||||||
|
print()
|
||||||
|
print("="*table_width)
|
||||||
|
|
||||||
|
#--------------------------------------------- Prompt
|
||||||
|
if prompt is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if numbering is None:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
idx = UI.read_int( prompt, 0, data_size )
|
||||||
|
return idx, data[idx]
|
||||||
1041
scripts/gyms/Walk.py
Executable file
1041
scripts/gyms/Walk.py
Executable file
File diff suppressed because it is too large
Load Diff
832
scripts/gyms/logs/Turn_R0_000/Walk.py
Executable file
832
scripts/gyms/logs/Turn_R0_000/Walk.py
Executable file
@@ -0,0 +1,832 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "0.7"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
print(time.time(), self.step_counter)
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
# is_fallen = height < 0.55
|
||||||
|
# if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# # Strong terminal penalty discourages risky turn-and-fall behaviors.
|
||||||
|
# return -1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# Keep reward simple: turn correctly, stay stable, avoid jerky actions.
|
||||||
|
|
||||||
|
delta_action_norm = float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
# Cap smoothness penalty so it regularizes behavior without dominating total reward.
|
||||||
|
smoothness_penalty = -min(self.reward_smoothness_cap, self.reward_smoothness_scale * delta_action_norm)
|
||||||
|
|
||||||
|
posture_penalty = -0.45 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.04 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -2.5 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = self._wrap_to_pi(target_yaw - robot_yaw)
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
|
||||||
|
# Reward reducing heading error between consecutive steps.
|
||||||
|
# Use a deadzone and smaller gain to avoid high-frequency jitter near alignment.
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.70 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.70, 0.70))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
turn_dir = float(np.sign(yaw_error))
|
||||||
|
# Continuous turn shaping prevents reward discontinuity near small heading error.
|
||||||
|
turn_gate = min(1.0, abs_yaw_error / math.radians(45.0))
|
||||||
|
turn_rate_reward = 0.70 * turn_gate * math.tanh(2.0 * turn_dir * yaw_rate)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(8.0) else 0.0
|
||||||
|
# After roughly aligning with target, prioritize standing stability over continued aggressive turning.
|
||||||
|
aligned_gate = max(0.0, 1.0 - abs_yaw_error / math.radians(18.0))
|
||||||
|
post_turn_ang_vel_penalty = -0.10 * aligned_gate * min(rp_ang_vel_mag, math.radians(60.0))
|
||||||
|
lower_body_speed_mag = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
post_turn_pose_bonus = 0.30 * aligned_gate * math.exp(-tilt_mag / 0.20) * math.exp(-lower_body_speed_mag / 1.10)
|
||||||
|
# Keep feet separation when aligned so robot does not collapse stance after turning.
|
||||||
|
aligned_stance_bonus = 0.20 * aligned_gate * min(1.0, stance_metric / max(self.min_stance_rad, 1e-4))
|
||||||
|
# Once roughly aligned, damp yaw oscillation and reward keeping a stable stance.
|
||||||
|
anti_oscillation_penalty = -0.08 * min(yaw_rate_abs, math.radians(35.0)) if abs_yaw_error < math.radians(7.0) else 0.0
|
||||||
|
stabilize_bonus = 0.45 if (
|
||||||
|
abs_yaw_error < math.radians(8.0)
|
||||||
|
and yaw_rate_abs < math.radians(10.0)
|
||||||
|
and tilt_mag < 0.28
|
||||||
|
) else 0.0
|
||||||
|
|
||||||
|
# 改进(线性分段,sigmoid 过渡)
|
||||||
|
if abs_yaw_error < math.radians(15.0):
|
||||||
|
alive_bonus = 2 * (1.0 - abs_yaw_error / math.radians(15.0)) ** 0.5 # 平方根让小角度更敏感
|
||||||
|
else:
|
||||||
|
alive_bonus = max(0.1, 2 * (1.0 - (abs_yaw_error - math.radians(15.0)) / math.radians(75.0)))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
# 改进(分段,偏离越多惩罚越重)
|
||||||
|
height_error = height - target_height
|
||||||
|
if abs(height_error) < 0.04:
|
||||||
|
height_penalty = -2.5 * abs(height_error) # 小偏离,保持线性
|
||||||
|
else:
|
||||||
|
height_penalty = -2.5 * 0.04 - 4.0 * (abs(height_error) - 0.04) # 大偏离,惩罚加速
|
||||||
|
|
||||||
|
total = (
|
||||||
|
alive_bonus
|
||||||
|
+ smoothness_penalty
|
||||||
|
+ posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ linkage_reward
|
||||||
|
+ waist_only_turn_penalty
|
||||||
|
+ yaw_link_reward
|
||||||
|
+ head_toward_bonus
|
||||||
|
+ heading_progress_reward
|
||||||
|
+ anti_oscillation_penalty
|
||||||
|
+ stabilize_bonus
|
||||||
|
+ height_penalty
|
||||||
|
# + post_turn_ang_vel_penalty
|
||||||
|
# + post_turn_pose_bonus
|
||||||
|
# + aligned_stance_bonus
|
||||||
|
# + heading_align_reward
|
||||||
|
+ turn_rate_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
# print(
|
||||||
|
# f"reward_debug: step={self.step_counter}, "
|
||||||
|
# f"alive_bonus:{alive_bonus:.4f}, "
|
||||||
|
# # f"heading_align_reward:{heading_align_reward:.4f}, "
|
||||||
|
# # f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
# f"head_towards_bonus:{head_toward_bonus},"
|
||||||
|
# f"posture_penalty:{posture_penalty:.4f}, "
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}, "
|
||||||
|
# f"smoothness_penalty:{smoothness_penalty:.4f}, "
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f}, "
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f}, "
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}, "
|
||||||
|
# f"anti_oscillation_penalty:{anti_oscillation_penalty:.4f}, "
|
||||||
|
# f"stabilize_bonus:{stabilize_bonus:.4f}, "
|
||||||
|
# f"turn_rate_reward:{turn_rate_reward:.4f}, "
|
||||||
|
# f"total:{total:.4f}"
|
||||||
|
# )
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
f"reward_debug: step={self.step_counter}, "
|
||||||
|
f"alive_bonus:{alive_bonus:.4f}, "
|
||||||
|
# f"heading_align_reward:{heading_align_reward:.4f}, "
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
f"head_towards_bonus:{head_toward_bonus},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}, "
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f}, "
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f}, "
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
f"linkage_reward:{linkage_reward:.4f}, "
|
||||||
|
f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f}, "
|
||||||
|
f"yaw_link_reward:{yaw_link_reward:.4f}, "
|
||||||
|
f"anti_oscillation_penalty:{anti_oscillation_penalty:.4f}, "
|
||||||
|
f"stabilize_bonus:{stabilize_bonus:.4f}, "
|
||||||
|
f"height_penalty:{height_penalty:.4f}, "
|
||||||
|
# f"post_turn_ang_vel_penalty:{post_turn_ang_vel_penalty:.4f}, "
|
||||||
|
# f"post_turn_pose_bonus:{post_turn_pose_bonus:.4f}, "
|
||||||
|
f"aligned_stance_bonus:{aligned_stance_bonus:.4f}, "
|
||||||
|
# f"turn_rate_reward:{turn_rate_reward:.4f}, "
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}, "
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}, "
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.1# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/",
|
||||||
|
max_grad_norm=float(os.environ.get("GYM_CPU_TRAIN_MAX_GRAD_NORM", "0.5"))
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=5,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
831
scripts/gyms/logs/Turn_R0_001/Walk.py
Executable file
831
scripts/gyms/logs/Turn_R0_001/Walk.py
Executable file
@@ -0,0 +1,831 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "0.7"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
# is_fallen = height < 0.55
|
||||||
|
# if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# # Strong terminal penalty discourages risky turn-and-fall behaviors.
|
||||||
|
# return -1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# Keep reward simple: turn correctly, stay stable, avoid jerky actions.
|
||||||
|
|
||||||
|
delta_action_norm = float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
# Cap smoothness penalty so it regularizes behavior without dominating total reward.
|
||||||
|
smoothness_penalty = -min(self.reward_smoothness_cap, self.reward_smoothness_scale * delta_action_norm)
|
||||||
|
|
||||||
|
posture_penalty = -0.45 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.04 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -2.5 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = self._wrap_to_pi(target_yaw - robot_yaw)
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
|
||||||
|
# Reward reducing heading error between consecutive steps.
|
||||||
|
# Use a deadzone and smaller gain to avoid high-frequency jitter near alignment.
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.70 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.70, 0.70))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
turn_dir = float(np.sign(yaw_error))
|
||||||
|
# Continuous turn shaping prevents reward discontinuity near small heading error.
|
||||||
|
turn_gate = min(1.0, abs_yaw_error / math.radians(45.0))
|
||||||
|
turn_rate_reward = 0.70 * turn_gate * math.tanh(2.0 * turn_dir * yaw_rate)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(8.0) else 0.0
|
||||||
|
# After roughly aligning with target, prioritize standing stability over continued aggressive turning.
|
||||||
|
aligned_gate = max(0.0, 1.0 - abs_yaw_error / math.radians(18.0))
|
||||||
|
post_turn_ang_vel_penalty = -0.10 * aligned_gate * min(rp_ang_vel_mag, math.radians(60.0))
|
||||||
|
lower_body_speed_mag = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
post_turn_pose_bonus = 0.30 * aligned_gate * math.exp(-tilt_mag / 0.20) * math.exp(-lower_body_speed_mag / 1.10)
|
||||||
|
# Keep feet separation when aligned so robot does not collapse stance after turning.
|
||||||
|
aligned_stance_bonus = 0.20 * aligned_gate * min(1.0, stance_metric / max(self.min_stance_rad, 1e-4))
|
||||||
|
# Once roughly aligned, damp yaw oscillation and reward keeping a stable stance.
|
||||||
|
anti_oscillation_penalty = -0.08 * min(yaw_rate_abs, math.radians(35.0)) if abs_yaw_error < math.radians(7.0) else 0.0
|
||||||
|
stabilize_bonus = 0.45 if (
|
||||||
|
abs_yaw_error < math.radians(8.0)
|
||||||
|
and yaw_rate_abs < math.radians(10.0)
|
||||||
|
and tilt_mag < 0.28
|
||||||
|
) else 0.0
|
||||||
|
|
||||||
|
# 改进(线性分段,sigmoid 过渡)
|
||||||
|
if abs_yaw_error < math.radians(15.0):
|
||||||
|
alive_bonus = 2 * (1.0 - abs_yaw_error / math.radians(15.0)) ** 0.5 # 平方根让小角度更敏感
|
||||||
|
else:
|
||||||
|
alive_bonus = max(0.1, 2 * (1.0 - (abs_yaw_error - math.radians(15.0)) / math.radians(75.0)))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
# 改进(分段,偏离越多惩罚越重)
|
||||||
|
height_error = height - target_height
|
||||||
|
if abs(height_error) < 0.04:
|
||||||
|
height_penalty = -2.5 * abs(height_error) # 小偏离,保持线性
|
||||||
|
else:
|
||||||
|
height_penalty = -2.5 * 0.04 - 4.0 * (abs(height_error) - 0.04) # 大偏离,惩罚加速
|
||||||
|
|
||||||
|
total = (
|
||||||
|
alive_bonus
|
||||||
|
+ smoothness_penalty
|
||||||
|
+ posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ linkage_reward
|
||||||
|
+ waist_only_turn_penalty
|
||||||
|
+ yaw_link_reward
|
||||||
|
+ head_toward_bonus
|
||||||
|
+ heading_progress_reward
|
||||||
|
+ anti_oscillation_penalty
|
||||||
|
+ stabilize_bonus
|
||||||
|
+ height_penalty
|
||||||
|
# + post_turn_ang_vel_penalty
|
||||||
|
# + post_turn_pose_bonus
|
||||||
|
# + aligned_stance_bonus
|
||||||
|
# + heading_align_reward
|
||||||
|
+ turn_rate_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
# print(
|
||||||
|
# f"reward_debug: step={self.step_counter}, "
|
||||||
|
# f"alive_bonus:{alive_bonus:.4f}, "
|
||||||
|
# # f"heading_align_reward:{heading_align_reward:.4f}, "
|
||||||
|
# # f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
# f"head_towards_bonus:{head_toward_bonus},"
|
||||||
|
# f"posture_penalty:{posture_penalty:.4f}, "
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}, "
|
||||||
|
# f"smoothness_penalty:{smoothness_penalty:.4f}, "
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f}, "
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f}, "
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}, "
|
||||||
|
# f"anti_oscillation_penalty:{anti_oscillation_penalty:.4f}, "
|
||||||
|
# f"stabilize_bonus:{stabilize_bonus:.4f}, "
|
||||||
|
# f"turn_rate_reward:{turn_rate_reward:.4f}, "
|
||||||
|
# f"total:{total:.4f}"
|
||||||
|
# )
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
f"reward_debug: step={self.step_counter}, "
|
||||||
|
f"alive_bonus:{alive_bonus:.4f}, "
|
||||||
|
# f"heading_align_reward:{heading_align_reward:.4f}, "
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
f"head_towards_bonus:{head_toward_bonus},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}, "
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f}, "
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f}, "
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
f"linkage_reward:{linkage_reward:.4f}, "
|
||||||
|
f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f}, "
|
||||||
|
f"yaw_link_reward:{yaw_link_reward:.4f}, "
|
||||||
|
f"anti_oscillation_penalty:{anti_oscillation_penalty:.4f}, "
|
||||||
|
f"stabilize_bonus:{stabilize_bonus:.4f}, "
|
||||||
|
f"height_penalty:{height_penalty:.4f}, "
|
||||||
|
# f"post_turn_ang_vel_penalty:{post_turn_ang_vel_penalty:.4f}, "
|
||||||
|
# f"post_turn_pose_bonus:{post_turn_pose_bonus:.4f}, "
|
||||||
|
f"aligned_stance_bonus:{aligned_stance_bonus:.4f}, "
|
||||||
|
# f"turn_rate_reward:{turn_rate_reward:.4f}, "
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}, "
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}, "
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.1# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/",
|
||||||
|
max_grad_norm=float(os.environ.get("GYM_CPU_TRAIN_MAX_GRAD_NORM", "0.5"))
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=5,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
831
scripts/gyms/logs/Turn_R0_002/Walk.py
Executable file
831
scripts/gyms/logs/Turn_R0_002/Walk.py
Executable file
@@ -0,0 +1,831 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
# is_fallen = height < 0.55
|
||||||
|
# if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# # Strong terminal penalty discourages risky turn-and-fall behaviors.
|
||||||
|
# return -1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# Keep reward simple: turn correctly, stay stable, avoid jerky actions.
|
||||||
|
|
||||||
|
delta_action_norm = float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
# Cap smoothness penalty so it regularizes behavior without dominating total reward.
|
||||||
|
smoothness_penalty = -min(self.reward_smoothness_cap, self.reward_smoothness_scale * delta_action_norm)
|
||||||
|
|
||||||
|
posture_penalty = -0.45 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.04 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||||
|
stance_metric = 0.5 * abs(hip_spread) + 0.5 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -3 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -2.5 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = self._wrap_to_pi(target_yaw - robot_yaw)
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
|
||||||
|
# Reward reducing heading error between consecutive steps.
|
||||||
|
# Use a deadzone and smaller gain to avoid high-frequency jitter near alignment.
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, 1, 1))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
turn_dir = float(np.sign(yaw_error))
|
||||||
|
# Continuous turn shaping prevents reward discontinuity near small heading error.
|
||||||
|
turn_gate = min(1.0, abs_yaw_error / math.radians(45.0))
|
||||||
|
turn_rate_reward = 0.70 * turn_gate * math.tanh(2.0 * turn_dir * yaw_rate)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(8.0) else 0.0
|
||||||
|
# After roughly aligning with target, prioritize standing stability over continued aggressive turning.
|
||||||
|
aligned_gate = max(0.0, 1.0 - abs_yaw_error / math.radians(18.0))
|
||||||
|
post_turn_ang_vel_penalty = -0.10 * aligned_gate * min(rp_ang_vel_mag, math.radians(60.0))
|
||||||
|
lower_body_speed_mag = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
post_turn_pose_bonus = 0.30 * aligned_gate * math.exp(-tilt_mag / 0.20) * math.exp(-lower_body_speed_mag / 1.10)
|
||||||
|
# Keep feet separation when aligned so robot does not collapse stance after turning.
|
||||||
|
aligned_stance_bonus = 0.20 * aligned_gate * min(1.0, stance_metric / max(self.min_stance_rad, 1e-4))
|
||||||
|
# Once roughly aligned, damp yaw oscillation and reward keeping a stable stance.
|
||||||
|
anti_oscillation_penalty = -0.08 * min(yaw_rate_abs, math.radians(35.0)) if abs_yaw_error < math.radians(7.0) else 0.0
|
||||||
|
stabilize_bonus = 0.6 if (
|
||||||
|
abs_yaw_error < math.radians(8.0)
|
||||||
|
and yaw_rate_abs < math.radians(10.0)
|
||||||
|
and tilt_mag < 0.28
|
||||||
|
) else 0.0
|
||||||
|
|
||||||
|
# 改进(线性分段,sigmoid 过渡)
|
||||||
|
if abs_yaw_error < math.radians(15.0):
|
||||||
|
alive_bonus = 2 * (1.0 - abs_yaw_error / math.radians(15.0)) ** 0.5 # 平方根让小角度更敏感
|
||||||
|
else:
|
||||||
|
alive_bonus = max(0.1, 2 * (1.0 - (abs_yaw_error - math.radians(15.0)) / math.radians(75.0)))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
# 改进(分段,偏离越多惩罚越重)
|
||||||
|
height_error = height - target_height
|
||||||
|
if abs(height_error) < 0.04:
|
||||||
|
height_penalty = -2.5 * abs(height_error) # 小偏离,保持线性
|
||||||
|
else:
|
||||||
|
height_penalty = -2.5 * 0.04 - 4.0 * (abs(height_error) - 0.04) # 大偏离,惩罚加速
|
||||||
|
|
||||||
|
total = (
|
||||||
|
alive_bonus
|
||||||
|
+ smoothness_penalty
|
||||||
|
+ posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ linkage_reward
|
||||||
|
+ waist_only_turn_penalty
|
||||||
|
+ yaw_link_reward
|
||||||
|
+ head_toward_bonus
|
||||||
|
+ heading_progress_reward
|
||||||
|
+ anti_oscillation_penalty
|
||||||
|
+ stabilize_bonus
|
||||||
|
+ height_penalty
|
||||||
|
# + post_turn_ang_vel_penalty
|
||||||
|
# + post_turn_pose_bonus
|
||||||
|
# + aligned_stance_bonus
|
||||||
|
# + heading_align_reward
|
||||||
|
+ turn_rate_reward
|
||||||
|
+ stance_collapse_penalty
|
||||||
|
+ cross_leg_penalty
|
||||||
|
)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
# print(
|
||||||
|
# f"reward_debug: step={self.step_counter}, "
|
||||||
|
# f"alive_bonus:{alive_bonus:.4f}, "
|
||||||
|
# # f"heading_align_reward:{heading_align_reward:.4f}, "
|
||||||
|
# # f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
# f"head_towards_bonus:{head_toward_bonus},"
|
||||||
|
# f"posture_penalty:{posture_penalty:.4f}, "
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}, "
|
||||||
|
# f"smoothness_penalty:{smoothness_penalty:.4f}, "
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f}, "
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f}, "
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}, "
|
||||||
|
# f"anti_oscillation_penalty:{anti_oscillation_penalty:.4f}, "
|
||||||
|
# f"stabilize_bonus:{stabilize_bonus:.4f}, "
|
||||||
|
# f"turn_rate_reward:{turn_rate_reward:.4f}, "
|
||||||
|
# f"total:{total:.4f}"
|
||||||
|
# )
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
f"reward_debug: step={self.step_counter}, "
|
||||||
|
f"alive_bonus:{alive_bonus:.4f}, "
|
||||||
|
# f"heading_align_reward:{heading_align_reward:.4f}, "
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
f"head_towards_bonus:{head_toward_bonus},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}, "
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f}, "
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f}, "
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||||
|
f"linkage_reward:{linkage_reward:.4f}, "
|
||||||
|
f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f}, "
|
||||||
|
f"yaw_link_reward:{yaw_link_reward:.4f}, "
|
||||||
|
f"anti_oscillation_penalty:{anti_oscillation_penalty:.4f}, "
|
||||||
|
f"stabilize_bonus:{stabilize_bonus:.4f}, "
|
||||||
|
f"height_penalty:{height_penalty:.4f}, "
|
||||||
|
# f"post_turn_ang_vel_penalty:{post_turn_ang_vel_penalty:.4f}, "
|
||||||
|
# f"post_turn_pose_bonus:{post_turn_pose_bonus:.4f}, "
|
||||||
|
f"aligned_stance_bonus:{aligned_stance_bonus:.4f}, "
|
||||||
|
# f"turn_rate_reward:{turn_rate_reward:.4f}, "
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}, "
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}, "
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.1# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/",
|
||||||
|
max_grad_norm=float(os.environ.get("GYM_CPU_TRAIN_MAX_GRAD_NORM", "0.5"))
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
755
scripts/gyms/logs/Turn_R0_003/Walk.py
Executable file
755
scripts/gyms/logs/Turn_R0_003/Walk.py
Executable file
@@ -0,0 +1,755 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.45 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.04 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4.0 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -1.2 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ stance_collapse_penalty
|
||||||
|
+ cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}",
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}",
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 600:
|
||||||
|
self.start_time = time.time()
|
||||||
|
self.debug_log(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}",
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.1# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/",
|
||||||
|
max_grad_norm=float(os.environ.get("GYM_CPU_TRAIN_MAX_GRAD_NORM", "0.5"))
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
754
scripts/gyms/logs/Turn_R0_004/Walk.py
Executable file
754
scripts/gyms/logs/Turn_R0_004/Walk.py
Executable file
@@ -0,0 +1,754 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.7, 0.7))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.02 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll if right_hip_roll > 0.03 and left_hip_roll > 0.03 else 0.0
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll if right_ankle_roll > 0.03 and left_ankle_roll > 0.03 else 0.0
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4.0 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -1.2 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -math.exp(15*abs(height_error))
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.1# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/",
|
||||||
|
max_grad_norm=float(os.environ.get("GYM_CPU_TRAIN_MAX_GRAD_NORM", "0.5"))
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
755
scripts/gyms/logs/Turn_R0_005/Walk.py
Executable file
755
scripts/gyms/logs/Turn_R0_005/Walk.py
Executable file
@@ -0,0 +1,755 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.7, 0.7))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.02 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll if right_hip_roll > 0.03 and left_hip_roll > 0.03 else 0.0
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll if right_ankle_roll > 0.03 and left_ankle_roll > 0.03 else 0.0
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4.0 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -1.2 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.1# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/",
|
||||||
|
max_grad_norm=float(os.environ.get("GYM_CPU_TRAIN_MAX_GRAD_NORM", "0.5"))
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
821
scripts/gyms/logs/Turn_R0_006/Walk.py
Executable file
821
scripts/gyms/logs/Turn_R0_006/Walk.py
Executable file
@@ -0,0 +1,821 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -0.1 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.7, 0.7))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.02 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.75 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.1 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.35 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 1 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=150, kd=40
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
823
scripts/gyms/logs/Turn_R0_007/Walk.py
Executable file
823
scripts/gyms/logs/Turn_R0_007/Walk.py
Executable file
@@ -0,0 +1,823 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.02 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.75 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.1 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.35 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 1 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=150, kd=40
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
849
scripts/gyms/logs/Turn_R0_008/Walk.py
Executable file
849
scripts/gyms/logs/Turn_R0_008/Walk.py
Executable file
@@ -0,0 +1,849 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.15 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.5 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=80, kd=10
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
853
scripts/gyms/logs/Turn_R0_009/Walk.py
Executable file
853
scripts/gyms/logs/Turn_R0_009/Walk.py
Executable file
@@ -0,0 +1,853 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_hip_pitch = float(joint_pos[11])
|
||||||
|
right_hip_pitch = float(joint_pos[17])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.15 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.3 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
action[11] = np.clip(action[11], -0.3, 0.3)
|
||||||
|
action[17] = np.clip(action[17], -0.3, 0.3)
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=80, kd=10
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
853
scripts/gyms/logs/Turn_R0_010/Walk.py
Executable file
853
scripts/gyms/logs/Turn_R0_010/Walk.py
Executable file
@@ -0,0 +1,853 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_hip_pitch = float(joint_pos[11])
|
||||||
|
right_hip_pitch = float(joint_pos[17])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.15 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.3 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
action[11] = np.clip(action[11], -0.1, 0.1)
|
||||||
|
action[17] = np.clip(action[17], -0.1, 0.1)
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=110, kd=29.5
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
853
scripts/gyms/logs/Turn_R0_011/Walk.py
Executable file
853
scripts/gyms/logs/Turn_R0_011/Walk.py
Executable file
@@ -0,0 +1,853 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_hip_pitch = float(joint_pos[11])
|
||||||
|
right_hip_pitch = float(joint_pos[17])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.15 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.3 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
action[11] = np.clip(action[11], -0.1, 0.1)
|
||||||
|
action[17] = np.clip(action[17], -0.1, 0.1)
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=110, kd=6
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
853
scripts/gyms/logs/Turn_R0_012/Walk.py
Executable file
853
scripts/gyms/logs/Turn_R0_012/Walk.py
Executable file
@@ -0,0 +1,853 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "90"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_hip_pitch = float(joint_pos[11])
|
||||||
|
right_hip_pitch = float(joint_pos[17])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.2 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.4 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
# + left_hip_yaw_penalty
|
||||||
|
# + right_hip_yaw_penalty
|
||||||
|
# + hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
# print(f"abs_yaw_error:{abs_yaw_error:.4f}")
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
action[11] = np.clip(action[11], -0.4, 0.4)
|
||||||
|
action[17] = np.clip(action[17], -0.4, 0.4)
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=110, kd=6
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
853
scripts/gyms/logs/Turn_R0_013/Walk.py
Executable file
853
scripts/gyms/logs/Turn_R0_013/Walk.py
Executable file
@@ -0,0 +1,853 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "90"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_hip_pitch = float(joint_pos[11])
|
||||||
|
right_hip_pitch = float(joint_pos[17])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.2 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.4 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
# + left_hip_yaw_penalty
|
||||||
|
# + right_hip_yaw_penalty
|
||||||
|
# + hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
# print(f"abs_yaw_error:{abs_yaw_error:.4f}")
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
action[11] = np.clip(action[11], -0.4, 0.4)
|
||||||
|
action[17] = np.clip(action[17], -0.4, 0.4)
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=80, kd=4.67
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
853
scripts/gyms/logs/Turn_R0_014/Walk.py
Executable file
853
scripts/gyms/logs/Turn_R0_014/Walk.py
Executable file
@@ -0,0 +1,853 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "120"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -1, 1))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_hip_pitch = float(joint_pos[11])
|
||||||
|
right_hip_pitch = float(joint_pos[17])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.2 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.5 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
# print(f"abs_yaw_error:{abs_yaw_error:.4f}")
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
action[11] = np.clip(action[11], -0.7, 0.7)
|
||||||
|
action[17] = np.clip(action[17], -0.7, 0.7)
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=80, kd=4.67
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
BIN
scripts/gyms/logs/Turn_around_normal_60deg.zip
Normal file
BIN
scripts/gyms/logs/Turn_around_normal_60deg.zip
Normal file
Binary file not shown.
853
scripts/gyms/logs/Turn_around_normal_60deg/Walk.py
Executable file
853
scripts/gyms/logs/Turn_around_normal_60deg/Walk.py
Executable file
@@ -0,0 +1,853 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "90"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_hip_pitch = float(joint_pos[11])
|
||||||
|
right_hip_pitch = float(joint_pos[17])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.2 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.4 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
# + left_hip_yaw_penalty
|
||||||
|
# + right_hip_yaw_penalty
|
||||||
|
# + hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
# print(f"abs_yaw_error:{abs_yaw_error:.4f}")
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
action[11] = np.clip(action[11], -0.4, 0.4)
|
||||||
|
action[17] = np.clip(action[17], -0.4, 0.4)
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=80, kd=4.67
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
BIN
scripts/gyms/logs/Turn_around_unnormal.zip
Normal file
BIN
scripts/gyms/logs/Turn_around_unnormal.zip
Normal file
Binary file not shown.
853
scripts/gyms/logs/Turn_around_unnormal/Walk.py
Executable file
853
scripts/gyms/logs/Turn_around_unnormal/Walk.py
Executable file
@@ -0,0 +1,853 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = float(os.environ.get("GYM_CPU_REWARD_DEBUG_INTERVAL_SEC", "600"))
|
||||||
|
self.reward_debug_burst_steps = int(os.environ.get("GYM_CPU_REWARD_DEBUG_BURST_STEPS", "10"))
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||||
|
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||||
|
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||||
|
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||||
|
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||||
|
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Randomize global target bearing so policy must learn to rotate toward it first.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
point1 = self.initial_position + target_offset
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
|
||||||
|
joint_pos_rad = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
joint_speed_rad = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||||
|
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
else:
|
||||||
|
position_penalty = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
# Turn-to-target shaping.
|
||||||
|
to_target = self.target_position - current_pos
|
||||||
|
dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
if dist_to_target > 1e-6:
|
||||||
|
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||||
|
else:
|
||||||
|
target_yaw = 0.0
|
||||||
|
|
||||||
|
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
yaw_error = target_yaw - robot_yaw
|
||||||
|
|
||||||
|
# Main heading objective: face the target direction.
|
||||||
|
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||||
|
|
||||||
|
abs_yaw_error = abs(yaw_error)
|
||||||
|
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||||
|
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||||
|
|
||||||
|
if self.last_yaw_error is None:
|
||||||
|
heading_progress_reward = 0.0
|
||||||
|
else:
|
||||||
|
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||||
|
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||||
|
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||||
|
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||||
|
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||||
|
self.last_yaw_error = yaw_error
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.6 * tilt_mag
|
||||||
|
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||||
|
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_hip_pitch = float(joint_pos[11])
|
||||||
|
right_hip_pitch = float(joint_pos[17])
|
||||||
|
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
max_leg_roll = 0.15 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||||
|
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||||
|
leg_separation = -left_hip_roll + right_hip_roll
|
||||||
|
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.3 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||||
|
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||||
|
yaw_rate_abs = abs(yaw_rate)
|
||||||
|
|
||||||
|
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||||
|
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||||
|
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||||
|
|
||||||
|
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||||
|
waist_speed = abs(float(joint_speed_rad[10]))
|
||||||
|
lower_body_speed = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||||
|
lower_body_follow_ratio = lower_body_speed / (waist_speed + 1e-4)
|
||||||
|
linkage_reward = 0.24 * min(1.0, lower_body_follow_ratio) * min(1.0, waist_speed / 1.2)
|
||||||
|
waist_only_turn_penalty = -0.20 * max(0.0, waist_speed - 1.35 * lower_body_speed)
|
||||||
|
|
||||||
|
# Extra posture linkage in yaw joints to avoid decoupled torso twist.
|
||||||
|
waist_yaw = abs(float(joint_pos_rad[10]))
|
||||||
|
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||||
|
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
head_toward_bonus +
|
||||||
|
heading_progress_reward +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
# + leg_proximity_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ hip_yaw_cross_penalty
|
||||||
|
+ position_penalty
|
||||||
|
# + linkage_reward
|
||||||
|
# + waist_only_turn_penalty
|
||||||
|
# + yaw_link_reward
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + hip_yaw_yaw_cross_penalty
|
||||||
|
# + stance_collapse_penalty
|
||||||
|
# + cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
# print(height_error, height_penalty)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||||
|
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"position_penalty:{position_penalty:.4f},"
|
||||||
|
# f"linkage_reward:{linkage_reward:.4f},"
|
||||||
|
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||||
|
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||||
|
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||||
|
|
||||||
|
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||||
|
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
f"alive_bonus:{alive_bonus:.4f},"
|
||||||
|
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = 4
|
||||||
|
action[7] = -4
|
||||||
|
action[2] = 0
|
||||||
|
action[6] = 0
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = -5
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = 5
|
||||||
|
action[10] = 0
|
||||||
|
action[11] = np.clip(action[11], -0.1, 0.1)
|
||||||
|
action[17] = np.clip(action[17], -0.1, 0.1)
|
||||||
|
# action[12] = -1.0
|
||||||
|
# action[18] = 1.0
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=110, kd=29.5
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
if self.step_counter % 10 == 0:
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Turn_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
624
scripts/gyms/logs/Walk_R0_000/Walk.py
Normal file
624
scripts/gyms/logs/Walk_R0_000/Walk.py
Normal file
@@ -0,0 +1,624 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-1.0,
|
||||||
|
high=1.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.5
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.035
|
||||||
|
self.reset_perturb_steps = 5
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
# 摔倒检测(重要!)
|
||||||
|
if height < 0.3:
|
||||||
|
if time.time() - self.start_time > 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print("fall_penalty: -20")
|
||||||
|
return -20.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 0.1
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -1.0 * (tilt_mag)
|
||||||
|
# ang_vel_penalty = -0.05 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height if abs(height - target_height) > 0.05 else 0.0
|
||||||
|
height_penalty = -2.0 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
# + ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
self.joint_nominal_position
|
||||||
|
+ self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
# self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = 20 # Reduced from 8 to decrease CPU/network pressure during init
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
n_steps_per_env = 1024 # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = 128 # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = 3e-4
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env):
|
||||||
|
def thunk():
|
||||||
|
return WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i) for i in range(n_envs)])
|
||||||
|
eval_env = SubprocVecEnv([init_env(n_envs)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=0.005, # Entropy coefficient for exploration
|
||||||
|
# clip_range=0.13, # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=0.99 , # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
# n_epochs=5
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 10, save_freq=n_steps_per_env * 10,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
server = Train_Server(self.server_p - 1, self.monitor_p, 1)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
trainer.train({})
|
||||||
|
# trainer.test({"model_file": "scripts/gyms/logs/Walk_R0_012/best_model.zip",
|
||||||
|
# "folder_dir": "scripts/gyms/logs/Walk_R0_012/",})
|
||||||
625
scripts/gyms/logs/Walk_R0_001/Walk.py
Normal file
625
scripts/gyms/logs/Walk_R0_001/Walk.py
Normal file
@@ -0,0 +1,625 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.1
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.035
|
||||||
|
self.reset_perturb_steps = 5
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
remain = max(0, 800 - self.step_counter)
|
||||||
|
return -8.0 - 0.01 * remain
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 0.3
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.03 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -1.0 * (tilt_mag)
|
||||||
|
# ang_vel_penalty = -0.05 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height if abs(height - target_height) > 0.05 else 0.0
|
||||||
|
height_penalty = -2.0 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
# + ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = 20 # Reduced from 8 to decrease CPU/network pressure during init
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
n_steps_per_env = 1024 # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = 128 # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = 3e-4
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env):
|
||||||
|
def thunk():
|
||||||
|
return WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i) for i in range(n_envs)])
|
||||||
|
eval_env = SubprocVecEnv([init_env(n_envs)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=0.005, # Entropy coefficient for exploration
|
||||||
|
# clip_range=0.13, # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=0.99 , # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
# n_epochs=5
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 10, save_freq=n_steps_per_env * 10,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
server = Train_Server(self.server_p - 1, self.monitor_p, 1)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
trainer.train({})
|
||||||
|
# trainer.test({"model_file": "scripts/gyms/logs/Walk_R0_000/best_model.zip",
|
||||||
|
# "folder_dir": "scripts/gyms/logs/Walk_R0_000/",})
|
||||||
625
scripts/gyms/logs/Walk_R0_002/Walk.py
Normal file
625
scripts/gyms/logs/Walk_R0_002/Walk.py
Normal file
@@ -0,0 +1,625 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.1
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.035
|
||||||
|
self.reset_perturb_steps = 5
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
remain = max(0, 800 - self.step_counter)
|
||||||
|
return -8.0 - 0.01 * remain
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 0.3
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.03 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -1.0 * (tilt_mag)
|
||||||
|
# ang_vel_penalty = -0.05 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height if abs(height - target_height) > 0.05 else 0.0
|
||||||
|
height_penalty = -2.0 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
# + ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = 20 # Reduced from 8 to decrease CPU/network pressure during init
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
n_steps_per_env = 1024 # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = 128 # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = 3e-4
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env):
|
||||||
|
def thunk():
|
||||||
|
return WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i) for i in range(n_envs)])
|
||||||
|
eval_env = SubprocVecEnv([init_env(n_envs)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=0.005, # Entropy coefficient for exploration
|
||||||
|
# clip_range=0.13, # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=0.99 , # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
# n_epochs=5
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 10, save_freq=n_steps_per_env * 10,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
server = Train_Server(self.server_p - 1, self.monitor_p, 1)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
trainer.train({})
|
||||||
|
# trainer.test({"model_file": "scripts/gyms/logs/Walk_R0_000/best_model.zip",
|
||||||
|
# "folder_dir": "scripts/gyms/logs/Walk_R0_000/",})
|
||||||
625
scripts/gyms/logs/Walk_R0_003/Walk.py
Normal file
625
scripts/gyms/logs/Walk_R0_003/Walk.py
Normal file
@@ -0,0 +1,625 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = True
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.035
|
||||||
|
self.reset_perturb_steps = 5
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
remain = max(0, 800 - self.step_counter)
|
||||||
|
return -8.0 - 0.01 * remain
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 0.3
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.03 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -1.0 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.05 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -2.0 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = 20 # Reduced from 8 to decrease CPU/network pressure during init
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
n_steps_per_env = 1024 # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = 128 # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = 1e-4
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env):
|
||||||
|
def thunk():
|
||||||
|
return WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i) for i in range(n_envs)])
|
||||||
|
eval_env = SubprocVecEnv([init_env(n_envs)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=0.001, # Entropy coefficient for exploration
|
||||||
|
# clip_range=0.13, # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=0.99 , # Discount factor
|
||||||
|
target_kl=0.03,
|
||||||
|
# n_epochs=5
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 10, save_freq=n_steps_per_env * 10,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
server = Train_Server(self.server_p - 1, self.monitor_p, 1)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
trainer.train({})
|
||||||
|
# trainer.test({"model_file": "scripts/gyms/logs/Walk_R0_000/best_model.zip",
|
||||||
|
# "folder_dir": "scripts/gyms/logs/Walk_R0_000/",})
|
||||||
626
scripts/gyms/logs/Walk_R0_004/Walk.py
Normal file
626
scripts/gyms/logs/Walk_R0_004/Walk.py
Normal file
@@ -0,0 +1,626 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = True
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = 20 # Reduced from 8 to decrease CPU/network pressure during init
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
n_steps_per_env = 256 # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = 512 # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = 3e-4
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env):
|
||||||
|
def thunk():
|
||||||
|
return WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i) for i in range(n_envs)])
|
||||||
|
eval_env = SubprocVecEnv([init_env(n_envs)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=0.05, # Entropy coefficient for exploration
|
||||||
|
# clip_range=0.13, # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=0.95 , # Discount factor
|
||||||
|
target_kl=0.03,
|
||||||
|
n_epochs=5
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 10, save_freq=n_steps_per_env * 10,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
server = Train_Server(self.server_p - 1, self.monitor_p, 1)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
trainer.train({})
|
||||||
|
# trainer.test({"model_file": "scripts/gyms/logs/Walk_R0_000/best_model.zip",
|
||||||
|
# "folder_dir": "scripts/gyms/logs/Walk_R0_000/",})
|
||||||
660
scripts/gyms/logs/Walk_R0_005/Walk.py
Executable file
660
scripts/gyms/logs/Walk_R0_005/Walk.py
Executable file
@@ -0,0 +1,660 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = True
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = 256 # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = 512 # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = 1e-4
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)])
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=0.03, # Entropy coefficient for exploration
|
||||||
|
clip_range=0.13, # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=0.95 , # Discount factor
|
||||||
|
target_kl=0.03,
|
||||||
|
n_epochs=5,
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 10, save_freq=n_steps_per_env * 10,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
server = Train_Server(self.server_p - 1, self.monitor_p, 1)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
trainer.train({"model_file": "scripts/gyms/logs/Walk_R0_004/best_model.zip"})
|
||||||
|
# trainer.test({"model_file": "scripts/gyms/logs/Walk_R0_004/best_model.zip",
|
||||||
|
# "folder_dir": "scripts/gyms/logs/Walk_R0_004/",})
|
||||||
679
scripts/gyms/logs/Walk_R0_006/Walk.py
Executable file
679
scripts/gyms/logs/Walk_R0_006/Walk.py
Executable file
@@ -0,0 +1,679 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = True
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 600:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)])
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
# tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Walk_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Walk_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
679
scripts/gyms/logs/Walk_R0_007/Walk.py
Executable file
679
scripts/gyms/logs/Walk_R0_007/Walk.py
Executable file
@@ -0,0 +1,679 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = True
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -1 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)])
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
# tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=100,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Walk_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Walk_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
704
scripts/gyms/logs/Walk_R0_008/Walk.py
Executable file
704
scripts/gyms/logs/Walk_R0_008/Walk.py
Executable file
@@ -0,0 +1,704 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
# Use simulator joint readings in training frame to shape lateral stance.
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4.0 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -1.2 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ stance_collapse_penalty
|
||||||
|
+ cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 600:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}",
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)])
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=100,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Walk_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Walk_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
705
scripts/gyms/logs/Walk_R0_009/Walk.py
Executable file
705
scripts/gyms/logs/Walk_R0_009/Walk.py
Executable file
@@ -0,0 +1,705 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
# Use simulator joint readings in training frame to shape lateral stance.
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4.0 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -1.2 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ stance_collapse_penalty
|
||||||
|
+ cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 600:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}",
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)])
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=100,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Walk_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Walk_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
705
scripts/gyms/logs/Walk_R0_010/Walk.py
Executable file
705
scripts/gyms/logs/Walk_R0_010/Walk.py
Executable file
@@ -0,0 +1,705 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
# Use simulator joint readings in training frame to shape lateral stance.
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4.0 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -1.2 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ stance_collapse_penalty
|
||||||
|
+ cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 600:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}",
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)])
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=100,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Walk_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Walk_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
BIN
scripts/gyms/logs/Walk_version_0.10.zip
Normal file
BIN
scripts/gyms/logs/Walk_version_0.10.zip
Normal file
Binary file not shown.
968
scripts/gyms/logs/Walk_version_0.10/Walk.py
Executable file
968
scripts/gyms/logs/Walk_version_0.10/Walk.py
Executable file
@@ -0,0 +1,968 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO, TD3, DDPG, SAC, A2C
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.reward_debug_interval_sec = 600.0
|
||||||
|
self.reward_debug_burst_steps = 10
|
||||||
|
self._reward_debug_last_time = time.time()
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
self._speed_estimate = 0.0
|
||||||
|
self._speed_from_acc = 0.0
|
||||||
|
self._prev_accelerometer = np.zeros(3, dtype=np.float32)
|
||||||
|
self._speed_smoothing = 0.85
|
||||||
|
self._fallback_dt = 0.02
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0, # 0: Head_yaw (he1)
|
||||||
|
0.0, # 1: Head_pitch (he2)
|
||||||
|
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
0.0, # 10: Waist (te1)
|
||||||
|
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
0.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
0.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
# self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.5
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = 180.0
|
||||||
|
self.reset_target_bearing_range_deg = 0.0
|
||||||
|
self.reset_target_distance_min = 5
|
||||||
|
self.reset_target_distance_max = 10
|
||||||
|
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||||
|
self.reset_target_distance_min, self.reset_target_distance_max = (
|
||||||
|
self.reset_target_distance_max,
|
||||||
|
self.reset_target_distance_min,
|
||||||
|
)
|
||||||
|
self.reset_joint_noise_rad = 0.025
|
||||||
|
self.reset_perturb_steps = 4
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.reward_smoothness_scale = 0.03
|
||||||
|
self.reward_smoothness_cap = 0.45
|
||||||
|
self.reward_forward_stability_gate = 0.35
|
||||||
|
self.reward_forward_tilt_hard_threshold = 0.50
|
||||||
|
self.reward_forward_tilt_hard_scale = 0.20
|
||||||
|
self.reward_head_toward_bonus = 1.0
|
||||||
|
self.turn_stationary_radius = 0.2
|
||||||
|
self.turn_stationary_penalty_scale = 3.0
|
||||||
|
self.stationary_start_steps = 20
|
||||||
|
self.stationary_step_eps = 0.015
|
||||||
|
self.stationary_penalty_scale = 1.2
|
||||||
|
self.train_stage = "walk"
|
||||||
|
self.in_place_radius = 0.18
|
||||||
|
self.in_place_center_reward_scale = 0.60
|
||||||
|
self.in_place_drift_penalty_scale = 1.20
|
||||||
|
self.waypoint_reach_distance = 0.3
|
||||||
|
self.num_waypoints = 1
|
||||||
|
self.exploration_start_steps = 40
|
||||||
|
self.exploration_scale = 0.012
|
||||||
|
self.exploration_cap = 0.2
|
||||||
|
self.exploration_target_novelty = 1.0
|
||||||
|
self.exploration_sigma = 0.7
|
||||||
|
self.reward_stride_swing_scale = 0.20
|
||||||
|
self.reward_stride_phase_scale = 0.18
|
||||||
|
self.reward_knee_drive_scale = 0.10
|
||||||
|
self.reward_knee_lift_scale = 0.12
|
||||||
|
self.reward_knee_lift_target = 0.15
|
||||||
|
self.reward_knee_lift_shortfall_scale = 0.05
|
||||||
|
self.reward_knee_overbend_threshold = 0.60
|
||||||
|
self.reward_knee_overbend_scale = 0.35
|
||||||
|
self.reward_hip_lift_scale = 0.12
|
||||||
|
self.reward_hip_lift_target = 0.80
|
||||||
|
self.reward_knee_alternate_scale = 0.10
|
||||||
|
self.reward_knee_bilateral_scale = 0.16
|
||||||
|
self.reward_single_leg_penalty_scale = 0.22
|
||||||
|
self.reward_knee_phase_switch_scale = 0.14
|
||||||
|
self.knee_phase_deadband = 0.10
|
||||||
|
self.knee_phase_min_interval = 18
|
||||||
|
self.knee_phase_target_interval = 22
|
||||||
|
self.knee_phase_fast_switch_penalty_scale = 0.10
|
||||||
|
self.knee_phase_max_hold_frames = 28
|
||||||
|
self.knee_phase_hold_penalty_scale = 0.18
|
||||||
|
self.reward_stride_cap = 0.80
|
||||||
|
self.reward_knee_explore_scale = 0.03
|
||||||
|
self.reward_knee_explore_delta_scale = 0.03
|
||||||
|
self.reward_knee_explore_cap = 0.10
|
||||||
|
self.reward_hip_pitch_explore_scale = 0.07
|
||||||
|
self.reward_hip_pitch_explore_delta_scale = 0.07
|
||||||
|
self.reward_hip_pitch_explore_cap = 0.10
|
||||||
|
self.reward_progress_scale = 18
|
||||||
|
self.reward_survival_scale = 0.5
|
||||||
|
self.reward_idle_penalty_scale = 0.6
|
||||||
|
self.reward_accel_penalty_scale = 0.08
|
||||||
|
self.reward_accel_penalty_cap = 0.40
|
||||||
|
self.reward_accel_abs_limit = 13.5
|
||||||
|
self.reward_accel_abs_penalty_scale = 0.05
|
||||||
|
self.reward_accel_abs_penalty_cap = 0.40
|
||||||
|
self.reward_heading_align_scale = 0.28
|
||||||
|
self.reward_heading_error_scale = 0.05
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.action_history_len = 50
|
||||||
|
self.prev_action_history = np.zeros((self.action_history_len, self.no_of_actions), dtype=np.float32)
|
||||||
|
self.history_idx = 0
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.prev_knee_balance = 0.0
|
||||||
|
self.prev_knee_phase_sign = 0
|
||||||
|
self.knee_phase_frames_since_switch = 0
|
||||||
|
self.knee_phase_hold_frames = 0
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _wrap_to_pi(angle_rad: float) -> float:
|
||||||
|
return (angle_rad + math.pi) % (2.0 * math.pi) - math.pi
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
target_distance = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
target_bearing_deg = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.prev_action_history.fill(0.0)
|
||||||
|
self.history_idx = 0
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.last_yaw_error = None
|
||||||
|
self.prev_knee_balance = 0.0
|
||||||
|
self.prev_knee_phase_sign = 0
|
||||||
|
self.knee_phase_frames_since_switch = 0
|
||||||
|
self.knee_phase_hold_frames = 0
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
self._reward_debug_steps_left = 0
|
||||||
|
self._speed_estimate = 0.0
|
||||||
|
self._speed_from_acc = 0.0
|
||||||
|
self._prev_accelerometer = np.array(
|
||||||
|
getattr(self.Player.robot, "accelerometer", np.zeros(3)),
|
||||||
|
dtype=np.float32,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Generate multiple waypoints along a path
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
self.point_list = []
|
||||||
|
current_point = self.initial_position.copy()
|
||||||
|
|
||||||
|
for i in range(self.num_waypoints):
|
||||||
|
# Each waypoint is placed further along the path
|
||||||
|
target_distance_wp = np.random.uniform(self.reset_target_distance_min, self.reset_target_distance_max)
|
||||||
|
self.target_distance_wp = target_distance_wp
|
||||||
|
target_bearing_deg_wp = np.random.uniform(-self.reset_target_bearing_range_deg, self.reset_target_bearing_range_deg)
|
||||||
|
|
||||||
|
target_offset = MathOps.rotate_2d_vec(
|
||||||
|
np.array([target_distance_wp, 0.0]),
|
||||||
|
heading_deg + target_bearing_deg_wp,
|
||||||
|
is_rad=False,
|
||||||
|
)
|
||||||
|
next_point = current_point + target_offset
|
||||||
|
self.point_list.append(next_point)
|
||||||
|
current_point = next_point.copy()
|
||||||
|
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
if self.train_stage == "in_place":
|
||||||
|
self.target_position = self.initial_position.copy()
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
prev_dist_to_target = float(np.linalg.norm(self.target_position - previous_pos))
|
||||||
|
curr_dist_to_target = float(np.linalg.norm(self.target_position - current_pos))
|
||||||
|
dist_delta = prev_dist_to_target - curr_dist_to_target
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
return -2.0
|
||||||
|
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = -float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
|
||||||
|
left_ankle_roll = -float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
left_knee_flex = abs(float(joint_pos[14]))
|
||||||
|
right_knee_flex = abs(float(joint_pos[20]))
|
||||||
|
avg_knee_flex = 0.5 * (left_knee_flex + right_knee_flex)
|
||||||
|
|
||||||
|
max_leg_roll = 0.5 # 防止劈叉姿势
|
||||||
|
split_penalty = -0.12 * max(0.0, (left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||||
|
left_hip_yaw = -float(joint_pos[13])
|
||||||
|
right_hip_yaw = float(joint_pos[19])
|
||||||
|
|
||||||
|
min_leg_separation = 0.04 # 最小腿间距(防止贴得太近)
|
||||||
|
inward_penalty = 0.3 * min(0.0, (left_hip_roll-min_leg_separation)) + 0.3 * min(0.0, (right_hip_roll-min_leg_separation)) # 惩罚左右腿过度内扣
|
||||||
|
|
||||||
|
|
||||||
|
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||||
|
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||||
|
|
||||||
|
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||||
|
ankle_roll_penalty = -0.12 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||||
|
|
||||||
|
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||||
|
ankle_roll_cross_penalty = -0.12 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||||
|
|
||||||
|
# 分别惩罚左右大腿过度转动
|
||||||
|
max_hip_yaw = 0.2 # 最大允许的yaw角度
|
||||||
|
left_hip_yaw_penalty = -0.6 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||||
|
right_hip_yaw_penalty = -0.6 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||||
|
|
||||||
|
target_vec = self.target_position - current_pos
|
||||||
|
target_dist = float(np.linalg.norm(target_vec))
|
||||||
|
if target_dist > 1e-6:
|
||||||
|
target_heading = math.atan2(float(target_vec[1]), float(target_vec[0]))
|
||||||
|
robot_heading = math.radians(float(robot.global_orientation_euler[2]))
|
||||||
|
heading_error = self._wrap_to_pi(target_heading - robot_heading)
|
||||||
|
heading_align_reward = self.reward_heading_align_scale * math.cos(heading_error)
|
||||||
|
heading_error_penalty = -self.reward_heading_error_scale * abs(heading_error)
|
||||||
|
else:
|
||||||
|
heading_align_reward = 0.0
|
||||||
|
heading_error_penalty = 0.0
|
||||||
|
|
||||||
|
# Forward-progress reward (distance delta) with anti-stuck shaping.
|
||||||
|
progress_reward = self.reward_progress_scale * dist_delta
|
||||||
|
survival_reward = self.reward_survival_scale
|
||||||
|
smoothness_penalty = -self.reward_smoothness_scale * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
step_displacement = float(np.linalg.norm(current_pos - previous_pos))
|
||||||
|
accel_signal = 0.0
|
||||||
|
accel_source = "imu_delta"
|
||||||
|
accel_now = np.array(getattr(robot, "accelerometer", np.zeros(3)), dtype=np.float32)
|
||||||
|
if accel_now.shape[0] >= 3:
|
||||||
|
# Use IMU acceleration delta to reduce gravity bias and punish abrupt bursts.
|
||||||
|
accel_signal = float(np.linalg.norm(accel_now[:3] - self._prev_accelerometer[:3]))
|
||||||
|
self._prev_accelerometer = accel_now
|
||||||
|
accel_penalty = -min(
|
||||||
|
self.reward_accel_penalty_cap,
|
||||||
|
self.reward_accel_penalty_scale * accel_signal,
|
||||||
|
)
|
||||||
|
accel_abs = float(np.linalg.norm(accel_now[:3])) if accel_now.shape[0] >= 3 else 0.0
|
||||||
|
accel_abs_over = max(0.0, accel_abs - self.reward_accel_abs_limit)
|
||||||
|
accel_abs_penalty = -min(
|
||||||
|
self.reward_accel_abs_penalty_cap,
|
||||||
|
self.reward_accel_abs_penalty_scale * accel_abs_over,
|
||||||
|
)
|
||||||
|
if self.step_counter > 30 and step_displacement < 0.015 and self.target_distance_wp > 0.3:
|
||||||
|
idle_penalty = -self.reward_idle_penalty_scale
|
||||||
|
else:
|
||||||
|
idle_penalty = 0.0
|
||||||
|
|
||||||
|
if self.step_counter > self.exploration_start_steps:
|
||||||
|
displacement_novelty = step_displacement / max(1e-6, self.stationary_step_eps)
|
||||||
|
exploration_bonus = min(
|
||||||
|
self.exploration_cap,
|
||||||
|
self.exploration_scale * max(0.0, displacement_novelty - self.exploration_target_novelty),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
exploration_bonus = 0.0
|
||||||
|
|
||||||
|
# Encourage active/varied knee motions early in training without dominating progress reward.
|
||||||
|
left_knee_act = float(action[14])
|
||||||
|
right_knee_act = float(action[20])
|
||||||
|
left_knee_delta = abs(left_knee_act - float(self.last_action_for_reward[14]))
|
||||||
|
right_knee_delta = abs(right_knee_act - float(self.last_action_for_reward[20]))
|
||||||
|
knee_action_mag = 0.5 * (abs(left_knee_act) + abs(right_knee_act))
|
||||||
|
knee_action_delta = 0.5 * (left_knee_delta + right_knee_delta)
|
||||||
|
if self.step_counter > 10:
|
||||||
|
knee_explore_reward = min(
|
||||||
|
self.reward_knee_explore_cap,
|
||||||
|
self.reward_knee_explore_scale * knee_action_mag
|
||||||
|
+ self.reward_knee_explore_delta_scale * knee_action_delta,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
knee_explore_reward = 0.0
|
||||||
|
|
||||||
|
# Directly encourage observable knee flexion instead of only action exploration.
|
||||||
|
knee_lift_shortfall_penalty = -self.reward_knee_lift_shortfall_scale * max(
|
||||||
|
0.0, self.reward_knee_lift_target - avg_knee_flex
|
||||||
|
)
|
||||||
|
|
||||||
|
# Encourage hip-pitch exploration to improve forward stride generation.
|
||||||
|
left_hip_pitch_act = float(action[11])
|
||||||
|
right_hip_pitch_act = float(action[17])
|
||||||
|
left_hip_pitch_delta = abs(left_hip_pitch_act - float(self.last_action_for_reward[11]))
|
||||||
|
right_hip_pitch_delta = abs(right_hip_pitch_act - float(self.last_action_for_reward[17]))
|
||||||
|
hip_pitch_action_mag = 0.5 * (abs(left_hip_pitch_act) + abs(right_hip_pitch_act))
|
||||||
|
hip_pitch_action_delta = 0.5 * (left_hip_pitch_delta + right_hip_pitch_delta)
|
||||||
|
if self.step_counter > 10:
|
||||||
|
hip_pitch_explore_reward = min(
|
||||||
|
self.reward_hip_pitch_explore_cap,
|
||||||
|
self.reward_hip_pitch_explore_scale * hip_pitch_action_mag
|
||||||
|
+ self.reward_hip_pitch_explore_delta_scale * hip_pitch_action_delta,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
hip_pitch_explore_reward = 0.0
|
||||||
|
|
||||||
|
if curr_dist_to_target < 0.3:
|
||||||
|
arrival_bonus = self.target_distance_wp * 8 ## 奖励到达目标点
|
||||||
|
else:
|
||||||
|
arrival_bonus = 0.0
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_error = height - target_height
|
||||||
|
|
||||||
|
height_penalty = -0.5 * (math.exp(15*abs(height_error))-1)
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
posture_penalty = -0.6 * (tilt_mag)
|
||||||
|
total = (
|
||||||
|
progress_reward
|
||||||
|
+ survival_reward
|
||||||
|
+ smoothness_penalty
|
||||||
|
+ accel_penalty
|
||||||
|
+ accel_abs_penalty
|
||||||
|
+ idle_penalty
|
||||||
|
+ split_penalty
|
||||||
|
+ inward_penalty
|
||||||
|
+ ankle_roll_penalty
|
||||||
|
+ ankle_roll_cross_penalty
|
||||||
|
+ left_hip_yaw_penalty
|
||||||
|
+ right_hip_yaw_penalty
|
||||||
|
+ heading_align_reward
|
||||||
|
+ heading_error_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + knee_explore_reward
|
||||||
|
# + knee_lift_shortfall_penalty
|
||||||
|
# + hip_pitch_explore_reward
|
||||||
|
+ arrival_bonus
|
||||||
|
+ height_penalty
|
||||||
|
+ posture_penalty
|
||||||
|
)
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||||
|
self._reward_debug_last_time = now
|
||||||
|
self._reward_debug_steps_left = max(1, self.reward_debug_burst_steps)
|
||||||
|
|
||||||
|
if self._reward_debug_steps_left > 0:
|
||||||
|
self._reward_debug_steps_left -= 1
|
||||||
|
self.debug_log(
|
||||||
|
f"progress_reward:{progress_reward:.4f},"
|
||||||
|
f"survival_reward:{survival_reward:.4f},"
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||||
|
f"accel_penalty:{accel_penalty:.4f},"
|
||||||
|
f"accel_source:{accel_source},"
|
||||||
|
f"accel_signal:{accel_signal:.4f},"
|
||||||
|
f"accel_abs:{accel_abs:.4f},"
|
||||||
|
f"accel_abs_penalty:{accel_abs_penalty:.4f},"
|
||||||
|
f"idle_penalty:{idle_penalty:.4f},"
|
||||||
|
f"split_penalty:{split_penalty:.4f},"
|
||||||
|
f"inward_penalty:{inward_penalty:.4f},"
|
||||||
|
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||||
|
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||||
|
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||||
|
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||||
|
f"heading_align_reward:{heading_align_reward:.4f},"
|
||||||
|
f"heading_error_penalty:{heading_error_penalty:.4f},"
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f},"
|
||||||
|
f"height_penalty:{height_penalty:.4f},"
|
||||||
|
# f"knee_explore_reward:{knee_explore_reward:.4f},"
|
||||||
|
f"posture_penalty:{posture_penalty:.4f},"
|
||||||
|
# f"knee_lift_shortfall_penalty:{knee_lift_shortfall_penalty:.4f},"
|
||||||
|
# f"hip_pitch_explore_reward:{hip_pitch_explore_reward:.4f},"
|
||||||
|
f"arrival_bonus:{arrival_bonus:.4f},"
|
||||||
|
f"total:{total:.4f}"
|
||||||
|
)
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||||
|
if self.previous_action is not None:
|
||||||
|
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||||
|
# Loosen upper-body constraints: keep motion bounded but no longer hard-lock head/arms/waist.
|
||||||
|
action[0:2] = 0
|
||||||
|
action[3] = np.clip(action[3], 3, 5)
|
||||||
|
action[7] = np.clip(action[7], -5, -3)
|
||||||
|
action[2] = np.clip(action[2], -6, 6)
|
||||||
|
action[6] = np.clip(action[6], -6, 6)
|
||||||
|
action[4] = 0
|
||||||
|
action[5] = np.clip(action[5], -8, -2)
|
||||||
|
action[8] = 0
|
||||||
|
action[9] = np.clip(action[9], 8, 2)
|
||||||
|
action[10] = np.clip(action[10], -0.6, 0.6)
|
||||||
|
# Boost knee command range so policy can produce visible knee flexion earlier.
|
||||||
|
action[14] = np.clip(action[14], 0, 10.0)
|
||||||
|
action[20] = np.clip(action[20], -10.0, 0)
|
||||||
|
# action[14] = 1 # the correct left knee sign
|
||||||
|
# action[20] = -1 # the correct right knee sign
|
||||||
|
# action[11] = 1
|
||||||
|
# action[17] = 1
|
||||||
|
# action[12] = -1
|
||||||
|
# action[18] = 1
|
||||||
|
# action[13] = -1.0
|
||||||
|
# action[19] = 1.0
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=60, kd=1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action.copy()
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
|
||||||
|
self.prev_action_history[self.history_idx] = action.copy()
|
||||||
|
self.history_idx = (self.history_idx + 1) % self.action_history_len
|
||||||
|
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Check if current waypoint is reached
|
||||||
|
if self.train_stage != "in_place":
|
||||||
|
dist_to_waypoint = float(np.linalg.norm(current_pos - self.target_position))
|
||||||
|
if dist_to_waypoint < self.waypoint_reach_distance:
|
||||||
|
# Move to next waypoint
|
||||||
|
self.waypoint_index += 1
|
||||||
|
if self.waypoint_index >= len(self.point_list):
|
||||||
|
# All waypoints completed
|
||||||
|
self.route_completed = True
|
||||||
|
else:
|
||||||
|
# Update target to next waypoint
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = 20
|
||||||
|
server_warmup_sec = 3.0
|
||||||
|
n_steps_per_env = 256 # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = 512 # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 90000000
|
||||||
|
learning_rate = 2e-4
|
||||||
|
ent_coef = 0.035
|
||||||
|
clip_range = 0.2
|
||||||
|
gamma = 0.97
|
||||||
|
n_epochs = 3
|
||||||
|
enable_eval = True
|
||||||
|
monitor_train_env = False
|
||||||
|
eval_freq_mult = 60
|
||||||
|
save_freq_mult = 60
|
||||||
|
eval_eps = 7
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
env = None
|
||||||
|
eval_env = None
|
||||||
|
servers = None
|
||||||
|
try:
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=monitor_train_env) for i in range(n_envs)], start_method="spawn")
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
if enable_eval:
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=ent_coef, # Entropy coefficient for exploration
|
||||||
|
clip_range=clip_range, # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=gamma, # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=n_epochs,
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * max(1, eval_freq_mult),
|
||||||
|
save_freq=n_steps_per_env * max(1, save_freq_mult),
|
||||||
|
eval_eps=max(1, eval_eps),
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
return
|
||||||
|
finally:
|
||||||
|
if env is not None:
|
||||||
|
env.close()
|
||||||
|
if eval_env is not None:
|
||||||
|
eval_env.close()
|
||||||
|
if servers is not None:
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = False
|
||||||
|
test_no_realtime = False
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Turn_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Turn_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
BIN
scripts/gyms/logs/Walk_version_0.2.0.zip
Normal file
BIN
scripts/gyms/logs/Walk_version_0.2.0.zip
Normal file
Binary file not shown.
BIN
scripts/gyms/logs/stand_stable_0.1.zip
Normal file
BIN
scripts/gyms/logs/stand_stable_0.1.zip
Normal file
Binary file not shown.
626
scripts/gyms/logs/stand_stable_0.1/Walk.py
Normal file
626
scripts/gyms/logs/stand_stable_0.1/Walk.py
Normal file
@@ -0,0 +1,626 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = True
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(self.Player.robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(self.Player.robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.3
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 1200:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.3
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = 20 # Reduced from 8 to decrease CPU/network pressure during init
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
n_steps_per_env = 256 # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = 512 # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = 3e-4
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env):
|
||||||
|
def thunk():
|
||||||
|
return WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i) for i in range(n_envs)])
|
||||||
|
eval_env = SubprocVecEnv([init_env(n_envs)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=0.05, # Entropy coefficient for exploration
|
||||||
|
# clip_range=0.13, # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=0.95 , # Discount factor
|
||||||
|
target_kl=0.03,
|
||||||
|
n_epochs=5
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 10, save_freq=n_steps_per_env * 10,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
server = Train_Server(self.server_p - 1, self.monitor_p, 1)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
trainer.train({})
|
||||||
|
# trainer.test({"model_file": "scripts/gyms/logs/Walk_R0_000/best_model.zip",
|
||||||
|
# "folder_dir": "scripts/gyms/logs/Walk_R0_000/",})
|
||||||
BIN
scripts/gyms/logs/stand_stable_final.zip
Normal file
BIN
scripts/gyms/logs/stand_stable_final.zip
Normal file
Binary file not shown.
705
scripts/gyms/logs/stand_stable_final/Walk.py
Executable file
705
scripts/gyms/logs/stand_stable_final/Walk.py
Executable file
@@ -0,0 +1,705 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
from time import sleep
|
||||||
|
from random import random
|
||||||
|
from random import uniform
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
|
from stable_baselines3 import PPO
|
||||||
|
from stable_baselines3.common.monitor import Monitor
|
||||||
|
from stable_baselines3.common.vec_env import SubprocVecEnv, DummyVecEnv
|
||||||
|
|
||||||
|
import gymnasium as gym
|
||||||
|
from gymnasium import spaces
|
||||||
|
|
||||||
|
from scripts.commons.Train_Base import Train_Base
|
||||||
|
from scripts.commons.Server import Server as Train_Server
|
||||||
|
|
||||||
|
from agent.base_agent import Base_Agent
|
||||||
|
from utils.math_ops import MathOps
|
||||||
|
|
||||||
|
from scipy.spatial.transform import Rotation as R
|
||||||
|
|
||||||
|
'''
|
||||||
|
Objective:
|
||||||
|
Learn how to run forward using step primitive
|
||||||
|
----------
|
||||||
|
- class Basic_Run: implements an OpenAI custom gym
|
||||||
|
- class Train: implements algorithms to train a new model or test an existing model
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class WalkEnv(gym.Env):
|
||||||
|
def __init__(self, ip, server_p) -> None:
|
||||||
|
|
||||||
|
# Args: Server IP, Agent Port, Monitor Port, Uniform No., Robot Type, Team Name, Enable Log, Enable Draw
|
||||||
|
self.Player = player = Base_Agent(
|
||||||
|
team_name="Gym",
|
||||||
|
number=1,
|
||||||
|
host=ip,
|
||||||
|
port=server_p
|
||||||
|
)
|
||||||
|
self.robot_type = self.Player.robot
|
||||||
|
self.step_counter = 0 # to limit episode size
|
||||||
|
self.force_play_on = True
|
||||||
|
|
||||||
|
self.target_position = np.array([0.0, 0.0]) # target position in the x-y plane
|
||||||
|
self.initial_position = np.array([0.0, 0.0]) # initial position in the x-y plane
|
||||||
|
self.target_direction = 0.0 # target direction in the x-y plane (relative to the robot's orientation)
|
||||||
|
self.isfallen = False
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.debug_every_n_steps = 5
|
||||||
|
self.enable_debug_joint_status = False
|
||||||
|
self.calibrate_nominal_from_neutral = True
|
||||||
|
self.auto_calibrate_train_sim_flip = True
|
||||||
|
self.nominal_calibrated_once = False
|
||||||
|
self.flip_calibrated_once = False
|
||||||
|
self._target_hz = 0.0
|
||||||
|
self._target_dt = 0.0
|
||||||
|
self._last_sync_time = None
|
||||||
|
target_hz_env = 0
|
||||||
|
if target_hz_env:
|
||||||
|
try:
|
||||||
|
self._target_hz = float(target_hz_env)
|
||||||
|
except ValueError:
|
||||||
|
self._target_hz = 0.0
|
||||||
|
if self._target_hz > 0.0:
|
||||||
|
self._target_dt = 1.0 / self._target_hz
|
||||||
|
|
||||||
|
# State space
|
||||||
|
# 原始观测大小: 78
|
||||||
|
obs_size = 78
|
||||||
|
self.obs = np.zeros(obs_size, np.float32)
|
||||||
|
self.observation_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(obs_size,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
action_dim = len(self.Player.robot.ROBOT_MOTORS)
|
||||||
|
self.no_of_actions = action_dim
|
||||||
|
self.action_space = spaces.Box(
|
||||||
|
low=-10.0,
|
||||||
|
high=10.0,
|
||||||
|
shape=(action_dim,),
|
||||||
|
dtype=np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
# 中立姿态
|
||||||
|
self.joint_nominal_position = np.array(
|
||||||
|
[
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
-1.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.8,
|
||||||
|
-0.4,
|
||||||
|
0.0,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
-0.8,
|
||||||
|
0.4,
|
||||||
|
0.0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||||
|
self.train_sim_flip = np.array(
|
||||||
|
[
|
||||||
|
1.0, # 0: Head_yaw (he1)
|
||||||
|
-1.0, # 1: Head_pitch (he2)
|
||||||
|
1.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||||
|
-1.0, # 3: Left_Shoulder_Roll (lae2)
|
||||||
|
-1.0, # 4: Left_Elbow_Pitch (lae3)
|
||||||
|
1.0, # 5: Left_Elbow_Yaw (lae4)
|
||||||
|
-1.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||||
|
-1.0, # 7: Right_Shoulder_Roll (rae2)
|
||||||
|
1.0, # 8: Right_Elbow_Pitch (rae3)
|
||||||
|
1.0, # 9: Right_Elbow_Yaw (rae4)
|
||||||
|
1.0, # 10: Waist (te1)
|
||||||
|
1.0, # 11: Left_Hip_Pitch (lle1)
|
||||||
|
-1.0, # 12: Left_Hip_Roll (lle2)
|
||||||
|
-1.0, # 13: Left_Hip_Yaw (lle3)
|
||||||
|
1.0, # 14: Left_Knee_Pitch (lle4)
|
||||||
|
1.0, # 15: Left_Ankle_Pitch (lle5)
|
||||||
|
-1.0, # 16: Left_Ankle_Roll (lle6)
|
||||||
|
-1.0, # 17: Right_Hip_Pitch (rle1)
|
||||||
|
-1.0, # 18: Right_Hip_Roll (rle2)
|
||||||
|
-1.0, # 19: Right_Hip_Yaw (rle3)
|
||||||
|
-1.0, # 20: Right_Knee_Pitch (rle4)
|
||||||
|
-1.0, # 21: Right_Ankle_Pitch (rle5)
|
||||||
|
-1.0, # 22: Right_Ankle_Roll (rle6)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.scaling_factor = 0.3
|
||||||
|
# self.scaling_factor = 1
|
||||||
|
|
||||||
|
# Encourage a minimum lateral stance so the policy avoids feet overlap.
|
||||||
|
self.min_stance_rad = 0.10
|
||||||
|
|
||||||
|
# Small reset perturbations for robustness training.
|
||||||
|
self.enable_reset_perturb = False
|
||||||
|
self.reset_beam_yaw_range_deg = 180 # randomize target direction fully to encourage learning a real walk instead of a fixed gait
|
||||||
|
self.reset_joint_noise_rad = 0.015
|
||||||
|
self.reset_perturb_steps = 3
|
||||||
|
self.reset_recover_steps = 8
|
||||||
|
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Track previous position
|
||||||
|
self.Player.server.connect()
|
||||||
|
# sleep(2.0) # Longer wait for connection to establish completely
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
|
def _reconnect_server(self):
|
||||||
|
try:
|
||||||
|
self.Player.server.shutdown()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.Player.server.connect()
|
||||||
|
self.Player.server.send_immediate(
|
||||||
|
f"(init {self.Player.robot.name} {self.Player.world.team_name} {self.Player.world.number})"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _safe_receive_world_update(self, retries=1):
|
||||||
|
last_exc = None
|
||||||
|
for attempt in range(retries + 1):
|
||||||
|
try:
|
||||||
|
self.Player.server.receive()
|
||||||
|
self.Player.world.update()
|
||||||
|
return
|
||||||
|
except (ConnectionResetError, OSError) as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt >= retries:
|
||||||
|
raise
|
||||||
|
self._reconnect_server()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
|
|
||||||
|
def debug_log(self, message):
|
||||||
|
print(message)
|
||||||
|
try:
|
||||||
|
log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "comm_debug.log")
|
||||||
|
with open(log_path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(message + "\n")
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def observe(self, init=False):
|
||||||
|
|
||||||
|
"""获取当前观测值"""
|
||||||
|
robot = self.Player.robot
|
||||||
|
world = self.Player.world
|
||||||
|
|
||||||
|
# Safety check: ensure data is available
|
||||||
|
|
||||||
|
# 计算目标速度
|
||||||
|
raw_target = self.target_position - world.global_position[:2]
|
||||||
|
velocity = MathOps.rotate_2d_vec(
|
||||||
|
raw_target,
|
||||||
|
-robot.global_orientation_euler[2],
|
||||||
|
is_rad=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算相对方向
|
||||||
|
rel_orientation = MathOps.vector_angle(velocity) * 0.3
|
||||||
|
rel_orientation = np.clip(rel_orientation, -0.25, 0.25)
|
||||||
|
|
||||||
|
velocity = np.concatenate([velocity, np.array([rel_orientation])])
|
||||||
|
velocity[0] = np.clip(velocity[0], -0.5, 0.5)
|
||||||
|
velocity[1] = np.clip(velocity[1], -0.25, 0.25)
|
||||||
|
|
||||||
|
# 关节状态
|
||||||
|
radian_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
radian_joint_speeds = np.deg2rad(
|
||||||
|
[robot.motor_speeds[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
|
||||||
|
qpos_qvel_previous_action = np.concatenate([
|
||||||
|
(radian_joint_positions * self.train_sim_flip - self.joint_nominal_position) / 4.6,
|
||||||
|
radian_joint_speeds / 110.0 * self.train_sim_flip,
|
||||||
|
self.previous_action / 10.0,
|
||||||
|
])
|
||||||
|
|
||||||
|
# 角速度
|
||||||
|
ang_vel = np.clip(np.deg2rad(robot.gyroscope) / 50.0, -1.0, 1.0)
|
||||||
|
|
||||||
|
# 投影的重力方向
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
|
||||||
|
# 组合观测
|
||||||
|
observation = np.concatenate([
|
||||||
|
qpos_qvel_previous_action,
|
||||||
|
ang_vel,
|
||||||
|
velocity,
|
||||||
|
projected_gravity,
|
||||||
|
])
|
||||||
|
|
||||||
|
observation = np.clip(observation, -10.0, 10.0)
|
||||||
|
return observation.astype(np.float32)
|
||||||
|
|
||||||
|
def sync(self):
|
||||||
|
''' Run a single simulation step '''
|
||||||
|
self._safe_receive_world_update(retries=1)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.send()
|
||||||
|
if self._target_dt > 0.0:
|
||||||
|
now = time.time()
|
||||||
|
if self._last_sync_time is None:
|
||||||
|
self._last_sync_time = now
|
||||||
|
return
|
||||||
|
elapsed = now - self._last_sync_time
|
||||||
|
remaining = self._target_dt - elapsed
|
||||||
|
if remaining > 0.0:
|
||||||
|
time.sleep(remaining)
|
||||||
|
now = time.time()
|
||||||
|
self._last_sync_time = now
|
||||||
|
|
||||||
|
def debug_joint_status(self):
|
||||||
|
robot = self.Player.robot
|
||||||
|
actual_joint_positions = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
)
|
||||||
|
target_joint_positions = getattr(
|
||||||
|
self,
|
||||||
|
'target_joint_positions',
|
||||||
|
np.zeros(len(robot.ROBOT_MOTORS), dtype=np.float32)
|
||||||
|
)
|
||||||
|
joint_error = actual_joint_positions - target_joint_positions
|
||||||
|
leg_slice = slice(11, None)
|
||||||
|
|
||||||
|
self.debug_log(
|
||||||
|
"[WalkDebug] "
|
||||||
|
f"step={self.step_counter} "
|
||||||
|
f"pos={np.round(self.Player.world.global_position, 3).tolist()} "
|
||||||
|
f"target_xy={np.round(self.target_position, 3).tolist()} "
|
||||||
|
f"target_leg={np.round(target_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"actual_leg={np.round(actual_joint_positions[leg_slice], 3).tolist()} "
|
||||||
|
f"err_norm={float(np.linalg.norm(joint_error)):.4f} "
|
||||||
|
f"fallen={self.Player.world.global_position[2] < 0.3}"
|
||||||
|
)
|
||||||
|
print(f"waist target={target_joint_positions[10]:.3f}, actual={actual_joint_positions[10]:.3f}")
|
||||||
|
|
||||||
|
def reset(self, seed=None, options=None):
|
||||||
|
'''
|
||||||
|
Reset and stabilize the robot
|
||||||
|
Note: for some behaviors it would be better to reduce stabilization or add noise
|
||||||
|
'''
|
||||||
|
r = self.Player.robot
|
||||||
|
super().reset(seed=seed)
|
||||||
|
if seed is not None:
|
||||||
|
np.random.seed(seed)
|
||||||
|
|
||||||
|
length1 = 2 # randomize target distance
|
||||||
|
length2 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
length3 = np.random.uniform(0.6, 1) # randomize target distance
|
||||||
|
angle2 = np.random.uniform(-30, 30) # randomize initial orientation
|
||||||
|
angle3 = np.random.uniform(-30, 30) # randomize target direction
|
||||||
|
|
||||||
|
self.step_counter = 0
|
||||||
|
self.waypoint_index = 0
|
||||||
|
self.route_completed = False
|
||||||
|
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||||
|
self.previous_pos = np.array([0.0, 0.0]) # Initialize for first step
|
||||||
|
self.walk_cycle_step = 0
|
||||||
|
|
||||||
|
# 随机 beam 目标位置和朝向,增加训练多样性
|
||||||
|
beam_x = (random() - 0.5) * 10
|
||||||
|
beam_y = (random() - 0.5) * 10
|
||||||
|
beam_yaw = uniform(-self.reset_beam_yaw_range_deg, self.reset_beam_yaw_range_deg)
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
self._safe_receive_world_update(retries=2)
|
||||||
|
self.Player.robot.commit_motor_targets_pd()
|
||||||
|
self.Player.server.commit_beam(pos2d=(beam_x, beam_y), rotation=beam_yaw)
|
||||||
|
self.Player.server.send()
|
||||||
|
|
||||||
|
# 执行 Neutral 技能直到完成,给机器人足够时间在 beam 位置稳定站立
|
||||||
|
finished_count = 0
|
||||||
|
for _ in range(50):
|
||||||
|
finished = self.Player.skills_manager.execute("Neutral")
|
||||||
|
self.sync()
|
||||||
|
if finished:
|
||||||
|
finished_count += 1
|
||||||
|
if finished_count >= 20: # 假设需要连续20次完成才算成功
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.enable_reset_perturb and self.reset_joint_noise_rad > 0.0:
|
||||||
|
perturb_action = np.zeros(self.no_of_actions, dtype=np.float32)
|
||||||
|
# Perturb waist + lower body only (10:), keep head/arms stable.
|
||||||
|
perturb_action[10:] = np.random.uniform(
|
||||||
|
-self.reset_joint_noise_rad,
|
||||||
|
self.reset_joint_noise_rad,
|
||||||
|
size=(self.no_of_actions - 10,)
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(self.reset_perturb_steps):
|
||||||
|
target_joint_positions = (self.joint_nominal_position + perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
for i in range(self.reset_recover_steps):
|
||||||
|
# Linearly fade perturbation to help policy start from near-neutral.
|
||||||
|
alpha = 1.0 - float(i + 1) / float(self.reset_recover_steps)
|
||||||
|
target_joint_positions = (self.joint_nominal_position + alpha * perturb_action) * self.train_sim_flip
|
||||||
|
for idx, target in enumerate(target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||||
|
)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# memory variables
|
||||||
|
self.sync()
|
||||||
|
self.initial_position = np.array(self.Player.world.global_position[:2])
|
||||||
|
self.previous_pos = self.initial_position.copy() # Critical: set to actual position
|
||||||
|
self.act = np.zeros(self.no_of_actions, np.float32)
|
||||||
|
# Build target in the robot's current forward direction instead of fixed global +x.
|
||||||
|
heading_deg = float(r.global_orientation_euler[2])
|
||||||
|
forward_offset = MathOps.rotate_2d_vec(np.array([length1, 0.0]), heading_deg, is_rad=False)
|
||||||
|
point1 = self.initial_position + forward_offset
|
||||||
|
point2 = point1 + MathOps.rotate_2d_vec(np.array([length2, 0]), angle2, is_rad=False)
|
||||||
|
point3 = point2 + MathOps.rotate_2d_vec(np.array([length3, 0]), angle3, is_rad=False)
|
||||||
|
self.point_list = [point1]
|
||||||
|
self.target_position = self.point_list[self.waypoint_index]
|
||||||
|
self.initial_height = self.Player.world.global_position[2]
|
||||||
|
|
||||||
|
return self.observe(True), {}
|
||||||
|
|
||||||
|
def render(self, mode='human', close=False):
|
||||||
|
return
|
||||||
|
|
||||||
|
def compute_reward(self, previous_pos, current_pos, action):
|
||||||
|
height = float(self.Player.world.global_position[2])
|
||||||
|
robot = self.Player.robot
|
||||||
|
|
||||||
|
orientation_quat_inv = R.from_quat(robot._global_cheat_orientation).inv()
|
||||||
|
projected_gravity = orientation_quat_inv.apply(np.array([0.0, 0.0, -1.0]))
|
||||||
|
tilt_mag = float(np.linalg.norm(projected_gravity[:2]))
|
||||||
|
ang_vel = np.deg2rad(robot.gyroscope)
|
||||||
|
ang_vel_mag = float(np.linalg.norm(ang_vel))
|
||||||
|
|
||||||
|
is_fallen = height < 0.55
|
||||||
|
if is_fallen:
|
||||||
|
# remain = max(0, 800 - self.step_counter)
|
||||||
|
# return -8.0 - 0.01 * remain
|
||||||
|
return -1.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# # 目标方向
|
||||||
|
# to_target = self.target_position - current_pos
|
||||||
|
# dist_to_target = float(np.linalg.norm(to_target))
|
||||||
|
# if dist_to_target < 0.5:
|
||||||
|
# return 15.0
|
||||||
|
|
||||||
|
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||||
|
# delta_pos = current_pos - previous_pos
|
||||||
|
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||||
|
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||||
|
|
||||||
|
# 奖励项
|
||||||
|
# progress_reward = 2 * forward_step
|
||||||
|
# lateral_penalty = -0.1 * lateral_step
|
||||||
|
alive_bonus = 2.0
|
||||||
|
|
||||||
|
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||||
|
smoothness_penalty = -0.01 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||||
|
|
||||||
|
posture_penalty = -0.3 * (tilt_mag)
|
||||||
|
ang_vel_penalty = -0.02 * ang_vel_mag
|
||||||
|
|
||||||
|
# Use simulator joint readings in training frame to shape lateral stance.
|
||||||
|
joint_pos = np.deg2rad(
|
||||||
|
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||||
|
) * self.train_sim_flip
|
||||||
|
left_hip_roll = float(joint_pos[12])
|
||||||
|
right_hip_roll = float(joint_pos[18])
|
||||||
|
left_ankle_roll = float(joint_pos[16])
|
||||||
|
right_ankle_roll = float(joint_pos[22])
|
||||||
|
|
||||||
|
hip_spread = left_hip_roll - right_hip_roll
|
||||||
|
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||||
|
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||||
|
|
||||||
|
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||||
|
stance_collapse_penalty = -4.0 * max(0.0, self.min_stance_rad - stance_metric)
|
||||||
|
cross_leg_penalty = -1.2 * max(0.0, -(hip_spread * ankle_spread))
|
||||||
|
|
||||||
|
target_height = self.initial_height
|
||||||
|
height_error = height - target_height
|
||||||
|
height_penalty = -0.5 * abs(height_error) # 惩罚高度偏离,系数可调
|
||||||
|
|
||||||
|
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||||
|
# if not hasattr(self, 'last_height'):
|
||||||
|
# self.last_height = height
|
||||||
|
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||||
|
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||||
|
# self.last_height = height
|
||||||
|
|
||||||
|
# 惩罚高度下降(负变化率)
|
||||||
|
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||||
|
|
||||||
|
# # 在 compute_reward 中
|
||||||
|
# if self.step_counter > 50:
|
||||||
|
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||||
|
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||||
|
# exploration_bonus = 0.05 * novelty
|
||||||
|
# else:
|
||||||
|
# exploration_bonus = 0
|
||||||
|
|
||||||
|
# self.prev_action_history[self.history_idx] = action
|
||||||
|
# self.history_idx = (self.history_idx + 1) % 50
|
||||||
|
|
||||||
|
|
||||||
|
total = (
|
||||||
|
# progress_reward +
|
||||||
|
alive_bonus +
|
||||||
|
# lateral_penalty +
|
||||||
|
# action_penalty +
|
||||||
|
smoothness_penalty +
|
||||||
|
posture_penalty
|
||||||
|
+ ang_vel_penalty
|
||||||
|
+ height_penalty
|
||||||
|
+ stance_collapse_penalty
|
||||||
|
+ cross_leg_penalty
|
||||||
|
# + exploration_bonus
|
||||||
|
# + height_down_penalty
|
||||||
|
)
|
||||||
|
if time.time() - self.start_time >= 600:
|
||||||
|
self.start_time = time.time()
|
||||||
|
print(
|
||||||
|
# f"progress_reward:{progress_reward:.4f}",
|
||||||
|
# f"lateral_penalty:{lateral_penalty:.4f}",
|
||||||
|
# f"action_penalty:{action_penalty:.4f}"s,
|
||||||
|
f"height_penalty:{height_penalty:.4f}",
|
||||||
|
f"smoothness_penalty:{smoothness_penalty:.4f},",
|
||||||
|
f"posture_penalty:{posture_penalty:.4f}",
|
||||||
|
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}",
|
||||||
|
f"cross_leg_penalty:{cross_leg_penalty:.4f}",
|
||||||
|
# f"ang_vel_penalty:{ang_vel_penalty:.4f}",
|
||||||
|
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||||
|
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def step(self, action):
|
||||||
|
|
||||||
|
r = self.Player.robot
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.target_joint_positions = (
|
||||||
|
# self.joint_nominal_position +
|
||||||
|
self.scaling_factor * action
|
||||||
|
)
|
||||||
|
self.target_joint_positions *= self.train_sim_flip
|
||||||
|
|
||||||
|
for idx, target in enumerate(self.target_joint_positions):
|
||||||
|
r.set_motor_target_position(
|
||||||
|
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=40, kd=1.0
|
||||||
|
)
|
||||||
|
|
||||||
|
self.previous_action = action
|
||||||
|
|
||||||
|
self.sync() # run simulation step
|
||||||
|
self.step_counter += 1
|
||||||
|
|
||||||
|
if self.enable_debug_joint_status and self.step_counter % self.debug_every_n_steps == 0:
|
||||||
|
self.debug_joint_status()
|
||||||
|
|
||||||
|
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||||
|
|
||||||
|
# Compute reward based on movement from previous step
|
||||||
|
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||||
|
|
||||||
|
# Update previous position
|
||||||
|
self.previous_pos = current_pos.copy()
|
||||||
|
self.last_action_for_reward = action.copy()
|
||||||
|
|
||||||
|
# Fall detection and penalty
|
||||||
|
is_fallen = self.Player.world.global_position[2] < 0.55
|
||||||
|
|
||||||
|
# terminal state: the robot is falling or timeout
|
||||||
|
terminated = is_fallen or self.step_counter > 800 or self.route_completed
|
||||||
|
truncated = False
|
||||||
|
|
||||||
|
return self.observe(), reward, terminated, truncated, {}
|
||||||
|
|
||||||
|
|
||||||
|
class Train(Train_Base):
|
||||||
|
def __init__(self, script) -> None:
|
||||||
|
super().__init__(script)
|
||||||
|
|
||||||
|
def train(self, args):
|
||||||
|
|
||||||
|
# --------------------------------------- Learning parameters
|
||||||
|
n_envs = int(os.environ.get("GYM_CPU_N_ENVS", "20"))
|
||||||
|
if n_envs < 1:
|
||||||
|
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||||
|
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||||
|
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||||
|
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||||
|
total_steps = 30000000
|
||||||
|
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||||
|
folder_name = f'Walk_R{self.robot_type}'
|
||||||
|
model_path = f'./scripts/gyms/logs/{folder_name}/'
|
||||||
|
|
||||||
|
print(f"Model path: {model_path}")
|
||||||
|
print(f"Using {n_envs} parallel environments")
|
||||||
|
|
||||||
|
# --------------------------------------- Run algorithm
|
||||||
|
def init_env(i_env, monitor=False):
|
||||||
|
def thunk():
|
||||||
|
env = WalkEnv(self.ip, self.server_p + i_env)
|
||||||
|
if monitor:
|
||||||
|
env = Monitor(env)
|
||||||
|
return env
|
||||||
|
|
||||||
|
return thunk
|
||||||
|
|
||||||
|
server_log_dir = os.path.join(model_path, "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
servers = Train_Server(self.server_p, self.monitor_p_1000, n_envs + 1, no_render=True, no_realtime=True) # include 1 extra server for testing
|
||||||
|
|
||||||
|
# Wait for servers to start
|
||||||
|
print(f"Starting {n_envs + 1} rcssservermj servers...")
|
||||||
|
if server_warmup_sec > 0:
|
||||||
|
print(f"Waiting {server_warmup_sec:.1f}s for server warmup...")
|
||||||
|
sleep(server_warmup_sec)
|
||||||
|
print("Servers started, creating environments...")
|
||||||
|
|
||||||
|
env = SubprocVecEnv([init_env(i, monitor=True) for i in range(n_envs)])
|
||||||
|
# Use single-process eval env to avoid extra subprocess fragility during callback evaluation.
|
||||||
|
eval_env = DummyVecEnv([init_env(n_envs, monitor=True)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Custom policy network architecture
|
||||||
|
policy_kwargs = dict(
|
||||||
|
net_arch=dict(
|
||||||
|
pi=[512, 256, 128], # Policy network: 3 layers
|
||||||
|
vf=[512, 256, 128] # Value network: 3 layers
|
||||||
|
),
|
||||||
|
activation_fn=__import__('torch.nn', fromlist=['ELU']).ELU,
|
||||||
|
)
|
||||||
|
|
||||||
|
if "model_file" in args: # retrain
|
||||||
|
model = PPO.load(args["model_file"], env=env, device="cpu", n_envs=n_envs, n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size, learning_rate=learning_rate)
|
||||||
|
else: # train new model
|
||||||
|
model = PPO(
|
||||||
|
"MlpPolicy",
|
||||||
|
env=env,
|
||||||
|
verbose=1,
|
||||||
|
n_steps=n_steps_per_env,
|
||||||
|
batch_size=minibatch_size,
|
||||||
|
learning_rate=learning_rate,
|
||||||
|
device="cpu",
|
||||||
|
policy_kwargs=policy_kwargs,
|
||||||
|
ent_coef=float(os.environ.get("GYM_CPU_TRAIN_ENT_COEF", "0.05")), # Entropy coefficient for exploration
|
||||||
|
clip_range=float(os.environ.get("GYM_CPU_TRAIN_CLIP_RANGE", "0.2")), # PPO clipping parameter
|
||||||
|
gae_lambda=0.95, # GAE lambda
|
||||||
|
gamma=float(os.environ.get("GYM_CPU_TRAIN_GAMMA", "0.95")), # Discount factor
|
||||||
|
# target_kl=0.03,
|
||||||
|
n_epochs=int(os.environ.get("GYM_CPU_TRAIN_EPOCHS", "5")),
|
||||||
|
tensorboard_log=f"./scripts/gyms/logs/{folder_name}/tensorboard/"
|
||||||
|
)
|
||||||
|
|
||||||
|
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||||
|
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=100,
|
||||||
|
backup_env_file=__file__)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sleep(1) # wait for child processes
|
||||||
|
print("\nctrl+c pressed, aborting...\n")
|
||||||
|
servers.kill()
|
||||||
|
return
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
eval_env.close()
|
||||||
|
servers.kill()
|
||||||
|
|
||||||
|
def test(self, args):
|
||||||
|
|
||||||
|
# Uses different server and monitor ports
|
||||||
|
server_log_dir = os.path.join(args["folder_dir"], "server_logs")
|
||||||
|
os.makedirs(server_log_dir, exist_ok=True)
|
||||||
|
test_no_render = os.environ.get("GYM_CPU_TEST_NO_RENDER", "0") == "1"
|
||||||
|
test_no_realtime = os.environ.get("GYM_CPU_TEST_NO_REALTIME", "0") == "1"
|
||||||
|
|
||||||
|
server = Train_Server(
|
||||||
|
self.server_p - 1,
|
||||||
|
self.monitor_p,
|
||||||
|
1,
|
||||||
|
no_render=test_no_render,
|
||||||
|
no_realtime=test_no_realtime,
|
||||||
|
)
|
||||||
|
env = WalkEnv(self.ip, self.server_p - 1)
|
||||||
|
model = PPO.load(args["model_file"], env=env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.export_model(args["model_file"], args["model_file"] + ".pkl",
|
||||||
|
False) # Export to pkl to create custom behavior
|
||||||
|
self.test_model(model, env, log_path=args["folder_dir"], model_path=args["folder_dir"])
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
env.close()
|
||||||
|
server.kill()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
# 创建默认参数
|
||||||
|
script_args = SimpleNamespace(
|
||||||
|
args=SimpleNamespace(
|
||||||
|
i='127.0.0.1', # Server IP
|
||||||
|
p=3100, # Server port
|
||||||
|
m=3200, # Monitor port
|
||||||
|
r=0, # Robot type
|
||||||
|
t='Gym', # Team name
|
||||||
|
u=1 # Uniform number
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
trainer = Train(script_args)
|
||||||
|
|
||||||
|
run_mode = os.environ.get("GYM_CPU_MODE", "train").strip().lower()
|
||||||
|
|
||||||
|
if run_mode == "test":
|
||||||
|
test_model_file = os.environ.get("GYM_CPU_TEST_MODEL", "scripts/gyms/logs/Walk_R0_004/best_model.zip")
|
||||||
|
test_folder = os.environ.get("GYM_CPU_TEST_FOLDER", "scripts/gyms/logs/Walk_R0_004/")
|
||||||
|
trainer.test({"model_file": test_model_file, "folder_dir": test_folder})
|
||||||
|
else:
|
||||||
|
retrain_model = os.environ.get("GYM_CPU_TRAIN_MODEL", "").strip()
|
||||||
|
if retrain_model:
|
||||||
|
trainer.train({"model_file": retrain_model})
|
||||||
|
else:
|
||||||
|
trainer.train({})
|
||||||
104
train.sh
Executable file
104
train.sh
Executable file
@@ -0,0 +1,104 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# ------------------------------
|
||||||
|
# 资源限制配置(cgroup v2 + systemd-run)
|
||||||
|
# ------------------------------
|
||||||
|
# 说明:
|
||||||
|
# 1) 这个脚本会把训练进程放进一个临时的 systemd scope 中,并施加 CPU/内存上限。
|
||||||
|
# 2) 仅限制“本次训练进程”,不会永久改系统配置。
|
||||||
|
# 3) 下面变量都支持“环境变量覆盖”,即你可以在命令前临时指定。
|
||||||
|
#
|
||||||
|
# CPU 核数基准(默认 20):
|
||||||
|
# 例如你的机器按 20 核预算来算,可保持默认。
|
||||||
|
CORES="${CORES:-20}"
|
||||||
|
# CPU 占用百分比(默认 100):
|
||||||
|
# 最终会与 CORES 相乘得到 CPUQuota。
|
||||||
|
# 例:CORES=20, UTIL_PERCENT=100 -> CPUQuota=2000%(约 20 核等效)
|
||||||
|
UTIL_PERCENT="${UTIL_PERCENT:-100}"
|
||||||
|
CPU_QUOTA="$((CORES * UTIL_PERCENT))%"
|
||||||
|
|
||||||
|
# 内存上限(默认关闭):
|
||||||
|
# 设为具体值(如 24G/28G)可限制训练最多占用内存;
|
||||||
|
# 设为 0/none/off/infinity 表示不设置 cgroup 内存上限。
|
||||||
|
MEMORY_MAX="${MEMORY_MAX:-0}"
|
||||||
|
|
||||||
|
# ------------------------------
|
||||||
|
# 精简运行参数(由 scripts/gyms/Walk.py 读取)
|
||||||
|
# ------------------------------
|
||||||
|
# 仅保留最常用开关,避免超长环境变量命令。
|
||||||
|
GYM_CPU_MODE="${GYM_CPU_MODE:-train}"
|
||||||
|
GYM_CPU_TRAIN_STAGE="${GYM_CPU_TRAIN_STAGE:-walk}"
|
||||||
|
GYM_CPU_TRAIN_MODEL="${GYM_CPU_TRAIN_MODEL:-}"
|
||||||
|
GYM_CPU_TEST_MODEL="${GYM_CPU_TEST_MODEL:-scripts/gyms/logs/Walk_R0_004/best_model.zip}"
|
||||||
|
GYM_CPU_TEST_FOLDER="${GYM_CPU_TEST_FOLDER:-scripts/gyms/logs/Walk_R0_004/}"
|
||||||
|
|
||||||
|
# Python 解释器选择策略:
|
||||||
|
# 1) 优先使用你手动传入的 PYTHON_BIN
|
||||||
|
# 2) 其次用当前激活 conda 环境(CONDA_PREFIX/bin/python)
|
||||||
|
# 3) 再回退到默认 mujoco 环境路径
|
||||||
|
# 4) 最后尝试系统 python / python3
|
||||||
|
DEFAULT_PYTHON="/home/solren/Downloads/Anaconda/envs/mujoco/bin/python"
|
||||||
|
CONDA_PYTHON="${CONDA_PREFIX:-}/bin/python"
|
||||||
|
|
||||||
|
# 安全保护:不要用 sudo 运行。
|
||||||
|
# 原因:sudo 可能导致 conda 环境与用户会话环境不一致,
|
||||||
|
# 会引发 python 路径丢失、systemd --user 会话不可见等问题。
|
||||||
|
if [[ "${EUID}" -eq 0 ]]; then
|
||||||
|
echo "Do not run this script with sudo; run as your normal user in conda env 'mujoco'."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 解析最终使用的 Python 可执行文件。
|
||||||
|
if [[ -n "${PYTHON_BIN:-}" ]]; then
|
||||||
|
PYTHON_EXEC="${PYTHON_BIN}"
|
||||||
|
elif [[ -n "${CONDA_PREFIX:-}" && -x "${CONDA_PYTHON}" ]]; then
|
||||||
|
PYTHON_EXEC="${CONDA_PYTHON}"
|
||||||
|
elif [[ -x "${DEFAULT_PYTHON}" ]]; then
|
||||||
|
PYTHON_EXEC="${DEFAULT_PYTHON}"
|
||||||
|
elif command -v python >/dev/null 2>&1; then
|
||||||
|
PYTHON_EXEC="$(command -v python)"
|
||||||
|
elif command -v python3 >/dev/null 2>&1; then
|
||||||
|
PYTHON_EXEC="$(command -v python3)"
|
||||||
|
else
|
||||||
|
echo "No Python executable found. Set PYTHON_BIN=/abs/path/to/python and retry."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 脚本所在目录(绝对路径),便于后续定位模块/相对路径。
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
|
||||||
|
# 打印当前生效配置,方便排障和复现实验。
|
||||||
|
echo "Starting training with limits: CPU=${CPU_QUOTA}, Memory=${MEMORY_MAX}"
|
||||||
|
echo "Mode: ${GYM_CPU_MODE}"
|
||||||
|
echo "Run knobs: GYM_CPU_MODE=${GYM_CPU_MODE}, GYM_CPU_TRAIN_STAGE=${GYM_CPU_TRAIN_STAGE}"
|
||||||
|
echo "Using Python: ${PYTHON_EXEC}"
|
||||||
|
if [[ -n "${CONDA_DEFAULT_ENV:-}" ]]; then
|
||||||
|
echo "Detected conda env: ${CONDA_DEFAULT_ENV}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
SYSTEMD_PROPS=("-p" "CPUQuota=${CPU_QUOTA}")
|
||||||
|
case "${MEMORY_MAX,,}" in
|
||||||
|
0|none|off|infinity)
|
||||||
|
echo "MemoryMax is disabled for this run (no cgroup memory cap)."
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
SYSTEMD_PROPS+=("-p" "MemoryMax=${MEMORY_MAX}")
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# 使用 systemd-run --user --scope 启动“受限资源”的训练进程:
|
||||||
|
# - CPUQuota: 总 CPU 配额
|
||||||
|
# - MemoryMax: 最大内存
|
||||||
|
# - env ... : 显式传递训练参数到 Python 进程
|
||||||
|
# - python -m scripts.gyms.Walk: 以模块方式启动训练入口
|
||||||
|
systemd-run --user --scope \
|
||||||
|
"${SYSTEMD_PROPS[@]}" \
|
||||||
|
env \
|
||||||
|
GYM_CPU_MODE="${GYM_CPU_MODE}" \
|
||||||
|
GYM_CPU_TRAIN_STAGE="${GYM_CPU_TRAIN_STAGE}" \
|
||||||
|
GYM_CPU_TRAIN_MODEL="${GYM_CPU_TRAIN_MODEL}" \
|
||||||
|
GYM_CPU_TEST_MODEL="${GYM_CPU_TEST_MODEL}" \
|
||||||
|
GYM_CPU_TEST_FOLDER="${GYM_CPU_TEST_FOLDER}" \
|
||||||
|
"${PYTHON_EXEC}" "-m" "scripts.gyms.Walk"
|
||||||
|
|
||||||
@@ -47,6 +47,7 @@ class World:
|
|||||||
self.their_team_players: list[OtherRobot] = [OtherRobot(is_teammate=False) for _ in
|
self.their_team_players: list[OtherRobot] = [OtherRobot(is_teammate=False) for _ in
|
||||||
range(self.MAX_PLAYERS_PER_TEAM)]
|
range(self.MAX_PLAYERS_PER_TEAM)]
|
||||||
self.field: Field = self.__initialize_field(field_name=field_name)
|
self.field: Field = self.__initialize_field(field_name=field_name)
|
||||||
|
self.WORLD_STEPTIME: float = 0.02 # Time step of the world in seconds
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user