Repository navigation
Expand file tree
/
Copy pathfunctionality_controller_rl.py
More file actions
121 lines (100 loc) · 3.77 KB
/
Copy pathfunctionality_controller_rl.py
File metadata and controls
121 lines (100 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Written or Adapted by the Imperial College London Team for the FLAIR project, 2023
# Authors for this file:
# Maxime Allard
# Manon Flageat
# Bryan Lim
# Antoine Cully
from functools import partial
from typing import Any, Tuple
from jax.config import config
config.update("jax_enable_x64", True)
import jax
import jax.numpy as jnp
import optax
import numpy as np
from functionality_controller.residual_rl_td3 import Actor, TrainState
class FunctionalityControllerRL:
"""Main FunctionalityController class, call at each lopp."""
def __init__(
self,
min_command: int,
max_command: int,
learning_rate: float,
) -> None:
"""
Args:
grid_resolution: resolution of the command grid, used by all processes across FC.
min_command: min value for the command grid, used by all processes across FC.
max_command: max value for the command grid, used by all processes across FC.
state_dim: which dimension to use for the state.
state_min_opt_clip: min to clip the state before prior computation.
state_max_opt_clip: max to clip the state before prior computation.
robot_width: parameter of the robot used for prior computation.
"""
# Init an empty observation just for network initialisation
self.dummy_observations = jnp.zeros((10,))
# Init RL components
self.learning_rate = learning_rate
self.actor_key = jax.random.PRNGKey(0)
self.actor = Actor(
action_dim=2,
action_scale=jnp.array(
(max_command - min_command) / 2.0
),
action_bias=jnp.array(
(max_command + min_command) / 2.0
),
)
self.params=self.actor.init(self.actor_key, self.dummy_observations)
# Create all adaptation objects
self.reset()
def reset(self):
"""Reset the FC."""
# Reset RL components
self.params=self.actor.init(self.actor_key, self.dummy_observations)
def get_command(
self,
joystick_linear_x_human_command: float,
joystick_angular_z_human_command: float,
flipper_angular_x_human_command: float,
state: np.array,
sensor_x: float,
sensor_y: float,
) -> Tuple[float, float, float, float, float, float, float]:
"""Main function called by the pipeline to get the command to execute.
Args:
joystick_linear_x_human_command
joystick_angular_z_human_command
flipper_angular_x_human_command
use_state: indicate if state-dependent prior only
use_state_gp: indicate if state-dependent prior with GP
Returns:
the new command to forward to the safety controller in form of a msg.
"""
if state is None or sensor_x is None or sensor_y is None:
return (
joystick_linear_x_human_command,
joystick_angular_z_human_command,
flipper_angular_x_human_command,
)
# Scale joystick signal
intent_x = joystick_linear_x_human_command
intent_y = joystick_angular_z_human_command
# Build observation
observations = jnp.concatenate(
[
jnp.asarray([intent_x, intent_y]),
jnp.asarray(state),
],
axis=0,
)
# Get actor prediction
residual = self.actor.apply(self.params, observations)
# Return action to be published
linear_velocity = intent_x + float(residual[0])
angular_velocity = intent_y + float(residual[1])
return (
linear_velocity,
angular_velocity,
flipper_angular_x_human_command,
)