forked from newton-physics/newton-actuators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpd.py
More file actions
142 lines (123 loc) · 5.33 KB
/
Copy pathpd.py
File metadata and controls
142 lines (123 loc) · 5.33 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# SPDX-FileCopyrightText: Copyright (c) 2025 The Newton Developers
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Stateless PD controller actuator."""
import math
from typing import Any
import warp as wp
from ..kernels import pd_controller_kernel
from .base import Actuator
class ActuatorPD(Actuator):
"""Stateless PD controller.
Control law: τ = clamp(constant + act + Kp·(target_pos - q) + Kd·(target_vel - v), ±max_force)
Stateless: no internal memory, computes torques directly from current state.
"""
@classmethod
def resolve_arguments(cls, args: dict[str, Any]) -> dict[str, Any]:
"""Resolve arguments with defaults.
Args:
args (dict): User-provided arguments.
Returns:
dict: Arguments with defaults (kp=0, kd=0, max_force=inf, constant_force=0).
"""
return {
"kp": args.get("kp", 0.0),
"kd": args.get("kd", 0.0),
"max_force": args.get("max_force", math.inf),
"constant_force": args.get("constant_force", 0.0),
}
def __init__(
self,
input_indices: wp.array,
output_indices: wp.array,
kp: wp.array,
kd: wp.array,
max_force: wp.array,
constant_force: wp.array = None,
state_pos_attr: str = "joint_q",
state_vel_attr: str = "joint_qd",
control_target_pos_attr: str = "joint_target_pos",
control_target_vel_attr: str = "joint_target_vel",
control_input_attr: str = "joint_act",
control_output_attr: str = "joint_f",
):
"""Initialize PD actuator.
Args:
input_indices (wp.array): DOF indices for reading state and targets. Shape (N,).
output_indices (wp.array): DOF indices for writing output. Shape (N,).
kp (wp.array): Proportional gains. Shape (N,).
kd (wp.array): Derivative gains. Shape (N,).
max_force (wp.array): Force limits. Shape (N,).
constant_force (wp.array, optional): Constant offsets. Shape (N,). None to skip.
state_pos_attr (str): Attribute on sim_state for positions.
state_vel_attr (str): Attribute on sim_state for velocities.
control_target_pos_attr (str): Attribute on sim_control for target positions.
control_target_vel_attr (str): Attribute on sim_control for target velocities.
control_input_attr (str): Attribute on sim_control for control input. None to skip.
control_output_attr (str): Attribute on sim_control for output forces.
"""
super().__init__(input_indices, output_indices, control_output_attr)
for name, arr in [("kp", kp), ("kd", kd), ("max_force", max_force)]:
if len(arr) != self.num_actuators:
raise ValueError(f"{name} length ({len(arr)}) must match num_actuators ({self.num_actuators})")
if constant_force is not None and len(constant_force) != self.num_actuators:
raise ValueError(
f"constant_force length ({len(constant_force)}) must match num_actuators ({self.num_actuators})"
)
self.kp = kp
self.kd = kd
self.max_force = max_force
self.constant_force = constant_force
self.state_pos_attr = state_pos_attr
self.state_vel_attr = state_vel_attr
self.control_target_pos_attr = control_target_pos_attr
self.control_target_vel_attr = control_target_vel_attr
self.control_input_attr = control_input_attr
def _run_controller(
self,
sim_state: Any,
sim_control: Any,
controller_output: wp.array,
output_indices: wp.array,
current_state: Any,
dt: float,
) -> None:
"""Compute PD control forces."""
control_input = None
if self.control_input_attr is not None:
control_input = getattr(sim_control, self.control_input_attr, None)
wp.launch(
kernel=pd_controller_kernel,
dim=self.num_actuators,
inputs=[
getattr(sim_state, self.state_pos_attr),
getattr(sim_state, self.state_vel_attr),
getattr(sim_control, self.control_target_pos_attr),
getattr(sim_control, self.control_target_vel_attr),
control_input,
self.input_indices,
self.input_indices,
output_indices,
self.kp,
self.kd,
self.max_force,
self.constant_force,
None, # saturation_effort (DC motor)
None, # velocity_limit (DC motor)
None, # lookup_angles (remotized)
None, # lookup_torques (remotized)
0, # lookup_size (remotized)
],
outputs=[controller_output],
)